1 | // jademx - JADE management using JMX |
2 | // Copyright 2005 Caboodle Networks, Inc. |
3 | // |
4 | // This library is free software; you can redistribute it and/or |
5 | // modify it under the terms of the GNU Lesser General Public |
6 | // License as published by the Free Software Foundation; either |
7 | // version 2.1 of the License, or (at your option) any later version. |
8 | // |
9 | // This library is distributed in the hope that it will be useful, |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | // Lesser General Public License for more details. |
13 | // |
14 | // You should have received a copy of the GNU Lesser General Public |
15 | // License along with this library; if not, write to the Free Software |
16 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
17 | |
18 | package jade.jademx.server; |
19 | |
20 | //import jade.util.Logger; |
21 | import jade.jademx.mbean.JadeBaseMBean; |
22 | import jade.jademx.mbean.JadeFactory; |
23 | import jade.jademx.mbean.JadeRuntime; |
24 | import jade.jademx.mbean.JademxException; |
25 | |
26 | import java.util.Enumeration; |
27 | import java.util.Hashtable; |
28 | import java.util.logging.Level; |
29 | import java.util.logging.Logger; // java instead of jade: weblogic classloader |
30 | |
31 | import javax.management.MBeanServer; |
32 | import javax.management.ObjectInstance; |
33 | import javax.management.ObjectName; |
34 | import javax.naming.Context; |
35 | import javax.naming.InitialContext; |
36 | import javax.naming.NamingException; |
37 | |
38 | /** |
39 | * Class to create, start, stop, and destroy JADE using jademx. |
40 | * This class is intended to be used in two ways: |
41 | * <ol> |
42 | * <li>As an instance of <a href= |
43 | * "http://docs.jboss.com/jbossas/javadoc/4.0.2/org/jboss/system/Service.html" |
44 | * >org.jboss.system.Service</a>. In this way, JADE can be a JBoss |
45 | * <em>service</em>. N.B. it is not necessary to explicitly declare that |
46 | * this class extends the JBoss service class; this |
47 | * class does not do that so as to improve portability.</li> |
48 | * <li>As a support class for use by other code.</li> |
49 | * </ol> |
50 | * @author David Bernstein, <a href="http://www.caboodlenetworks.com" |
51 | * >Caboodle Networks, Inc.</a> |
52 | */ |
53 | public class JadeService implements JadeServiceMBean { |
54 | |
55 | /** default name of config */ |
56 | public final static String DEFAULT_CONFIG_RESOURCE = "jademx-config.xml"; |
57 | |
58 | /** default JNDI name */ // WebLogic annoyed with "java:/" prefix |
59 | public final static String DEFAULT_JNDI_NAME = "jade"; // "java:/jade"; |
60 | |
61 | /** default ObjectName domain */ |
62 | public final static String DEFAULT_OBJECT_NAME_DOMAIN = "jade"; |
63 | |
64 | /** default config doc name to being a resource instead of URL */ |
65 | private final static boolean DEFAULT_CONFIG_DOC_NAME_IS_RESOURCE = false; |
66 | |
67 | /** my logger */ |
68 | private final Logger logger = |
69 | //Logger.getMyLogger(JadeService.class.getName()); |
70 | Logger.getLogger(JadeService.class.getName()); |
71 | |
72 | /** my ObjectName, obtained via <code>preRegister()</code> */ |
73 | private ObjectName objectName = null; |
74 | |
75 | /** ObjectName domain for self and subordintate MBeans*/ |
76 | private String objectNameDomain = DEFAULT_OBJECT_NAME_DOMAIN; |
77 | |
78 | /** name of config doc: configDocNameIsUrl governs meaning */ |
79 | private String configDocName = DEFAULT_CONFIG_RESOURCE; |
80 | |
81 | /** true iff configDocName isa URL, else it's a resource name */ |
82 | private boolean configDocNameIsUrl = DEFAULT_CONFIG_DOC_NAME_IS_RESOURCE; |
83 | |
84 | /** the JNDI name to use for binding */ |
85 | private String jndiName = DEFAULT_JNDI_NAME; |
86 | |
87 | /** the currently bound JNDI name */ |
88 | private String boundName = null; |
89 | |
90 | /** JNDI InitialContext properties */ |
91 | private Hashtable jndiInitialContextProperties = null; |
92 | |
93 | ///** my MBeanServer */ |
94 | private MBeanServer mBeanServer = null; |
95 | |
96 | /** my JadeFactory */ |
97 | private JadeFactory jadeFactory = null; |
98 | |
99 | /** my JadeRuntime */ |
100 | private JadeRuntime jadeRuntime = null; |
101 | |
102 | /** ... */ |
103 | private String jadeMXServerClassName = null; |
104 | |
105 | /** whether currently registered with MBeanServer */ |
106 | private volatile boolean registered = false; |
107 | |
108 | // use the default constructor |
109 | |
110 | /* (non-Javadoc) |
111 | * @see jade.jademx.server.JadeServiceMBean#setObjectNameDomain(java.lang.String) |
112 | */ |
113 | public synchronized void setObjectNameDomain( String objectNameDomain ) { |
114 | this.objectNameDomain = objectNameDomain; |
115 | logger.log( Level.CONFIG, "objectNameDomain:"+objectNameDomain); |
116 | } |
117 | |
118 | /* (non-Javadoc) |
119 | * @see jade.jademx.server.JadeServiceMBean#getObjectNameDomain() |
120 | */ |
121 | public synchronized String getObjectNameDomain() { |
122 | return objectNameDomain; |
123 | } |
124 | |
125 | /* (non-Javadoc) |
126 | * @see jade.jademx.server.JadeServiceMBean#setConfigDocResource(java.lang.String) |
127 | */ |
128 | public synchronized void setConfigDocResource( String configDocName ) { |
129 | this.configDocName = configDocName; |
130 | configDocNameIsUrl = false; |
131 | logger.log( Level.CONFIG, "configDocResource:"+configDocName); |
132 | } |
133 | |
134 | /* (non-Javadoc) |
135 | * @see jade.jademx.server.JadeServiceMBean#getConfigDocResource() |
136 | */ |
137 | public synchronized String getConfigDocResource() { |
138 | return ( configDocNameIsUrl ? null : configDocName ); |
139 | } |
140 | |
141 | /* (non-Javadoc) |
142 | * @see jade.jademx.server.JadeServiceMBean#setConfigDocUrl(java.lang.String) |
143 | */ |
144 | public synchronized void setConfigDocUrl( String configDocName ) { |
145 | this.configDocName = configDocName; |
146 | configDocNameIsUrl = true; |
147 | logger.log( Level.CONFIG, "configDocUrl:"+configDocName); |
148 | } |
149 | |
150 | /* (non-Javadoc) |
151 | * @see jade.jademx.server.JadeServiceMBean#getConfigDocUrl() |
152 | */ |
153 | public synchronized String getConfigDocUrl() { |
154 | return ( configDocNameIsUrl ? configDocName : null ); |
155 | } |
156 | |
157 | /* (non-Javadoc) |
158 | * @see jade.jademx.server.JadeServiceMBean#setJndiName(java.lang.String) |
159 | */ |
160 | public synchronized void setJndiName( String jndiName ) { |
161 | this.jndiName = jndiName; |
162 | logger.log( Level.CONFIG, "jndiName:"+jndiName); |
163 | } |
164 | |
165 | /* (non-Javadoc) |
166 | * @see jade.jademx.server.JadeServiceMBean#getJndiName() |
167 | */ |
168 | public synchronized String getJndiName() { |
169 | return jndiName; |
170 | } |
171 | |
172 | |
173 | /** |
174 | * set properties to use when creating InitialContext for JNDI |
175 | * @param props desired JNDI InitialContext properties |
176 | */ |
177 | public synchronized void setJndiInitialContextProperties( Hashtable props ){ |
178 | this.jndiInitialContextProperties = props; |
179 | logger.log( Level.CONFIG, "jndiInitialContextProperties:"+ |
180 | jndiInitialContextProperties); |
181 | } |
182 | |
183 | |
184 | // BEGIN org.jboss.system.Service IMPLEMENTATION |
185 | |
186 | //TODO: consider how can recover if create/start/stop/destroy fail |
187 | // halfway thru........... |
188 | // probably want to be able to do shutdown on jadeRuntime if platforms |
189 | // partially created.... |
190 | |
191 | /** initial state or after destroy() was called */ |
192 | private final static String STATE_INIT = "init"; |
193 | /** create() was called */ |
194 | private final static String STATE_CREATED = "created"; |
195 | /** start() was called */ |
196 | private final static String STATE_STARTED = "started"; |
197 | /** stop() was called */ |
198 | private final static String STATE_STOPPED = "stopped"; |
199 | |
200 | /** state */ |
201 | private String state = STATE_INIT; |
202 | |
203 | /** JNDI context */ |
204 | private Context context; |
205 | |
206 | /* (non-Javadoc) |
207 | * @see jade.jademx.server.JadeServiceMBean#create() |
208 | */ |
209 | public synchronized void create() throws Exception { |
210 | if ( STATE_INIT == state ) { |
211 | logger.log( Level.CONFIG, "creating JadeMXServer from class "+ |
212 | jadeMXServerClassName ); |
213 | JadeMXServer jadeMXServer = (JadeMXServer) |
214 | Class.forName( jadeMXServerClassName ).newInstance(); |
215 | jadeFactory = new JadeFactory( jadeMXServer ); |
216 | jadeRuntime = (JadeRuntime)jadeFactory.runtimeInstance(); |
217 | jadeRuntime.setParentObjectName( objectName ); |
218 | state = STATE_CREATED; |
219 | } |
220 | } |
221 | |
222 | /* (non-Javadoc) |
223 | * @see jade.jademx.server.JadeServiceMBean#start() |
224 | */ |
225 | public synchronized void start() throws Exception { |
226 | if ( STATE_CREATED == state ) { |
227 | if ( null != jndiName ) { |
228 | context = new InitialContext( jndiInitialContextProperties ); |
229 | ObjectInstance oi = |
230 | new ObjectInstance( objectName, this.getClass().getName() ); |
231 | logger.log( Level.CONFIG, "binding to JNDI name "+ |
232 | jndiName ); |
233 | context.bind( jndiName, oi ); |
234 | boundName = jndiName; |
235 | } |
236 | logger.log( Level.FINE, "creating platforms..." ); |
237 | if ( configDocNameIsUrl ) { |
238 | jadeRuntime.platformsFromConfigUrl( configDocName ); |
239 | } |
240 | else { |
241 | jadeRuntime.platformsFromConfigResource( configDocName ); |
242 | } |
243 | state = STATE_STARTED; |
244 | } |
245 | } |
246 | |
247 | /* (non-Javadoc) |
248 | * @see jade.jademx.server.JadeServiceMBean#stop() |
249 | */ |
250 | public synchronized void stop() { |
251 | if ( STATE_STARTED == state ) { |
252 | if ( null != boundName ) { |
253 | logger.log( Level.INFO, "unbinding from JNDI name "+boundName ); |
254 | try { |
255 | context.unbind( boundName ); |
256 | } |
257 | catch ( NamingException ne ) { |
258 | logger.log( Level.SEVERE, "exception unbinding name \""+ |
259 | boundName+"\" from ObjectInstance "+objectName, ne ); |
260 | } |
261 | boundName = null; |
262 | } |
263 | try { |
264 | logger.log( Level.INFO, "shutting down JadeRuntime"); |
265 | jadeRuntime.shutdown(); |
266 | } |
267 | catch ( JademxException je ) { |
268 | logger.log( Level.SEVERE, |
269 | "exception shutting down jademx", je ); |
270 | } |
271 | state = STATE_STOPPED; |
272 | } |
273 | } |
274 | |
275 | /* (non-Javadoc) |
276 | * @see jade.jademx.server.JadeServiceMBean#destroy() |
277 | */ |
278 | public synchronized void destroy() { |
279 | if ( STATE_STARTED == state ) { |
280 | stop(); |
281 | } |
282 | if ( ( STATE_STOPPED == state ) || ( STATE_CREATED == state ) ) { |
283 | logger.log( Level.INFO, "destroying..."); |
284 | jadeRuntime = null; |
285 | jadeFactory = null; |
286 | state = STATE_INIT; |
287 | } |
288 | } |
289 | |
290 | // END org.jboss.system.Service IMPLEMENTATION |
291 | |
292 | // BEGIN javax.management.MBeanRegistration IMPLEMENTATION |
293 | |
294 | /* (non-Javadoc) |
295 | * @see javax.management.MBeanRegistration#preRegister( |
296 | * javax.management.MBeanServer, javax.management.ObjectName) |
297 | */ |
298 | public ObjectName preRegister(MBeanServer server, ObjectName name) |
299 | throws Exception { |
300 | mBeanServer = server; |
301 | objectName = name; |
302 | // from method definition: |
303 | // |
304 | // name - |
305 | // The object name of the MBean. This name is null if the name |
306 | // parameter to one of the createMBean or registerMBean methods in the |
307 | // MBeanServer interface is null. In that case, this method must |
308 | // return a non-null ObjectName for the new MBean. |
309 | // Returns: |
310 | // The name under which the MBean is to be registered. This value must |
311 | // not be null. If the name parameter is not null, it will usually but |
312 | // not necessarily be the returned value. |
313 | if ( null == objectName ) { |
314 | Hashtable props = new Hashtable(); |
315 | props.put( JadeBaseMBean.OBJ_NAME_KEY_TYPE, PROP_SERVICE ); |
316 | objectName = new ObjectName( objectNameDomain, props ); |
317 | } |
318 | return objectName; |
319 | } |
320 | |
321 | /** name of service property value */ |
322 | public static final String PROP_SERVICE = "Service"; |
323 | |
324 | /* (non-Javadoc) |
325 | * @see javax.management.MBeanRegistration#postRegister(java.lang.Boolean) |
326 | */ |
327 | public void postRegister(Boolean registrationDone) { |
328 | if ( registrationDone.booleanValue() ) { |
329 | registered = true; |
330 | } |
331 | else { |
332 | objectName = null; |
333 | } |
334 | } |
335 | |
336 | /* (non-Javadoc) |
337 | * @see javax.management.MBeanRegistration#preDeregister() |
338 | */ |
339 | public void preDeregister() throws Exception { |
340 | // nop |
341 | } |
342 | |
343 | /* (non-Javadoc) |
344 | * @see javax.management.MBeanRegistration#postDeregister() |
345 | */ |
346 | public void postDeregister() { |
347 | objectName = null; |
348 | mBeanServer = null; |
349 | registered = false; |
350 | } |
351 | |
352 | // END javax.management.MBeanRegistration IMPLEMENTATION |
353 | |
354 | /** |
355 | * get ObjectName for this MBean |
356 | * @return ObjectName for this MBean |
357 | */ |
358 | public ObjectName getObjectName() { |
359 | return objectName; |
360 | } |
361 | |
362 | /* (non-Javadoc) |
363 | * @see jade.jademx.mbean.JadeBaseMBean#unregister() |
364 | */ |
365 | public void unregister() { |
366 | if ( null != objectName ) { |
367 | logger.log( Level.FINEST, "about to unregister "+this); |
368 | try { |
369 | mBeanServer.unregisterMBean( objectName ); |
370 | } |
371 | catch ( Exception e ) { |
372 | logger.log( Level.SEVERE, "exception unregistering "+this, e ); |
373 | } |
374 | logger.log( Level.FINEST, "did unregister "+this); |
375 | } |
376 | else { |
377 | logger.log( Level.FINEST, "no ObjectName to unregister "); |
378 | } |
379 | } |
380 | |
381 | /* (non-Javadoc) |
382 | * @see jade.jademx.server.JadeServiceMBean#getBoundName() |
383 | */ |
384 | public String getBoundName() { |
385 | return boundName; |
386 | } |
387 | |
388 | /* (non-Javadoc) |
389 | * @see jade.jademx.server.JadeServiceMBean#getJadeMXServerClassName() |
390 | */ |
391 | public String getJadeMXServerClassName() { |
392 | return jadeMXServerClassName; |
393 | } |
394 | |
395 | /* (non-Javadoc) |
396 | * @see jade.jademx.server.JadeServiceMBean#setJadeMXServerClassName(java.lang.String) |
397 | */ |
398 | public void setJadeMXServerClassName(String jadeMXServerClassName) { |
399 | this.jadeMXServerClassName = jadeMXServerClassName; |
400 | } |
401 | |
402 | /* (non-Javadoc) |
403 | * @see jade.jademx.server.JadeServiceMBean#getRuntime() |
404 | */ |
405 | public JadeRuntime getRuntime() { |
406 | return jadeRuntime; |
407 | } |
408 | |
409 | /* (non-Javadoc) |
410 | * @see jade.jademx.server.JadeServiceMBean#getRuntimeName() |
411 | */ |
412 | public String getRuntimeName() { |
413 | return jadeRuntime.toString(); |
414 | } |
415 | |
416 | /* (non-Javadoc) |
417 | * @see jade.jademx.server.JadeServiceMBean#getRuntimeObjectNames() |
418 | */ |
419 | public ObjectName getRuntimeObjectName() { |
420 | return jadeRuntime.getObjectName(); |
421 | } |
422 | |
423 | /* (non-Javadoc) |
424 | * @see jade.jademx.server.JadeServiceMBean#getType() |
425 | */ |
426 | public String getType() { |
427 | return ATTR_TYPE; |
428 | } |
429 | |
430 | /* (non-Javadoc) |
431 | * @see java.lang.Object#toString() |
432 | */ |
433 | public String toString() { |
434 | StringBuffer props = new StringBuffer(); |
435 | Enumeration propKeys = jndiInitialContextProperties.keys(); |
436 | boolean first = true; |
437 | while ( propKeys.hasMoreElements() ) { |
438 | String propKey = (String)propKeys.nextElement(); |
439 | String propVal = (String)jndiInitialContextProperties.get(propKey); |
440 | if ( first ) { |
441 | first = false; |
442 | } |
443 | else { |
444 | props.append(','); |
445 | } |
446 | props.append( propKey ); |
447 | props.append( '=' ); |
448 | props.append( propVal ); |
449 | } |
450 | return this.getClass().getName()+"["+ |
451 | "jadeMXServerClassName="+jadeMXServerClassName+ |
452 | ",objectNameDomain="+objectNameDomain+ |
453 | ",configDocName="+configDocName+ |
454 | ",configDocNameIsUrl="+configDocNameIsUrl+ |
455 | ",jndiName="+jndiName+ |
456 | ",jndiInitialContextProperties=("+props+")"+ |
457 | ",registered="+registered+ |
458 | ",objectName="+objectName+ |
459 | "]"; |
460 | } |
461 | |
462 | |
463 | |
464 | } |