001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/portal/owswatch/ServiceWatcherFactory.java $ 002 /*---------------------------------------------------------------------------- 003 This file is part of deegree, http://deegree.org/ 004 Copyright (C) 2001-2009 by: 005 Department of Geography, University of Bonn 006 and 007 lat/lon GmbH 008 009 This library is free software; you can redistribute it and/or modify it under 010 the terms of the GNU Lesser General Public License as published by the Free 011 Software Foundation; either version 2.1 of the License, or (at your option) 012 any later version. 013 This library is distributed in the hope that it will be useful, but WITHOUT 014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 015 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more 016 details. 017 You should have received a copy of the GNU Lesser General Public License 018 along with this library; if not, write to the Free Software Foundation, Inc., 019 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 020 021 Contact information: 022 023 lat/lon GmbH 024 Aennchenstr. 19, 53177 Bonn 025 Germany 026 http://lat-lon.de/ 027 028 Department of Geography, University of Bonn 029 Prof. Dr. Klaus Greve 030 Postfach 1147, 53001 Bonn 031 Germany 032 http://www.geographie.uni-bonn.de/deegree/ 033 034 e-mail: info@deegree.org 035 ----------------------------------------------------------------------------*/ 036 037 package org.deegree.portal.owswatch; 038 039 import java.io.Serializable; 040 import java.util.ArrayList; 041 import java.util.Iterator; 042 import java.util.List; 043 import java.util.Map; 044 045 import org.deegree.framework.util.StringTools; 046 import org.deegree.portal.owswatch.configs.GeneralConfig; 047 import org.deegree.portal.owswatch.configs.OwsWatchConfig; 048 import org.deegree.portal.owswatch.configs.OwsWatchConfigFactory; 049 import org.deegree.portal.owswatch.configs.User; 050 051 /** 052 * Instantiates an instance of ServiceWatcher with instances of ServiceConfiguration and ServiceLog inside as parsed 053 * from the configurations file 054 * 055 * @author <a href="mailto:elmasry@lat-lon.de">Moataz Elmasry</a> 056 * @author last edited by: $Author: jmays $ 057 * 058 * @version $Revision: 20271 $, $Date: 2009-10-21 13:07:15 +0200 (Mi, 21 Okt 2009) $ 059 */ 060 public class ServiceWatcherFactory implements Serializable { 061 062 /** 063 * 064 */ 065 private static final long serialVersionUID = -7205941737006048817L; 066 067 private static String confFilePath = null; 068 069 private static String webinfPath = null; 070 071 private static ServicesConfigurationFactory servicesParser = null; 072 073 private static OwsWatchConfig conf = null; 074 075 private static ServiceWatcher watcher = null; 076 077 private static ServiceWatcherFactory factory = null; 078 079 private static String protDirPath = null; 080 081 private static EmailSender sender = null; 082 083 private static String servletAddr = null; 084 085 private ServiceWatcherFactory( String confFilePath, String webinfPath ) throws ConfigurationsException { 086 ServiceWatcherFactory.confFilePath = confFilePath; 087 ServiceWatcherFactory.webinfPath = webinfPath; 088 parse(); 089 } 090 091 /** 092 * @return ServiceWatcher 093 * @throws ConfigurationsException 094 */ 095 public ServiceWatcher getServiceWatcherInstance() 096 throws ConfigurationsException { 097 return watcher; 098 } 099 100 /** 101 * @param confFilePath 102 * @param webinfPath 103 * @return An instance of ServiceWatchFactory using singletone pattern 104 * @throws ConfigurationsException 105 */ 106 public static ServiceWatcherFactory getInstance( String confFilePath, String webinfPath ) 107 throws ConfigurationsException { 108 if ( factory != null ) { 109 return factory; 110 } 111 factory = new ServiceWatcherFactory( confFilePath, webinfPath ); 112 return factory; 113 } 114 115 /** 116 * @return ServiceWatcher 117 * @throws ConfigurationsException 118 */ 119 private static ServiceWatcher parse() 120 throws ConfigurationsException { 121 try { 122 conf = OwsWatchConfigFactory.createOwsWatchConfig( confFilePath, webinfPath ); 123 } catch ( Exception e ) { 124 throw new ConfigurationsException( e.getLocalizedMessage() ); 125 } 126 127 servicesParser = new ServicesConfigurationFactory(); 128 List<ServiceConfiguration> services = parseServiceConfigurations( servicesParser, conf, webinfPath ); 129 130 watcher = createServiceWatcher( conf, services ); 131 return watcher; 132 } 133 134 private static ServiceWatcher createServiceWatcher( OwsWatchConfig conf, List<ServiceConfiguration> services ) 135 throws ConfigurationsException { 136 137 servletAddr = conf.getGeneral().getServerAddress(); 138 servletAddr = StringTools.concat( 100, servletAddr, !servletAddr.endsWith( "/" ) ? "/" : "", "wprotocol" ); 139 StringBuilder builder = new StringBuilder( webinfPath ); 140 protDirPath = builder.append( conf.getGeneral().getProtFolderPath() ).toString(); 141 142 ServiceWatcher watcher = new ServiceWatcher(); 143 GeneralConfig general = conf.getGeneral(); 144 145 sender = new EmailSender( general.getMailFrom(), general.getMailServer(), getUserEmails( general.getUsers() ) ); 146 147 Iterator<ServiceConfiguration> it = services.iterator(); 148 while ( it.hasNext() ) { 149 ServiceConfiguration service = it.next(); 150 try { 151 ServiceLog serviceLog = new ServiceLog( protDirPath, service.getServiceid(), service.getServiceName(), 152 service.getServiceType(), servletAddr, sender ); 153 watcher.addService( service, serviceLog ); 154 } catch ( Exception e ) { 155 throw new ConfigurationsException( e.getLocalizedMessage() ); 156 } 157 } 158 return watcher; 159 } 160 161 /** 162 * builds a file path from the given webinfPath and the services instances path located in OwsWatchconfig. after 163 * calling this function, other values from the parser could be extracted like the prefix and the idSequence 164 * 165 * @param servicesParser 166 * @param conf 167 * @param webinfPath 168 * @return List of ServiceConfiguration 169 */ 170 private static List<ServiceConfiguration> parseServiceConfigurations( ServicesConfigurationFactory servicesParser, 171 OwsWatchConfig conf, String webinfPath ) 172 throws ConfigurationsException { 173 String serviceAddr = conf.getGeneral().getServerAddress(); 174 serviceAddr = StringTools.concat( 100, serviceAddr, !serviceAddr.endsWith( "/" ) ? "/" : "", "wprotocol" ); 175 176 StringBuilder builder = new StringBuilder( webinfPath ); 177 String servicesUrl = builder.toString() + conf.getGeneral().getServiceInstancesPath(); 178 try { 179 return servicesParser.parseServices( servicesUrl ); 180 } catch ( Exception e ) { 181 throw new ConfigurationsException( e.getLocalizedMessage() ); 182 } 183 } 184 185 /** 186 * @return Path of the configuration file 187 */ 188 public String getConfFilePath() { 189 return confFilePath; 190 } 191 192 /** 193 * @return WEB-INF folder path 194 */ 195 public String getWebinfPath() { 196 return webinfPath; 197 } 198 199 /** 200 * @return {@link OwsWatchConfigFactory} 201 */ 202 public OwsWatchConfig getConf() { 203 return conf; 204 } 205 206 /** 207 * @return {@link ServicesConfigurationFactory} 208 */ 209 public ServicesConfigurationFactory getServicesParser() { 210 return servicesParser; 211 } 212 213 protected static List<String> getUserEmails( Map<String, User> users ) { 214 // Adding users emails 215 Iterator<String> it = users.keySet().iterator(); 216 List<String> list = new ArrayList<String>(); 217 while ( it.hasNext() ) { 218 User user = users.get( it.next() ); 219 list.add( user.getEmail() ); 220 } 221 return list; 222 } 223 224 /** 225 * @return Protocol directory path 226 */ 227 public String getProtDirPath() { 228 return protDirPath; 229 } 230 231 /** 232 * @return EmailSender 233 */ 234 public EmailSender getSender() { 235 return sender; 236 } 237 238 /** 239 * @return String 240 */ 241 public String getServletAddr() { 242 return servletAddr; 243 } 244 }