KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > management > j2ee > LocalJBossServerDomain


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.management.j2ee;
23
24 import org.jboss.deployment.DeploymentInfo;
25 import org.jboss.deployment.MainDeployerConstants;
26 import org.jboss.deployment.SubDeployer;
27 import org.jboss.logging.Logger;
28 import org.jboss.management.j2ee.factory.DefaultManagedObjectFactoryMap;
29 import org.jboss.management.j2ee.factory.ManagedObjectFactory;
30 import org.jboss.management.j2ee.factory.ManagedObjectFactoryMap;
31 import org.jboss.system.ServiceControllerMBean;
32 import org.jboss.system.ServiceMBean;
33 import org.jboss.system.ServiceMBeanSupport;
34
35 import javax.management.JMException JavaDoc;
36 import javax.management.MBeanException JavaDoc;
37 import javax.management.MBeanServer JavaDoc;
38 import javax.management.Notification JavaDoc;
39 import javax.management.NotificationListener JavaDoc;
40 import javax.management.ObjectName JavaDoc;
41 import java.net.InetAddress JavaDoc;
42 import java.util.Collection JavaDoc;
43 import java.util.Iterator JavaDoc;
44 import java.util.Set JavaDoc;
45
46 /**
47  * The integration MBean for the local JBoss server management domain. This
48  * bridges between the core JBoss JSR-77 agnostic code to the JSR-77
49  * managed object interfaces.
50  *
51  * @author <a HREF="mailto:andreas@jboss.org">Andreas Schaefer</a>
52  * @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
53  * @version $Revision: 40550 $
54  */

