1 /*
2 * Created on 2004.06.05.
3 *
4 * In p2p project
5 */
6
7
8 package net.sf.bigyo.container.jmx;
9
10 import java.io.File;
11 import java.io.IOException;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.List;
16 import java.util.Map;
17
18 import javax.management.AttributeNotFoundException;
19 import javax.management.InstanceAlreadyExistsException;
20 import javax.management.InstanceNotFoundException;
21 import javax.management.JMException;
22 import javax.management.MBeanException;
23 import javax.management.MBeanServer;
24 import javax.management.MBeanServerFactory;
25 import javax.management.MalformedObjectNameException;
26 import javax.management.NotCompliantMBeanException;
27 import javax.management.ObjectName;
28 import javax.management.ReflectionException;
29 import javax.management.remote.JMXAuthenticator;
30 import javax.management.remote.JMXConnectorServer;
31 import javax.management.remote.JMXConnectorServerFactory;
32 import javax.management.remote.JMXServiceURL;
33
34 import mx4j.tools.naming.NamingService;
35 import mx4j.tools.remote.PasswordAuthenticator;
36
37 import org.apache.log4j.LogManager;
38 import org.apache.log4j.Logger;
39
40 /***
41 * @author zsombor
42 *
43 * Created at 3:22:25 net.sf.bigyo.container.jmx.MX4JRemote
44 *
45 * @bigyo-component remote-management
46 */
47 public class MX4JRemote {
48 final static Logger LOG = LogManager.getLogger(MX4JRemote.class);
49
50 JMXConnectorServer connectorServer;
51 MBeanServer server;
52
53 MX4JConfig config;
54 //private int port = 1099;
55
56 //protected String host;
57
58 //String passwordFile;//= "mx4j.properties";
59
60 List publications = new ArrayList();
61
62 /***
63 * @bigyo-constructor
64 */
65 public MX4JRemote(MX4JConfig config) {
66 this.config = config;
67 }
68
69 /***
70 * @bigyo-start @throws
71 * MalformedObjectNameException
72 * @throws InstanceAlreadyExistsException
73 * @throws NotCompliantMBeanException
74 * @throws IOException
75 * @throws AttributeNotFoundException
76 * @throws InstanceNotFoundException
77 * @throws MBeanException
78 * @throws ReflectionException
79 */
80 public synchronized void startManagement() throws MalformedObjectNameException, InstanceAlreadyExistsException,
81 NotCompliantMBeanException, IOException, AttributeNotFoundException, InstanceNotFoundException,
82 MBeanException, ReflectionException {
83
84 LOG.info("MX4j connector startManagement called");
85
86 // Specify the authenticator in the environment Map, using the
87 // standard property JMXConnector.AUTHENTICATOR
88 Map environment = null;
89 if (config.getPasswdFile() != null) {
90 //TODO: sajat authenticator db alapjan
91 environment = new HashMap();
92 JMXAuthenticator authenticator = new PasswordAuthenticator(config.getPasswdFile());
93 environment.put(JMXConnectorServer.AUTHENTICATOR, authenticator);
94 }
95
96 // The MBeanServer
97 server = MBeanServerFactory.createMBeanServer();
98
99 LOG.info("MBeanServer created:" + server);
100
101 // Register and start the rmiregistry MBean, needed by JSR 160
102 // RMIConnectorServer
103 ObjectName namingName = ObjectName.getInstance("naming:type=rmiregistry");
104 NamingService ns = new NamingService(config.getPort());
105 ns.start();
106 server.registerMBean(ns, namingName);
107 /*
108 * server.createMBean("mx4j.tools.naming.NamingService", namingName,
109 * null); server.invoke(namingName, "start", null, null); int namingPort =
110 * ((Integer)server.getAttribute(namingName, "Port")).intValue();
111 */
112 int namingPort = ns.getPort();
113 LOG.info("naming server started at " + namingPort);
114
115 String jndiPath = "/jmxconnector";
116 JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://localhost/jndi/rmi://localhost:" + namingPort
117 + jndiPath);
118
119 // Create and start the RMIConnectorServer
120 connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server);
121 connectorServer.start();
122
123 LOG.info("connector server started");
124
125 for (Iterator iter = publications.iterator(); iter.hasNext();) {
126 PublishCommand pc = (PublishCommand) iter.next();
127 LOG.info("publish '" + pc.mbean + "' named as '" + pc.objectName + '\'');
128 server.registerMBean(pc.mbean, ObjectName.getInstance(pc.objectName));
129
130 }
131
132 }
133
134 /***
135 * @bigyo-stop
136 * @throws IOException
137 */
138 public void stopManagement() throws IOException {
139 if (connectorServer != null)
140 connectorServer.stop();
141 }
142
143
144 static class PublishCommand {
145 String objectName;
146 Object mbean;
147
148 /***
149 * @param objectName
150 * @param mbean
151 */
152 public PublishCommand(String objectName, Object mbean) {
153 super();
154 this.objectName = objectName;
155 this.mbean = mbean;
156 }
157 }
158
159 /***
160 * publish the MBean with the specified name.
161 *
162 * @param objectName
163 * @param mbean
164 * @throws JMException
165 */
166 public synchronized void publishJMX(String objectName, Object mbean) throws JMException {
167 if (server != null) {
168 LOG.info("publish '" + mbean + "' named as '" + objectName + '\'');
169 server.registerMBean(mbean, ObjectName.getInstance(objectName));
170 } else {
171 LOG.info("skip publish '" + mbean + "' named as '" + objectName + '\'');
172 publications.add(new PublishCommand(objectName, mbean));
173 }
174
175 }
176
177 }
This page was automatically generated by Maven