KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > jmx > JMXServerJob


1 package org.oddjob.jmx;
2
3 import java.io.IOException JavaDoc;
4 import java.net.MalformedURLException JavaDoc;
5
6 import javax.management.JMException JavaDoc;
7 import javax.management.MBeanServer JavaDoc;
8 import javax.management.MBeanServerFactory JavaDoc;
9 import javax.management.ObjectName JavaDoc;
10 import javax.management.remote.JMXConnectorServer JavaDoc;
11 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
12 import javax.management.remote.JMXServiceURL JavaDoc;
13
14 import org.oddjob.OddjobException;
15 import org.oddjob.arooa.ArooaConstants;
16 import org.oddjob.arooa.ArooaContext;
17 import org.oddjob.arooa.registry.ComponentRegistry;
18 import org.oddjob.jmx.server.IconicInfo;
19 import org.oddjob.jmx.server.InterfaceManagerFactory;
20 import org.oddjob.jmx.server.OddjobMBeanFactory;
21 import org.oddjob.jmx.server.OddjobSchedulerInfo;
22 import org.oddjob.jmx.server.ResetableInfo;
23 import org.oddjob.jmx.server.RunnableInfo;
24 import org.oddjob.jmx.server.ServerContext;
25 import org.oddjob.jmx.server.ServerModel;
26 import org.oddjob.jmx.server.StatefulInfo;
27 import org.oddjob.jmx.server.StoppableInfo;
28 import org.oddjob.jmx.server.StructuralInfo;
29 import org.oddjob.util.ThreadManager;
30
31 /**
32  * A JXMServer. To turn on JMX debugs use -Dmx4j.log.priority=trace as a
33  * JVM arg.
34  *
35  * @oddjob.description A job which allows client a local job hierarchy to
36  * be monitored and managed remotely via JMX.
37  * <p>
38  * @oddjob.example
39  *
40  * Typical configuration is:
41  * <code><pre>
42  * &lt;rmi-server name="My Server"
43  * url="service:jmx:rmi://ignored/jndi/rmi://localhost/my-oddjob"
44  * root="${job.root}"/&gt;
45  * </pre></code>
46  *
47  * @author Rob Gordon
48  */

49 public class JMXServerJob {
50 // private static final Logger logger = Logger.getLogger(JMXServerJob.class);
51

52     /**
53      * @oddjob.property
54      * @oddjob.description A name, can be any text.
55      * @oddjob.required No.
56      */

57     private String JavaDoc name;
58
59     /**
60      * @oddjob.property
61      * @oddjob.description The root node.
62      * @oddjob.required Yes.
63      */

64     private Object JavaDoc root;
65     
66     /**
67      * @oddjob.property
68      * @oddjob.description The JMX service URL.
69      * @oddjob.required Yes.
70      */

71     private String JavaDoc url;
72
73     /**
74      * @oddjob.property
75      * @oddjob.description The log format for formatting log messages. For more
76      * information on the format please see <a HREF="http://logging.apache.org/log4j/docs/">
77      * http://logging.apache.org/log4j/docs/</a>
78      * @oddjob.required No.
79      */

80     private String JavaDoc logFormat;
81     
82     /**
83      * The ThreadManager. This job will not
84      * stop until the ThreadManager says it can.
85      */

86     private ThreadManager threadManager = new ThreadManager();
87     
88     /** Useful for testing */
89     private String JavaDoc address;
90
91     /** Remember the context so it can be used to resolve ids for the
92      * client.
93      */

94     private ArooaContext context;
95     
96     /** Bean Factory */
97     private OddjobMBeanFactory factory;
98     /** Root MBean name */
99     private ObjectName JavaDoc rootName;
100     /** Server Model */
101     private ServerModel model;
102     /** Connector server */
103     private JMXConnectorServer JavaDoc cntorServer;
104     
105     /**
106      * Get the name.
107      *
108      * @return The name.
109      */

110     public String JavaDoc getName() {
111         return name;
112     }
113     
114     /**
115      * Set the name
116      *
117      * @param name The name.
118      */

119     public void setName(String JavaDoc name) {
120         this.name = name;
121     }
122     
123     /**
124      * Set the root node directly.
125      *
126      * @param rootNode The root node for the monitor tree.
127      */

128     public void setRoot(Object JavaDoc rootNode) {
129         this.root = rootNode;
130     }
131
132     /**
133      * Get the root node of the monitor tree.
134      *
135      * @return The root node.
136      */

137     public Object JavaDoc getRoot() {
138         return this.root;
139     }
140
141     public String JavaDoc getAddress() {
142         return address;
143     }
144     
145     /**
146      * Set the name to bind the root node as in the naming service.
147      *
148      * @param url The name for the naming service.
149      */

150     public void setUrl(String JavaDoc bindAs) {
151         this.url = bindAs;
152     }
153     
154     /**
155      * Get the name the root node is bound as in the naming service.
156      *
157      * @return The name used in the naming service.
158      */

159     public String JavaDoc getUrl() {
160         return url;
161     }
162     
163     public boolean setContext(ArooaContext context) {
164         this.context = context;
165         return true;
166     }
167
168     public void start()
169     throws JMException JavaDoc, MalformedURLException JavaDoc, IOException JavaDoc {
170         if (root == null) {
171             throw new OddjobException("No root node.");
172         }
173         if (url == null) {
174             throw new OddjobException("Url missing.");
175         }
176         
177         JMXServiceURL JavaDoc address = new JMXServiceURL JavaDoc(url);
178
179         MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer();
180
181         // this is the list of supported interfaces. One day this will be
182
// configurable.
183
// note that some interface are hardwired in the factory because
184
// they are aspects of the server.
185
InterfaceManagerFactory imf = new InterfaceManagerFactory();
186         imf.addInterfaceInfo(new StatefulInfo());
187         imf.addInterfaceInfo(new StructuralInfo());
188         imf.addInterfaceInfo(new IconicInfo());
189         imf.addInterfaceInfo(new RunnableInfo());
190         imf.addInterfaceInfo(new StoppableInfo());
191         imf.addInterfaceInfo(new ResetableInfo());
192         imf.addInterfaceInfo(new OddjobSchedulerInfo());
193         
194         ComponentRegistry componentRegistry;
195         if (context == null) {
196             // job is being used in code - we'll supply an empty registry in model.
197
componentRegistry = null;
198         }
199         else {
200             componentRegistry = (ComponentRegistry) context.get(ArooaConstants.COMPONENT_REGISTRY);
201         }
202
203         model = new ServerModel(
204                 address.getURLPath(),
205                 threadManager,
206                 imf);
207         model.setComponentRegistry(componentRegistry);
208         model.setLogFormat(logFormat);
209         model.setRoot(root);
210                 
211         factory = new OddjobMBeanFactory(server);
212         rootName = factory.createMBeanFor(root,
213                 new ServerContext(model));
214         
215     
216         cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(address, null, server);
217         cntorServer.start();
218         this.address = cntorServer.getAddress().toString();
219     }
220     
221     /**
222      *
223      * @throws Exception
224      */

225     public void stop() throws Exception JavaDoc {
226         threadManager.stopAll();
227         factory.destroy(rootName);
228         model.destroy();
229         this.address = null;
230         cntorServer.stop();
231     }
232
233     /*
234      * (non-Javadoc)
235      * @see java.lang.Object#toString()
236      */

237     public String JavaDoc toString() {
238         if (name == null) {
239             return "Oddjob Server";
240         }
241         return name;
242     }
243 }
244
Popular Tags