KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > system > server > jmx > LazyMBeanServer


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.system.server.jmx;
23
24 import java.io.ObjectInputStream JavaDoc;
25 import java.lang.reflect.Constructor JavaDoc;
26 import java.util.Set JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Collections JavaDoc;
29 import javax.management.MBeanServer JavaDoc;
30 import javax.management.ObjectInstance JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.management.ReflectionException JavaDoc;
33 import javax.management.InstanceAlreadyExistsException JavaDoc;
34 import javax.management.MBeanRegistrationException JavaDoc;
35 import javax.management.MBeanException JavaDoc;
36 import javax.management.NotCompliantMBeanException JavaDoc;
37 import javax.management.InstanceNotFoundException JavaDoc;
38 import javax.management.QueryExp JavaDoc;
39 import javax.management.AttributeNotFoundException JavaDoc;
40 import javax.management.AttributeList JavaDoc;
41 import javax.management.Attribute JavaDoc;
42 import javax.management.InvalidAttributeValueException JavaDoc;
43 import javax.management.NotificationListener JavaDoc;
44 import javax.management.NotificationFilter JavaDoc;
45 import javax.management.ListenerNotFoundException JavaDoc;
46 import javax.management.MBeanInfo JavaDoc;
47 import javax.management.IntrospectionException JavaDoc;
48 import javax.management.OperationsException JavaDoc;
49 import javax.management.MBeanServerDelegate JavaDoc;
50 import javax.management.loading.ClassLoaderRepository JavaDoc;
51
52 /**
53  * An MBeanServer implementation that bridges between the platform MBeanServer
54  * and the jboss implementation. This is used by the @{link MBeanServerBuilderImpl}
55  * to bridge the period from the vm startup until the jboss server is loaded
56  * and the jboss MBeanServer is available.
57  *
58  * @author Scott.Stark@jboss.org
59  * @version $Revision: 46137 $
60  */

