001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/ogcwebservices/wfs/WFServiceFactory.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53115 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042: ---------------------------------------------------------------------------*/
043: package org.deegree.ogcwebservices.wfs;
044:
045: import java.io.IOException;
046: import java.net.URL;
047:
048: import org.deegree.framework.log.ILogger;
049: import org.deegree.framework.log.LoggerFactory;
050: import org.deegree.framework.xml.InvalidConfigurationException;
051: import org.deegree.i18n.Messages;
052: import org.deegree.io.datastore.DatastoreException;
053: import org.deegree.io.datastore.LockManager;
054: import org.deegree.ogcwebservices.OGCWebServiceException;
055: import org.deegree.ogcwebservices.wfs.configuration.WFSConfiguration;
056: import org.deegree.ogcwebservices.wfs.configuration.WFSConfigurationDocument;
057: import org.xml.sax.SAXException;
058:
059: /**
060: * Factory class for creating instances of {@link WFService}.
061: *
062: * TODO manage several instances of different WFServices in a pool
063: *
064: * @see WFService
065: *
066: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
067: * @author last edited by: $Author: apoth $
068: *
069: * @version $Revision: 9348 $, $Date: 2007-12-27 08:59:14 -0800 (Thu, 27 Dec 2007) $
070: */
071: public class WFServiceFactory {
072:
073: private static final ILogger LOG = LoggerFactory
074: .getLogger(WFServiceFactory.class);
075:
076: private static WFSConfiguration CONFIG;
077:
078: private static boolean lockManagerInitialized;
079:
080: private WFServiceFactory() {
081: // prevent instantiation
082: }
083:
084: /**
085: *
086: * @return
087: * @throws OGCWebServiceException
088: */
089: public static WFService createInstance()
090: throws OGCWebServiceException {
091: if (CONFIG == null) {
092: throw new OGCWebServiceException(WFServiceFactory.class
093: .getName(),
094: "configuration has not been initialized");
095: }
096: if (!lockManagerInitialized) {
097: // TODO synchronize using other object
098: synchronized (LOG) {
099: try {
100: LockManager.initialize(CONFIG.getDeegreeParams()
101: .getLockManagerDirectory());
102: } catch (DatastoreException e) {
103: LOG.logError(e.getMessage(), e);
104: throw new OGCWebServiceException(
105: "WFSServiceFactory", e.getMessage());
106: }
107: }
108: lockManagerInitialized = true;
109: }
110: return new WFService(CONFIG);
111: }
112:
113: /**
114: *
115: * @param wfsConfiguration
116: * @return
117: * @throws OGCWebServiceException
118: */
119: public static WFService createInstance(
120: WFSConfiguration wfsConfiguration)
121: throws OGCWebServiceException {
122: LOG.logDebug("Creating WFService instance.");
123: if (!lockManagerInitialized) {
124: // TODO synchronize using other object
125: synchronized (LOG) {
126: try {
127: LockManager.initialize(wfsConfiguration
128: .getDeegreeParams()
129: .getLockManagerDirectory());
130: } catch (DatastoreException e) {
131: LOG.logError(e.getMessage(), e);
132: throw new OGCWebServiceException(
133: "WFSServiceFactory", e.getMessage());
134: }
135: }
136: lockManagerInitialized = true;
137: }
138: return new WFService(wfsConfiguration);
139: }
140:
141: /**
142: * Sets the <code>WFSConfiguration</code>. Afterwards, all <code>WFService</code> instances
143: * returned by <code>createInstance()</code> will use this configuration.
144: *
145: * @param config
146: * @throws InvalidConfigurationException
147: */
148: public synchronized static void setConfiguration(
149: WFSConfiguration config)
150: throws InvalidConfigurationException {
151: validateConfiguration(config);
152: CONFIG = config;
153: // TODO: if service instances have already been created
154: // - destroy all instances
155: // - create new service instances and put them in the pool
156: }
157:
158: /**
159: *
160: * @param serviceConfigurationURL
161: * @throws InvalidConfigurationException
162: * @throws IOException
163: */
164: public synchronized static void setConfiguration(
165: URL serviceConfigurationURL)
166: throws InvalidConfigurationException, IOException {
167:
168: try {
169: WFSConfigurationDocument cd = new WFSConfigurationDocument();
170: cd.load(serviceConfigurationURL);
171: setConfiguration(cd.getConfiguration());
172: } catch (InvalidConfigurationException e) {
173: throw new InvalidConfigurationException(
174: "WFSServiceFactory", e.getMessage());
175: } catch (SAXException e) {
176: throw new InvalidConfigurationException(
177: "WFSServiceFactory", e.getMessage());
178: }
179:
180: }
181:
182: /**
183: * @return CONFIG
184: */
185: public static WFSConfiguration getConfiguration() {
186: return CONFIG;
187: }
188:
189: private static void validateConfiguration(WFSConfiguration config)
190: throws InvalidConfigurationException {
191: String[] versions = config.getServiceIdentification()
192: .getServiceTypeVersions();
193: for (String version : versions) {
194: if (!WFService.VERSION.equals(version)) {
195: String msg = Messages.getMessage(
196: "WFS_CONF_UNSUPPORTED_VERSION", version,
197: WFService.VERSION);
198: throw new InvalidConfigurationException(msg);
199: }
200: }
201: }
202: }
|