001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/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 04:32:57 -0800 (Thu, 27 Dec 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
071: .getBundle(BUNDLE_NAME);
072:
073: protected Properties params;
074:
075: protected MappedFeatureType ft;
076:
077: /**
078: * Creates a new <code>IdGenerator</code> instance.
079: *
080: * @param params
081: * configuration parameters
082: */
083: protected IdGenerator(Properties params) {
084: this .params = params;
085: }
086:
087: /**
088: * Returns a new primary key.
089: *
090: * @param ta
091: * datastore transaction (context)
092: * @return a new primary key.
093: * @throws IdGenerationException
094: * if the generation of the id could not be performed
095: */
096: public abstract Object getNewId(DatastoreTransaction ta)
097: throws IdGenerationException;
098:
099: /**
100: * Returns a new id for a feature of the given type.
101: *
102: * @param ft
103: * feature type
104: * @param ta
105: * datastore transaction (context)
106: * @return a new feature id.
107: * @throws IdGenerationException
108: */
109: public abstract FeatureId getNewId(MappedFeatureType ft,
110: DatastoreTransaction ta) throws IdGenerationException;
111:
112: /**
113: * Returns a concrete <code>IdGenerator</code> instance which is identified by the given type
114: * code.
115: *
116: * @param type
117: * type code
118: * @param params
119: * initialization parameters for the IdGenerator
120: * @return concrete IdGenerator instance
121: */
122: public static final IdGenerator getInstance(String type,
123: Properties params) {
124: IdGenerator generator = null;
125: String className = null;
126: try {
127: className = RESOURCE_BUNDLE.getString(type);
128: Class idGeneratorClass = Class.forName(className);
129:
130: // get constructor
131: Class[] parameterTypes = new Class[] { Properties.class };
132: Constructor constructor = idGeneratorClass
133: .getConstructor(parameterTypes);
134:
135: // call constructor
136: Object arglist[] = new Object[] { params };
137: generator = (IdGenerator) constructor.newInstance(arglist);
138: } catch (InvocationTargetException e) {
139: String msg = "Could not instantiate IdGenerator with type '"
140: + type
141: + "': "
142: + e.getTargetException().getMessage();
143: throw new RuntimeException(msg);
144: } catch (ClassNotFoundException e) {
145: String msg = "IdGenerator class '" + className
146: + "' not found: " + e.getMessage();
147: throw new RuntimeException(msg);
148: } catch (Exception e) {
149: String msg = "Could not instantiate IdGenerator with type '"
150: + type + "': " + e.getMessage();
151: throw new RuntimeException(msg);
152: }
153: return generator;
154: }
155: }
|