61 public class LazyMBeanServer
62    implements MBeanServer JavaDoc
63 {
64    private static MBeanServer JavaDoc registeredServer;
65    private static MBeanServer JavaDoc theServer;
66    private static MBeanServer JavaDoc platformServer;
67    private static String JavaDoc defaultDomain;
68    private static MBeanServerDelegate JavaDoc theDelegate;
69    /** A HashSet<String> of the jmx domain prefixes that need to be
70     * handled by the platform server
71     */

72    private static Set JavaDoc platformServerDomains = Collections.synchronizedSet(new HashSet JavaDoc());
73    /** A HashSet<String> of the jmx domain prefixes that need to be
74     * handled by the jboss server
75     */

76    private static Set JavaDoc serverDomains = Collections.synchronizedSet(new HashSet JavaDoc());
77
78    /**
79     * Called by the ServerImpl to reset theServer implementation to the jboss
80     * version so that our xmbean and the like function correctly.
81     *
82     * @param server - the existing MBeanServer
83     * @return org.jboss.mx.server.MBeanServerImp
84     * @throws Exception
85     */

86    public static MBeanServer JavaDoc resetToJBossServer(MBeanServer JavaDoc server)
87       throws Exception JavaDoc
88    {
89       MBeanServer JavaDoc coreServer = server;
90       if( theDelegate != null )
91       {
92          Class JavaDoc[] sig = {String JavaDoc.class, MBeanServer JavaDoc.class, MBeanServerDelegate JavaDoc.class};
93          Object JavaDoc[] args = {defaultDomain, null, theDelegate};
94          ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
95          Class JavaDoc c = loader.loadClass("org.jboss.mx.server.MBeanServerImpl");
96          Constructor JavaDoc ctor = c.getConstructor(sig);
97          theServer = (MBeanServer JavaDoc) ctor.newInstance(args);
98          coreServer = theServer;
99          String JavaDoc[] domains = theServer.getDomains();
100          for(int n = 0; n < domains.length; n ++)
101          {
102             serverDomains.add(domains[n]);
103          }
104       }
105       return coreServer;
106    }
107    /**
108     * Provide access to the original MBeanServer that was registered so that
109     * it can be removed on shutdown for example.
110     *
111     * @param server - the ServerImple MBeanServer
112     * @return either the argument, or the last LazyMBeanServer created
113     */

114    public static MBeanServer JavaDoc getRegisteredMBeanServer(MBeanServer JavaDoc server)
115    {
116       MBeanServer JavaDoc outerServer = server;
117       if( registeredServer != null )
118          outerServer = registeredServer;
119       return outerServer;
120    }
121
122    /**
123     * Updates the platformServerDomains and serverDomains sets of jmx
124     * domain names.
125     */

126    public static synchronized void reloadDomains()
127    {
128       // Initialize the platform server domains
129
String JavaDoc[] domains = platformServer.getDomains();
130       for(int n = 0; n < domains.length; n ++)
131       {
132          platformServerDomains.add(domains[n]);
133       }
134       domains = theServer.getDomains();
135       for(int n = 0; n < domains.length; n ++)
136       {
137          serverDomains.add(domains[n]);
138       }
139    }
140    LazyMBeanServer(String JavaDoc domain, MBeanServer JavaDoc outer,
141       MBeanServerDelegate JavaDoc delegate)
142    {
143       defaultDomain = domain;
144       platformServer = outer;
145       theServer = outer;
146       registeredServer = this;
147       theDelegate = delegate;
148       // Initialize the platform server domains
149
String JavaDoc[] domains = platformServer.getDomains();
150       for(int n = 0; n < domains.length; n ++)
151       {
152          platformServerDomains.add(domains[n]);
153       }
154    }
155
156    public ObjectInstance JavaDoc createMBean(String JavaDoc className, ObjectName JavaDoc name)
157       throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc,
158       MBeanRegistrationException JavaDoc, MBeanException JavaDoc,
159       NotCompliantMBeanException JavaDoc
160    {
161       try
162       {
163          return createMBean(className, name, null);
164       }
165       catch (InstanceNotFoundException JavaDoc e)
166       {
167          throw new MBeanException JavaDoc(e);
168       }
169    }
170
171    public ObjectInstance JavaDoc createMBean(String JavaDoc className, ObjectName JavaDoc name,
172                  ObjectName JavaDoc loaderName)
173       throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc,
174       MBeanRegistrationException JavaDoc, MBeanException JavaDoc,
175       NotCompliantMBeanException JavaDoc, InstanceNotFoundException JavaDoc
176    {
177       return getServer(name).createMBean(className, name, loaderName);
178    }
179
180    public ObjectInstance JavaDoc createMBean(String JavaDoc className, ObjectName JavaDoc name,
181                  Object JavaDoc params[], String JavaDoc signature[])
182       throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc,
183       MBeanRegistrationException JavaDoc, MBeanException JavaDoc,
184       NotCompliantMBeanException JavaDoc
185    {
186       return getServer(name).createMBean(className, name, params, signature);
187    }
188
189    public ObjectInstance JavaDoc createMBean(String JavaDoc className, ObjectName JavaDoc name,
190                  ObjectName JavaDoc loaderName, Object JavaDoc params[],
191                  String JavaDoc signature[])
192       throws ReflectionException JavaDoc, InstanceAlreadyExistsException JavaDoc,
193       MBeanRegistrationException JavaDoc, MBeanException JavaDoc,
194       NotCompliantMBeanException JavaDoc, InstanceNotFoundException JavaDoc
195    {
196       return getServer(name).createMBean(className, name, loaderName, params, signature);
197    }
198
199    public ObjectInstance JavaDoc registerMBean(Object JavaDoc object, ObjectName JavaDoc name)
200       throws InstanceAlreadyExistsException JavaDoc, MBeanRegistrationException JavaDoc,
201       NotCompliantMBeanException JavaDoc
202    {
203       return theServer.registerMBean(object, name);
204    }
205
206    public void unregisterMBean(ObjectName JavaDoc name)
207       throws InstanceNotFoundException JavaDoc, MBeanRegistrationException JavaDoc
208    {
209       theServer.unregisterMBean(name);
210    }
211
212    public ObjectInstance JavaDoc getObjectInstance(ObjectName JavaDoc name)
213       throws InstanceNotFoundException JavaDoc
214    {
215       ObjectInstance JavaDoc oi = getServer(name).getObjectInstance(name);
216       return oi;
217    }
218
219    public Set JavaDoc queryMBeans(ObjectName JavaDoc name, QueryExp JavaDoc query)
220    {
221       // query both servers
222
Set JavaDoc beans = platformServer.queryMBeans(name, query);
223       Set JavaDoc beans2 = theServer.queryMBeans(name, query);
224       beans.addAll(beans2);
225       return beans;
226    }
227
228    public Set JavaDoc queryNames(ObjectName JavaDoc name, QueryExp JavaDoc query)
229    {
230       Set JavaDoc names = platformServer.queryNames(name, query);
231       Set JavaDoc names2 = theServer.queryNames(name, query);
232       names.addAll(names2);
233       return names;
234    }
235
236    public boolean isRegistered(ObjectName JavaDoc name)
237    {
238       return getServer(name).isRegistered(name);
239    }
240
241    public Integer JavaDoc getMBeanCount()
242    {
243       return theServer.getMBeanCount();
244    }
245
246    public Object JavaDoc getAttribute(ObjectName JavaDoc name, String JavaDoc attribute)
247       throws MBeanException JavaDoc, AttributeNotFoundException JavaDoc,
248       InstanceNotFoundException JavaDoc, ReflectionException JavaDoc
249    {
250       return getServer(name).getAttribute(name, attribute);
251    }
252
253    public AttributeList JavaDoc getAttributes(ObjectName JavaDoc name, String JavaDoc[] attributes)
254       throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc
255    {
256       return getServer(name).getAttributes(name, attributes);
257    }
258
259    public void setAttribute(ObjectName JavaDoc name, Attribute JavaDoc attribute)
260       throws InstanceNotFoundException JavaDoc, AttributeNotFoundException JavaDoc,
261       InvalidAttributeValueException JavaDoc, MBeanException JavaDoc,
262       ReflectionException JavaDoc
263    {
264       getServer(name).setAttribute(name, attribute);
265    }
266
267    public AttributeList JavaDoc setAttributes(ObjectName JavaDoc name,
268                   AttributeList JavaDoc attributes)
269   throws InstanceNotFoundException JavaDoc, ReflectionException JavaDoc
270    {
271       return getServer(name).setAttributes(name, attributes);
272    }
273
274    public Object JavaDoc invoke(ObjectName JavaDoc name, String JavaDoc operationName,
275          Object JavaDoc params[], String JavaDoc signature[])
276       throws InstanceNotFoundException JavaDoc, MBeanException JavaDoc,
277       ReflectionException JavaDoc
278    {
279       Object JavaDoc value = getServer(name).invoke(name, operationName, params, signature);
280       return value;
281    }
282
283    public String JavaDoc getDefaultDomain()
284    {
285       return theServer.getDefaultDomain();
286    }
287
288    public String JavaDoc[] getDomains()
289    {
290       return theServer.getDomains();
291    }
292
293    public void addNotificationListener(ObjectName JavaDoc name,
294               NotificationListener JavaDoc listener,
295               NotificationFilter JavaDoc filter,
296               Object JavaDoc handback)
297       throws InstanceNotFoundException JavaDoc
298    {
299       getServer(name).addNotificationListener(name, listener, filter, handback);
300    }
301
302    public void addNotificationListener(ObjectName JavaDoc name,
303               ObjectName JavaDoc listener,
304               NotificationFilter JavaDoc filter,
305               Object JavaDoc handback)
306       throws InstanceNotFoundException JavaDoc
307    {
308       getServer(name).addNotificationListener(name, listener, filter, handback);
309    }
310
311    public void removeNotificationListener(ObjectName JavaDoc name,
312                  ObjectName JavaDoc listener)
313   throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc
314    {
315       getServer(name).removeNotificationListener(name, listener);
316    }
317
318    public void removeNotificationListener(ObjectName JavaDoc name,
319                  ObjectName JavaDoc listener,
320                  NotificationFilter JavaDoc filter,
321                  Object JavaDoc handback)
322       throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc
323    {
324       getServer(name).removeNotificationListener(name, listener, filter, handback);
325    }
326
327    public void removeNotificationListener(ObjectName JavaDoc name,
328                  NotificationListener JavaDoc listener)
329       throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc
330    {
331       getServer(name).removeNotificationListener(name, listener);
332    }
333
334    public void removeNotificationListener(ObjectName JavaDoc name,
335                  NotificationListener JavaDoc listener,
336                  NotificationFilter JavaDoc filter,
337                  Object JavaDoc handback)
338       throws InstanceNotFoundException JavaDoc, ListenerNotFoundException JavaDoc
339    {
340       getServer(name).removeNotificationListener(name, listener, filter, handback);
341    }
342
343    /**
344     * Obtains the MBeanInfo for the given ObjectName. In the event of an
345     * InstanceNotFoundException, the jmx domain to server sets are reloaded
346     * by calling reloadDomains(), and the lookup retried.
347     * @param name
348     * @return
349     * @throws InstanceNotFoundException
350     * @throws IntrospectionException
351     * @throws ReflectionException
352     */

353    public MBeanInfo JavaDoc getMBeanInfo(ObjectName JavaDoc name)
354       throws InstanceNotFoundException JavaDoc, IntrospectionException JavaDoc,
355       ReflectionException JavaDoc
356    {
357       MBeanInfo JavaDoc info = null;
358       try
359       {
360          info = getServer(name).getMBeanInfo(name);
361       }
362       catch(InstanceNotFoundException JavaDoc e)
363       {
364          reloadDomains();
365          info = getServer(name).getMBeanInfo(name);
366       }
367       return info;
368    }
369
370    public boolean isInstanceOf(ObjectName JavaDoc name, String JavaDoc className)
371       throws InstanceNotFoundException JavaDoc
372    {
373       return getServer(name).isInstanceOf(name, className);
374    }
375
376    public Object JavaDoc instantiate(String JavaDoc className)
377       throws ReflectionException JavaDoc, MBeanException JavaDoc
378    {
379       return theServer.instantiate(className);
380    }
381
382    public Object JavaDoc instantiate(String JavaDoc className, ObjectName JavaDoc loaderName)
383       throws ReflectionException JavaDoc, MBeanException JavaDoc,
384       InstanceNotFoundException JavaDoc
385    {
386       return theServer.instantiate(className, loaderName);
387    }
388
389    public Object JavaDoc instantiate(String JavaDoc className, Object JavaDoc params[],
390               String JavaDoc signature[])
391       throws ReflectionException JavaDoc, MBeanException JavaDoc
392    {
393       return theServer.instantiate(className, params, signature);
394    }
395
396    public Object JavaDoc instantiate(String JavaDoc className, ObjectName JavaDoc loaderName,
397               Object JavaDoc params[], String JavaDoc signature[])
398       throws ReflectionException JavaDoc, MBeanException JavaDoc,
399       InstanceNotFoundException JavaDoc
400    {
401       return theServer.instantiate(className, loaderName, params, signature);
402    }
403
404    public ObjectInputStream JavaDoc deserialize(ObjectName JavaDoc name, byte[] data)
405       throws InstanceNotFoundException JavaDoc, OperationsException JavaDoc
406    {
407       return getServer(name).deserialize(name, data);
408    }
409
410    public ObjectInputStream JavaDoc deserialize(String JavaDoc className, byte[] data)
411       throws OperationsException JavaDoc, ReflectionException JavaDoc
412    {
413       return theServer.deserialize(className, data);
414    }
415
416    public ObjectInputStream JavaDoc deserialize(String JavaDoc className,
417                ObjectName JavaDoc loaderName,
418                byte[] data)
419       throws InstanceNotFoundException JavaDoc, OperationsException JavaDoc,
420       ReflectionException JavaDoc
421    {
422       return theServer.deserialize(className, loaderName, data);
423    }
424
425    public ClassLoader JavaDoc getClassLoaderFor(ObjectName JavaDoc mbeanName)
426       throws InstanceNotFoundException JavaDoc
427    {
428       return getServer(mbeanName).getClassLoaderFor(mbeanName);
429    }
430
431    public ClassLoader JavaDoc getClassLoader(ObjectName JavaDoc loaderName)
432   throws InstanceNotFoundException JavaDoc
433    {
434       return getServer(loaderName).getClassLoader(loaderName);
435    }
436
437    public ClassLoaderRepository JavaDoc getClassLoaderRepository()
438    {
439       return theServer.getClassLoaderRepository();
440    }
441
442    /**
443     * Choose the MBeanServer based on the ObjectName domain. If the serverDomain
444     * contains the domain of the name, theServer is returned. If the
445     * platformServerDomains set contains the domain of the name, the
446     * platformServer is returned. For an unknown domain theServer is returned.
447     *
448     * @param name
449     * @return Either the platform MBeanServer, or the jboss MBeanServer
450     */

451    private MBeanServer JavaDoc getServer(ObjectName JavaDoc name)
452    {
453       String JavaDoc domain = name != null ? name.getDomain() : "";
454       if( serverDomains.contains(domain) )
455          return theServer;
456       if( platformServerDomains.contains(domain) )
457          return platformServer;
458       return theServer;
459    }
460 }
461
Popular Tags