KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > kernel > jmx > RMIConnector


1 /**
2  * PETALS: PETALS Services Platform
3  * Copyright (C) 2005 EBM WebSourcing
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or any later version.
9  *
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
18  * USA.
19  *
20  * Initial developer(s): EBM WebSourcing
21  * --------------------------------------------------------------------------
22  * $Id: MX4JRMIAdaptor.java,v 1.2 2005/07/22 10:24:27 ddesjardins Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.petals.kernel.jmx;
26
27 import java.rmi.registry.LocateRegistry JavaDoc;
28 import java.rmi.registry.Registry JavaDoc;
29
30 import javax.management.remote.JMXConnectorServer JavaDoc;
31 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
32 import javax.management.remote.JMXServiceURL JavaDoc;
33 import javax.management.remote.rmi.RMIConnectorServer JavaDoc;
34
35 import org.objectweb.fractal.fraclet.annotation.FractalComponent;
36 import org.objectweb.fractal.fraclet.annotation.LifeCycle;
37 import org.objectweb.fractal.fraclet.annotation.LifeCycleType;
38 import org.objectweb.fractal.fraclet.annotation.Monolog;
39 import org.objectweb.fractal.fraclet.annotation.Requires;
40 import org.objectweb.fractal.jmx.agent.AdminAttributes;
41 import org.objectweb.petals.util.LoggingUtil;
42 import org.objectweb.petals.util.SystemUtil;
43 import org.objectweb.util.monolog.api.Logger;
44
45 /**
46  * This is the Fractal JMX adaptor for use with MX4J tools.
47  *
48  * @author ddesjardins - eBMWebsourcing
49  */

50 @FractalComponent
51 public class RMIConnector {
52
53     /**
54      * AdminAttributes
55      */

56     @Requires(name="adminAtt",signature=org.objectweb.fractal.jmx.agent.AdminAttributes.class)
57     protected AdminAttributes adminAttributes;
58
59     /**
60      * Logger
61      */

62     protected LoggingUtil log;
63
64     /**
65      * Logger
66      */

67     @Monolog(name="logger")
68     protected Logger logger;
69
70     /**
71      * JMX server
72      */

73     protected JMXConnectorServer JavaDoc cs;
74
75     /**
76      * Start the JMX RMI Server
77      */

78     @LifeCycle(on=LifeCycleType.START)
79     public void start() {
80         log=new LoggingUtil(null);
81         log.start();
82         try {
83             // Retreive the port and host properties
84
int port = Integer.parseInt(SystemUtil.getJmxPort());
85             String JavaDoc host = SystemUtil.getHost();
86
87             if (cs != null && cs.isActive()
88                 && cs.getAddress().getPort() == port) {
89                 return;
90             }
91             if (cs != null) {
92                 cs.stop();
93             }
94             // create an rmi connector
95
Registry JavaDoc registry = LocateRegistry.getRegistry(port);
96             try {
97                 registry.list();
98             } catch (Exception JavaDoc e) {
99                 LocateRegistry.createRegistry(port);
100             }
101             String JavaDoc sJmxURL = "service:jmx:rmi:///jndi/rmi://" + host + ":"
102                 + port + "/management/rmi-jmx-connector";
103             JMXServiceURL JavaDoc jmxUrl = new JMXServiceURL JavaDoc(sJmxURL);
104             // Server with security (need keystore that are not yet done)
105
// HashMap<String, Object> env = new HashMap<String, Object>();
106
// env.put("jmx.remote.x.password.file", "conf" + File.separator
107
// + "password.properties");
108
// env.put("jmx.remote.x.access.file", "conf" + File.separator
109
// + "access.properties");
110
// RMIConnectorServer connectorServer = (RMIConnectorServer)
111
// JMXConnectorServerFactory
112
// .newJMXConnectorServer(jmxUrl, env, beanServer);
113

114             // Server without security
115
RMIConnectorServer JavaDoc connectorServer = (RMIConnectorServer JavaDoc) JMXConnectorServerFactory
116                 .newJMXConnectorServer(jmxUrl, null, adminAttributes
117                     .getRawMBeanServer());
118             cs = connectorServer;
119             // Start the server
120
cs.start();
121             // Write the address of the server on the console
122
log.info("JMX RMI server started at : "
123                 + cs.getAddress().getURLPath());
124         } catch (Exception JavaDoc e) {
125             log.error(
126                 "Error during the instanciation of the MX4J RMI adaptor:", e);
127         }
128         log.end();
129     }
130
131     /**
132      * Stop the RMI Server
133      */

134     @LifeCycle(on=LifeCycleType.STOP)
135     public void stop() {
136         log.start();
137         if (cs != null) {
138             StopJMXServerThread stopJMXServerThread = new StopJMXServerThread(
139                 cs, log);
140             stopJMXServerThread.start();
141         }
142         log.end();
143     }
144
145 }
146
Popular Tags