01: // $Id: JmxServer.java 897 2007-02-09 15:36:10Z grro $
02: /*
03: * Copyright (c) xsocket.org, 2006 - 2007. All rights reserved.
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library; if not, write to the Free Software
17: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: *
19: * Please refer to the LGPL license at: http://www.gnu.org/copyleft/lesser.txt
20: * The latest copy of this software may be found on http://www.xsocket.org/
21: */
22: package org.xsocket.mail;
23:
24: import java.io.IOException;
25: import java.lang.management.ManagementFactory;
26: import java.net.InetAddress;
27: import java.rmi.registry.LocateRegistry;
28: import java.rmi.registry.Registry;
29:
30: import javax.management.MBeanServer;
31: import javax.management.remote.JMXConnectorServer;
32: import javax.management.remote.JMXConnectorServerFactory;
33: import javax.management.remote.JMXServiceURL;
34:
35: /**
36: * Helper class to run a JMXConnectorServer by using rmi
37: *
38: * @author grro@xsocket.org
39: */
40: public final class JmxServer {
41:
42: private JMXConnectorServer server = null;
43:
44: /**
45: * start the server
46: *
47: * @param name the name space
48: */
49: public void start(String name) {
50: start(name, 1199);
51: }
52:
53: /**
54: * start the server
55: *
56: * @param name the name space
57: * @param rmiPort the rmi port to use
58: */
59: public void start(String name, int rmiPort) {
60: try {
61: Registry registry = LocateRegistry.createRegistry(rmiPort);
62: registry.unbind(name);
63: } catch (Exception ignore) {
64: }
65:
66: try {
67: JMXServiceURL url = new JMXServiceURL(
68: "service:jmx:rmi:///jndi/rmi://"
69: + InetAddress.getLocalHost().getHostName()
70: + ":" + rmiPort + "/" + name);
71:
72: MBeanServer mbeanSrv = ManagementFactory
73: .getPlatformMBeanServer();
74: server = JMXConnectorServerFactory.newJMXConnectorServer(
75: url, null, mbeanSrv);
76: server.start();
77: System.out
78: .println("JMX RMI Agent has been bound on address");
79: System.out.println(url);
80: } catch (Exception e) {
81: e.printStackTrace();
82: }
83: }
84:
85: /**
86: * stops the server
87: *
88: */
89: public void stop() {
90: try {
91: server.stop();
92: } catch (IOException ioe) {
93: // ignore
94: }
95: }
96: }
|