KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > remoting > ident > Identity


1 /***************************************
2  * *
3  * JBoss: The OpenSource J2EE WebOS *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  ***************************************/

9 package org.jboss.remoting.ident;
10
11 import java.io.File JavaDoc;
12 import java.io.FileInputStream JavaDoc;
13 import java.io.FileOutputStream JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.io.OutputStream JavaDoc;
16 import java.io.Serializable JavaDoc;
17 import java.net.InetAddress JavaDoc;
18 import java.rmi.dgc.VMID JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Random JavaDoc;
22 import java.util.WeakHashMap JavaDoc;
23 import javax.management.MBeanServer JavaDoc;
24 import javax.management.ObjectName JavaDoc;
25 import org.jboss.remoting.network.NetworkRegistry;
26
27 /**
28  * Identity is used to uniquely identify a JBoss server instance on the network.
29  *
30  * @author <a HREF="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
31  * @version $Revision: 1.2 $
32  */

33 public class Identity implements Serializable JavaDoc
34 {
35    static final long serialVersionUID = -2788084303665751253L;
36
37    private static transient Random JavaDoc random = new Random JavaDoc(System.currentTimeMillis());
38    public static transient String JavaDoc DEFAULT_DOMAIN = "JBOSS";
39    private static transient String JavaDoc _domain = System.getProperty("jboss.identity.domain", DEFAULT_DOMAIN);
40    private static transient Map JavaDoc identities = new WeakHashMap JavaDoc(2);
41
42    private final String JavaDoc instanceid;
43    private final InetAddress JavaDoc ip;
44    private final String JavaDoc serverid;
45    private String JavaDoc domain;
46    private int hashCode;
47
48    private Identity(InetAddress JavaDoc addr, String JavaDoc instanceid, String JavaDoc serverid)
49    {
50       this.ip = addr;
51       this.instanceid = instanceid;
52       this.serverid = serverid;
53       this.domain = ((_domain == null || _domain.equals("")) ? DEFAULT_DOMAIN : _domain);
54       calcHashCode();
55    }
56
57    private void calcHashCode()
58    {
59       this.hashCode = (ip.hashCode() + instanceid.hashCode() + serverid.hashCode() - domain.hashCode());
60    }
61
62    /**
63     * set the domain for all active mbean servers
64     */

65    public static void setDomain(String JavaDoc domain)
66    {
67       Iterator JavaDoc iter = identities.keySet().iterator();
68       while(iter.hasNext())
69       {
70          Identity ident = (Identity) identities.get(iter.next());
71          if(ident != null)
72          {
73             ident.domain = domain;
74          }
75          ident.calcHashCode();
76       }
77       System.setProperty("jboss.identity.domain", domain);
78       _domain = domain;
79       NetworkRegistry.getInstance().changeDomain(domain);
80    }
81
82
83    public int hashCode()
84    {
85       return hashCode;
86    }
87
88
89    public String JavaDoc toString()
90    {
91       return "JBOSS Identity [address:" + ip + ",instanceid:" + instanceid + ",JMX id:" + serverid + ",domain:" + domain + "]";
92    }
93
94    /**
95     * return the domain for the server
96     *
97     * @return
98     */

99    public final String JavaDoc getDomain()
100    {
101       return domain;
102    }
103
104    /**
105     * return the JBOSS Instance ID, which is the same between reboots of the same
106     * JBOSS server instance.
107     *
108     * @return
109     */

110    public String JavaDoc getInstanceId()
111    {
112       return this.instanceid;
113    }
114
115    /**
116     * return the JBOSS IP Address of the instance
117     *
118     * @return
119     */

120    public InetAddress JavaDoc getAddress()
121    {
122       return this.ip;
123    }
124
125    /**
126     * return the JMX server ID for the JBoss instance, which is different between
127     * reboots of the same JBOSS server instance.
128     *
129     * @return
130     */

131    public String JavaDoc getJMXId()
132    {
133       return this.serverid;
134    }
135
136    /**
137     * returns true if the identity represents the same JVM
138     *
139     * @param identity
140     * @return
141     */

142    public boolean isSameJVM(Identity identity)
143    {
144       return identity.equals(this);
145    }
146
147    /**
148     * returns true if the identity represents the same JBOSS Instance, although
149     * may not be the exact same process space since the JVM process space might be
150     * different (such as a reboot).
151     *
152     * @param identity
153     * @return
154     */

155    public boolean isSameInstance(Identity identity)
156    {
157       return identity.getInstanceId().equals(instanceid);
158    }
159
160    /**
161     * returns true if the identity is on the same JBOSS machine, represented by the
162     * same IP address (this may not work in the case several physically different
163     * machines have the same IP Address).
164     *
165     * @param identity
166     * @return
167     */

168    public boolean isSameMachine(Identity identity)
169    {
170       return identity.getAddress().equals(ip);
171    }
172
173    public boolean equals(Object JavaDoc obj)
174    {
175       if(obj instanceof Identity)
176       {
177          return hashCode == obj.hashCode();
178       }
179       return false;
180    }
181
182    public static synchronized final Identity get(MBeanServer JavaDoc server)
183    {
184       if(identities.containsKey(server))
185       {
186          return (Identity) identities.get(server);
187       }
188       try
189       {
190          String JavaDoc serverid = (String JavaDoc) server.getAttribute(new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate"), "MBeanServerId");
191          Identity identity = new Identity(InetAddress.getLocalHost(), createId(server), serverid);
192          identities.put(server, identity);
193          return identity;
194       }
195       catch(Exception JavaDoc ex)
196       {
197          throw new RuntimeException JavaDoc("Exception creating identity: " + ex.getMessage());
198       }
199    }
200
201    private static final synchronized String JavaDoc createId(MBeanServer JavaDoc server)
202    {
203       // we can set as a system property
204
String JavaDoc myid = System.getProperty("jboss.identity");
205       if(myid != null)
206       {
207          return myid;
208       }
209       String JavaDoc id = null;
210       File JavaDoc file = null;
211       try
212       {
213          // FIRST TRY THE JBOSS guy to determine our data directory
214
ObjectName JavaDoc obj = new ObjectName JavaDoc("jboss.system:type=ServerConfig");
215          File JavaDoc dir = (File JavaDoc) server.getAttribute(obj, "ServerDataDir");
216          if(dir != null)
217          {
218             if(dir.exists() == false)
219             {
220                dir.mkdirs();
221             }
222             file = new File JavaDoc(dir, "jboss.identity");
223          }
224       }
225       catch(Exception JavaDoc ex)
226       {
227       }
228       if(file == null)
229       {
230          // we may not have that mbean, which is OK
231
String JavaDoc fl = System.getProperty("jboss.identity.dir", ".");
232          File JavaDoc dir = new File JavaDoc(fl);
233          if(dir.exists() == false)
234          {
235             dir.mkdirs();
236          }
237          file = new File JavaDoc(dir, "jboss.identity");
238       }
239       if(file.exists() && file.canRead())
240       {
241          InputStream JavaDoc is = null;
242          try
243          {
244             is = new FileInputStream JavaDoc(file);
245             byte buf[] = new byte[800];
246             int c = is.read(buf);
247             id = new String JavaDoc(buf, 0, c);
248          }
249          catch(Exception JavaDoc ex)
250          {
251             throw new RuntimeException JavaDoc("Error loading jboss.identity: " + ex.toString());
252          }
253          finally
254          {
255             if(is != null)
256             {
257                try
258                {
259                   is.close();
260                }
261                catch(Exception JavaDoc ig)
262                {
263                }
264             }
265          }
266       }
267       else
268       {
269          OutputStream JavaDoc out = null;
270          try
271          {
272             id = createUniqueID();
273             if(file.exists() == false)
274             {
275                file.createNewFile();
276             }
277             out = new FileOutputStream JavaDoc(file);
278             out.write(id.getBytes());
279          }
280          catch(Exception JavaDoc ex)
281          {
282             throw new RuntimeException JavaDoc("Error creating Instance ID: " + ex.toString());
283          }
284          finally
285          {
286             if(out != null)
287             {
288                try
289                {
290                   out.flush();
291                   out.close();
292                }
293                catch(Exception JavaDoc ig)
294                {
295                }
296             }
297          }
298       }
299       System.setProperty("jboss.identity", id);
300       return id;
301    }
302
303    public static final String JavaDoc createUniqueID()
304    {
305       String JavaDoc id = new VMID JavaDoc().toString();
306       // colons don't work in JMX
307
return id.replace(':', 'x') + random.nextInt(1000);
308    }
309 }
310
Popular Tags