KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > mx > remoting > service > JMXConnectorServerService


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.mx.remoting.service;
8
9 import java.io.IOException JavaDoc;
10 import java.net.InetAddress JavaDoc;
11 import java.rmi.registry.LocateRegistry JavaDoc;
12 import java.rmi.registry.Registry JavaDoc;
13 import java.rmi.RemoteException JavaDoc;
14 import javax.management.MBeanServer JavaDoc;
15 import javax.management.ObjectName JavaDoc;
16 import javax.management.remote.JMXConnectorServer JavaDoc;
17 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
18 import javax.management.remote.JMXServiceURL JavaDoc;
19 import org.jboss.logging.Logger;
20
21 /**
22  * Service mbean for starting the JMX Remoting (JSR-160) connector server.
23  * @author <a HREF="mailto:tom.elrod@jboss.com">Tom Elrod</a>
24  */

25 public class JMXConnectorServerService implements JMXConnectorServerServiceMBean
26 {
27    /**
28     * Default jndi path used to specify location of connector server.
29     * Value is '/jmxconnector'.
30     */

31    public static final String JavaDoc JNDI_PATH_DEFAULT = "/jmxconnector";
32
33    private MBeanServer JavaDoc mbeanServer;
34
35    private int registryPort = Registry.REGISTRY_PORT;
36    private String JavaDoc bindAddress = null;
37    private String JavaDoc jndiPath = JNDI_PATH_DEFAULT;
38    private JMXConnectorServer JavaDoc connectorServer = null;
39    private Registry JavaDoc rmiRegistry = null;
40
41    private static final Logger log = Logger.getLogger(JMXConnectorServerService.class);
42
43    public void setRegistryPort(int registryPort)
44    {
45       this.registryPort = registryPort;
46    }
47
48    public int getRegistryPort()
49    {
50       return registryPort;
51    }
52
53    public void setBindAddress(String JavaDoc bindAddress)
54    {
55       this.bindAddress = bindAddress;
56    }
57
58    public String JavaDoc getBindAddress()
59    {
60       return bindAddress;
61    }
62
63    public String JavaDoc getJndiPath()
64    {
65       return jndiPath;
66    }
67
68    public void setJndiPath(String JavaDoc jndiPath)
69    {
70       this.jndiPath = jndiPath;
71    }
72
73    public void create() throws Exception JavaDoc
74    {
75       // do nothing, putting code in start
76
}
77
78    public void start() throws Exception JavaDoc
79    {
80       // check to see if registry already created
81
rmiRegistry = LocateRegistry.getRegistry(registryPort);
82       if(rmiRegistry != null)
83       {
84          try
85          {
86             rmiRegistry.list();
87          }
88          catch(RemoteException JavaDoc e)
89          {
90             log.debug("No registry running at port " + registryPort + ". Will create one.");
91             rmiRegistry = LocateRegistry.createRegistry(registryPort);
92          }
93       }
94       else
95       {
96          rmiRegistry = LocateRegistry.createRegistry(registryPort);
97       }
98
99       if(bindAddress == null || bindAddress.equals("0.0.0.0"))
100       {
101          bindAddress = InetAddress.getLocalHost().getHostName();
102       }
103
104       String JavaDoc serviceURL = "service:jmx:rmi://" + bindAddress + "/jndi/rmi://" + bindAddress + ":" + registryPort + jndiPath;
105
106       JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc(serviceURL);
107
108       // create new connector server and start it
109
connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbeanServer);
110       connectorServer.start();
111
112       log.debug("JMX Connector server: " + serviceURL);
113    }
114
115    public void stop() throws IOException JavaDoc
116    {
117       if(connectorServer != null)
118       {
119          connectorServer.stop();
120       }
121    }
122
123    public void destroy()
124    {
125       // do nothing, putting code in stop
126
}
127
128    public ObjectName JavaDoc preRegister(MBeanServer JavaDoc mBeanServer, ObjectName JavaDoc objectName) throws Exception JavaDoc
129    {
130       this.mbeanServer = mBeanServer;
131       return objectName;
132    }
133
134    public void postRegister(Boolean JavaDoc aBoolean)
135    {
136       // no op, needed for MBeanRegistration interface
137
}
138
139    public void preDeregister() throws Exception JavaDoc
140    {
141       // no op, needed for MBeanRegistration interface
142
}
143
144    public void postDeregister()
145    {
146       // no op, needed for MBeanRegistration interface
147
}
148 }
Popular Tags