KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > MBeanHelper


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: MBeanHelper.java,v 1.5 2006/03/08 01:34:34 kravtch Exp $
26  * @author: alexkrav
27  *
28  * $Log: MBeanHelper.java,v $
29  * Revision 1.5 2006/03/08 01:34:34 kravtch
30  * Bug #6239362(many MBean server validation messages are not i18n-ed)
31  * admin-core/admin:
32  * - "Target exception message" string literal put into localstrings
33  * Submitted by: kravtch
34  * Reviewed by: Kedar
35  * Affected modules admin-core/admin;
36  * Tests passed: QLT/EE
37  *
38  * Revision 1.4 2005/12/25 03:47:28 tcfujii
39  * Updated copyright text and year.
40  *
41  * Revision 1.3 2005/08/16 22:19:29 kravtch
42  * M3: 1. ConfigMBeans: Support for generic getXXXNamesList() operation (request from management-rules).
43  * 2. MBeanRegistry: support for getElementPrintName() to provide readable element's description for validator's messages
44  * Submitted by: kravtch
45  * Reviewed by: Shreedhar
46  * Affected modules admin-core/admin
47  * Tests passed: QLT/EE + devtests
48  *
49  * Revision 1.2 2005/06/27 21:19:40 tcfujii
50  * Issue number: CDDL header updates.
51  *
52  * Revision 1.1.1.1 2005/05/27 22:52:02 dpatil
53  * GlassFish first drop
54  *
55  * Revision 1.9 2004/11/14 07:04:16 tcfujii
56  * Updated copyright text and/or year.
57  *
58  * Revision 1.8 2004/02/20 03:56:05 qouyang
59  *
60  *
61  * First pass at code merge.
62  *
63  * Details for the merge will be published at:
64  * http://javaweb.sfbay.sun.com/~qouyang/workspace/PE8FCSMerge/02202004/
65  *
66  * Revision 1.7 2003/12/13 16:52:50 qouyang
67  * MERGE: from PE8FCS_BRANCH (archived by TAG S1AS8PE-FCSBRANCH-MERGE-POINT-12_Dec_2003)
68  * to HEAD (archived by TAG S1AS8EE-HEADBRANCH-PRE-MERGE-12_Dec_2003)
69  *
70  * Revision 1.6.4.2 2004/02/02 07:25:12 tcfujii
71  * Copyright updates notices; reviewer: Tony Ng
72  *
73  * Revision 1.6.4.1 2003/12/01 21:52:37 kravtch
74  * Bug #4939964
75  * Reviewer: Sridatta
76  * admin.config.ManagedConfigBean.createChildByType() now analyzes registryEntries.AttributeInfo for each "empty" valued attribute (similar to setAttribute(), but could not use MBeanAttributeInfo because MBean is not exists yet). If "emptyValueAllowed" field in registrEntry.AttributeInfo is not "true", then "empty" attribute will be ignored.
77  *
78  * Revision 1.6 2003/09/26 21:33:02 kravtch
79  * Bug #4926266
80  * Reviewer: Sridatta
81  * - new test cases added to Validator's ThreadPoolTest DELETE - isThreadPoolReferencedFromOrb and isThreadPoolReferencedFromResAdapter to avoid deleteion of the referenced element
82  * - correspondent localStrings are added;
83  * - ManagedConfigBean.deleteSelf() now throws Exception;
84  * - MBeanHelper.invokeOperationInBean() will not suppress runtime exceptions any more;
85  *
86  * Revision 1.5 2003/09/08 00:55:27 se113266
87  * Bugsfixed: 4917554
88  * CheckinComments: isSignatureEqual throws NPE when signature==null and
89  * sign!=null. Added a check
90  * Reviewed by: Sridatta
91  *
92  * Revision 1.4 2003/08/29 02:16:40 kravtch
93  * Bug #4910964 (and similar others)
94  * Reviewer: Sridatta
95  *
96  * Exception handling and logging enchancements:
97  * - extraction target exception for MBeanException and TargetInvocationException:
98  * - switch to localStrings usage;
99  * - throwing exception for config MBeans if error in creation of ConfigBean;
100  * - exceptions for null-results in configbean operations,like getXXbyYYY() [changes commented because of crashing of quick test]
101  *
102  * Revision 1.3 2003/07/18 20:14:42 kravtch
103  * 1. ALL config mbeans are now covered by descriptors.xml
104  * 2. new infrastructure for runtime mbeans is added
105  * 3. generic constructors added to jsr77Mdl beans (String[])
106  * 4. new test cases are added to admintest
107  * 5. MBeanRegistryFactory has now different methods to obtain admin/runtime registries
108  * 6. runtime-descriptors xml-file is added to build
109  *
110  * Revision 1.2 2003/06/25 20:03:36 kravtch
111  * 1. java file headers modified
112  * 2. properties handling api is added
113  * 3. fixed bug for xpathes containing special symbols;
114  * 4. new testcases added for jdbc-resource
115  * 5. introspector modified by not including base classes operations;
116  *
117  *
118 */

