001    /*----------------    FILE HEADER  ------------------------------------------
002    
003     This file is part of deegree.
004     Copyright (C) 2001-2007 by:
005     lat/lon GmbH
006     http://www.lat-lon.de
007    
008     This library is free software; you can redistribute it and/or
009     modify it under the terms of the GNU Lesser General Public
010     License as published by the Free Software Foundation; either
011     version 2.1 of the License, or (at your option) any later version.
012    
013     This library is distributed in the hope that it will be useful,
014     but WITHOUT ANY WARRANTY; without even the implied warranty of
015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
016     Lesser General Public License for more details.
017    
018     You should have received a copy of the GNU Lesser General Public
019     License along with this library; if not, write to the Free Software
020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
021    
022     Contact:
023    
024     Andreas Poth
025     lat/lon GmbH
026     Aennchenstr. 19
027     53115 Bonn
028     Germany
029     E-Mail: poth@lat-lon.de
030     
031     ---------------------------------------------------------------------------*/
032    package org.deegree.model.crs;
033    
034    import java.io.InputStream;
035    import java.net.URL;
036    import java.util.HashMap;
037    import java.util.Map;
038    import java.util.Properties;
039    
040    import org.deegree.framework.log.ILogger;
041    import org.deegree.framework.log.LoggerFactory;
042    import org.deegree.model.crs.CSAccess;
043    
044    /**
045     * 
046     * 
047     * 
048     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
049     * @author last edited by: $Author: poth $
050     * 
051     * @version. $Revision: 1.2 $, $Date: 2007-06-11 11:33:58 $
052     */
053    public class CSAccessFactory {
054    
055        private static final ILogger LOG = LoggerFactory.getLogger( CSAccessFactory.class );
056    
057        /**
058         * access key to default CSAccess class 
059         */
060        public static final String CSA_DEFAULT = "default";
061    
062        public static final String CSA_DEEGREE_OLD = "deegree_old";
063        
064        public static final String CSA_DEEGREE_NEW = "deegree_new";
065    
066        private static final String CSACCESS_RESOURCE = "csaccessfactory.properties";
067    
068        private static Map<String, CSAccess> csAccess = new HashMap<String, CSAccess>();
069    
070        /**
071         * returns an instance of the default CSAccess class (default CSAccess assigend to key
072         * 'deegree_new')
073         * 
074         * @return instance of the default CSAccess class
075         */
076        public static CSAccess getCSAccess() {
077            return getCSAccess( CSA_DEFAULT );
078        }
079    
080        /**
081         * returns an instance of the CSAccess class assigend to the passed key
082         * 
083         * @param csAccessKey
084         * @return instance of the CSAccess class assigend to the passed key
085         */
086        public static CSAccess getCSAccess( String csAccessKey ) {
087            
088            if ( csAccess.get( csAccessKey ) == null ) {
089                initialCSAccess( csAccessKey );
090            }
091            return csAccess.get( csAccessKey );
092        }
093    
094        /**
095         * creates an instance of the CSAccess class assigend to the passed key and writes it into the
096         * caching map
097         * 
098         * @param csAccessKey
099         */
100        private static void initialCSAccess( String csAccessKey ) {
101    
102            URL url = CSAccessFactory.class.getResource( CSACCESS_RESOURCE );
103            Properties props = new Properties();
104            try {
105                InputStream is = url.openStream();
106                props.load( is );
107                is.close();
108            } catch ( Exception e ) {
109                LOG.logError( e.getMessage(), e );
110                throw new CSAccessFactoryException( "could not load csaccess resource: " + CSACCESS_RESOURCE );
111            }
112            String clzzName = props.getProperty( csAccessKey );
113            
114            if ( clzzName == null ) {
115                throw new CSAccessFactoryException( "no CSAccess class defined for key: " + csAccessKey );
116            }
117    
118            try {
119                csAccess.put( csAccessKey, (CSAccess) Class.forName( clzzName ).newInstance() );
120            } catch ( InstantiationException e ) {
121                LOG.logError( e.getMessage(), e );
122                throw new CSAccessFactoryException( "could not initialize: " + clzzName );
123            } catch ( IllegalAccessException e ) {
124                LOG.logError( e.getMessage(), e );
125                throw new CSAccessFactoryException( "the current process is not allowed to access: " + clzzName );
126            } catch ( ClassNotFoundException e ) {
127                LOG.logError( e.getMessage(), e );
128                throw new CSAccessFactoryException( "class: " + clzzName + " has not been found in classpath" );
129            }
130    
131        }
132    
133    }