KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > util > AgentID


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.mx.util;
23
24 import java.net.InetAddress JavaDoc;
25 import java.security.AccessController JavaDoc;
26 import java.security.PrivilegedExceptionAction JavaDoc;
27 import java.security.PrivilegedActionException JavaDoc;
28 import java.util.Random JavaDoc;
29
30 import javax.management.MBeanServer JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32
33 import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
34
35 import org.jboss.mx.server.ServerConstants;
36
37 /**
38  * Utility class for creating JMX agent identifiers. Also contains the
39  * helper method for retrieving the <tt>AgentID</tt> of an existing MBean server
40  * instance.
41  *
42  * @see javax.management.MBeanServerDelegateMBean
43  *
44  * @author <a HREF="mailto:juha@jboss.org">Juha Lindfors</a>.
45  * @version $Revision: 37459 $
46  *
47  */

48 public class AgentID
49    implements ServerConstants
50 {
51    // Static ----------------------------------------------------
52
private static SynchronizedLong id = new SynchronizedLong(0);
53
54    private static final Random JavaDoc rand = new Random JavaDoc(System.currentTimeMillis());
55
56    /**
57     * Creates a new agent ID string. The identifier is of the form
58     * <tt>&lt;ip.address&gt;/&lt;creation time in ms&gt;/&lt;VMID+(random int 0-100)&gt;/&lt;sequence #&gt;</tt>.<P>
59     *
60     * This AgentID string is globally unique.
61     *
62     * @return Agent ID string
63     */

64    public static String JavaDoc create()
65    {
66       String JavaDoc ipAddress = null;
67
68       try
69       {
70          ipAddress = (String JavaDoc) AccessController.doPrivileged(
71             new PrivilegedExceptionAction JavaDoc()
72             {
73                public Object JavaDoc run() throws Exception JavaDoc
74                {
75                   return InetAddress.getLocalHost().getHostAddress();
76                }
77             }
78          );
79       }
80       catch(PrivilegedActionException JavaDoc e)
81       {
82          ipAddress = "127.0.0.1";
83       }
84       // use the VMID to create a more unique ID that can be used to guarantee that this
85
// MBeanServerID is unique across multiple JVMs, even on the same host
86
String JavaDoc vmid = new java.rmi.dgc.VMID JavaDoc().toString().replace(':','x').replace('-','X') + rand.nextInt(100);
87
88       return ipAddress + "/" + System.currentTimeMillis() + "/" + vmid + "/"+ (id.increment());
89    }
90     /**
91      * test
92      *
93      * @param args
94      */

95    public static void main (String JavaDoc args[])
96    {
97        for (int c=0;c<10;c++)
98         System.out.println(AgentID.create());
99    }
100
101    /**
102     * Returns the agent identifier string of a given MBean server instance.
103     *
104     * @return <tt>MBeanServerId</tt> attribute of the MBean server delegate.
105     */

106    public static String JavaDoc get(MBeanServer JavaDoc server)
107    {
108       try
109       {
110          ObjectName JavaDoc name = new ObjectName JavaDoc(MBEAN_SERVER_DELEGATE);
111          String JavaDoc agentID = (String JavaDoc)server.getAttribute(name, "MBeanServerId");
112       
113          return agentID;
114       }
115       catch (Throwable JavaDoc t)
116       {
117          throw new Error JavaDoc("Cannot find the MBean server delegate: " + t.toString());
118       }
119    }
120 }
121       
122
123
124
125
Popular Tags