119
120 package com.sun.enterprise.admin;
121
122 import java.util.ArrayList JavaDoc;
123 import java.lang.reflect.Method JavaDoc;
124 import java.lang.reflect.InvocationTargetException JavaDoc;
125
126 import com.sun.enterprise.admin.meta.MBeanMetaConstants;
127 import com.sun.enterprise.admin.meta.MBeanRegistryFactory;
128 import com.sun.enterprise.admin.meta.MBeanRegistry;
129 import com.sun.enterprise.admin.meta.MBeanRegistryEntry;
130
131 import com.sun.enterprise.admin.meta.naming.MBeanNamingDescriptor;
132 import com.sun.enterprise.admin.meta.naming.MBeanNamingInfo;
133
134
135 //import com.sun.enterprise.config.ConfigBean;
136

137 //JMX imports
138
import javax.management.MBeanException JavaDoc;
139 import javax.management.Descriptor JavaDoc;
140 import javax.management.ObjectName JavaDoc;
141 import javax.management.MBeanInfo JavaDoc;
142 import javax.management.MBeanOperationInfo JavaDoc;
143 import javax.management.MBeanAttributeInfo JavaDoc;
144 import javax.management.MBeanParameterInfo JavaDoc;
145 import javax.management.modelmbean.ModelMBeanInfo JavaDoc;
146
147 // Logging
148
import java.util.logging.Logger JavaDoc;
149 import java.util.logging.Level JavaDoc;
150 import com.sun.logging.LogDomains;
151 import com.sun.enterprise.util.i18n.StringManager;
152
153 /**
154  *
155  */

