KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > platform > PID


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.platform;
23
24 import java.io.Serializable JavaDoc;
25
26 import java.util.Random JavaDoc;
27
28 /**
29  * Provides access to the process identifier for this virtual machine.
30  *
31  * <p>Currently does not support native access and generates random numbers
32  * for the process id.
33  *
34  * @version <tt>$Revision: 1958 $</tt>
35  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
36  */

37 public class PID
38    implements Serializable JavaDoc, Cloneable JavaDoc
39 {
40    /** The <tt>int</tt> process identifier. */
41    protected final int id;
42
43    /**
44     * Construct a new PID.
45     *
46     * @param id Process identifier.
47     */

48    protected PID(final int id) {
49       this.id = id;
50    }
51
52    /**
53     * Get the <tt>int</tt> process identifier.
54     *
55     * @return <tt>int</tt> process identifier.
56     */

57    public final int getID() {
58       return id;
59    }
60
61    /**
62     * Return a string representation of this PID.
63     *
64     * @return A string representation of this PID.
65     */

66    public String JavaDoc toString() {
67       return String.valueOf(id);
68    }
69
70    /**
71     * Return a string representation of this PID.
72     *
73     * @return A string representation of this PID.
74     */

75    public String JavaDoc toString(int radix) {
76       return Integer.toString(id, radix);
77    }
78
79    /**
80     * Return the hash code of this PID.
81     *
82     * @return The hash code of this PID.
83     */

84    public int hashCode() {
85       return id;
86    }
87
88    /**
89     * Check if the given object is equal to this PID.
90     *
91     * @param obj Object to test equality with.
92     * @return True if object is equals to this PID.
93     */

94    public boolean equals(final Object JavaDoc obj) {
95       if (obj == this) return true;
96
97       if (obj != null && obj.getClass() == getClass()) {
98          PID pid = (PID)obj;
99          return pid.id == id;
100       }
101
102       return false;
103    }
104
105    /**
106     * Returns a copy of this PID.
107     *
108     * @return A copy of this PID.
109     */

110    public Object JavaDoc clone() {
111       try {
112          return super.clone();
113       }
114       catch (CloneNotSupportedException JavaDoc e) {
115          throw new InternalError JavaDoc();
116       }
117    }
118
119
120    /////////////////////////////////////////////////////////////////////////
121
// Instance Access //
122
/////////////////////////////////////////////////////////////////////////
123

124    /** The single instance of PID for the running Virtual Machine */
125    private static PID instance = null;
126
127    /**
128     * Get the PID for the current virtual machine.
129     *
130     * @return Process identifier.
131     */

132    public synchronized static PID getInstance() {
133       if (instance == null) {
134          instance = create();
135       }
136       return instance;
137    }
138
139    /**
140     * Create the PID for the current virtual mahcine.
141     *
142     * @return Process identifier.
143     */

144    private static PID create() {
145       // for now just return a random integer.
146
int random = Math.abs(new Random JavaDoc().nextInt());
147       return new PID(random);
148    }
149 }
150
Popular Tags