001 //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/io/datastore/idgenerator/IdGenerator.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.idgenerator;
037
038 import java.lang.reflect.Constructor;
039 import java.lang.reflect.InvocationTargetException;
040 import java.util.Properties;
041 import java.util.ResourceBundle;
042
043 import org.deegree.io.datastore.DatastoreTransaction;
044 import org.deegree.io.datastore.FeatureId;
045 import org.deegree.io.datastore.schema.MappedFeatureType;
046
047 /**
048 * Abstract base class for generators that are used to create primary keys (especially {@link FeatureId}s).
049 *
050 * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider </a>
051 * @author last edited by: $Author: mschneider $
052 *
053 * @version $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
054 */
055 public abstract class IdGenerator {
056
057 /** Default generator type based on UUIDs. */
058 public static String TYPE_UUID = "UUID";
059
060 private static final String BUNDLE_NAME = "org.deegree.io.datastore.idgenerator.idgenerator";
061
062 private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle( BUNDLE_NAME );
063
064 /** Configuration properties. */
065 protected Properties params;
066
067 /** {@link MappedFeatureType} that this generator is bound to. */
068 protected MappedFeatureType ft;
069
070 /**
071 * Creates a new <code>IdGenerator</code> instance.
072 *
073 * @param params
074 * configuration parameters
075 */
076 protected IdGenerator( Properties params ) {
077 this.params = params;
078 }
079
080 /**
081 * Returns a new primary key.
082 *
083 * @param ta
084 * datastore transaction (context)
085 * @return a new primary key.
086 * @throws IdGenerationException
087 * if the generation of the id could not be performed
088 */
089 public abstract Object getNewId( DatastoreTransaction ta )
090 throws IdGenerationException;
091
092 /**
093 * Returns a new id for a feature of the given type.
094 *
095 * @param ft
096 * feature type
097 * @param ta
098 * datastore transaction (context)
099 * @return a new feature id.
100 * @throws IdGenerationException
101 */
102 public abstract FeatureId getNewId( MappedFeatureType ft, DatastoreTransaction ta )
103 throws IdGenerationException;
104
105 /**
106 * Returns a concrete <code>IdGenerator</code> instance which is identified by the given type code.
107 *
108 * @param type
109 * type code
110 * @param params
111 * initialization parameters for the IdGenerator
112 * @return concrete IdGenerator instance
113 */
114 @SuppressWarnings("unchecked")
115 public static final IdGenerator getInstance( String type, Properties params ) {
116 IdGenerator generator = null;
117 String className = null;
118 try {
119 className = RESOURCE_BUNDLE.getString( type );
120 Class<IdGenerator> idGeneratorClass = (Class<IdGenerator>) Class.forName( className );
121
122 // get constructor
123 Class<?>[] parameterTypes = new Class[] { Properties.class };
124 Constructor<IdGenerator> constructor = idGeneratorClass.getConstructor( parameterTypes );
125
126 // call constructor
127 Object arglist[] = new Object[] { params };
128 generator = constructor.newInstance( arglist );
129 } catch ( InvocationTargetException e ) {
130 String msg = "Could not instantiate IdGenerator with type '" + type + "': "
131 + e.getTargetException().getMessage();
132 throw new RuntimeException( msg );
133 } catch ( ClassNotFoundException e ) {
134 String msg = "IdGenerator class '" + className + "' not found: " + e.getMessage();
135 throw new RuntimeException( msg );
136 } catch ( Exception e ) {
137 String msg = "Could not instantiate IdGenerator with type '" + type + "': " + e.getMessage();
138 throw new RuntimeException( msg );
139 }
140 return generator;
141 }
142 }