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