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