KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > id > VMID


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util.id;
23
24 import java.net.InetAddress JavaDoc;
25
26 import java.security.AccessController JavaDoc;
27 import java.security.PrivilegedAction JavaDoc;
28 import java.util.Arrays JavaDoc;
29
30 import org.jboss.util.HashCode;
31 import org.jboss.util.platform.PID;
32
33 /**
34  * An object that uniquely identifies a virtual machine.
35  *
36  * <p>The identifier is composed of:
37  * <ol>
38  * <li>The Internet address of the physical machine.</li>
39  * <li>The process identifier of the virtual machine.</li>
40  * <li>A UID to guarantee uniqness across multipule virtual
41  * machines on the same physical machine.</li>
42  * </ol>
43  *
44  * <pre>
45  * [ address ] - [ process id ] - [ time ] - [ counter ]
46  * |------- UID --------|
47  * </pre>
48  *
49  * <p>Numbers are converted to radix(Character.MAX_RADIX) when converting
50  * to strings.
51  *
52  * @see UID
53  *
54  * @version <tt>$Revision: 1958 $</tt>
55  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
56  */

57 public class VMID
58    implements ID
59 {
60    /** The address of the current virtual machine */
61    protected final byte[] address;
62
63    /** The process identifier of the current virtual machine */
64    protected final PID pid;
65
66    /** A unique identifier to ensure uniqueness across the host machine */
67    protected final UID uid;
68
69    /** The hash code of this VMID */
70    protected final int hashCode;
71
72    /**
73     * Construct a new VMID.
74     *
75     * @param address The address of the current virtual machine.
76     * @param pid Process identifier.
77     * @param uid Unique identifier.
78     *
79     * @see #getInstance() For getting a VMID instance reference.
80     */

81    protected VMID(final byte[] address, final PID pid, final UID uid) {
82       this.address = address;
83       this.pid = pid;
84       this.uid = uid;
85
86       // generate a hashCode for this VMID
87
int code = pid.hashCode();
88       code ^= uid.hashCode();
89       code ^= HashCode.generate(address);
90       hashCode = code;
91    }
92
93    /**
94     * Copy a VMID.
95     *
96     * @param vmid VMID to copy.
97     */

98    protected VMID(final VMID vmid) {
99       this.address = vmid.address;
100       this.pid = vmid.pid;
101       this.uid = vmid.uid;
102       this.hashCode = vmid.hashCode;
103    }
104
105    /**
106     * Get the address portion of this VMID.
107     *
108     * @return The address portion of this VMID.
109     */

110    public final byte[] getAddress() {
111       return address;
112    }
113
114    /**
115     * Get the process identifier portion of this VMID.
116     *
117     * @return The process identifier portion of this VMID.
118     */

119    public final PID getProcessID() {
120       return pid;
121    }
122
123    /**
124     * Get the UID portion of this VMID.
125     *
126     * @return The UID portion of this VMID.
127     */

128    public final UID getUID() {
129       return uid;
130    }
131
132    /**
133     * Return a string representation of this VMID.
134     *
135     * @return A string representation of this VMID.
136     */

137    public String JavaDoc toString() {
138       StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
139       
140       for (int i=0; i<address.length; i++) {
141          int n = (int) (address[i] & 0xFF);
142          buff.append(Integer.toString(n, Character.MAX_RADIX));
143       }
144
145       buff.append("-").append(pid.toString(Character.MAX_RADIX));
146       buff.append("-").append(uid);
147
148       return buff.toString();
149    }
150
151    /**
152     * Return the hash code of this VMID.
153     *
154     * @return The hash code of this VMID.
155     */

156    public final int hashCode() {
157       return hashCode;
158    }
159
160    /**
161     * Check if the given object is equal to this VMID.
162     *
163     * <p>A VMID is equals to another VMID if the address,
164     * process identifer and UID portions are equal.
165     *
166     * @param obj Object to test equality with.
167     * @return True if object is equals to this VMID.
168     */

169    public boolean equals(final Object JavaDoc obj) {
170       if (obj == this) return true;
171
172       if (obj != null && obj.getClass() == getClass()) {
173          VMID vmid = (VMID)obj;
174          return
175             Arrays.equals(vmid.address, address) &&
176             vmid.pid.equals(pid) &&
177             vmid.uid.equals(uid);
178       }
179
180       return false;
181    }
182
183    /**
184     * Returns a copy of this VMID.
185     *
186     * @return A copy of this VMID.
187     */

188    public Object JavaDoc clone() {
189       try {
190          return super.clone();
191       }
192       catch (CloneNotSupportedException JavaDoc e) {
193          throw new InternalError JavaDoc();
194       }
195    }
196
197    /**
198     * Returns a VMID as a string.
199     *
200     * @return VMID as a string.
201     */

202    public static String JavaDoc asString() {
203       return getInstance().toString();
204    }
205
206
207    /////////////////////////////////////////////////////////////////////////
208
// Instance Access //
209
/////////////////////////////////////////////////////////////////////////
210

211    /** The single instance of VMID for the running Virtual Machine */
212    private static VMID instance = null;
213
214    /**
215     * Get the VMID for the current virtual machine.
216     *
217     * @return Virtual machine identifier.
218     *
219     */

220    public synchronized static VMID getInstance() {
221       if (instance == null) {
222          instance = create();
223       }
224       return instance;
225    }
226
227    /**
228     * The address used when conventional methods fail to return the address
229     * of the current machine.
230     */

231    public static final byte[] UNKNOWN_HOST = { 0, 0, 0, 0 };
232
233    /**
234     * Return the current host internet address.
235     */

236    private static byte[] getHostAddress() {
237       return (byte[]) AccessController.doPrivileged(new PrivilegedAction JavaDoc() {
238             public Object JavaDoc run() {
239                try {
240                   return InetAddress.getLocalHost().getAddress();
241                }
242                catch (Exception JavaDoc e) {
243                   return UNKNOWN_HOST;
244                }
245             }
246          });
247    }
248
249    /**
250     * Create the VMID for the current virtual mahcine.
251     *
252     * @return Virtual machine identifer.
253     */

254    private static VMID create() {
255       // get the local internet address for the current host
256
byte[] address = getHostAddress();
257          
258       return new VMID(address, PID.getInstance(), new UID());
259    }
260 }
261
Popular Tags