KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > management > mejb > ManagementBean


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.mejb;
23
24 import org.jboss.logging.Logger;
25 import org.jboss.management.j2ee.J2EEDomain;
26 import org.jboss.mx.util.MBeanServerLocator;
27
28 import javax.ejb.CreateException JavaDoc;
29 import javax.ejb.EJBException JavaDoc;
30 import javax.ejb.SessionBean JavaDoc;
31 import javax.ejb.SessionContext JavaDoc;
32 import javax.management.*;
33 import javax.management.j2ee.ManagementHome JavaDoc;
34 import javax.naming.Context JavaDoc;
35 import javax.naming.InitialContext JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37 import java.rmi.RemoteException JavaDoc;
38 import java.util.Set JavaDoc;
39
40 /**
41  * Management Session Bean to enable the client to manage the
42  * server its is deployed on. This is the implementation of the
43  * JSR-77 specification MEJB.
44  *
45  * @author <a HREF="mailto:marc.fleury@jboss.org">Marc Fleury</a>
46  * @author Andreas Schaefer
47  * @author Scott.Stark@jboss.org
48  * @version $Revision: 37459 $
49  * @ejb:bean name="MEJB"
50  * display-name="JBoss Management EJB (MEJB)"
51  * type="Stateless"
52  * jndi-name="ejb/mgmt/MEJB"
53  * @ejb:interface extends="javax.management.j2ee.Management"
54  * @ejb:home generate="none"
55  * remote-class="javax.management.j2ee.ManagementHome"
56  * @ejb:env-entry description="JNDI-Name of the MBeanServer to be used to look it up. If 'null' the first of all listed local MBeanServer is taken"
57  * name="Server-Name"
58  * value="null"
59  * @ejb:transaction type="Supports"
60  */

