1 22 package org.jboss.mx.util; 23 24 import java.net.InetAddress ; 25 import java.security.AccessController ; 26 import java.security.PrivilegedExceptionAction ; 27 import java.security.PrivilegedActionException ; 28 import java.util.Random ; 29 30 import javax.management.MBeanServer ; 31 import javax.management.ObjectName ; 32 33 import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong; 34 35 import org.jboss.mx.server.ServerConstants; 36 37 48 public class AgentID 49 implements ServerConstants 50 { 51 private static SynchronizedLong id = new SynchronizedLong(0); 53 54 private static final Random rand = new Random (System.currentTimeMillis()); 55 56 64 public static String create() 65 { 66 String ipAddress = null; 67 68 try 69 { 70 ipAddress = (String ) AccessController.doPrivileged( 71 new PrivilegedExceptionAction () 72 { 73 public Object run() throws Exception 74 { 75 return InetAddress.getLocalHost().getHostAddress(); 76 } 77 } 78 ); 79 } 80 catch(PrivilegedActionException e) 81 { 82 ipAddress = "127.0.0.1"; 83 } 84 String vmid = new java.rmi.dgc.VMID ().toString().replace(':','x').replace('-','X') + rand.nextInt(100); 87 88 return ipAddress + "/" + System.currentTimeMillis() + "/" + vmid + "/"+ (id.increment()); 89 } 90 95 public static void main (String args[]) 96 { 97 for (int c=0;c<10;c++) 98 System.out.println(AgentID.create()); 99 } 100 101 106 public static String get(MBeanServer server) 107 { 108 try 109 { 110 ObjectName name = new ObjectName (MBEAN_SERVER_DELEGATE); 111 String agentID = (String )server.getAttribute(name, "MBeanServerId"); 112 113 return agentID; 114 } 115 catch (Throwable t) 116 { 117 throw new Error ("Cannot find the MBean server delegate: " + t.toString()); 118 } 119 } 120 } 121 122 123 124 125 | Popular Tags |