156 //*******************************************************************************************
157
public class MBeanHelper implements MBeanMetaConstants
158 {
159     // Logging
160
static protected Logger JavaDoc _sLogger = LogDomains.getLogger(LogDomains.ADMIN_LOGGER);
161     static protected StringManager _localStrings = StringManager.getManager( BaseAdminMBean.class );
162
163     public static Object JavaDoc INVOKE_ERROR_SIGNAL_OBJECT = new Object JavaDoc();
164     
165     //********************************************************************************************************************
166
// get Descriptor's field value by its name
167
public static Object JavaDoc getDescriptorFieldValue(ModelMBeanInfo JavaDoc info, String JavaDoc name) throws MBeanException JavaDoc
168     {
169         Descriptor descr = info.getMBeanDescriptor();
170         return descr.getFieldValue(name);
171         
172     }
173
174     //********************************************************************************************************************
175
// get "Location" field value from MBean's Descriptor
176
public static String JavaDoc[] getLocation(ModelMBeanInfo JavaDoc info) throws MBeanException JavaDoc
177     {
178         return (String JavaDoc[])getDescriptorFieldValue(info, NMLOCATION_FIELD_NAME);
179         
180     }
181
182     //********************************************************************************************************************
183
// get "XPath" field value from MBean's Descriptor (contains XPath pattern from mbean-descriptor-entry)
184
public static String JavaDoc getXPathPattern(ModelMBeanInfo JavaDoc info) throws MBeanException JavaDoc
185     {
186         return (String JavaDoc)getDescriptorFieldValue(info, XPATH_FIELD_NAME);
187     }
188
189     
190 /* //********************************************************************************************************************
191     public static ObjectName[] getChildObjectNames(ModelMBeanInfo parentInfo, ConfigBean[] children) throws Exception
192     {
193         ObjectName[] objNames = new ObjectName[children.length];
194         for(int i=0; i<children.length; i++)
195             objNames[i] = getChildObjectName(parentInfo, children[i]);
196         return objNames;
197     }
198     //********************************************************************************************************************
199     public static ObjectName getChildObjectName(ModelMBeanInfo parentInfo, ConfigBean childBean) throws Exception
200     {
201         Descriptor descr = parentInfo.getMBeanDescriptor();
202         //String rel = childBean.getRelativeXPath();
203         String rel = childBean.getAbsoluteXPath("");
204         rel = rel.substring(rel.lastIndexOf('/')+1);
205         String xpath = (String)descr.getFieldValue(XPATH_FIELD_NAME);
206         String prefix;
207         int pos = rel.indexOf('[');
208         if(pos>0)
209            prefix = xpath+"/"+rel.substring(0, pos);
210         else
211            prefix = xpath+"/"+rel;
212         MBeanRegistry registry = MBeanRegistryFactory.getMBeanRegistry();
213         MBeanRegistryEntry entry = registry.findMBeanRegistryEntryByXPathPattern(prefix);
214         MBeanNamingDescriptor namingDescr = entry.getNamingDescriptor();
215         
216         //
217         String[] parentLocation = (String[])descr.getFieldValue(NMLOCATION_FIELD_NAME);
218         String[] childLocation = parentLocation;
219         childLocation = new String[parentLocation.length+1];
220         for(int i=0; i<parentLocation.length; i++)
221         {
222             childLocation[i] = parentLocation[i];
223         }
224         if(pos>0)
225         {
226             int beg = rel.indexOf('=', pos) + 1;
227             if(rel.charAt(beg)=='\'')
228                 beg++;
229             int end = rel.indexOf(']', beg) - 1;
230             if(rel.charAt(end)=='\'')
231                 end--;
232             childLocation[parentLocation.length]=rel.substring(beg,end+1);
233         }
234         else
235             childLocation[parentLocation.length]=rel;
236         MBeanNamingInfo nmi = new MBeanNamingInfo(namingDescr, namingDescr.getType(), childLocation);
237         return nmi.getObjectName();
238     }
239 */

240    //********************************************************************************************************************
241
private static Class JavaDoc getAttributeClass(String JavaDoc signature) throws Exception JavaDoc
242     {
243         if (signature.equals(Boolean.TYPE.getName()))
244             return Boolean.TYPE;
245         else if (signature.equals(Byte.TYPE.getName()))
246             return Byte.TYPE;
247         else if (signature.equals(Character.TYPE.getName()))
248             return Character.TYPE;
249         else if (signature.equals(Double.TYPE.getName()))
250             return Double.TYPE;
251         else if (signature.equals(Float.TYPE.getName()))
252             return Float.TYPE;
253         else if (signature.equals(Integer.TYPE.getName()))
254             return Integer.TYPE;
255         else if (signature.equals(Long.TYPE.getName()))
256             return Long.TYPE;
257         else if (signature.equals(Short.TYPE.getName()))
258             return Short.TYPE;
259         else {
260             try {
261                 ClassLoader JavaDoc cl=Thread.currentThread().getContextClassLoader();
262                 if( cl!=null )
263                     return cl.loadClass(signature);
264             } catch( ClassNotFoundException JavaDoc e ) {
265             }
266 // try {
267
return Class.forName(signature);
268 // } catch (ClassNotFoundException e) {
269
// throw new ReflectionException
270
// (e, "Cannot find Class for " + signature);
271
// }
272
}
273     }
274    //********************************************************************************************************************
275
public static String JavaDoc[] getParamTypesFromOperationInfo(MBeanOperationInfo JavaDoc opInfo)
276     {
277         MBeanParameterInfo JavaDoc[] params = opInfo.getSignature();
278         if(params==null)
279             return new String JavaDoc[0];
280         ArrayList JavaDoc signature = new ArrayList JavaDoc();
281         for(int i=0; i<params.length; i++)
282             signature.add(params[i].getType());
283         return (String JavaDoc[])signature.toArray(new String JavaDoc[signature.size()]);
284     }
285     //********************************************************************************************************************
286
public static Class JavaDoc[] getSignatureFromOperationInfo(MBeanOperationInfo JavaDoc opInfo) throws Exception JavaDoc
287     {
288         MBeanParameterInfo JavaDoc[] params = opInfo.getSignature();
289         if(params==null)
290             return new Class JavaDoc[0];
291         ArrayList JavaDoc signature = new ArrayList JavaDoc();
292         for(int i=0; i<params.length; i++)
293             signature.add(getAttributeClass(params[i].getType()));
294         return (Class JavaDoc[])signature.toArray(new Class JavaDoc[signature.size()]);
295     }
296
297     //********************************************************************************************************************
298
public static MBeanOperationInfo JavaDoc findMatchingOperationInfo(MBeanInfo JavaDoc mbeanInfo, String JavaDoc name, String JavaDoc signature[])
299     {
300         MBeanOperationInfo JavaDoc[] opInfos = mbeanInfo.getOperations();
301         if(opInfos==null)
302             return null;
303         boolean bMatch;
304         for(int i=0; i<opInfos.length; i++)
305         {
306             if(name.equals(opInfos[i].getName()))
307             {
308                 String JavaDoc sign[] = getParamTypesFromOperationInfo(opInfos[i]);
309                 if(isSignaturesEqual(sign, signature))
310                     return opInfos[i];
311             }
312         }
313         return null;
314     }
315
316     //********************************************************************************************************************
317
public static MBeanAttributeInfo JavaDoc findMatchingAttributeInfo(MBeanInfo JavaDoc mbeanInfo, String JavaDoc name)
318     {
319         MBeanAttributeInfo JavaDoc[] attrInfos = mbeanInfo.getAttributes();
320         if(attrInfos==null)
321             return null;
322         boolean bMatch;
323         for(int i=0; i<attrInfos.length; i++)
324         {
325             if(name.equals(attrInfos[i].getName()))
326             {
327                 return attrInfos[i];
328             }
329         }
330         return null;
331     }
332
333     //********************************************************************************************************************
334
public static Object JavaDoc invokeOperationInBean(MBeanOperationInfo JavaDoc opInfo, Object JavaDoc bean, Object JavaDoc[] params) throws Exception JavaDoc
335     {
336         return invokeOperationInBean(opInfo.getName(), opInfo, bean, params);
337     }
338     //********************************************************************************************************************
339
public static Object JavaDoc invokeOperationInBean(String JavaDoc opName, MBeanOperationInfo JavaDoc opInfo, Object JavaDoc bean, Object JavaDoc[] params) throws Exception JavaDoc
340     {
341         Method JavaDoc method = null;
342         try
343         {
344             method = findMatchingOperationMethod(opName, opInfo, bean);
345         }
346         catch (Exception JavaDoc e)
347         {
348             if(e instanceof SecurityException JavaDoc)
349                 _sLogger.log(Level.FINEST, "invokeOperationInBean() failed", e);
350         }
351         if(method==null)
352            return INVOKE_ERROR_SIGNAL_OBJECT;
353         return method.invoke(bean, params);
354     }
355     
356     //********************************************************************************************************************
357
public static Method JavaDoc findMatchingOperationMethod(String JavaDoc opName, MBeanOperationInfo JavaDoc opInfo, Object JavaDoc bean) throws Exception JavaDoc
358     {
359 // try
360
{
361            Class JavaDoc signature[] = getSignatureFromOperationInfo(opInfo);
362            return bean.getClass().getMethod(opName, signature);
363         }
364 /* catch (NoSuchMethodException nsme)
365         {
366         }
367         catch (SecurityException nsme)
368         {
369         }
370         return null;
371  */

372     }
373     //********************************************************************************************************************
374
private static boolean isSignaturesEqual(String JavaDoc sign[], String JavaDoc signature[])
375     {
376         //signature test
377
if ((signature==null||signature.length==0) &&
378             (sign==null||sign.length==0) )
379             return true;
380         if((signature==null && sign!=null) ||
381            (signature!=null && sign==null) )
382            return false;
383         if(signature.length != sign.length)
384             return false;
385         boolean bMatch = true;
386         for(int j=0; j<sign.length; j++)
387         {
388             if(!sign[j].equals(signature[j]))
389             {
390                 bMatch = false;
391                 break;
392             }
393         }
394         return bMatch;
395     }
396     //****************************************************************************
397
public static MBeanException JavaDoc extractAndWrapTargetException(Exception JavaDoc e, String JavaDoc wrapMsg)
398     {
399          while(e instanceof InvocationTargetException JavaDoc ||
400                e instanceof MBeanException JavaDoc)
401          {
402              if(e instanceof InvocationTargetException JavaDoc)
403                 {
404                     Throwable JavaDoc t = ((InvocationTargetException JavaDoc)e).getTargetException();
405                     if (t instanceof Exception JavaDoc)
406                         e = (Exception JavaDoc)t;
407                     else
408                         e = new Exception JavaDoc(t.getMessage());
409                 }
410              else
411                  if(e instanceof MBeanException JavaDoc)
412                     {
413                         e = ((MBeanException JavaDoc)e).getTargetException();
414                     }
415          }
416         String JavaDoc msg = e.getMessage();
417         String JavaDoc targetMsgPref = _localStrings.getString( "admin.server.core.mbean.target_exception_prefix");
418         if(msg!=null)
419             return new MBeanException JavaDoc(e, wrapMsg + "\n"+ targetMsgPref + ": " + e.getMessage());
420         else
421             return new MBeanException JavaDoc(e, wrapMsg);
422     }
423 }
424
Popular Tags