KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > runtime > BaseRuntimeMBean


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * $Id: BaseRuntimeMBean.java,v 1.5 2006/03/12 01:26:57 jluehe Exp $
26  * @author: alexkrav
27  *
28  * $Log: BaseRuntimeMBean.java,v $
29  * Revision 1.5 2006/03/12 01:26:57 jluehe
30  * Renamed AS's org.apache.commons.* to com.sun.org.apache.commons.*, to avoid collisions with org.apache.commons.* packages bundled by webapps.
31  *
32  * Tests run: QL, Servlet TCK
33  *
34  * Revision 1.4 2005/12/25 03:47:41 tcfujii
35  * Updated copyright text and year.
36  *
37  * Revision 1.3 2005/11/14 22:53:15 kravtch
38  * Bug #6338666 (Adding customRealm in adminGUI in the server-config throws BadRealmException in server.log)
39  * - ManagedConfigMBean.createChildByType() now sets Properties as well;
40  * - BaseConfigMBean: convenience methods getManagedConfigBean() & getConfigBeanObjectName() are added;
41  * - ConfigsMBean.createAuthRealm(): instead of call to MBeanServer - use ManagedConfigBean.createChildByType()
42  *
43  * Submitted by: kravtch
44  * Reviewed by: Kedar
45  * Affected modules admin/mbeans; admin-core/util; tools
46  * Tests passed: QLT/EE + devtests
47  *
48  * Revision 1.2 2005/06/27 21:19:46 tcfujii
49  * Issue number: CDDL header updates.
50  *
51  * Revision 1.1.1.1 2005/05/27 22:52:02 dpatil
52  * GlassFish first drop
53  *
54  * Revision 1.8 2004/11/14 07:04:23 tcfujii
55  * Updated copyright text and/or year.
56  *
57  * Revision 1.7 2004/02/20 03:56:18 qouyang
58  *
59  *
60  * First pass at code merge.
61  *
62  * Details for the merge will be published at:
63  * http://javaweb.sfbay.sun.com/~qouyang/workspace/PE8FCSMerge/02202004/
64  *
65  * Revision 1.6.4.2 2004/02/02 07:25:22 tcfujii
66  * Copyright updates notices; reviewer: Tony Ng
67  *
68  * Revision 1.6.4.1 2003/12/15 18:15:50 kravtch
69  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70  * Bug #4965366
71  * Reviewer: Sreeni
72  * Checked in PE8FCS_BRANCH
73  *
74  * BaseRuntimeMBean.getAttributes() now returns empty AttributeList if there is null or empty names array argument.
75  *
76  * Revision 1.6 2003/08/29 02:16:42 kravtch
77  * Bug #4910964 (and similar others)
78  * Reviewer: Sridatta
79  *
80  * Exception handling and logging enchancements:
81  * - extraction target exception for MBeanException and TargetInvocationException:
82  * - switch to localStrings usage;
83  * - throwing exception for config MBeans if error in creation of ConfigBean;
84  * - exceptions for null-results in configbean operations,like getXXbyYYY() [changes commented because of crashing of quick test]
85  *
86  * Revision 1.5 2003/08/24 02:58:50 kravtch
87  * dependancy from J2EEManagedObjectMdl
88  * is removed: J2EEManagedObjectMdl->Object
89  * CVS: ----------------------------------------------------------------------
90  *
91  * Revision 1.4 2003/08/14 23:16:17 kravtch
92  * invokeOperation() signature changed;
93  * BaseConfigMBean now uses mcb.invokeOperation();
94  *
95  * Revision 1.3 2003/08/07 00:41:07 kravtch
96  * - new DTD related changes;
97  * - properties support added;
98  * - getDefaultAttributeValue() implemented for config MBeans;
99  * - merge Jsr77 and config activity in runtime mbeans;
100  *
101  * Revision 1.2 2003/07/29 18:59:37 kravtch
102  * MBeanRegistryEntry:
103  * - support for toFormatString();
104  * - instantiateMBean() method modified to instantiate runtime MBeans as well;
105  * MBeanRegistryFactory:
106  * - fixed bug in getRuntimeRegistry();
107  * MBeanNamingInfo:
108  * - less strict requirements for parm_list_array size in constructor (can be more then needed);
109  * BaseRuntimeMBean:
110  * - exception ClassCastException("Managed resource is not a Jsr77ModelBean") handling;
111  * ManagedJsr77MdlBean:
112  * - call managed bean bug fixed ( getDeclaredMethod()->getMethod())
113  * admin/dtds/runtime-mbeans-descriptors.xml - modified to represent new runtime mbeans;
114  *
115  * Revision 1.1 2003/07/18 20:14:46 kravtch
116  * 1. ALL config mbeans are now covered by descriptors.xml
117  * 2. new infrastructure for runtime mbeans is added
118  * 3. generic constructors added to jsr77Mdl beans (String[])
119  * 4. new test cases are added to admintest
120  * 5. MBeanRegistryFactory has now different methods to obtain admin/runtime registries
121  * 6. runtime-descriptors xml-file is added to build
122  *
123  *
124  *
125 */

