KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Hashtable JavaDoc;
25
26 import javax.management.JMException JavaDoc;
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.MalformedObjectNameException JavaDoc;
29 import javax.management.ObjectName JavaDoc;
30
31 import org.jboss.logging.Logger;
32
33 /**
34  * Root class of the JBoss implementation of a custom MBean managed object.
35  *
36  * @author <a HREF="mailto:andreas@jboss.org">Andreas Schaefer</a>
37  * @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
38  * @author <a HREF="mailto:thomas.diesler@jboss.org">Thomas Diesler</a>
39  * @version $Revision: 41367 $
40  */

41 public class MBean extends J2EEManagedObject
42    implements MBeanMBean
43 {
44
45    // Attributes ----------------------------------------------------
46
private static Logger log = Logger.getLogger(MBean.class);
47
48    private ObjectName JavaDoc jbossServiceName;
49    private StateManagement mState;
50    private boolean monitorsStateChanges = false;
51    /** Does the mbean have a State attribute */
52    private boolean hasState;
53    
54    // Static --------------------------------------------------------
55

56    /**
57     * Create a
58     *
59     * @param pServer
60     * @param pServiceModule
61     * @param pTarget
62     * @return ObjectName of the MBean
63     */

64    public static ObjectName JavaDoc create(MBeanServer JavaDoc pServer, String JavaDoc pServiceModule,
65                                    ObjectName JavaDoc pTarget)
66    {
67       String JavaDoc pName = pTarget.toString();
68       ObjectName JavaDoc mbeanName = null;
69       try
70       {
71          if (pServiceModule == null)
72          {
73             log.debug("Parent SAR Module not defined");
74             return null;
75          }
76
77          MBean mbean = new MBean(pName, new ObjectName JavaDoc(pServiceModule), pTarget);
78          mbeanName = mbean.getObjectName();
79          pServer.registerMBean(mbean, mbeanName);
80       }
81       catch (Exception JavaDoc e)
82       {
83          log.debug("Could not create JSR-77 MBean: " + pName, e);
84       }
85       return mbeanName;
86    }
87
88    public static void destroy(MBeanServer JavaDoc pServer, String JavaDoc pName)
89    {
90       try
91       {
92          if (pName.indexOf(J2EEManagedObject.TYPE + "=" + J2EETypeConstants.MBean) >= 0)
93          {
94             J2EEManagedObject.removeObject(pServer,
95                     pName);
96          }
97          else
98          {
99             J2EEManagedObject.removeObject(pServer,
100                     pName,
101                     J2EEDomain.getDomainName() + ":" +
102                     J2EEManagedObject.TYPE + "=" + J2EETypeConstants.MBean +
103                     "," + "*");
104          }
105       }
106       catch (Exception JavaDoc e)
107       {
108          log.error("Could not destroy JSR-77 MBean: " + pName, e);
109       }
110    }
111
112    // Constructors --------------------------------------------------
113

114    /**
115     * @param pName Name of the MBean
116     */

117    public MBean(String JavaDoc pName, ObjectName JavaDoc pServiceModule, ObjectName JavaDoc pTarget)
118            throws
119            MalformedObjectNameException JavaDoc,
120            InvalidParentException
121    {
122       super(J2EETypeConstants.MBean, pName, pServiceModule);
123       mState = new StateManagement(this);
124       jbossServiceName = pTarget;
125    }
126
127    /**
128     * Does the MBean monitor state changes of the JBoss MBean service.
129     *
130     * @return True if the underlying JBoss MBean service is monitored for state
131     * changes.
132     * @jmx:managed-attribute
133     */

134    public boolean isstateMonitored()
135    {
136       return monitorsStateChanges;
137    }
138
139    public boolean isstateManageable()
140    {
141       return hasState;
142    }
143
144    // J2EEManagedObjectMBean implementation -------------------------
145

146    public void postCreation()
147    {
148       try
149       {
150          // First check if the service implements the NotificationBroadcaster
151
monitorsStateChanges = getServer().isInstanceOf(jbossServiceName,
152                  "javax.management.NotificationBroadcaster");
153          if (monitorsStateChanges)
154          {
155             getServer().addNotificationListener(jbossServiceName, mState, null, null);
156          }
157       }
158       catch (Exception JavaDoc jme)
159       {
160          log.debug("Failed to register as listener of: " + jbossServiceName, jme);
161       }
162       sendNotification(NotificationConstants.OBJECT_CREATED, "MBean created");
163
164       // Initialize the state
165
try
166       {
167          Integer JavaDoc jbossState = (Integer JavaDoc)getServer().getAttribute(jbossServiceName, "State");
168          int jsr77State = StateManagement.convertJBossState(jbossState.intValue());
169          mState.setState(jsr77State);
170       }
171       catch (Exception JavaDoc e)
172       {
173          log.trace("Failed to initialize state from: '" + jbossServiceName +
174                "' : " + e.getClass().getName() + " : " + e.getMessage());
175          hasState = false;
176       }
177    }
178
179    public void preDestruction()
180    {
181       sendNotification(NotificationConstants.OBJECT_DELETED, "MBean destroyed");
182       // Remove the listener of the target MBean
183
try
184       {
185          if( monitorsStateChanges )
186             getServer().removeNotificationListener(jbossServiceName, mState);
187       }
188       catch (JMException JavaDoc jme)
189       {
190          // When the service is not available anymore then just ignore the exception
191
}
192    }
193
194    // javax.managment.j2ee.EventProvider implementation -------------
195

196    public String JavaDoc[] getEventTypes()
197    {
198       return StateManagement.stateTypes;
199    }
200
201    public String JavaDoc getEventType(int pIndex)
202    {
203       if (pIndex >= 0 && pIndex < StateManagement.stateTypes.length)
204       {
205          return StateManagement.stateTypes[pIndex];
206       }
207       else
208       {
209          return null;
210       }
211    }
212
213    // javax.management.j2ee.StateManageable implementation ----------
214

215    public long getStartTime()
216    {
217       return mState.getStartTime();
218    }
219
220    public int getState()
221    {
222       return mState.getState();
223    }
224
225    public String JavaDoc getStateString()
226    {
227       return mState.getStateString();
228    }
229
230    public void mejbStart()
231    {
232       try
233       {
234          getServer().invoke(jbossServiceName,
235                  "start",
236                  new Object JavaDoc[]{},
237                  new String JavaDoc[]{});
238       }
239       catch (Exception JavaDoc e)
240       {
241          getLog().error("Start of MBean failed", e);
242       }
243    }
244
245    public void mejbStartRecursive()
246    {
247       // No recursive start here
248
mejbStart();
249    }
250
251    public void mejbStop()
252    {
253       try
254       {
255          getServer().invoke(jbossServiceName,
256                  "stop",
257                  new Object JavaDoc[]{},
258                  new String JavaDoc[]{});
259       }
260       catch (Exception JavaDoc e)
261       {
262          getLog().error("Stop of MBean failed", e);
263       }
264    }
265
266    // java.lang.Object overrides --------------------------------------
267

268    public String JavaDoc toString()
269    {
270       return "MBean { " + super.toString() + " } []";
271    }
272
273    // Package protected ---------------------------------------------
274

275    // Protected -----------------------------------------------------
276

277    /**
278     * @return A hashtable with the SAR-Module, J2EE-Application and J2EE-Server as parent
279     */

280    protected Hashtable JavaDoc getParentKeys(ObjectName JavaDoc pParent)
281    {
282       Hashtable JavaDoc lReturn = new Hashtable JavaDoc();
283       Hashtable JavaDoc lProperties = pParent.getKeyPropertyList();
284       lReturn.put(J2EETypeConstants.ServiceModule, lProperties.get("name"));
285       // J2EE-Application is never a parent of a MBean therefore set it to "null"
286
lReturn.put(J2EETypeConstants.J2EEApplication, "null");
287       // J2EE-Server is already parent of J2EE-Application therefore lookup
288
// the name by the J2EE-Server type
289
lReturn.put(J2EETypeConstants.J2EEServer, lProperties.get(J2EETypeConstants.J2EEServer));
290
291       return lReturn;
292    }
293
294    // Private -------------------------------------------------------
295

296    // Inner classes -------------------------------------------------
297

298 }
299
Popular Tags