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.1 2007-08-24 22:17:36 ewestfal Exp $
031     */
032    public class GUID implements Serializable
033    {
034            static final long serialVersionUID = -6163239155380515945L;    /**
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        {
049            try
050            {
051                localIPAddress = InetAddress.getLocalHost().getHostAddress();
052            }
053            catch (UnknownHostException e)
054            {
055                localIPAddress = "localhost";
056            }
057        }
058    
059        /**
060         * public no args constructor.
061         */
062        public GUID()
063        {
064            UID uid = new UID();
065            StringBuffer buf = new StringBuffer();
066            buf.append(localIPAddress);
067            buf.append(":");
068            buf.append(uid.toString());
069            guid = buf.toString();
070        }
071     
072        /**
073         * public constructor.
074         * The caller is responsible to feed a globally unique 
075         * String into the theGuidString parameter
076         * @param theGuidString a globally unique String
077         */    
078        public GUID(String theGuidString)
079        {
080            guid = theGuidString;   
081        }
082    
083        /**
084         *  returns the String representation of the GUID
085         */
086        public String toString()
087        {
088            return guid;
089        }
090        /**
091         * @see java.lang.Object#equals(Object)
092         */
093        public boolean equals(Object obj)
094        {
095            if (obj instanceof GUID)
096            {
097                    if (guid.equals(((GUID) obj).guid))
098                    {
099                            return true;    
100                    }       
101            }
102            return false;
103        }
104    
105        /**
106         * @see java.lang.Object#hashCode()
107         */
108        public int hashCode()
109        {
110            return guid.hashCode();
111        }
112    
113    }