1 31 32 package org.opencms.util; 33 34 import org.opencms.main.CmsIllegalArgumentException; 35 import org.opencms.main.CmsInitException; 36 import org.opencms.main.CmsLog; 37 import org.opencms.main.CmsRuntimeException; 38 39 import java.io.Externalizable ; 40 import java.io.IOException ; 41 import java.io.ObjectInput ; 42 import java.io.ObjectOutput ; 43 import java.io.Serializable ; 44 45 import org.apache.commons.logging.Log; 46 47 import org.doomdark.uuid.EthernetAddress; 48 import org.doomdark.uuid.UUID; 49 import org.doomdark.uuid.UUIDGenerator; 50 51 73 public final class CmsUUID extends Object implements Serializable , Cloneable , Comparable , Externalizable { 74 75 76 private static final Log LOG = CmsLog.getLog(CmsUUID.class); 77 78 79 private static EthernetAddress m_ethernetAddress; 80 81 82 private static boolean m_isNotInitialized = true; 83 84 85 private static UUID m_opencmsUUID = UUIDGenerator.getInstance().generateNameBasedUUID( 86 new UUID(UUID.NAMESPACE_DNS), 87 "www.opencms.org"); 88 89 90 private static final CmsUUID NULL_UUID = new CmsUUID(UUID.getNullUUID()); 91 92 93 private static final long serialVersionUID = 1736324454709298676L; 94 95 96 private UUID m_uuid; 97 98 104 public CmsUUID() { 105 106 if (m_isNotInitialized) { 107 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_INVALID_ETHERNET_ADDRESS_0)); 108 } 109 m_uuid = UUIDGenerator.getInstance().generateTimeBasedUUID(m_ethernetAddress); 110 } 111 112 117 public CmsUUID(byte[] data) { 118 119 m_uuid = new UUID(data); 120 } 121 122 128 public CmsUUID(String uuid) 129 throws NumberFormatException { 130 131 m_uuid = new UUID(uuid); 132 } 133 134 139 private CmsUUID(UUID uuid) { 140 141 m_uuid = uuid; 142 } 143 144 152 public static void checkId(CmsUUID id, boolean canBeNull) { 153 154 if (canBeNull && id == null) { 155 return; 156 } 157 if ((!canBeNull && id == null) || id.isNullUUID()) { 158 throw new CmsIllegalArgumentException(Messages.get().container(Messages.ERR_INVALID_UUID_1, id)); 159 } 160 } 161 162 169 public static CmsUUID getConstantUUID(String name) { 170 171 return new CmsUUID(UUIDGenerator.getInstance().generateNameBasedUUID(m_opencmsUUID, name)); 172 } 173 174 179 public static String getDummyEthernetAddress() { 180 181 return UUIDGenerator.getInstance().getDummyAddress().toString(); 182 } 183 184 190 public static CmsUUID getNullUUID() { 191 192 return NULL_UUID; 193 } 194 195 201 public static CmsUUID getOpenCmsUUID() { 202 203 return new CmsUUID(m_opencmsUUID); 204 } 205 206 215 public static void init(String ethernetAddress) throws CmsInitException { 216 217 try { 218 m_ethernetAddress = new EthernetAddress(ethernetAddress); 219 } catch (Exception e) { 220 throw new CmsInitException(Messages.get().container( 221 Messages.ERR_INVALID_ETHERNET_ADDRESS_1, 222 ethernetAddress)); 223 } 224 m_isNotInitialized = false; 225 } 226 227 234 public static boolean isValidUUID(String uuid) { 235 236 try { 237 return (null != uuid) && (null != UUID.valueOf(uuid)); 238 } catch (NumberFormatException e) { 239 } 241 return false; 242 } 243 244 249 public Object clone() { 250 251 if (this == NULL_UUID) { 252 return NULL_UUID; 253 } 254 return new CmsUUID((UUID)m_uuid.clone()); 255 } 256 257 260 public int compareTo(Object obj) { 261 262 if (obj instanceof CmsUUID) { 263 return m_uuid.compareTo(((CmsUUID)obj).m_uuid); 264 } 265 return 0; 266 } 267 268 271 public boolean equals(Object obj) { 272 273 if (obj == this) { 274 return true; 275 } 276 if (obj instanceof CmsUUID) { 277 return ((CmsUUID)obj).m_uuid.equals(m_uuid); 278 } 279 return false; 280 } 281 282 287 public int hashCode() { 288 289 return m_uuid.hashCode(); 290 } 291 292 297 public boolean isNullUUID() { 298 299 if (this == NULL_UUID) { 300 return true; 301 } 302 return m_uuid.equals(UUID.getNullUUID()); 303 } 304 305 308 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException { 309 310 Object o = null; 311 312 try { 313 314 o = in.readObject(); 315 316 } catch (Exception e) { 317 if (in.readLong() == serialVersionUID) { 321 m_uuid = new UUID((String )in.readObject()); 322 } else { 323 throw new IOException ("Cannot read externalized UUID because of a version mismatch."); 324 } 325 } 326 327 if (o != null) { 328 329 if (o instanceof String ) { 330 if (LOG.isDebugEnabled()) { 332 LOG.debug(Messages.get().getBundle().key(Messages.LOG_READ_UUID_1, o)); 333 } 334 m_uuid = new UUID((String )o); 335 } else if (o instanceof UUID) { 336 if (LOG.isDebugEnabled()) { 338 LOG.debug(Messages.get().getBundle().key(Messages.LOG_READ_UUID_OLD_1, o)); 339 } 340 m_uuid = (UUID)o; 341 } 342 } 343 344 if (m_uuid == null) { 346 if (LOG.isDebugEnabled()) { 348 LOG.debug(Messages.get().getBundle().key(Messages.LOG_ERR_READ_UUID_0)); 349 } 350 throw new IOException ("Cannot read externalized UUID."); 351 } 352 353 } 354 355 360 public byte[] toByteArray() { 361 362 return m_uuid.toByteArray(); 363 } 364 365 368 public String toString() { 369 370 return m_uuid.toString(); 371 } 372 373 377 public void writeExternal(ObjectOutput out) throws IOException { 378 379 if (LOG.isDebugEnabled()) { 380 LOG.debug(Messages.get().getBundle().key(Messages.LOG_WRITE_UUID_1, m_uuid.toString())); 381 } 382 out.writeObject(m_uuid.toString()); 383 } 384 } | Popular Tags |