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