001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/io/datastore/idgenerator/UUIDGenerator.java $
002 /*---------------- FILE HEADER ------------------------------------------
003
004 This file is part of deegree.
005 Copyright (C) 2001-2006 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.util.Properties;
046 import java.util.UUID;
047
048 import org.deegree.datatypes.Types;
049 import org.deegree.io.datastore.DatastoreTransaction;
050 import org.deegree.io.datastore.FeatureId;
051 import org.deegree.io.datastore.schema.MappedFeatureType;
052 import org.deegree.io.datastore.schema.MappedGMLId;
053
054 /**
055 * Primary key and {@link FeatureId} generator that is based on UUIDs.
056 *
057 * @author <a href="mailto:schneider@lat-lon.de">Markus Schneider </a>
058 * @author last edited by: $Author: apoth $
059 *
060 * @version $Revision: 7843 $, $Date: 2007-07-25 09:44:47 +0200 (Mi, 25 Jul 2007) $
061 */
062 public class UUIDGenerator extends IdGenerator {
063
064 private UUID factory;
065
066 /**
067 * Creates a new <code>UUIDGenerator</code> instance.
068 * <p>
069 * Supported configuration parameters:
070 * <table>
071 * <tr><th>Name</th><th>optional?</th><th>Usage</th></tr>
072 * <tr><td>macAddress</td><td>yes</td><td>specify MAC address component of UUID</td></tr>
073 * </table>
074 *
075 * @param params
076 * configuration parameters
077 * @throws IdGenerationException
078 */
079 public UUIDGenerator( Properties params ) throws IdGenerationException {
080 super( params );
081 String macAddress = params.getProperty( "macAddress" );
082 if ( macAddress != null ) {
083 long decoded;
084 try {
085 decoded = Long.decode( macAddress );
086 } catch ( NumberFormatException e ) {
087 throw new IdGenerationException( "Value for parameter 'macAddress': '" + macAddress
088 + "' can not be decoded as Long." );
089 }
090 factory = new UUID( decoded, (long)(Math.random() * Long.MAX_VALUE) );
091 } else {
092 factory = UUID.randomUUID();
093 }
094 }
095
096 /**
097 * Returns a new primary key.
098 *
099 * @param ta
100 * datastore transaction (context)
101 * @return a new primary key.
102 * @throws IdGenerationException
103 * if the generation of the id could not be performed
104 */
105 @Override
106 public String getNewId( DatastoreTransaction ta )
107 throws IdGenerationException {
108 return "UUID_" + factory.toString();
109 }
110
111 /**
112 * Returns a new id for a feature of the given type.
113 *
114 * @param ft
115 * (mapped) feature type (irrelevant for this generator)
116 * @param ta
117 * datastore transaction (context)
118 * @return a new feature id.
119 * @throws IdGenerationException
120 */
121 @Override
122 public FeatureId getNewId( MappedFeatureType ft, DatastoreTransaction ta )
123 throws IdGenerationException {
124
125 FeatureId fid = null;
126
127 MappedGMLId fidDefinition = ft.getGMLId();
128 if ( fidDefinition.getKeySize() != 1 ) {
129 throw new IdGenerationException( "Cannot generate feature ids that are mapped to "
130 + fidDefinition.getKeySize() + " columns." );
131 }
132 if ( fidDefinition.getIdFields()[0].getType() == Types.VARCHAR ||
133 fidDefinition.getIdFields()[0].getType() == Types.CHAR ) {
134 String uuidString = "UUID_" + factory.toString();
135 fid = new FeatureId( ft, new Object[] { uuidString } );
136 } else {
137 // TODO add handling of NUMERIC columns (>= 128 bit)
138 throw new IdGenerationException( "UUIDGenerator may currently only be used for VARCHAR fields." );
139 }
140 return fid;
141 }
142 }