55 public class LocalJBossServerDomain extends ServiceMBeanSupport
56    implements NotificationListener JavaDoc, LocalJBossServerDomainMBean
57 {
58    /**
59     * Class logger.
60     */

61    private static final Logger log = Logger.getLogger(LocalJBossServerDomain.class);
62
63    private J2EEDomain serverDomain;
64
65    /**
66     * The name of the MainDeployer
67     */

68    private ObjectName JavaDoc mainDeployer;
69    /**
70     * The name of the SARDeployer
71     */

72    private ObjectName JavaDoc sarDeployer;
73    /**
74     * The name of the EARDeployer
75     */

76    private ObjectName JavaDoc earDeployer;
77    /**
78     * The name of the EJBDeployer
79     */

80    private ObjectName JavaDoc ejbDeployer;
81    /**
82     * The name of the RARDeployer
83     */

84    private ObjectName JavaDoc rarDeployer;
85    /**
86     * The name of the JCA connection manager deployer
87     */

88    private ObjectName JavaDoc jcaCMDeployer;
89    /**
90     * The name of the WARDeployer
91     */

92    private ObjectName JavaDoc warDeployer;
93    /**
94     * The client application deployer
95     */

96    private ObjectName JavaDoc carDeployer;
97    /**
98     * The name of the JMS service
99     */

100    private ObjectName JavaDoc jmsService;
101    /**
102     * The name of the JNDI service
103     */

104    private ObjectName JavaDoc jndiService;
105    /**
106     * The name of the JTA service
107     */

108    private ObjectName JavaDoc jtaService;
109    /**
110     * The name of the UserTransaction service
111     */

112    private ObjectName JavaDoc userTxService;
113    /**
114     * The name of the JavaMail service
115     */

116    private ObjectName JavaDoc mailService;
117    /**
118     * The name of the RMI_IIOP service
119     */

120    private ObjectName JavaDoc rmiiiopService;
121    /**
122     * The name of the service which emites URL binding events
123     */

124    private ObjectName JavaDoc jndiBindingService;
125    /**
126     * A mapping of JMX notifications to ManagedObjectFactory instances
127     */

128    private ManagedObjectFactoryMap managedObjFactoryMap;
129    private Class JavaDoc managedObjFactoryMapClass = DefaultManagedObjectFactoryMap.class;
130
131    // -------------------------------------------------------------------------
132
// Constructors
133
// -------------------------------------------------------------------------
134

135    public LocalJBossServerDomain()
136    {
137    }
138
139    // -------------------------------------------------------------------------
140
// Properties (Getters/Setters)
141
// -------------------------------------------------------------------------
142

143    /**
144     * @return The MainDeployer mbean name
145     * @jmx:managed-attribute
146     */

147    public ObjectName JavaDoc getMainDeployer()
148    {
149       return mainDeployer;
150    }
151
152    /**
153     * @param name The MainDeployer mbean name
154     * @jmx:managed-attribute
155     */

156    public void setMainDeployer(ObjectName JavaDoc name)
157    {
158       this.mainDeployer = name;
159    }
160
161    /**
162     * @return The SARDeployer mbean name
163     * @jmx:managed-attribute
164     */

165    public ObjectName JavaDoc getSARDeployer()
166    {
167       return sarDeployer;
168    }
169
170    /**
171     * @param name The SARDeployer mbean name
172     * @jmx:managed-attribute
173     */

174    public void setSARDeployer(ObjectName JavaDoc name)
175    {
176       this.sarDeployer = name;
177    }
178
179    /**
180     * @return The EARDeployer mbean name
181     * @jmx:managed-attribute
182     */

183    public ObjectName JavaDoc getEARDeployer()
184    {
185       return earDeployer;
186    }
187
188    /**
189     * @param name The EARDeployer mbean name
190     * @jmx:managed-attribute
191     */

192    public void setEARDeployer(ObjectName JavaDoc name)
193    {
194       this.earDeployer = name;
195    }
196
197    /**
198     * @return The EJBDeployer mbean name
199     * @jmx:managed-attribute
200     */

201    public ObjectName JavaDoc getEJBDeployer()
202    {
203       return ejbDeployer;
204    }
205
206    /**
207     * @param name The EJBDeployer mbean name
208     * @jmx:managed-attribute
209     */

210    public void setEJBDeployer(ObjectName JavaDoc name)
211    {
212       this.ejbDeployer = name;
213    }
214
215    /**
216     * @return The RARDeployer mbean name
217     * @jmx:managed-attribute
218     */

219    public ObjectName JavaDoc getRARDeployer()
220    {
221       return rarDeployer;
222    }
223
224    /**
225     * @param name The RARDeployer mbean name
226     * @jmx:managed-attribute
227     */

228    public void setRARDeployer(ObjectName JavaDoc name)
229    {
230       this.rarDeployer = name;
231    }
232
233    /**
234     * @return The JCA Connection manager deployer name
235     * @jmx:managed-attribute
236     */

237    public ObjectName JavaDoc getCMDeployer()
238    {
239       return jcaCMDeployer;
240    }
241
242    /**
243     * @param name The JCA Connection manager deployer name
244     * @jmx:managed-attribute
245     */

246    public void setCMDeployer(ObjectName JavaDoc name)
247    {
248       this.jcaCMDeployer = name;
249    }
250
251    /**
252     * @return The WARDeployer mbean name
253     * @jmx:managed-attribute
254     */

255    public ObjectName JavaDoc getWARDeployer()
256    {
257       return warDeployer;
258    }
259
260    /**
261     * @param name The WARDeployer mbean name
262     * @jmx:managed-attribute
263     */

264    public void setWARDeployer(ObjectName JavaDoc name)
265    {
266       this.warDeployer = name;
267    }
268
269    /**
270     * @return The CARDeployer mbean name
271     * @jmx:managed-attribute
272     */

273    public ObjectName JavaDoc getCARDeployer()
274    {
275       return carDeployer;
276    }
277    /**
278     * @param name The CARDeployer mbean name
279     * @jmx:managed-attribute
280     */

281    public void setCARDeployer(ObjectName JavaDoc name)
282    {
283       this.carDeployer = name;
284    }
285
286    /**
287     * @return The JMS service mbean name
288     * @jmx:managed-attribute
289     */

290    public ObjectName JavaDoc getJMSService()
291    {
292       return jmsService;
293    }
294
295    /**
296     * @param name The JMS service mbean name
297     * @jmx:managed-attribute
298     */

299    public void setJMSService(ObjectName JavaDoc name)
300    {
301       this.jmsService = name;
302    }
303
304    /**
305     * @return The JNDI service mbean name
306     * @jmx:managed-attribute
307     */

308    public ObjectName JavaDoc getJNDIService()
309    {
310       return jndiService;
311    }
312
313    /**
314     * @param name The JNDI service mbean name
315     * @jmx:managed-attribute
316     */

317    public void setJNDIService(ObjectName JavaDoc name)
318    {
319       this.jndiService = name;
320    }
321
322    /**
323     * @return The JTA service mbean name
324     * @jmx:managed-attribute
325     */

326    public ObjectName JavaDoc getJTAService()
327    {
328       return jtaService;
329    }
330
331    /**
332     * @param name The JTA service mbean name
333     * @jmx:managed-attribute
334     */

335    public void setJTAService(ObjectName JavaDoc name)
336    {
337       this.jtaService = name;
338    }
339
340    /**
341     * @return The JavaMail service mbean name
342     * @jmx:managed-attribute
343     */

344    public ObjectName JavaDoc getMailService()
345    {
346       return mailService;
347    }
348
349    /**
350     * @param name The JavaMail service mbean name
351     * @jmx:managed-attribute
352     */

353    public void setMailService(ObjectName JavaDoc name)
354    {
355       this.mailService = name;
356    }
357
358    /**
359     * @return The UserTransaction service mbean name
360     * @jmx:managed-attribute
361     */

362    public ObjectName JavaDoc getUserTransactionService()
363    {
364       return userTxService;
365    }
366
367    /**
368     * @param name The UserTransaction service mbean name
369     * @jmx:managed-attribute
370     */

371    public void setUserTransactionService(ObjectName JavaDoc name)
372    {
373       this.userTxService = name;
374    }
375
376    /**
377     * @return The RMI/IIOP service mbean name
378     * @jmx:managed-attribute
379     */

380    public ObjectName JavaDoc getRMI_IIOPService()
381    {
382       return rmiiiopService;
383    }
384
385    /**
386     * @param name The RMI/IIOP service mbean name
387     * @jmx:managed-attribute
388     */

389    public void setRMI_IIOPService(ObjectName JavaDoc name)
390    {
391       this.rmiiiopService = name;
392    }
393
394    /**
395     * @return The Jndi binding service mbean name
396     * @jmx:managed-attribute
397     */

398    public ObjectName JavaDoc getJndiBindingService()
399    {
400       return jndiBindingService;
401    }
402
403    /**
404     * @param name The Jndi binding service mbean name
405     * @jmx:managed-attribute
406     */

407    public void setJndiBindingService(ObjectName JavaDoc name)
408    {
409       this.jndiBindingService = name;
410    }
411
412    /**
413     * @return The ManagementObjFactoryMap class
414     * @jmx:managed-attribute
415     */

416    public Class JavaDoc getManagementObjFactoryMapClass()
417    {
418       return managedObjFactoryMapClass;
419    }
420
421    /**
422     * @param cls The ManagementObjFactoryMap class
423     * @jmx:managed-attribute
424     */

425    public void setManagementObjFactoryMapClass(Class JavaDoc cls)
426    {
427       this.managedObjFactoryMapClass = cls;
428    }
429
430    /**
431     * The JMX nofication callback. Here we create/destroy JSR77 MBeans based
432     * on the create/destory notifications.
433     *
434     * @param msg the notification msg
435     * @param handback currently unused
436     */

437    public void handleNotification(Notification JavaDoc msg, Object JavaDoc handback)
438    {
439       MBeanServer JavaDoc mbeanServer = getServer();
440       if (managedObjFactoryMap == null || mbeanServer == null)
441       {
442          return;
443       }
444
445       log.debug("handleNotification: " + msg);
446       String JavaDoc type = msg.getType();
447       Object JavaDoc userData = msg.getUserData();
448       try
449       {
450          /* As this section keeps growing I should change this to a command
451          dispatch pattern as well. An issue here is that there is a choice
452          made about what 'data' to pass the ManagedObjectFactory based on
453          the event type that probably should be hidden in the factory map.
454          */

455          if (type.equals(ServiceMBean.CREATE_EVENT))
456          {
457             ManagedObjectFactory factory = managedObjFactoryMap.getFactory(msg);
458             if (factory != null)
459             {
460                factory.create(mbeanServer, userData);
461             }
462          }
463          else if (type.equals(ServiceMBean.DESTROY_EVENT))
464          {
465             ManagedObjectFactory factory = managedObjFactoryMap.getFactory(msg);
466             if (factory != null)
467             {
468                factory.destroy(mbeanServer, userData);
469             }
470          }
471          else if (type.equals(SubDeployer.START_NOTIFICATION))
472          {
473             ManagedObjectFactory factory = managedObjFactoryMap.getFactory(msg);
474             if (factory != null)
475             {
476                factory.create(mbeanServer, userData);
477             }
478          }
479          else if (type.equals(SubDeployer.DESTROY_NOTIFICATION))
480          {
481             ManagedObjectFactory factory = managedObjFactoryMap.getFactory(msg);
482             if (factory != null)
483             {
484                DeploymentInfo di = (DeploymentInfo) msg.getUserData();
485                factory.destroy(mbeanServer, di);
486             }
487          }
488          else if (type.equals(MainDeployerConstants.ADD_DEPLOYER))
489          {
490             ObjectName JavaDoc deployerName = (ObjectName JavaDoc) msg.getUserData();
491             registerWithDeployer(deployerName);
492          }
493          else if (type.equals(MainDeployerConstants.REMOVE_DEPLOYER))
494          {
495             ObjectName JavaDoc deployerName = (ObjectName JavaDoc) msg.getUserData();
496             unregisterWithDeployer(deployerName);
497          }
498       }
499       catch (Throwable JavaDoc t)
500       {
501          log.debug("Failed to handle event", t);
502       }
503    }
504
505    public String JavaDoc toString()
506    {
507       return "LocalJBossServerDomain { " + super.toString() + " } []";
508    }
509
510    public void createService() throws Exception JavaDoc
511    {
512       MBeanServer JavaDoc server = getServer();
513
514       setupJ2EEMBeans(server);
515
516       registerWithController(server);
517
518       populateFactoryMap();
519       registerWithCurrentDeployers(server);
520    }
521
522    /**
523     * Called to destroy the service. This unregisters with all deployers and
524     * then removes all MBeans in this services domain to remove all JSR77
525     * beans.
526     *
527     * @throws Exception
528     */

529    protected void destroyService() throws Exception JavaDoc
530    {
531       MBeanServer JavaDoc server = getServer();
532
533       unregisterWithCurrentDeployers(server);
534
535       cleanupLeftoverMBeans(server);
536
537       unregisterWithController(server);
538    }
539
540    /**
541     * Register as a listener of the given deployer.
542     *
543     * @param deployerName
544     */

545    protected void registerWithDeployer(ObjectName JavaDoc deployerName)
546    {
547       log.debug("Registering as listener of deployer: " + deployerName);
548       try
549       {
550          getServer().addNotificationListener(deployerName, this, null, null);
551       }
552       catch (Exception JavaDoc e)
553       {
554          log.debug("Failed to register with deployer: " + deployerName, e);
555       }
556    }
557
558    /**
559     * Unregister as a listener of the given deployer.
560     *
561     * @param deployerName
562     */

563    protected void unregisterWithDeployer(ObjectName JavaDoc deployerName)
564    {
565       log.debug("Unregistering as listener of deployer: " + deployerName);
566       try
567       {
568          getServer().removeNotificationListener(deployerName, this);
569       }
570       catch (Exception JavaDoc e)
571       {
572          log.debug("Failed to unregister with deployer: " + deployerName, e);
573       }
574    }
575
576    /**
577     * Build the ManagedObjectFactoryMap used to obtain the ManagedObjectFactory
578     * instances from notification msgs.
579     *
580     * @throws Exception
581     */

582    private void populateFactoryMap() throws Exception JavaDoc
583    {
584       // Create the ManagedObjectFactoryMap
585
managedObjFactoryMap = (ManagedObjectFactoryMap) managedObjFactoryMapClass.newInstance();
586       managedObjFactoryMap.setSARDeployer(sarDeployer);
587       managedObjFactoryMap.setEARDeployer(earDeployer);
588       managedObjFactoryMap.setEJBDeployer(ejbDeployer);
589       managedObjFactoryMap.setRARDeployer(rarDeployer);
590       managedObjFactoryMap.setCMDeployer(jcaCMDeployer);
591       managedObjFactoryMap.setWARDeployer(warDeployer);
592       managedObjFactoryMap.setJMSResource(jmsService);
593       managedObjFactoryMap.setJNDIResource(jndiService);
594       managedObjFactoryMap.setJTAResource(jtaService);
595       managedObjFactoryMap.setJTAResource(userTxService);
596       managedObjFactoryMap.setJavaMailResource(mailService);
597       managedObjFactoryMap.setRMI_IIOPResource(rmiiiopService);
598    }
599
600    /**
601     * Create the J2EEServer and JVM MBeans.
602     *
603     * @param mbeanServer the MBeanServer to register the mbeans with.
604     */

605    private void setupJ2EEMBeans(MBeanServer JavaDoc mbeanServer)
606    {
607       // Create Server Component
608
try
609       {
610          log.debug("setupJ2EEMBeans(), create J2EEServer instance");
611          Package JavaDoc pkg = Package.getPackage("org.jboss");
612          String JavaDoc vendor = pkg.getSpecificationVendor();
613          String JavaDoc version = pkg.getImplementationVersion();
614          // Create the createService
615
serverDomain = new J2EEDomain(serviceName.getDomain());
616          ObjectName JavaDoc domain = serverDomain.getObjectName();
617          mbeanServer.registerMBean(serverDomain, domain);
618          // Create single Local J2EEServer MBean
619
J2EEServer j2eeServer = new J2EEServer("Local", domain, vendor, version);
620          ObjectName JavaDoc lServer = j2eeServer.getObjectName();
621          mbeanServer.registerMBean(j2eeServer, lServer);
622
623          // Create the JVM MBean
624
String JavaDoc hostName = "localhost";
625          try
626          {
627             InetAddress JavaDoc lLocalHost = InetAddress.getLocalHost();
628             hostName = lLocalHost.getHostName();
629          }
630          catch (Exception JavaDoc e)
631          {
632             // Ignore when host address is not accessible (localhost is used instead)
633
}
634          JVM jvm = new JVM("localhost", lServer,
635                  System.getProperty("java.version"),
636                  System.getProperty("java.vendor"),
637                  hostName);
638          ObjectName JavaDoc jvmName = jvm.getObjectName();
639          mbeanServer.registerMBean(jvm, jvmName);
640       }
641       catch (JMException JavaDoc jme)
642       {
643          log.debug("setupJ2EEMBeans - unexpected JMException", jme);
644       }
645       catch (Exception JavaDoc e)
646       {
647          log.debug("setupJ2EEMBeans - unexpected exception", e);
648       }
649    }
650
651    /**
652     * Register as a listener of the ServiceControllerMBean
653     *
654     * @param mbeanServer
655     */

656    private void registerWithController(MBeanServer JavaDoc mbeanServer)
657    {
658       try
659       {
660          mbeanServer.addNotificationListener(ServiceControllerMBean.OBJECT_NAME,
661                  this, null, null);
662          log.debug("Registered as listener of: " + ServiceControllerMBean.OBJECT_NAME);
663       }
664       catch (JMException JavaDoc jme)
665       {
666          log.debug("unexpected exception", jme);
667       }
668       catch (Exception JavaDoc e)
669       {
670          log.debug("unexpected exception", e);
671       }
672    }
673
674    /**
675     * Unregister as a listener of the ServiceControllerMBean.
676     *
677     * @param mbeanServer
678     */

679    private void unregisterWithController(MBeanServer JavaDoc mbeanServer)
680    {
681       try
682       {
683          mbeanServer.removeNotificationListener(ServiceControllerMBean.OBJECT_NAME, this);
684          log.debug("UNRegistered as listener of: " + ServiceControllerMBean.OBJECT_NAME);
685       }
686       catch (JMException JavaDoc jme)
687       {
688          log.debug("unexpected exception", jme);
689       }
690       catch (Exception JavaDoc e)
691       {
692          log.debug("unexpected exception", e);
693       }
694    }
695
696    /**
697     * Register with deployers known to the MainDeployer
698     *
699     * @param mbeanServer
700     * @throws Exception thrown on failure to register as a listener of the
701     * MainDeployer or to obtain the list of deployers
702     */

703    private void registerWithCurrentDeployers(MBeanServer JavaDoc mbeanServer)
704            throws Exception JavaDoc
705    {
706       log.debug("Registering with all deployers, mainDeployer=" + mainDeployer);
707       mbeanServer.addNotificationListener(mainDeployer, this, null, null);
708
709       // Obtain the deployers list
710
log.debug("Getting current deployers");
711       Object JavaDoc[] args = {};
712       String JavaDoc[] sig = {};
713       Collection JavaDoc deployers = (Collection JavaDoc) mbeanServer.invoke(mainDeployer,
714               "listDeployers", args, sig);
715       Iterator JavaDoc iter = deployers.iterator();
716       while (iter.hasNext())
717       {
718          ObjectName JavaDoc name = (ObjectName JavaDoc) iter.next();
719          registerWithDeployer(name);
720       }
721    }
722
723    /**
724     * Unregister with all deployers known to the MainDeployer
725     *
726     * @param mbeanServer
727     * @throws Exception thrown on failure to unregister as a listener of the
728     * MainDeployer or to obtain the list of deployers
729     */

730    private void unregisterWithCurrentDeployers(MBeanServer JavaDoc mbeanServer)
731            throws Exception JavaDoc
732    {
733       log.debug("Unregistering with all deployers, mainDeployer=" + mainDeployer);
734       mbeanServer.removeNotificationListener(mainDeployer, this);
735
736       // Obtain the deployers list
737
log.debug("Getting current deployers");
738       Object JavaDoc[] args = {};
739       String JavaDoc[] sig = {};
740       Collection JavaDoc deployers = (Collection JavaDoc) mbeanServer.invoke(mainDeployer,
741               "listDeployers", args, sig);
742       Iterator JavaDoc iter = deployers.iterator();
743       while (iter.hasNext())
744       {
745          ObjectName JavaDoc name = (ObjectName JavaDoc) iter.next();
746          unregisterWithDeployer(name);
747       }
748    }
749
750    /**
751     * Query for all mbeans in this services domain and unregisters them.
752     *
753     * @throws Exception if the domain query fails
754     */

755    private void cleanupLeftoverMBeans(MBeanServer JavaDoc mbeanServer) throws Exception JavaDoc
756    {
757       String JavaDoc domain = serviceName.getDomain();
758       ObjectName JavaDoc domainName = new ObjectName JavaDoc(domain + ":*");
759       Set JavaDoc domainNames = mbeanServer.queryNames(domainName, null);
760       log.debug("Found " + domainNames.size() + " domain mbeans");
761       Iterator JavaDoc domainIter = domainNames.iterator();
762       while (domainIter.hasNext())
763       {
764          try
765          {
766             ObjectName JavaDoc name = (ObjectName JavaDoc) domainIter.next();
767             if (name.equals(serviceName))
768                continue;
769             server.unregisterMBean(name);
770          }
771          catch (MBeanException JavaDoc ignore)
772          {
773          }
774       }
775    }
776 }
777
Popular Tags