KickJava   Java API By Example, From Geeks To Geeks.

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


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.security.InvalidParameterException JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import javax.management.InstanceNotFoundException JavaDoc;
29 import javax.management.JMException JavaDoc;
30 import javax.management.MBeanRegistration JavaDoc;
31 import javax.management.MBeanServer JavaDoc;
32 import javax.management.MalformedObjectNameException JavaDoc;
33 import javax.management.Notification JavaDoc;
34 import javax.management.ObjectName JavaDoc;
35
36 import org.jboss.logging.Logger;
37 import org.jboss.management.j2ee.statistics.StatisticsProvider;
38 import org.jboss.mx.util.JBossNotificationBroadcasterSupport;
39 import org.jboss.mx.util.ObjectNameConverter;
40
41 /**
42  * Root class of the JBoss JSR-77 implementation of J2EEManagedObject.
43  *
44  * @author <a HREF="mailto:andreas@jboss.org">Andreas Schaefer</a>
45  * @author <a HREF="mailto:scott.stark@jboss.org">Scott Stark</a>
46  * @author <a HREF="mailto:thomas.diesler@jboss.org">Thomas Diesler</a>
47  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
48  * @version $Revision: 44311 $
49  */

50 public abstract class J2EEManagedObject extends JBossNotificationBroadcasterSupport
51    implements J2EEManagedObjectMBean, MBeanRegistration JavaDoc
52 {
53    // Constants -----------------------------------------------------
54
public static final String JavaDoc TYPE = "j2eeType";
55    public static final String JavaDoc NAME = "name";
56
57    // Attributes ----------------------------------------------------
58
private static Logger log = Logger.getLogger(J2EEManagedObject.class);
59
60    private ObjectName JavaDoc parentName = null;
61    private ObjectName JavaDoc name = null;
62    
63    protected MBeanServer JavaDoc server;
64
65    // Static --------------------------------------------------------
66

67    /**
68     * Retrieves the type out of an JSR-77 object name
69     *
70     * @param pName Object Name to check if null then
71     * it will be treated like NO type found
72     * @return The type of the given Object Name or an EMPTY
73     * string if either Object Name null or type not found
74     */

75    protected static String JavaDoc getType(String JavaDoc pName)
76    {
77       String JavaDoc lType = null;
78       if (pName != null)
79       {
80          ObjectName JavaDoc oname = newObjectName(pName);
81          lType = (String JavaDoc) oname.getKeyPropertyList().get(TYPE);
82       }
83       // Return an empty string if type not found
84
return lType == null ? "" : lType;
85    }
86
87    /**
88     * Retrieves the type out of an JSR-77 object name
89     *
90     * @param pName Object Name to check if null then
91     * it will be treated like NO type found
92     * @return The type of the given Object Name or an EMPTY
93     * string if either Object Name null or type not found
94     */

95    protected static String JavaDoc getType(ObjectName JavaDoc pName)
96    {
97       String JavaDoc lType = null;
98       if (pName != null)
99       {
100          lType = (String JavaDoc) pName.getKeyPropertyList().get(TYPE);
101       }
102       // Return an empty string if type not found
103
return lType == null ? "" : lType;
104    }
105
106    /**
107     * Return the ObjectName that is represented by the given string.
108     *
109     * @param pName a object name
110     */

111    protected static ObjectName JavaDoc newObjectName(String JavaDoc pName)
112    {
113       try
114       {
115          return new ObjectName JavaDoc(pName);
116       }
117       catch (MalformedObjectNameException JavaDoc e)
118       {
119          throw new IllegalArgumentException JavaDoc("Invalid object name: " + pName);
120       }
121    }
122
123    protected static ObjectName JavaDoc removeObject(MBeanServer JavaDoc pServer, String JavaDoc pSearchCriteria)
124            throws JMException JavaDoc
125    {
126       ObjectName JavaDoc lSearch = ObjectNameConverter.convert(pSearchCriteria);
127       log.debug("removeObject(), search for: " + pSearchCriteria + ", search criteria: " + lSearch);
128       Set JavaDoc lNames = pServer.queryNames(lSearch, null);
129       if (!lNames.isEmpty())
130       {
131          ObjectName JavaDoc lName = (ObjectName JavaDoc) lNames.iterator().next();
132          pServer.unregisterMBean(lName);
133          return lName;
134       }
135       return null;
136    }
137
138    protected static ObjectName JavaDoc removeObject(MBeanServer JavaDoc pServer, String JavaDoc pName, String JavaDoc pSearchCriteria)
139            throws JMException JavaDoc
140    {
141       String JavaDoc lEncryptedName = ObjectNameConverter.convertCharacters(pName, true);
142       ObjectName JavaDoc lSearch = new ObjectName JavaDoc(pSearchCriteria + "," + NAME + "=" + lEncryptedName);
143       log.debug("removeObject(), name: " + pName + ", encrypted name: " + lEncryptedName + ", search criteria: " + lSearch);
144       Set JavaDoc lNames = pServer.queryNames(lSearch, null);
145       if (!lNames.isEmpty())
146       {
147          ObjectName JavaDoc lName = (ObjectName JavaDoc) lNames.iterator().next();
148          pServer.unregisterMBean(lName);
149          return lName;
150       }
151       return null;
152    }
153
154    // Constructors --------------------------------------------------
155

156    /**
157     * Constructor for the root J2EEDomain object
158     *
159     * @param domainName domain portion to use for the JMX ObjectName
160     * @param j2eeType JSR77 j2ee-type of the resource being created
161     * @param resName Name of the managed resource
162     * @throws InvalidParameterException If the given Domain Name, Type or Name is null
163     */

164    public J2EEManagedObject(String JavaDoc domainName, String JavaDoc j2eeType, String JavaDoc resName)
165            throws MalformedObjectNameException JavaDoc
166    {
167       log = Logger.getLogger(getClass());
168       if (domainName == null)
169       {
170          throw new InvalidParameterException JavaDoc("Domain Name must be set");
171       }
172       Hashtable JavaDoc lProperties = new Hashtable JavaDoc();
173       lProperties.put(TYPE, j2eeType);
174       lProperties.put(NAME, resName);
175       name = ObjectNameConverter.convert(domainName, lProperties);
176       log.debug("ctor, name: " + name);
177    }
178
179    /**
180     * Constructor for any Managed Object except the root J2EEMangement.
181     *
182     * @param j2eeType JSR77 j2ee-type of the resource being created
183     * @param resName name of the resource
184     * @param jsr77ParentName Object Name of the parent of this Managed Object
185     * which must be defined
186     * @throws InvalidParameterException If the given Type, Name or Parent is null
187     */

188    public J2EEManagedObject(String JavaDoc j2eeType, String JavaDoc resName, ObjectName JavaDoc jsr77ParentName)
189            throws MalformedObjectNameException JavaDoc,
190            InvalidParentException
191    {
192       log = Logger.getLogger(getClass());
193       Hashtable JavaDoc lProperties = getParentKeys(jsr77ParentName);
194       lProperties.put(TYPE, j2eeType);
195       lProperties.put(NAME, resName);
196       name = ObjectNameConverter.convert(J2EEDomain.getDomainName(), lProperties);
197       setparent(jsr77ParentName.getCanonicalName());
198    }
199
200    // Public --------------------------------------------------------
201

202    public Logger getLog()
203    {
204       return log;
205    }
206    
207    public MBeanServer JavaDoc getServer()
208    {
209       return server;
210    }
211
212    public ObjectName JavaDoc getObjectName()
213    {
214       return name;
215    }
216    
217    // J2EEManagedObjectMBean implementation ----------------------------------------------
218

219    /**
220     * @jmx:managed-attribute
221     */

222    public String JavaDoc getobjectName()
223    {
224       return name.getCanonicalName();
225    }
226
227    /**
228     * @jmx:managed-attribute
229     */

230    public String JavaDoc getparent()
231    {
232       String JavaDoc parent = null;
233       
234       if (parentName != null)
235          parent = parentName.getCanonicalName();
236       
237       return parent;
238    }
239
240    /**
241     * @jmx:managed-attribute
242     */

243    public void setparent(String JavaDoc pParent) throws InvalidParentException
244    {
245       if (pParent == null)
246       {
247          throw new InvalidParameterException JavaDoc("Parent must be set");
248       }
249       parentName = newObjectName(pParent);
250    }
251
252    /**
253     * @jmx:managed-operation
254     */

255    public void addChild(ObjectName JavaDoc pChild)
256    {
257    }
258
259    /**
260     * @jmx:managed-operation
261     */

262    public void removeChild(ObjectName JavaDoc pChild)
263    {
264    }
265
266    /**
267     * @jmx:managed-attribute
268     */

269    public boolean isstateManageable()
270    {
271       return this instanceof StateManageable;
272    }
273
274    /**
275     * @jmx:managed-attribute
276     */

277    public boolean isstatisticsProvider()
278    {
279       return this instanceof StatisticsProvider;
280    }
281
282    /**
283     * @jmx:managed-attribute
284     */

285    public boolean iseventProvider()
286    {
287       return this instanceof EventProvider;
288    }
289
290    // MBeanRegistration implementation ------------------------------
291

292    public ObjectName JavaDoc preRegister(MBeanServer JavaDoc server, ObjectName JavaDoc name)
293    {
294       this.server = server;
295       return name;
296    }
297
298    /**
299     * Last steps to be done after MBean is registered on MBeanServer. This
300     * method is made final because it contains vital steps mandatory to all
301     * J2EEManagedObjects. To perform your own Post-Creation steps please
302     * override {@link #postCreation postCreation()} method.
303     */

304    public final void postRegister(Boolean JavaDoc registrationDone)
305    {
306       // This try-catch block is here because of debugging purposes because
307
// runtime exception in JMX client is a awful thing to figure out
308
try
309       {
310          log.debug("postRegister(), parent: " + parentName);
311          if (registrationDone.booleanValue())
312          {
313             // Let the subclass handle post creation steps
314
postCreation();
315             if (parentName != null)
316             {
317                try
318                {
319                   // Notify the parent about its new child
320
if (parentName.getKeyProperty("name").compareTo("null") != 0)
321                   {
322                      getServer().invoke(parentName,
323                              "addChild",
324                              new Object JavaDoc[]{name},
325                              new String JavaDoc[]{ObjectName JavaDoc.class.getName()});
326                   }
327                   else
328                   {
329                      ObjectName JavaDoc j2eeServerName = J2EEDomain.getDomainServerName(server);
330                      server.invoke(j2eeServerName,
331                              "addChild",
332                              new Object JavaDoc[]{name},
333                              new String JavaDoc[]{ObjectName JavaDoc.class.getName()});
334                   }
335                }
336                catch (JMException JavaDoc e)
337                {
338                   log.debug("Failed to add child", e);
339                   registrationDone = Boolean.FALSE;
340                }
341             }
342          }
343       }
344       catch (RuntimeException JavaDoc re)
345       {
346          log.debug("postRegister() caught this exception", re);
347          throw re;
348       }
349    }
350
351    /**
352     * Last steps to be done before MBean is unregistered on MBeanServer. This
353     * method is made final because it contains vital steps mandatory to all
354     * J2EEManagedObjects. To perform your own Pre-Destruction steps please
355     * override {@link #preDestruction preDestruction()} method.
356     */

357    public final void preDeregister()
358            throws Exception JavaDoc
359    {
360       log.debug("preDeregister(), parent: " + parentName);
361       // Only remove child if it is a child (root has not parent)
362
if (parentName != null)
363       {
364          try
365          {
366             // Notify the parent about the removal of its child
367
server.invoke(parentName,
368                     "removeChild",
369                     new Object JavaDoc[]{name},
370                     new String JavaDoc[]{ObjectName JavaDoc.class.getName()});
371          }
372          catch (InstanceNotFoundException JavaDoc infe)
373          {
374          }
375          preDestruction();
376       }
377    }
378    public void postDeregister()
379    {
380       server = null;
381    }
382
383    /**
384     * An overload of the super sendNotification that only takes the event
385     * type and msg. The source will be set to the managed object name, the
386     * sequence will be the getNextNotificationSequenceNumber() value, and the
387     * timestamp System.currentTimeMillis().
388     *
389     * @param type the notification event type
390     * @param info the notification event msg info
391     */

392    public void sendNotification(String JavaDoc type, String JavaDoc info)
393    {
394       Notification JavaDoc msg = new Notification JavaDoc(type, this.getObjectName(),
395               this.getNextNotificationSequenceNumber(),
396               System.currentTimeMillis(),
397               info);
398       super.sendNotification(msg);
399    }
400
401    // Object overrides ---------------------------------------------------
402

403    public String JavaDoc toString()
404    {
405       return "J2EEManagedObject [ name: " + name + ", parent: " + parentName + " ];";
406    }
407
408    // Package protected ---------------------------------------------
409

410    // Protected -----------------------------------------------------
411

412    protected void postCreation()
413    {
414    }
415
416    protected void preDestruction()
417    {
418    }
419
420    /**
421     * This method can be overwritten by any subclass which must
422     * return &lt;parent-j2eeType&gt; indicating its parents. By
423     * default it returns an empty hashtable instance.
424     *
425     * @param pParent The direct parent of this class
426     * @return An empty hashtable
427     */

428    protected Hashtable JavaDoc getParentKeys(ObjectName JavaDoc pParent)
429    {
430       return new Hashtable JavaDoc();
431    }
432
433    /**
434     * The <code>getNextNotificationSequenceNumber</code> method returns
435     * the next sequence number for use in notifications.
436     *
437     * @return a <code>long</code> value
438     */

439    protected long getNextNotificationSequenceNumber()
440    {
441       return super.nextNotificationSequenceNumber();
442    }
443
444    // Private -------------------------------------------------------
445

446    // Inner classes -------------------------------------------------
447
}
448
Popular Tags