001 //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/io/datastore/idgenerator/UUIDGenerator.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.util.Properties;
039 import java.util.UUID;
040
041 import org.deegree.datatypes.Types;
042 import org.deegree.io.datastore.DatastoreTransaction;
043 import org.deegree.io.datastore.FeatureId;
044 import org.deegree.io.datastore.schema.MappedFeatureType;
045 import org.deegree.io.datastore.schema.MappedGMLId;
046
047 /**
048 * Primary key and {@link FeatureId} generator that is based on UUIDs.
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 class UUIDGenerator extends IdGenerator {
056
057 private long mac;
058
059 private boolean macSet;
060
061 /**
062 * Creates a new <code>UUIDGenerator</code> instance.
063 * <p>
064 * Supported configuration parameters: <table>
065 * <tr>
066 * <th>Name</th>
067 * <th>optional?</th>
068 * <th>Usage</th>
069 * </tr>
070 * <tr>
071 * <td>macAddress</td>
072 * <td>yes</td>
073 * <td>specify MAC address component of UUID</td>
074 * </tr>
075 * </table>
076 *
077 * @param params
078 * configuration parameters
079 * @throws IdGenerationException
080 */
081 public UUIDGenerator( Properties params ) throws IdGenerationException {
082 super( params );
083 String macAddress = params.getProperty( "macAddress" );
084 if ( macAddress != null ) {
085 long decoded;
086 try {
087 decoded = Long.decode( macAddress );
088 } catch ( NumberFormatException e ) {
089 throw new IdGenerationException( "Value for parameter 'macAddress': '" + macAddress
090 + "' can not be decoded as Long." );
091 }
092 mac = decoded;
093 macSet = true;
094 }
095 }
096
097 /**
098 * Returns a new primary key.
099 *
100 * @param ta
101 * datastore transaction (context)
102 * @return a new primary key.
103 * @throws IdGenerationException
104 * if the generation of the id could not be performed
105 */
106 @Override
107 public String getNewId( DatastoreTransaction ta )
108 throws IdGenerationException {
109 return "UUID_" + ( macSet ? new UUID( mac, (long) ( Math.random() * Long.MAX_VALUE ) ) : UUID.randomUUID() );
110 }
111
112 /**
113 * Returns a new id for a feature of the given type.
114 *
115 * @param ft
116 * (mapped) feature type (irrelevant for this generator)
117 * @param ta
118 * datastore transaction (context)
119 * @return a new feature id.
120 * @throws IdGenerationException
121 */
122 @Override
123 public FeatureId getNewId( MappedFeatureType ft, DatastoreTransaction ta )
124 throws IdGenerationException {
125
126 FeatureId fid = null;
127
128 MappedGMLId fidDefinition = ft.getGMLId();
129 if ( fidDefinition.getKeySize() != 1 ) {
130 throw new IdGenerationException( "Cannot generate feature ids that are mapped to "
131 + fidDefinition.getKeySize() + " columns." );
132 }
133 if ( fidDefinition.getIdFields()[0].getType() == Types.VARCHAR
134 || fidDefinition.getIdFields()[0].getType() == Types.CHAR ) {
135 String uuidString = "UUID_"
136 + ( macSet ? new UUID( mac, (long) ( Math.random() * Long.MAX_VALUE ) )
137 : UUID.randomUUID() );
138 fid = new FeatureId( ft, new Object[] { uuidString } );
139 } else {
140 // TODO add handling of NUMERIC columns (>= 128 bit)
141 throw new IdGenerationException( "UUIDGenerator may currently only be used for VARCHAR fields." );
142 }
143 return fid;
144 }
145 }