001: package org.apache.ojb.broker.util;
002:
003: /* Copyright 2002-2005 The Apache Software Foundation
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: import java.io.Serializable;
019: import java.net.InetAddress;
020: import java.net.UnknownHostException;
021: import java.rmi.server.UID;
022:
023: /**
024: * simple GUID (Globally Unique ID) implementation.
025: * A GUID is composed of two parts:
026: * 1. The IP-Address of the local machine.
027: * 2. A java.rmi.server.UID
028: *
029: * @author Thomas Mahler
030: * @version $Id: GUID.java,v 1.7.2.1 2005/12/21 22:27:47 tomdz Exp $
031: */
032: public class GUID implements Serializable {
033: static final long serialVersionUID = -6163239155380515945L;
034: /**
035: * holds the hostname of the local machine.
036: */
037: private static String localIPAddress;
038:
039: /**
040: * String representation of the GUID
041: */
042: private String guid;
043:
044: /**
045: * compute the local IP-Address
046: */
047: static {
048: try {
049: localIPAddress = InetAddress.getLocalHost()
050: .getHostAddress();
051: } catch (UnknownHostException e) {
052: localIPAddress = "localhost";
053: }
054: }
055:
056: /**
057: * public no args constructor.
058: */
059: public GUID() {
060: UID uid = new UID();
061: StringBuffer buf = new StringBuffer();
062: buf.append(localIPAddress);
063: buf.append(":");
064: buf.append(uid.toString());
065: guid = buf.toString();
066: }
067:
068: /**
069: * public constructor.
070: * The caller is responsible to feed a globally unique
071: * String into the theGuidString parameter
072: * @param theGuidString a globally unique String
073: */
074: public GUID(String theGuidString) {
075: guid = theGuidString;
076: }
077:
078: /**
079: * returns the String representation of the GUID
080: */
081: public String toString() {
082: return guid;
083: }
084:
085: /**
086: * @see java.lang.Object#equals(Object)
087: */
088: public boolean equals(Object obj) {
089: if (obj instanceof GUID) {
090: if (guid.equals(((GUID) obj).guid)) {
091: return true;
092: }
093: }
094: return false;
095: }
096:
097: /**
098: * @see java.lang.Object#hashCode()
099: */
100: public int hashCode() {
101: return guid.hashCode();
102: }
103:
104: }
|