61 public class ManagementBean
62         implements SessionBean JavaDoc
63 {
64    // -------------------------------------------------------------------------
65
// Static
66
// -------------------------------------------------------------------------
67
/**
68     * Logger to log. Can be used because this EJB is specific to JBoss
69     */

70    private static Logger log = Logger.getLogger(ManagementBean.class);
71
72    // -------------------------------------------------------------------------
73
// Members
74
// -------------------------------------------------------------------------
75

76    private SessionContext JavaDoc mContext;
77
78    /**
79     * Reference to the MBeanServer all the methods of this Connector are
80     * forwarded to
81     */

82    private MBeanServer mbeanServer;
83    /**
84     * JMX Name of the HA Management Service used in a Cluster
85     */

86    private ObjectName mManagementService;
87
88    // -------------------------------------------------------------------------
89
// Methods
90
// -------------------------------------------------------------------------
91

92    /**
93     * @throws RemoteException Necessary for a EJB
94     * @ejb:interface-method view-type="remote"
95     */

96    public Object JavaDoc getAttribute(ObjectName pName, String JavaDoc pAttribute)
97            throws
98            MBeanException,
99            AttributeNotFoundException,
100            InstanceNotFoundException,
101            ReflectionException,
102            RemoteException JavaDoc
103    {
104       try
105       {
106          if (mManagementService == null)
107          {
108             return mbeanServer.getAttribute(pName, pAttribute);
109          }
110          else
111          {
112             return mbeanServer.invoke(mManagementService,
113                     "getAttribute",
114                     new Object JavaDoc[]{
115                        pName,
116                        pAttribute
117                     },
118                     new String JavaDoc[]{
119                        ObjectName.class.getName(),
120                        String JavaDoc.class.getName()
121                     });
122          }
123       }
124               // The CTS expects an AttributeNotFoundException, but ModelMBeanInfoSupport
125
// throws IllegalArgumentException - we need to rethrow to make the CTS happy.
126
catch (RuntimeOperationsException e)
127       {
128          if (e.getTargetException() instanceof IllegalArgumentException JavaDoc)
129          {
130             throw new AttributeNotFoundException("MBean attribute not found: " + pAttribute);
131          }
132          throw e;
133       }
134    }
135
136    /**
137     * @throws RemoteException Necessary for a EJB
138     * @ejb:interface-method view-type="remote"
139     */

140    public AttributeList getAttributes(ObjectName pName, String JavaDoc[] pAttributes)
141            throws
142            InstanceNotFoundException,
143            ReflectionException,
144            RemoteException JavaDoc
145    {
146       if (mManagementService == null)
147       {
148          return mbeanServer.getAttributes(pName, pAttributes);
149       }
150       else
151       {
152          try
153          {
154             return (AttributeList) mbeanServer.invoke(mManagementService,
155                     "getAttributes",
156                     new Object JavaDoc[]{
157                        pName,
158                        pAttributes
159                     },
160                     new String JavaDoc[]{
161                        ObjectName.class.getName(),
162                        String JavaDoc[].class.getName()
163                     });
164          }
165          catch (MBeanException me)
166          {
167             log.error("getAttributes() got exception from cluster service", me);
168             // Should never happens
169
return null;
170          }
171       }
172    }
173
174    /**
175     * @throws RemoteException Necessary for a EJB
176     * @ejb:interface-method view-type="remote"
177     */

178    public String JavaDoc getDefaultDomain()
179            throws RemoteException JavaDoc
180    {
181       if (mManagementService == null)
182       {
183          return J2EEDomain.getDomainName();
184       }
185       else
186       {
187          try
188          {
189             return (String JavaDoc) mbeanServer.getAttribute(mManagementService,
190                     "DefaultDomain");
191          }
192          catch (JMException jme)
193          {
194             // Should never happen
195
log.error("getDefaultDomain() got exception from cluster service", jme);
196             return null;
197          }
198       }
199    }
200
201    /**
202     * @throws RemoteException Necessary for a EJB
203     * @ejb:interface-method view-type="remote"
204     */

205    public Integer JavaDoc getMBeanCount()
206            throws RemoteException JavaDoc
207    {
208       if (mManagementService == null)
209       {
210          try
211          {
212             Set JavaDoc mbeans = this.queryNames(new ObjectName("*:*"), null);
213             return new Integer JavaDoc(mbeans.size());
214          }
215          catch (Exception JavaDoc e)
216          {
217          }
218          return new Integer JavaDoc(0);
219       }
220       else
221       {
222          try
223          {
224             return (Integer JavaDoc) mbeanServer.invoke(mManagementService,
225                     "getMBeanCount",
226                     new Object JavaDoc[]{},
227                     new String JavaDoc[]{});
228          }
229          catch (JMException jme)
230          {
231             log.error("getMBeanCount() got exception from cluster service", jme);
232             // Should never happen
233
return null;
234          }
235       }
236    }
237
238    /**
239     * @throws RemoteException Necessary for a EJB
240     * @ejb:interface-method view-type="remote"
241     */

242    public MBeanInfo getMBeanInfo(ObjectName pName)
243            throws
244            IntrospectionException,
245            InstanceNotFoundException,
246            ReflectionException,
247            RemoteException JavaDoc
248    {
249       if (mManagementService == null)
250       {
251          return mbeanServer.getMBeanInfo(pName);
252       }
253       else
254       {
255          try
256          {
257             return (MBeanInfo) mbeanServer.invoke(mManagementService,
258                     "getMBeanInfo",
259                     new Object JavaDoc[]{
260                        pName
261                     },
262                     new String JavaDoc[]{
263                        ObjectName.class.getName()
264                     });
265          }
266          catch (MBeanException me)
267          {
268             log.error("getMBeanInfo() got exception from cluster service", me);
269             // Should never happen
270
return null;
271          }
272       }
273    }
274
275    /**
276     * @throws RemoteException Necessary for a EJB
277     * @ejb:interface-method view-type="remote"
278     */

279    public javax.management.j2ee.ListenerRegistration JavaDoc getListenerRegistry()
280            throws RemoteException JavaDoc
281    {
282       return new ListenerRegistration((ManagementHome JavaDoc) mContext.getEJBObject().getEJBHome(),
283               new String JavaDoc[]{});
284    }
285
286    /**
287     * @throws RemoteException Necessary for a EJB
288     * @ejb:interface-method view-type="remote"
289     */

290    public Object JavaDoc invoke(ObjectName pName, String JavaDoc pOperationName, Object JavaDoc[] pParams, String JavaDoc[] pSignature)
291            throws
292            InstanceNotFoundException,
293            MBeanException,
294            ReflectionException,
295            RemoteException JavaDoc
296    {
297       // Convert start(), startRecursive() and stop() to the
298
// internal methods: mejbStart(), mejbStartRecursive() and mejbStop
299
if (pOperationName.equals("start"))
300       {
301          pOperationName = "mejbStart";
302       }
303       else if (pOperationName.equals("startRecursive"))
304       {
305          pOperationName = "mejbStartRecursive";
306       }
307       else if (pOperationName.equals("stop"))
308       {
309          pOperationName = "mejbStop";
310       }
311       if (mManagementService == null)
312       {
313          return mbeanServer.invoke(pName,
314                  pOperationName,
315                  pParams,
316                  pSignature);
317       }
318       else
319       {
320          return mbeanServer.invoke(mManagementService,
321                  "invoke",
322                  new Object JavaDoc[]{
323                     pName,
324                     pOperationName,
325                     pParams,
326                     pSignature
327                  },
328                  new String JavaDoc[]{
329                     ObjectName.class.getName(),
330                     String JavaDoc.class.getName(),
331                     Object JavaDoc[].class.getName(),
332                     String JavaDoc[].class.getName()
333                  });
334       }
335    }
336
337    /**
338     * @throws RemoteException Necessary for a EJB
339     * @ejb:interface-method view-type="remote"
340     */

341    public boolean isRegistered(ObjectName pName)
342            throws RemoteException JavaDoc
343    {
344       if (mManagementService == null)
345       {
346          return mbeanServer.isRegistered(pName);
347       }
348       else
349       {
350          try
351          {
352             Boolean JavaDoc lCheck = (Boolean JavaDoc) mbeanServer.invoke(mManagementService,
353                     "isRegistered",
354                     new Object JavaDoc[]{
355                        pName
356                     },
357                     new String JavaDoc[]{
358                        ObjectName.class.getName()
359                     });
360             if (lCheck != null)
361             {
362                return lCheck.booleanValue();
363             }
364          }
365          catch (JMException jme)
366          {
367             log.error("isRegistered() got exception from cluster service", jme);
368             // Should never happen
369
}
370          return false;
371       }
372    }
373
374    /**
375     * @throws RemoteException Necessary for a EJB
376     * @ejb:interface-method view-type="remote"
377     */

378    public Set JavaDoc queryNames(ObjectName pName, QueryExp pQuery)
379            throws RemoteException JavaDoc
380    {
381       if (mManagementService == null)
382       {
383          return mbeanServer.queryNames(pName, pQuery);
384       }
385       else
386       {
387          try
388          {
389             return (Set JavaDoc) mbeanServer.invoke(mManagementService,
390                     "queryNames",
391                     new Object JavaDoc[]{
392                        pName,
393                        pQuery
394                     },
395                     new String JavaDoc[]{
396                        ObjectName.class.getName(),
397                        QueryExp.class.getName()
398                     });
399          }
400          catch (JMException jme)
401          {
402             log.error("queryNames() got exception from cluster service", jme);
403             // Should never happen
404
return null;
405          }
406       }
407    }
408
409    /**
410     * @throws RemoteException Necessary for a EJB
411     * @ejb:interface-method view-type="remote"
412     */

413    public void setAttribute(ObjectName pName, Attribute pAttribute)
414            throws
415            AttributeNotFoundException,
416            InstanceNotFoundException,
417            InvalidAttributeValueException,
418            MBeanException,
419            ReflectionException,
420            RemoteException JavaDoc
421    {
422       if (mManagementService == null)
423       {
424          mbeanServer.setAttribute(pName, pAttribute);
425       }
426       else
427       {
428          mbeanServer.invoke(mManagementService,
429                  "setAttribute",
430                  new Object JavaDoc[]{
431                     pName,
432                     pAttribute
433                  },
434                  new String JavaDoc[]{
435                     ObjectName.class.getName(),
436                     String JavaDoc.class.getName()
437                  });
438       }
439    }
440
441    /**
442     * @throws RemoteException Necessary for a EJB
443     * @ejb:interface-method view-type="remote"
444     */

445    public AttributeList setAttributes(ObjectName pName, AttributeList pAttributes)
446            throws
447            InstanceNotFoundException,
448            ReflectionException,
449            RemoteException JavaDoc
450    {
451       if (mManagementService == null)
452       {
453          return mbeanServer.setAttributes(pName, pAttributes);
454       }
455       else
456       {
457          try
458          {
459             return (AttributeList) mbeanServer.invoke(mManagementService,
460                     "setAttributes",
461                     new Object JavaDoc[]{
462                        pName,
463                        pAttributes
464                     },
465                     new String JavaDoc[]{
466                        ObjectName.class.getName(),
467                        AttributeList.class.getName()
468                     });
469          }
470          catch (MBeanException me)
471          {
472             log.error("setAttributes() got exception from cluster service", me);
473             // Should never happen
474
return null;
475          }
476       }
477    }
478
479    /**
480     * @throws RemoteException Necessary for a EJB
481     * @ejb:interface-method view-type="remote"
482     */

483    public ObjectInstance createMBean(String JavaDoc pClass,
484                                      ObjectName pName,
485                                      Object JavaDoc[] pParameters,
486                                      String JavaDoc[] pSignature)
487            throws
488            InstanceAlreadyExistsException,
489            MBeanException,
490            MBeanRegistrationException,
491            NotCompliantMBeanException,
492            ReflectionException,
493            RemoteException JavaDoc
494    {
495       if (mManagementService == null)
496       {
497          return mbeanServer.createMBean(pClass, pName, pParameters, pSignature);
498       }
499       else
500       {
501          try
502          {
503             return (ObjectInstance) mbeanServer.invoke(mManagementService,
504                     "createMBean",
505                     new Object JavaDoc[]{
506                        pClass,
507                        pName,
508                        pParameters,
509                        pSignature
510                     },
511                     new String JavaDoc[]{
512                        String JavaDoc.class.getName(),
513                        ObjectName.class.getName(),
514                        Object JavaDoc[].class.getName(),
515                        String JavaDoc[].class.getName()
516                     });
517          }
518          catch (InstanceNotFoundException infe)
519          {
520             log.error("createMBean() got exception from cluster service", infe);
521             // Should never happen
522
return null;
523          }
524       }
525    }
526
527    /**
528     * @throws RemoteException Necessary for a EJB
529     * @ejb:interface-method view-type="remote"
530     */

531    public void unregisterMBean(ObjectName pName)
532            throws
533            InstanceNotFoundException,
534            MBeanRegistrationException,
535            RemoteException JavaDoc
536    {
537       if (mManagementService == null)
538       {
539          mbeanServer.unregisterMBean(pName);
540       }
541       else
542       {
543          try
544          {
545             mbeanServer.invoke(mManagementService,
546                     "unregisterMBean",
547                     new Object JavaDoc[]{
548                        pName
549                     },
550                     new String JavaDoc[]{
551                        ObjectName.class.getName()
552                     });
553          }
554                  // Should never happen
555
catch (MBeanException me)
556          {
557             log.error("unregisterMBean() got exception from cluster service", me);
558          }
559          catch (ReflectionException re)
560          {
561             log.error("unregisterMBean() got exception from cluster service", re);
562          }
563       }
564    }
565
566    /**
567     * @throws RemoteException Necessary for a EJB
568     * @ejb:interface-method view-type="remote"
569     */

570    public void addNotificationListener(ObjectName pBroadcaster,
571                                        ObjectName pListener,
572                                        NotificationFilter pFilter,
573                                        Object JavaDoc pHandback)
574            throws
575            InstanceNotFoundException,
576            RemoteException JavaDoc
577    {
578       if (mManagementService == null)
579       {
580          mbeanServer.addNotificationListener(pBroadcaster, pListener, pFilter, pHandback);
581       }
582       else
583       {
584          try
585          {
586             mbeanServer.invoke(mManagementService,
587                     "addNotificationListener",
588                     new Object JavaDoc[]{
589                        pBroadcaster,
590                        pListener,
591                        pFilter,
592                        pHandback
593                     },
594                     new String JavaDoc[]{
595                        ObjectName.class.getName(),
596                        ObjectName.class.getName(),
597                        NotificationFilter.class.getName(),
598                        Object JavaDoc.class.getName()
599                     });
600          }
601                  // Should never happen
602
catch (MBeanException me)
603          {
604             log.error("addNotificationListener() got exception from cluster service", me);
605          }
606          catch (ReflectionException re)
607          {
608             log.error("addNotificationListener() got exception from cluster service", re);
609          }
610       }
611    }
612
613    /**
614     * @throws RemoteException Necessary for a EJB
615     * @ejb:interface-method view-type="remote"
616     */

617    public void removeNotificationListener(ObjectName pBroadcaster,
618                                           ObjectName pListener)
619            throws
620            InstanceNotFoundException,
621            ListenerNotFoundException,
622            RemoteException JavaDoc
623    {
624       if (mManagementService == null)
625       {
626          mbeanServer.removeNotificationListener(pBroadcaster, pListener);
627       }
628       else
629       {
630          try
631          {
632             mbeanServer.invoke(mManagementService,
633                     "removeNotificationListener",
634                     new Object JavaDoc[]{
635                        pBroadcaster,
636                        pListener
637                     },
638                     new String JavaDoc[]{
639                        ObjectName.class.getName(),
640                        ObjectName.class.getName()
641                     });
642          }
643                  // Should never happen
644
catch (MBeanException me)
645          {
646             log.error("removeNotificationListener() got exception from cluster service", me);
647          }
648          catch (ReflectionException re)
649          {
650             log.error("removeNotificationListener() got exception from cluster service", re);
651          }
652       }
653    }
654
655    /**
656     * Create the Session Bean which takes the first available
657     * MBeanServer as target server
658     *
659     * @throws CreateException
660     * @ejb:create-method
661     */

662    public void ejbCreate()
663            throws
664            CreateException JavaDoc
665    {
666       if (mbeanServer == null)
667       {
668          try
669          {
670             Context JavaDoc jndiCtx = new InitialContext JavaDoc();
671             String JavaDoc serverName = (String JavaDoc) jndiCtx.lookup("java:comp/env/Server-Name");
672             serverName = serverName.trim();
673             if (serverName == null || serverName.length() == 0 || serverName.equals("null"))
674             {
675                try
676                {
677                   mbeanServer = MBeanServerLocator.locateJBoss();
678                }
679                catch (IllegalStateException JavaDoc e)
680                {
681                   throw new CreateException JavaDoc("No local JMX MBeanServer available");
682                }
683             }
684             else
685             {
686                Object JavaDoc lServer = jndiCtx.lookup(serverName);
687                if (lServer != null)
688                {
689                   if (lServer instanceof MBeanServer)
690                   {
691                      mbeanServer = (MBeanServer) lServer;
692                   }
693                   else
694                   {
695                      throw new CreateException JavaDoc("Server: " + lServer + " reference by Server-Name: " + serverName +
696                              " is not of type MBeanServer");
697                   }
698                }
699                else
700                {
701                   throw new CreateException JavaDoc("Server-Name " + serverName
702                           + " does not reference an Object in JNDI");
703                }
704             }
705          }
706          catch (NamingException JavaDoc ne)
707          {
708             throw new EJBException JavaDoc(ne);
709          }
710       }
711
712       // Check to see if a HA-Management Service is available to be used
713
try
714       {
715          ObjectName haManagement = new ObjectName("jboss:service=HAManagement");
716          ObjectInstance oi = mbeanServer.getObjectInstance(haManagement);
717          mManagementService = oi.getObjectName();
718       }
719       catch (Exception JavaDoc e)
720       {
721          log.debug("ejbCreate() failed to locate jboss:service=HAManagement", e);
722       }
723    }
724
725    /**
726     * Describes the instance and its content for debugging purpose
727     *
728     * @return Debugging information about the instance and its content
729     */

730    public String JavaDoc toString()
731    {
732       return "Management [ " + " ]";
733    }
734
735    // -------------------------------------------------------------------------
736
// Framework Callbacks
737
// -------------------------------------------------------------------------
738

739    /**
740     * Set the associated session context. The container invokes this method on
741     * an instance after the instance has been created.
742     * <p>This method is called with no transaction context.
743     *
744     * @param aContext A SessionContext interface for the instance. The instance
745     * should store the reference to the context in an instance variable.
746     * @throws EJBException Should something go wrong while seting the context,
747     * an EJBException will be thrown.
748     */

749    public void setSessionContext(SessionContext JavaDoc aContext)
750            throws
751            EJBException JavaDoc
752    {
753       mContext = aContext;
754    }
755
756
757    /**
758     * The activate method is called when the instance is activated from its
759     * "passive" state. The instance should acquire any resource that it has
760     * released earlier in the ejbPassivate() method.
761     * <p>This method is called with no transaction context.
762     *
763     * @throws EJBException Thrown by the method to indicate a failure caused
764     * by a system-level error
765     */

766    public void ejbActivate()
767            throws
768            EJBException JavaDoc
769    {
770    }
771
772
773    /**
774     * The passivate method is called before the instance enters the "passive"
775     * state. The instance should release any resources that it can re-acquire
776     * later in the ejbActivate() method.
777     * <p>After the passivate method completes, the instance must be in a state
778     * that allows the container to use the Java Serialization protocol to
779     * externalize and store away the instance's state.
780     * <p>This method is called with no transaction context.
781     *
782     * @throws EJBException Thrown by the method to indicate a failure caused
783     * by a system-level error
784     */

785    public void ejbPassivate()
786            throws
787            EJBException JavaDoc
788    {
789    }
790
791
792    /**
793     * A container invokes this method before it ends the life of the session
794     * object. This happens as a result of a client's invoking a remove
795     * operation, or when a container decides to terminate the session object
796     * after a timeout.
797     * <p>This method is called with no transaction context.
798     *
799     * @throws EJBException Thrown by the method to indicate a failure caused
800     * by a system-level error
801     */

802    public void ejbRemove()
803            throws
804            EJBException JavaDoc
805    {
806    }
807
808 }
809
Popular Tags