Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
GUID |
|
| 1.6;1.6 |
1 | package org.apache.ojb.broker.util; | |
2 | ||
3 | /* Copyright 2002-2005 The Apache Software Foundation | |
4 | * | |
5 | * Licensed under the Apache License, Version 2.0 (the "License"); | |
6 | * you may not use this file except in compliance with the License. | |
7 | * You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | import java.io.Serializable; | |
19 | import java.net.InetAddress; | |
20 | import java.net.UnknownHostException; | |
21 | import java.rmi.server.UID; | |
22 | ||
23 | /** | |
24 | * simple GUID (Globally Unique ID) implementation. | |
25 | * A GUID is composed of two parts: | |
26 | * 1. The IP-Address of the local machine. | |
27 | * 2. A java.rmi.server.UID | |
28 | * | |
29 | * @author Thomas Mahler | |
30 | * @version $Id: GUID.java,v 1.1 2007-08-24 22:17:36 ewestfal Exp $ | |
31 | */ | |
32 | public class GUID implements Serializable | |
33 | { | |
34 | static final long serialVersionUID = -6163239155380515945L; /** | |
35 | * holds the hostname of the local machine. | |
36 | */ | |
37 | private static String localIPAddress; | |
38 | ||
39 | /** | |
40 | * String representation of the GUID | |
41 | */ | |
42 | private String guid; | |
43 | ||
44 | /** | |
45 | * compute the local IP-Address | |
46 | */ | |
47 | static | |
48 | { | |
49 | try | |
50 | { | |
51 | localIPAddress = InetAddress.getLocalHost().getHostAddress(); | |
52 | } | |
53 | catch (UnknownHostException e) | |
54 | { | |
55 | localIPAddress = "localhost"; | |
56 | } | |
57 | } | |
58 | ||
59 | /** | |
60 | * public no args constructor. | |
61 | */ | |
62 | public GUID() | |
63 | { | |
64 | UID uid = new UID(); | |
65 | StringBuffer buf = new StringBuffer(); | |
66 | buf.append(localIPAddress); | |
67 | buf.append(":"); | |
68 | buf.append(uid.toString()); | |
69 | guid = buf.toString(); | |
70 | } | |
71 | ||
72 | /** | |
73 | * public constructor. | |
74 | * The caller is responsible to feed a globally unique | |
75 | * String into the theGuidString parameter | |
76 | * @param theGuidString a globally unique String | |
77 | */ | |
78 | public GUID(String theGuidString) | |
79 | { | |
80 | guid = theGuidString; | |
81 | } | |
82 | ||
83 | /** | |
84 | * returns the String representation of the GUID | |
85 | */ | |
86 | public String toString() | |
87 | { | |
88 | return guid; | |
89 | } | |
90 | /** | |
91 | * @see java.lang.Object#equals(Object) | |
92 | */ | |
93 | public boolean equals(Object obj) | |
94 | { | |
95 | if (obj instanceof GUID) | |
96 | { | |
97 | if (guid.equals(((GUID) obj).guid)) | |
98 | { | |
99 | 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 | } |