126
127 package com.sun.enterprise.admin.runtime;
128
129 import java.lang.reflect.InvocationTargetException JavaDoc;
130 import java.util.Iterator JavaDoc;
131 import java.util.Hashtable JavaDoc;
132 import java.util.ArrayList JavaDoc;
133
134 import javax.management.ObjectName JavaDoc;
135
136 //import com.sun.enterprise.admin.meta.MBeanRegistryEntry;
137

138 import javax.management.modelmbean.DescriptorSupport JavaDoc;
139 //import javax.management.modelmbean.InvalidTargetObjectTypeException;
140
//import javax.management.modelmbean.ModelMBean;
141
import javax.management.Descriptor JavaDoc;
142 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
143 import javax.management.modelmbean.ModelMBeanOperationInfo JavaDoc;
144 import javax.management.modelmbean.ModelMBeanAttributeInfo JavaDoc;
145 import javax.management.*;
146
147 import com.sun.org.apache.commons.modeler.BaseModelMBean;
148 //base admin imports
149
import com.sun.enterprise.admin.BaseAdminMBean;
150 import com.sun.enterprise.admin.MBeanHelper;
151
152 import com.sun.enterprise.admin.config.ManagedConfigBean;
153 import com.sun.enterprise.admin.config.ConfigMBeanHelper;
154 import com.sun.enterprise.admin.config.MBeanConfigException;
155
156 //Config imports
157
import com.sun.enterprise.config.ConfigException;
158 //import com.sun.enterprise.config.serverbeans.ServerXPathHelper;
159
import com.sun.enterprise.config.serverbeans.ServerTags;
160 import com.sun.enterprise.config.ConfigContext;
161 import com.sun.enterprise.config.ConfigFactory;
162 import com.sun.enterprise.config.ConfigBean;
163 import com.sun.enterprise.config.ConfigBeansFactory;
164 import com.sun.enterprise.config.serverbeans.ElementProperty;
165
166 //naming
167
import com.sun.enterprise.admin.meta.naming.MBeanNamingInfo;
168 import com.sun.enterprise.admin.meta.MBeanMetaConstants;
169 import com.sun.enterprise.admin.meta.MBeanRegistryFactory;
170 import com.sun.enterprise.admin.meta.MBeanRegistry;
171
172 //import com.sun.enterprise.management.model.J2EEManagedObjectMdl;
173

174 /**
175  * <p>Base class for Config MBeans which implements basic config
176  * activity according to ModelMBeanInfo provided by MBeanRegistry
177  *
178  *
179  */

