001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mx.util;
023:
024: import java.net.InetAddress;
025: import java.security.AccessController;
026: import java.security.PrivilegedExceptionAction;
027: import java.security.PrivilegedActionException;
028: import java.util.Random;
029:
030: import javax.management.MBeanServer;
031: import javax.management.ObjectName;
032:
033: import EDU.oswego.cs.dl.util.concurrent.SynchronizedLong;
034:
035: import org.jboss.mx.server.ServerConstants;
036:
037: /**
038: * Utility class for creating JMX agent identifiers. Also contains the
039: * helper method for retrieving the <tt>AgentID</tt> of an existing MBean server
040: * instance.
041: *
042: * @see javax.management.MBeanServerDelegateMBean
043: *
044: * @author <a href="mailto:juha@jboss.org">Juha Lindfors</a>.
045: * @version $Revision: 57200 $
046: *
047: */
048: public class AgentID implements ServerConstants {
049: // Static ----------------------------------------------------
050: private static SynchronizedLong id = new SynchronizedLong(0);
051:
052: private static final Random rand = new Random(System
053: .currentTimeMillis());
054:
055: /**
056: * Creates a new agent ID string. The identifier is of the form
057: * <tt><ip.address>/<creation time in ms>/<VMID+(random int 0-100)>/<sequence #></tt>.<P>
058: *
059: * This AgentID string is globally unique.
060: *
061: * @return Agent ID string
062: */
063: public static String create() {
064: String ipAddress = null;
065:
066: try {
067: ipAddress = (String) AccessController
068: .doPrivileged(new PrivilegedExceptionAction() {
069: public Object run() throws Exception {
070: return InetAddress.getLocalHost()
071: .getHostAddress();
072: }
073: });
074: } catch (PrivilegedActionException e) {
075: ipAddress = "127.0.0.1";
076: }
077: // use the VMID to create a more unique ID that can be used to guarantee that this
078: // MBeanServerID is unique across multiple JVMs, even on the same host
079: String vmid = new java.rmi.dgc.VMID().toString().replace(':',
080: 'x').replace('-', 'X')
081: + rand.nextInt(100);
082:
083: return ipAddress + "/" + System.currentTimeMillis() + "/"
084: + vmid + "/" + (id.increment());
085: }
086:
087: /**
088: * test
089: *
090: * @param args
091: */
092: public static void main(String args[]) {
093: for (int c = 0; c < 10; c++)
094: System.out.println(AgentID.create());
095: }
096:
097: /**
098: * Returns the agent identifier string of a given MBean server instance.
099: *
100: * @return <tt>MBeanServerId</tt> attribute of the MBean server delegate.
101: */
102: public static String get(MBeanServer server) {
103: try {
104: ObjectName name = new ObjectName(MBEAN_SERVER_DELEGATE);
105: String agentID = (String) server.getAttribute(name,
106: "MBeanServerId");
107:
108: return agentID;
109: } catch (Throwable t) {
110: throw new Error("Cannot find the MBean server delegate: "
111: + t.toString());
112: }
113: }
114: }
|