001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/io/datastore/DatastoreRegistry.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     Aennchenstraße 19
030     53177 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.io.datastore;
044    
045    import java.util.HashMap;
046    import java.util.Map;
047    import java.util.MissingResourceException;
048    import java.util.ResourceBundle;
049    
050    import org.deegree.i18n.Messages;
051    
052    /**
053     * Responsible for the lookup of {@link Datastore} instances by their configuration information.
054     * <p>
055     * This is necessary to ensure that application schemas which use the same backend and identical
056     * configuration information are served by the same {@link Datastore} instance.
057     * 
058     * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider </a>
059     * @author last edited by: $Author: apoth $
060     * 
061     * @version $Revision: 9342 $, $Date: 2007-12-27 13:32:57 +0100 (Do, 27 Dez 2007) $
062     */
063    public class DatastoreRegistry {
064    
065        private static final String BUNDLE_NAME = "org.deegree.io.datastore.datastores";
066    
067        private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle( BUNDLE_NAME );
068    
069        private static Map<DatastoreConfiguration, Datastore> dsMap = new HashMap<DatastoreConfiguration, Datastore>();
070    
071        /**
072         * Returns the {@link Class} of a {@link Datastore} identified by the passed name. The mapping
073         * between common names and Datastore classes is stored in the file 
074         * <code>org.deegree.io.datastore.datastores.datastore.properties</code>.
075         * 
076         * @param commonName
077         * @return Datastore class
078         * @throws IllegalArgumentException
079         */
080        public static Class getDatastoreClass( String commonName )
081                                throws IllegalArgumentException {
082            Class datastoreClass = null;
083            String className = null;
084            try {
085                className = RESOURCE_BUNDLE.getString( commonName );
086                datastoreClass = Class.forName( className );
087            } catch ( MissingResourceException e ) {
088                String msg = Messages.getMessage( "DATASTORE_REGISTRY_BACKEND_UNKNOWN", commonName,
089                                                  BUNDLE_NAME );
090                throw new IllegalArgumentException( msg );
091            } catch ( ClassNotFoundException e ) {
092                throw new IllegalArgumentException( "Could not instantiate datastore class '"
093                                                    + className + ": " + e.getMessage() );
094            }
095            return datastoreClass;
096        }
097    
098        /**
099         * Returns the {@link Datastore} instance that serves the given {@link DatastoreConfiguration}.
100         *  
101         * @param config
102         * @return Datastore instance or null, if no Datastore is registered for this configuration
103         */
104        public static Datastore getDatastore( DatastoreConfiguration config ) {
105            return dsMap.get( config );
106        }
107    
108        /**
109         * Registers a new {@link Datastore} instance. 
110         * 
111         * @param ds
112         * @throws DatastoreException if Datastore for configuration is already registered
113         */
114        public static synchronized void registerDatastore( Datastore ds )
115                                throws DatastoreException {
116            if ( dsMap.get( ds.getConfiguration() ) != null ) {
117                String msg = Messages.getMessage( "DATASTORE_REGISTRY_ALREADY_REGISTERED",
118                                                  ds.getConfiguration() );
119                throw new DatastoreException( msg );
120            }
121            dsMap.put( ds.getConfiguration(), ds );
122        }
123    
124        /**
125         * Returns the {@link Datastore} instance that serves the given {@link DatastoreConfiguration}
126         * from the registry.
127         * 
128         * @param config
129         * @throws DatastoreException if no Datastore for configuration is registered
130         */
131        public static synchronized void deregisterDatastore( DatastoreConfiguration config )
132                                throws DatastoreException {
133            if ( dsMap.get( config ) != null ) {
134                String msg = Messages.getMessage( "DATASTORE_REGISTRY_NOT_REGISTERED", config );
135                throw new DatastoreException( msg );
136            }
137        }
138    }