180
181 public class BaseRuntimeMBean extends BaseAdminMBean implements MBeanRegistration
182 {
183
184     // ----------------------------------------------------- Instance Variables
185
private ManagedConfigBean mcb = null;
186     private ManagedJsr77MdlBean mrb = null;
187     protected MBeanRegistry m_registry = null;
188     
189
190     // ----------------------------------------------------------- Constructors
191
/**
192      * Construct a <code>ModelMBean</code> with default
193      * <code>ModelMBeanInfo</code> information.
194      *
195      * @exception MBeanException if the initializer of an object
196      * throws an exception
197      * @exception RuntimeOperationsException if an IllegalArgumentException
198      * occurs
199      */

200     public BaseRuntimeMBean() {
201
202         super();
203         m_registry = MBeanRegistryFactory.getRuntimeMBeanRegistry();
204     }
205
206
207  /**
208      * Set the instance handle of the object against which we will execute
209      * all methods in this ModelMBean management interface.
210      *
211      * @param resource The resource object to be managed
212      * @param type The type of reference for the managed resource
213      * ("ObjectReference", "Handle", "IOR", "EJBHandle", or
214      * "RMIReference" OR "ConfigBeanReference" or "Jsr77ModelBeanReference" )
215      *
216      * @exception InstanceNotFoundException if the managed resource object
217      * cannot be found
218      * @exception InvalidTargetObjectTypeException if this ModelMBean is
219      * asked to handle a reference type it cannot deal with
220      * @exception MBeanException if the initializer of the object throws
221      * an exception
222      * @exception RuntimeOperationsException if the managed resource or the
223      * resource type is <code>null</code> or invalid
224      */

225     public void setManagedResource(Object JavaDoc resource, String JavaDoc type)
226         throws InstanceNotFoundException, /*InvalidTargetObjectTypeException,*/
227         MBeanException, RuntimeOperationsException {
228
229         if (resource == null)
230             throw new RuntimeOperationsException
231                 (new IllegalArgumentException JavaDoc("Managed resource is null"),
232                  "Managed resource is null");
233         
234         if (MBeanMetaConstants.CONFIG_BEAN_REF.equalsIgnoreCase(type)) {
235             if(! (resource instanceof ConfigBean)) {
236                 throw new RuntimeOperationsException
237                  (new ClassCastException JavaDoc("Managed resource is not a ConfigBean"),
238                  "Managed resource is not a ConfigBean");
239             }
240             this.mcb = new ManagedConfigBean(this, (ConfigBean) resource, m_registry);
241             
242         } else {
243             if (MBeanMetaConstants.JSR77_MODEL_BEAN_REF.equalsIgnoreCase(type))
244             {
245 /* if(! (resource instanceof J2EEManagedObjectMdl)) {
246                     throw new RuntimeOperationsException
247                      (new ClassCastException("Managed resource is not a Jsr77ModelBean :"+resource.getClass().getName()),
248                      "Managed resource is not a Jsr77ModelBean: "+resource.getClass().getName());
249                 }
250  */

251                 this.mrb = new ManagedJsr77MdlBean(this, /*(J2EEManagedObjectMdl)*/ resource);
252             } else
253             {
254                 super.setManagedResource(resource, type);
255             }
256         }
257     }
258
259     
260
261
262
263     // --------------------------------------------------- DynamicMBean Methods
264

265
266     /**
267      * Obtain and return the value of a specific attribute of this MBean.
268      *
269      * @param name Name of the requested attribute
270      *
271      * @exception AttributeNotFoundException if this attribute is not
272      * supported by this MBean
273      * @exception MBeanException if the initializer of an object
274      * throws an exception
275      * @exception ReflectionException if a Java reflection exception
276      * occurs when invoking the getter
277      */

278     public Object JavaDoc getAttribute(String JavaDoc name) throws AttributeNotFoundException, MBeanException, ReflectionException
279     {
280         ModelMBeanAttributeInfo JavaDoc attrInfo = (ModelMBeanAttributeInfo JavaDoc)MBeanHelper.findMatchingAttributeInfo((MBeanInfo JavaDoc)info, name);
281         if(attrInfo==null)
282             throw new AttributeNotFoundException();
283         //FIXME add check "is readable"
284
//1. MBean
285
try
286         {
287             return super.getAttribute(name);
288         } catch (Exception JavaDoc e) {}
289
290         //2. runtime managed bean
291
if(mrb!=null)
292         {
293             try {
294                 return mrb.getAttribute(attrInfo, name);
295             } catch (Exception JavaDoc e) {}
296         }
297
298         //3. config managed bean
299
if(mcb!=null)
300         {
301             try {
302                 return mcb.getAttribute(attrInfo, name);
303             } catch (Exception JavaDoc e) {}
304         }
305         throw new AttributeNotFoundException(); //?????
306
}
307     
308     public AttributeList getAttributes(String JavaDoc[] attributeNames)
309     {
310         AttributeList list = new AttributeList();
311         if(attributeNames!=null)
312             for(int i=0; i<attributeNames.length; i++)
313             {
314                 try {
315                     Object JavaDoc value = getAttribute(attributeNames[i]);
316                     list.add(new Attribute(attributeNames[i], value));
317                 } catch (Exception JavaDoc e) {}
318             }
319         return list;
320     }
321
322     /**
323      * Set the value of a specific attribute of this MBean.
324      *
325      * @param attribute The identification of the attribute to be set
326      * and the new value
327      *
328      * @exception AttributeNotFoundException if this attribute is not
329      * supported by this MBean
330      * @exception MBeanException if the initializer of an object
331      * throws an exception
332      * @exception ReflectionException if a Java reflection exception
333      * occurs when invoking the getter
334      */

335     public void setAttribute(Attribute attribute)
336         throws AttributeNotFoundException, MBeanException,
337         ReflectionException
338     {
339         ModelMBeanAttributeInfo JavaDoc attrInfo = (ModelMBeanAttributeInfo JavaDoc)MBeanHelper.findMatchingAttributeInfo((MBeanInfo JavaDoc)info, attribute.getName());
340         if(attrInfo==null)
341             throw new AttributeNotFoundException();
342         //FIXME add check "is writable"
343

344         //1. MBean
345
try
346         {
347             super.setAttribute(attribute);
348             return;
349         } catch (Exception JavaDoc e) {}
350
351         //2. runtime managed bean
352
if(mrb!=null)
353         {
354             try {
355                 mrb.setAttribute(attrInfo, attribute);
356                 return;
357             } catch (Exception JavaDoc e) {}
358         }
359
360         //3. config managed bean
361
if(mcb!=null)
362         {
363             try {
364                 mcb.setAttribute(attrInfo, attribute);
365                 return;
366             } catch (Exception JavaDoc e) {}
367         }
368
369     }
370
371     /****************************************************************************************************************
372      * Sets the values of several MBean's attributes.
373      * @param attrList A list of attributes: The identification of the attributes to be set and the values they are to be set to.
374      * @return The list of attributes that were set, with their new values.
375      */

376     public AttributeList setAttributes(AttributeList list)
377     {
378         if(list==null || list.size()<=0)
379             return null;
380         AttributeList listRes = new AttributeList();
381         for(int i=0; i<list.size(); i++)
382         {
383             try {
384                 Attribute attr = (Attribute)list.get(i);
385                 setAttribute(attr);
386                 listRes.add(attr);
387             } catch (Exception JavaDoc e) {}
388         }
389         return listRes;
390     }
391
392
393     /**
394      * Invoke a particular method on this MBean, and return any returned
395      * value.
396      *
397      * <p><strong>IMPLEMENTATION NOTE</strong> - This implementation will
398      * attempt to invoke this method on the MBean itself, or (if not
399      * available) on the managed resource object associated with this
400      * MBean.</p>
401      *
402      * @param name Name of the operation to be invoked
403      * @param params Array containing the method parameters of this operation
404      * @param signature Array containing the class names representing
405      * the signature of this operation
406      *
407      * @exception MBeanException if the initializer of an object
408      * throws an exception
409      * @exception ReflectioNException if a Java reflection exception
410      * occurs when invoking a method
411      */

412     public Object JavaDoc invoke(String JavaDoc name, Object JavaDoc params[], String JavaDoc signature[])
413         throws MBeanException, ReflectionException {
414             ModelMBeanOperationInfo JavaDoc opInfo = (ModelMBeanOperationInfo JavaDoc)MBeanHelper.findMatchingOperationInfo((MBeanInfo JavaDoc)info, name, signature);
415             if (opInfo == null)
416             {
417                 String JavaDoc msg = _localStrings.getString( "admin.server.core.mbean.config.base.operation_is_not_found", mbeanType, name);
418                 throw new MBeanException
419                     (new ServiceNotFoundException(msg), msg);
420             }
421
422             //Generic Config invoker
423
Object JavaDoc ret;
424             try
425             {
426                 ret = MBeanHelper.invokeOperationInBean(opInfo, this, params);
427                 if(ret!=MBeanHelper.INVOKE_ERROR_SIGNAL_OBJECT)
428                     return ret;
429
430                 if(mrb!=null)
431                 {
432                     ret = mrb.invokeOperation(opInfo, params, signature);
433                     if(ret!=MBeanHelper.INVOKE_ERROR_SIGNAL_OBJECT)
434                         return ret;
435                 }
436
437                 if(mcb!=null)
438                 {
439                     ret = mcb.invokeOperation(opInfo, params, signature);
440                     if(ret!=MBeanHelper.INVOKE_ERROR_SIGNAL_OBJECT)
441                         return ret;
442                 }
443
444             }
445             catch (MBeanException mbe)
446             {
447                 throw mbe;
448             }
449             catch (Exception JavaDoc e)
450             {
451                 String JavaDoc msg = _localStrings.getString( "admin.server.core.mbean.runtime.base.invoke_error", mbeanType, name);
452                 throw MBeanHelper.extractAndWrapTargetException(e, msg);
453             }
454
455             //TBD FIXME
456
//value = ConfigBeanHandler.invoke(this.cb, name, params, signature);
457
return super.invoke(name, params, signature);
458     }
459
460     //convenient method for deploy operations
461
protected ObjectName JavaDoc createChildElementByType(String JavaDoc childElementName, Attribute[] attrs) throws Exception JavaDoc
462     {
463         return createChildElementByType(childElementName, attrs, true, false);
464     }
465     protected ObjectName JavaDoc createChildElementByType(String JavaDoc childElementName, Attribute[] attrs, boolean bSkipNullValued) throws Exception JavaDoc
466     {
467         return createChildElementByType(childElementName, attrs, bSkipNullValued, false);
468     }
469     protected ObjectName JavaDoc createChildElementByType(String JavaDoc childElementName, Attribute[] attrs, boolean bSkipNullValued, boolean bOnlyOne) throws Exception JavaDoc
470     {
471         AttributeList list = new AttributeList();
472         for(int i=0; i<attrs.length; i++)
473         {
474             if(!bSkipNullValued || attrs[i].getValue()!=null)
475                 list.add(attrs[i]);
476         }
477         ConfigBean bean = mcb.createChildByType(childElementName, list, null, bOnlyOne);
478         return ConfigMBeanHelper.getChildObjectName(m_registry, info, bean);
479     }
480
481     
482     // -------------------- Registration --------------------
483
// XXX We can add some method patterns here - like setName() and
484
// setDomain() for code that doesn't implement the Registration
485

486     public ObjectName JavaDoc preRegister(MBeanServer server,
487                                   ObjectName JavaDoc name)
488             throws Exception JavaDoc
489     {
490         return name;
491     }
492
493     public void postRegister(Boolean JavaDoc registrationDone) {
494     }
495
496     public void preDeregister() throws Exception JavaDoc {
497     }
498
499     public void postDeregister() {
500     }
501     
502 /* public boolean destroyConfigElement() throws Exception //FIXME: MBeanException?
503     {
504         if(mcb==null)
505         {//FIXME: should be exception if null
506             return false;
507         }
508         mcb.deleteSelf();
509         //now unregister MBean
510         ObjectName objectName = ConfigMBeanHelper.getOwnObjectName(m_registry, info);
511         if(objectName!=null)
512         {
513             try{
514                 MBeanServer server = (MBeanServer)(MBeanServerFactory.findMBeanServer(null)).get(0);
515                 server.unregisterMBean(objectName);
516                 }
517             catch (Throwable t)
518                 {
519                     System.out.println("!!!!!!!!!!!!!! Can not unregister MBean: "+objectName);
520                 }
521             return true;
522         }
523
524         return false;
525         //FIXME handle exceptions
526         //return false;
527     }
528 */

529 }
530
Popular Tags