001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/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 13:32:57 +0100 (Do, 27 Dez 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 ) throws IdGenerationException { 089 super( params ); 090 String macAddress = params.getProperty( "macAddress" ); 091 if ( macAddress != null ) { 092 long decoded; 093 try { 094 decoded = Long.decode( macAddress ); 095 } catch ( NumberFormatException e ) { 096 throw new IdGenerationException( "Value for parameter 'macAddress': '" + macAddress 097 + "' can not be decoded as Long." ); 098 } 099 mac = decoded; 100 macSet = true; 101 } 102 } 103 104 /** 105 * Returns a new primary key. 106 * 107 * @param ta 108 * datastore transaction (context) 109 * @return a new primary key. 110 * @throws IdGenerationException 111 * if the generation of the id could not be performed 112 */ 113 @Override 114 public String getNewId( DatastoreTransaction ta ) 115 throws IdGenerationException { 116 return "UUID_" + ( macSet ? new UUID( mac, (long) ( Math.random() * Long.MAX_VALUE ) ) : UUID.randomUUID() ); 117 } 118 119 /** 120 * Returns a new id for a feature of the given type. 121 * 122 * @param ft 123 * (mapped) feature type (irrelevant for this generator) 124 * @param ta 125 * datastore transaction (context) 126 * @return a new feature id. 127 * @throws IdGenerationException 128 */ 129 @Override 130 public FeatureId getNewId( MappedFeatureType ft, DatastoreTransaction ta ) 131 throws IdGenerationException { 132 133 FeatureId fid = null; 134 135 MappedGMLId fidDefinition = ft.getGMLId(); 136 if ( fidDefinition.getKeySize() != 1 ) { 137 throw new IdGenerationException( "Cannot generate feature ids that are mapped to " 138 + fidDefinition.getKeySize() + " columns." ); 139 } 140 if ( fidDefinition.getIdFields()[0].getType() == Types.VARCHAR 141 || fidDefinition.getIdFields()[0].getType() == Types.CHAR ) { 142 String uuidString = "UUID_" 143 + ( macSet ? new UUID( mac, (long) ( Math.random() * Long.MAX_VALUE ) ) 144 : UUID.randomUUID() ); 145 fid = new FeatureId( ft, new Object[] { uuidString } ); 146 } else { 147 // TODO add handling of NUMERIC columns (>= 128 bit) 148 throw new IdGenerationException( "UUIDGenerator may currently only be used for VARCHAR fields." ); 149 } 150 return fid; 151 } 152 }