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.
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.
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.
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.
22
package org.jboss.remoting.ident;
24
import org.jboss.remoting.network.NetworkRegistry;
25
import org.jboss.remoting.util.SecurityUtility;
27
import javax.management.MBeanServer;
28
import javax.management.ObjectName;
30
import java.io.FileInputStream;
31
import java.io.FileNotFoundException;
32
import java.io.FileOutputStream;
33
import java.io.IOException;
34
import java.io.InputStream;
35
import java.io.OutputStream;
36
import java.io.Serializable;
37
import java.net.InetAddress;
38
import java.net.UnknownHostException;
39
import java.rmi.dgc.VMID;
40
import java.security.AccessController;
41
import java.security.PrivilegedAction;
42
import java.security.PrivilegedActionException;
43
import java.security.PrivilegedExceptionAction;
44
import java.util.Iterator;
46
import java.util.Random;
47
import java.util.WeakHashMap;
50
* Identity is used to uniquely identify a JBoss server instance on the network.
52
* @author <a href="mailto:jhaynie@vocalocity.net">Jeff Haynie</a>
53
* @version $Revision: 5002 $
55
public class Identity implements Serializable
57
static final long serialVersionUID = -2788084303665751253L;
59
private static transient Random random = new Random(System.currentTimeMillis());
60
public static transient String DEFAULT_DOMAIN = "JBOSS";
61
private static transient String _domain = DEFAULT_DOMAIN;
65
_domain = getSystemProperty("jboss.identity.domain", DEFAULT_DOMAIN);
68
private static transient Map identities = new WeakHashMap(2);
70
private final String instanceid;
71
private final InetAddress ip;
72
private final String serverid;
73
private String domain;
76
private Identity(InetAddress addr, String instanceid, String serverid)
79
this.instanceid = instanceid;
80
this.serverid = serverid;
81
this.domain = ((_domain == null || _domain.equals("")) ? DEFAULT_DOMAIN : _domain);
85
private void calcHashCode()
87
this.hashCode = (ip.hashCode() + instanceid.hashCode() + serverid.hashCode() - domain.hashCode());
91
* set the domain for all active mbean servers
93
public static void setDomain(String domain)
95
Iterator iter = identities.keySet().iterator();
98
Identity ident = (Identity) identities.get(iter.next());
101
ident.domain = domain;
103
ident.calcHashCode();
105
setSystemProperty("jboss.identity.domain", domain);
107
NetworkRegistry.getInstance().changeDomain(domain);
111
public int hashCode()
117
public String toString()
119
return "JBOSS Identity\n\taddress:" + ip + "\n\tinstanceid:" + instanceid + "\n\tJMX id:" + serverid + "\n\tdomain:" + domain + "\n";
123
* return the domain for the server
127
public final String getDomain()
133
* return the JBOSS Instance ID, which is the same between reboots of the same
134
* JBOSS server instance.
138
public String getInstanceId()
140
return this.instanceid;
144
* return the JBOSS IP Address of the instance
148
public InetAddress getAddress()
154
* return the JMX server ID for the JBoss instance, which is different between
155
* reboots of the same JBOSS server instance.
159
public String getJMXId()
161
return this.serverid;
165
* returns true if the identity represents the same JVM
170
public boolean isSameJVM(Identity identity)
172
return identity.equals(this);
176
* returns true if the identity represents the same JBOSS Instance, although
177
* may not be the exact same process space since the JVM process space might be
178
* different (such as a reboot).
183
public boolean isSameInstance(Identity identity)
185
return identity.getInstanceId().equals(instanceid);
189
* returns true if the identity is on the same JBOSS machine, represented by the
190
* same IP address (this may not work in the case several physically different
191
* machines have the same IP Address).
196
public boolean isSameMachine(Identity identity)
198
return identity.getAddress().equals(ip);
201
public boolean equals(Object obj)
203
if(obj instanceof Identity)
205
return hashCode == obj.hashCode();
210
public static synchronized final Identity get(final MBeanServer server)
212
if(identities.containsKey(server))
214
return (Identity) identities.get(server);
218
InetAddress localHost = getLocalHost();
219
ObjectName objectName = new ObjectName("JMImplementation:type=MBeanServerDelegate");
220
String serverid = (String) getMBeanAttribute(server, objectName, "MBeanServerId");
221
Identity identity = new Identity(localHost, createId(server), serverid);
222
identities.put(server, identity);
227
String type = ex.getClass().getName();
228
final RuntimeException rex = new RuntimeException("Exception creating identity: " + type + ": " + ex.getMessage());
229
rex.setStackTrace(ex.getStackTrace());
234
private static final synchronized String createId(final MBeanServer server)
236
// we can set as a system property
237
String myid = getSystemProperty("jboss.identity");
246
// FIRST TRY THE JBOSS guy to determine our data directory
247
final ObjectName obj = new ObjectName("jboss.system:type=ServerConfig");
248
File dir = (File) getMBeanAttribute(server, obj, "ServerDataDir");
251
if(fileExists(dir) == false)
255
file = new File(dir, "jboss.identity");
263
// we may not have that mbean, which is OK
264
String fl = getSystemProperty("jboss.identity.dir", ".");
266
File dir = new File(fl);
267
if(fileExists(dir) == false)
271
file = new File(dir, "jboss.identity");
274
boolean canRead = canRead(file);
275
if(fileExists(file) && canRead)
277
InputStream is = null;
280
is = getFileInputStream(file);
281
byte buf[] = new byte[800];
282
int c = is.read(buf);
283
id = new String(buf, 0, c);
287
throw new RuntimeException("Error loading jboss.identity: " + ex.toString());
305
OutputStream out = null;
308
id = createUniqueID();
309
if(fileExists(file) == false)
314
out = getFileOutputStream(file);
315
out.write(id.getBytes());
319
throw new RuntimeException("Error creating Instance ID: " + ex.toString());
337
setSystemProperty("jboss.identity", id);
342
public static final String createUniqueID()
344
String id = new VMID().toString();
345
// colons don't work in JMX
346
return id.replace(':', 'x') + random.nextInt(1000);
349
static private boolean fileExists(final File file)
354
if (SecurityUtility.skipAccessControl())
356
return file.exists();
359
return ((Boolean)AccessController.doPrivileged( new PrivilegedAction()
363
return new Boolean(file.exists());
368
static private boolean mkdirs(final File dir)
370
if (SecurityUtility.skipAccessControl())
375
return ((Boolean) AccessController.doPrivileged( new PrivilegedAction()
379
return new Boolean(dir.mkdirs());
384
static private FileInputStream getFileInputStream(final File file) throws FileNotFoundException
386
if (SecurityUtility.skipAccessControl())
388
return new FileInputStream(file);
393
return (FileInputStream)AccessController.doPrivileged( new PrivilegedExceptionAction()
395
public Object run() throws FileNotFoundException
397
return new FileInputStream(file);
401
catch (PrivilegedActionException e)
403
throw (FileNotFoundException) e.getCause();
407
static private FileOutputStream getFileOutputStream(final File file)
408
throws FileNotFoundException
410
if (SecurityUtility.skipAccessControl())
412
return new FileOutputStream(file);
417
return (FileOutputStream)AccessController.doPrivileged( new PrivilegedExceptionAction()
419
public Object run() throws FileNotFoundException
421
return new FileOutputStream(file);
425
catch (PrivilegedActionException e)
427
throw (FileNotFoundException) e.getCause();
431
static private boolean canRead(final File file)
433
if (SecurityUtility.skipAccessControl())
435
return file.canRead();
438
return ((Boolean)AccessController.doPrivileged( new PrivilegedAction()
442
return new Boolean(file.canRead());
447
static private boolean createNewFile(final File file) throws IOException
449
if (SecurityUtility.skipAccessControl())
451
return file.createNewFile();
456
return ((Boolean)AccessController.doPrivileged( new PrivilegedExceptionAction()
458
public Object run() throws Exception
460
return new Boolean(file.createNewFile());
466
throw (IOException) e.getCause();
470
static private Object getMBeanAttribute(final MBeanServer server, final ObjectName objectName, final String attribute)
473
if (SecurityUtility.skipAccessControl())
475
return server.getAttribute(objectName, attribute);
480
return AccessController.doPrivileged( new PrivilegedExceptionAction()
482
public Object run() throws Exception
484
return server.getAttribute(objectName, attribute);
488
catch (PrivilegedActionException e)
490
throw (Exception) e.getCause();
494
static private String getSystemProperty(final String name, final String defaultValue)
496
if (SecurityUtility.skipAccessControl())
497
return System.getProperty(name, defaultValue);
502
value = (String)AccessController.doPrivileged( new PrivilegedExceptionAction()
504
public Object run() throws Exception
506
return System.getProperty(name, defaultValue);
510
catch (PrivilegedActionException e)
512
throw (RuntimeException) e.getCause();
518
static private String getSystemProperty(final String name)
520
if (SecurityUtility.skipAccessControl())
521
return System.getProperty(name);
526
value = (String)AccessController.doPrivileged( new PrivilegedExceptionAction()
528
public Object run() throws Exception
530
return System.getProperty(name);
534
catch (PrivilegedActionException e)
536
throw (RuntimeException) e.getCause();
542
static private void setSystemProperty(final String name, final String value)
544
if (SecurityUtility.skipAccessControl())
546
System.setProperty(name, value);
552
AccessController.doPrivileged( new PrivilegedExceptionAction()
554
public Object run() throws Exception
556
return System.setProperty(name, value);
560
catch (PrivilegedActionException e)
562
throw (RuntimeException) e.getCause();
566
static private InetAddress getLocalHost() throws UnknownHostException
568
if (SecurityUtility.skipAccessControl())
572
return InetAddress.getLocalHost();
574
catch (IOException e)
576
return InetAddress.getByName("127.0.0.1");
582
return (InetAddress) AccessController.doPrivileged( new PrivilegedExceptionAction()
584
public Object run() throws IOException
588
return InetAddress.getLocalHost();
590
catch (IOException e)
592
return InetAddress.getByName("127.0.0.1");
597
catch (PrivilegedActionException e)
599
throw (UnknownHostException) e.getCause();