KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > mbean > meta > MBeanEasyConfig


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 package com.sun.enterprise.admin.server.core.mbean.meta;
25
26
27 import com.sun.enterprise.admin.util.PropertiesStringSource;
28
29 //JDK imports
30
import java.util.ArrayList JavaDoc;
31 import java.lang.reflect.Method JavaDoc;
32 import java.lang.reflect.Constructor JavaDoc;
33
34 //JMX imports
35
import javax.management.MBeanAttributeInfo JavaDoc;
36 import javax.management.MBeanConstructorInfo JavaDoc;
37 import javax.management.MBeanOperationInfo JavaDoc;
38 import javax.management.MBeanParameterInfo JavaDoc;
39 import javax.management.MBeanNotificationInfo JavaDoc;
40 import javax.management.MBeanInfo JavaDoc;
41 import javax.management.IntrospectionException JavaDoc;
42
43 //Admin imports
44
//import com.sun.enterprise.admin.common.exception.ServerInstanceException;
45
import com.sun.enterprise.admin.common.exception.MBeanConfigException;
46
47 //i18n import
48
import com.sun.enterprise.util.i18n.StringManager;
49
50 /****************************************************************************************************************
51     This <code>class</code> is for holding and providing <code>MBeanInfo</code> data
52     <strong>for Dynamic MBeans</strong> (operations,attributes,MBean description...).
53 */

54
55 public class MBeanEasyConfig //extends AdminBase implements MBeanRegistration
56
{
57     // i18n StringManager
58
private static StringManager localStrings =
59         StringManager.getManager( MBeanEasyConfig.class );
60  
61   final static String JavaDoc DESCR_LINE_DELIMITER = ",";
62   
63   //STRING SOURCE FILE
64
final static String JavaDoc STRINGSOUREC_FILENAME = "MBeanConfig.properties";
65   
66   //EXCEPTIONS
67
final static String JavaDoc EXCEPTION_LINE_EMPTY = localStrings.getString( "admin.server.core.mbean.meta.empty_description_line" );
68   final static String JavaDoc EXCEPTION_WRONG_LINE_FORMAT = localStrings.getString( "admin.server.core.mbean.meta.wrong_fields_number_in_description_line" );
69   final static String JavaDoc EXCEPTION_NAME_FLD_EMPTY = localStrings.getString( "admin.server.core.mbean.meta.empty_name_in_description_line" );
70   final static String JavaDoc EXCEPTION_CLASSNAME_FLD_EMPTY = localStrings.getString( "admin.server.core.mbean.meta.empty_classname_in_description_line" );
71   final static String JavaDoc EXCEPTION_UNKNOWN_RWTYPE = localStrings.getString( "admin.server.core.mbean.meta.unknown_rw_type_in_description_line" );
72   final static String JavaDoc EXCEPTION_UNKNOWN_IMPACT_TYPE = localStrings.getString( "admin.server.core.mbean.meta.unknown_impact_type_in_description" );
73   final static String JavaDoc EXCEPTION_OPER_NOT_FOUND = localStrings.getString( "admin.server.core.mbean.meta.unfound_operation_in_bean_class" );
74   
75   final static String JavaDoc EXCEPTION_WRONG_PARAM_FORMAT = localStrings.getString( "admin.server.core.mbean.meta.wrong_operation_parameter_in_description" );
76   final static String JavaDoc EXCEPTION_WRONG_PARAM_DIM = localStrings.getString( "admin.server.core.mbean.meta.wrong_operation_parameter_dimension_in_description" );
77    final static String JavaDoc EXCEPTION_WRONG_PARAM_CLASS = localStrings.getString( "admin.server.core.mbean.meta.wrong_parameter_attribute_class_in_description" );
78  final static String JavaDoc EXCEPTION_EMPTY_PARAM_TYPE = localStrings.getString( "admin.server.core.mbean.meta.empty_parameter_attribute_type_in_description_line" );
79 //IMPACT CODES
80
final static String JavaDoc TYPE_INFO = "INFO";
81   final static String JavaDoc TYPE_ACTION = "ACTION";
82   final static String JavaDoc TYPE_ACTION_INFO = "ACTION_INFO";
83   final static String JavaDoc TYPE_UNKNOWN = "UNKNOWN";
84
85   //info members
86
private MBeanAttributeInfo JavaDoc[] m_attrs = new MBeanAttributeInfo JavaDoc[0];
87   private MBeanConstructorInfo JavaDoc[] m_cstrs = new MBeanConstructorInfo JavaDoc[0];
88   private MBeanOperationInfo JavaDoc[] m_opers = new MBeanOperationInfo JavaDoc[0];
89   private MBeanNotificationInfo JavaDoc[] m_notfs = new MBeanNotificationInfo JavaDoc[0];
90   private Class JavaDoc m_beanClass = null; //configured MBean Class
91
private String JavaDoc m_descr = null; //MBean dscription
92

93   /****************************************************************************************************************
94     * Constructs of <code>MBeansEasyConfig</code> from provided
95     * text descriptions of MBean components (operations, attributes,...).
96     * @param configuringMBeanClass the configuring Dynamic MBean object Class.
97     * @param attrDescr array of text decriptions of MBean attributes,
98     * each string represents one attribute in format:
99     * <ul>&ltATTR_NAME>,&ltATTR_OBJECT_CLASS>, &ltATTR_RW_TYPE>, &ltATTR_DESCRIPTION_RESOURCE_ID>.
100     * <br>E.g.
101     * <ul> {
102     * <ul> "courses, java.lang.String[], RW, MBEAN_DESCR_ATTR_COURSES",
103     * <br> "beginTime, java.util.Date, RW, MBEAN_DESCR_ATTR_BEGINTIME",
104     * <br> "sum, int, R , MBEAN_DESCR_ATTR_SUM"
105     * </ul><br> };</ul></ul>
106     * @param operDescr array of text decriptions of MBean operations,
107     * <ul>each string represents one operation in format:
108     * <br>&ltOP_NAME>,&ltOP_TYPE>,&ltOP_DESCRIPTION_RESOURCE_ID>
109   * e.g.:
110   * <br> {
111   * <ul> "eat, ACTION, MBEAN_DESCR_OPER_EAT",
112   * <br> "throw_up, ACTION_INFO, MBEAN_DESCR_OPER_THROW_UP",
113   * <br> "calc, INFO, MBEAN_DESCR_OPER_CALC"
114   * </ul> }; (note that last description includes comma) </ul>
115   * @throws MBeanConfigException exception if the param parsing is not successful
116   */

117   public MBeanEasyConfig(Class JavaDoc configuringMBeanClass, String JavaDoc[] attrDescr, String JavaDoc[] operDescr, String JavaDoc descr) throws MBeanConfigException
118   {
119       m_beanClass = configuringMBeanClass; // configuring MBean save
120
m_descr = descr; //save MBean descriptor
121
if(m_descr==null || m_descr.length()==0)
122          m_descr = ""+getPureClassName(configuringMBeanClass)+".mbean";
123          
124       // create INFOs
125
m_attrs = createAttributesInfo(m_beanClass, attrDescr);
126       m_cstrs = createConstructorsInfo(m_beanClass, null);
127       m_opers = createOperationsInfo(m_beanClass, operDescr);
128       //m_notfs = createNotificationsInfo(notfsDescr);
129
}
130         
131   //**********************************************************************************************************************************************************************************************************************************************
132
/****************************************************************************************************************
133       Creates <code>MBeanAttributeInfo[]</code> array according to descriptions
134       given in text format (see constructor comments).
135       @param configuringMBeanClass the configuring MBean Class object;
136       @param attrDescr array of text decriptions of MBean <strong>attributes</strong>,
137       @throws MBeanConfigException exception if the param parsing is not successful
138   */

139   public static MBeanAttributeInfo JavaDoc[] createAttributesInfo(Class JavaDoc configuringMBeanClass, String JavaDoc[] attrDescr) throws MBeanConfigException
140     {
141     if(attrDescr==null || attrDescr.length<1)
142       return new MBeanAttributeInfo JavaDoc[0];
143     MBeanAttributeInfo JavaDoc[] infos = new MBeanAttributeInfo JavaDoc[attrDescr.length];
144     String JavaDoc beanName = getPureClassName(configuringMBeanClass);
145    // the weak restriction - all descr strings should be non-empty
146
for(int i=0; i<attrDescr.length; i++)
147       {
148         infos[i] = parseAttributeDescrLine(beanName, attrDescr[i]);
149       }
150     return infos;
151   }
152     
153   /****************************************************************************************************************
154       Creates <code>MBeanOperationInfo[]</code> array according to descriptions
155       given in text format (see constructor comments) and to configuring
156       MBean class reflection.
157       @param configuringMBeanClass the configuring MBean object;
158       @param operDescr array of text decriptions of MBean <strong>operations</strong>,
159       @throws MBeanConfigException exception if the param parsing is not successful
160   */

161   public static MBeanOperationInfo JavaDoc[] createOperationsInfo(Class JavaDoc configuringMBeanClass, String JavaDoc[] operDescr) throws MBeanConfigException
162     {
163     if(configuringMBeanClass==null)
164       return new MBeanOperationInfo JavaDoc[0];
165     if(operDescr==null || operDescr.length<1)
166       return new MBeanOperationInfo JavaDoc[0];
167     ArrayList JavaDoc arr = new ArrayList JavaDoc();
168     for(int i=0; i<operDescr.length; i++)
169       {
170         MBeanOperationInfo JavaDoc[] overloads = parseOperationDescrLine(configuringMBeanClass, operDescr[i]);
171         for(int j=0; j<overloads.length; j++)
172           arr.add(overloads[j]);
173       }
174         
175     MBeanOperationInfo JavaDoc[] infos = new MBeanOperationInfo JavaDoc[arr.size()];
176     // the weak restriction - all descr strings should be non-empty
177
for(int i=0; i<infos.length; i++)
178       {
179         infos[i] = (MBeanOperationInfo JavaDoc)arr.get(i);
180       }
181     return infos;
182   }
183     
184     
185   /****************************************************************************************************************
186       Creates <code>MBeanConstructorInfo[]</code> array according to configuring
187       MBean class reflection.
188       @param configuringMBeanClass the configuring MBean Class object
189       @param constructorDescr constructors text decriptions
190   */

191   public static MBeanConstructorInfo JavaDoc[] createConstructorsInfo(Class JavaDoc configuringMBeanClass, String JavaDoc constructorDescr)
192     {
193     if(configuringMBeanClass==null)
194       return new MBeanConstructorInfo JavaDoc[0];
195     if(constructorDescr==null)
196       {
197          String JavaDoc nameDescr = getPureClassName(configuringMBeanClass)+".constructor";
198          constructorDescr = getResourceString(nameDescr, nameDescr);
199       }
200     //enumerate all constructors
201
Constructor JavaDoc[] ctrs = configuringMBeanClass.getConstructors();
202     MBeanConstructorInfo JavaDoc[] infos = new MBeanConstructorInfo JavaDoc[ctrs.length];
203     for(int i=0; i<ctrs.length; i++)
204       {
205         infos[i] = new MBeanConstructorInfo JavaDoc(constructorDescr, ctrs[i]);
206       }
207     return infos;
208   }
209     
210   /****************************************************************************************************************
211       Get <code>MBeanInfo</code> description object for configurable MBean.
212       @return <code>MBeanInfo</code> description object for configurable MBean.
213   */

214   public MBeanInfo JavaDoc getMBeanInfo()
215   {
216     if(m_beanClass==null)
217       return null;
218     else
219       return new MBeanInfo JavaDoc(m_beanClass.getName(), m_descr,
220                             m_attrs, m_cstrs, m_opers, m_notfs);
221
222   }
223
224   /****************************************************************************************************************
225       Sets (updates) description of configured <code>MBean</code>
226       @param beanDescription the descriptions of MBean.
227   */

228   public void setMBeanDescription(String JavaDoc beanDescription)
229   {
230       m_descr = beanDescription; //set MBean descriptor
231
}
232
233   /****************************************************************************************************************
234       Sets (updates) <code>MBeanAttributeInfo[]</code> component of contained <code>MBeanInfo</code>
235       @param attributesInfo array of attribute descriptions.
236   */

237   public void setAttributesInfo(MBeanAttributeInfo JavaDoc[] attributesInfo)
238   {
239      if(attributesInfo==null)
240         attributesInfo = new MBeanAttributeInfo JavaDoc[0];
241      m_attrs = attributesInfo;
242   }
243         
244   /****************************************************************************************************************
245       Sets (updates) <code>MBeanConstructorInfo[]</code> component of contained <code>MBeanInfo</code>
246       @param constructorInfo array of operation descriptions.
247   */

248   public void setOperationsInfo(MBeanConstructorInfo JavaDoc[] constructorInfo)
249   {
250      if(constructorInfo ==null)
251         constructorInfo = new MBeanConstructorInfo JavaDoc[0];
252      m_cstrs = constructorInfo;
253   }
254         
255   /****************************************************************************************************************
256       Sets (updates) <code>MBeanOperationInfo[]</code> component of contained <code>MBeanInfo</code>
257       @param operationsInfo array of operation descriptions.
258   */

259   public void setOperationsInfo(MBeanOperationInfo JavaDoc[] operationsInfo)
260   {
261      if(operationsInfo ==null)
262         operationsInfo = new MBeanOperationInfo JavaDoc[0];
263      m_opers = operationsInfo;
264   }
265         
266   /*****************************************************************************************************************
267       Sets ( updates) <code>MBeanNotificationInfo[]</code> component of contained <code>MBeanInfo</code>
268       @param notificationsInfo array of notifications descriptions.
269   */

270   public void setNotificationsInfo(MBeanNotificationInfo JavaDoc[] notificationsInfo)
271   {
272      if(notificationsInfo==null)
273         notificationsInfo = new MBeanNotificationInfo JavaDoc[0];
274      m_notfs = notificationsInfo;
275   }
276         
277         
278   /*****************************************************************************************************************
279       Converts string value presentation to target type (esp. useful for CLI).
280       @param value The string value to convert
281       @param targetType The string description of target value type (@see Class)
282       @return The target type object which represents value in the proper type.
283   */

284   public static Object JavaDoc convertStringValueToProperType(String JavaDoc value, String JavaDoc targetType) throws MBeanConfigException
285   {
286     if(value==null)
287       return null;
288     
289     Class JavaDoc cl;
290     try
291     {
292         if((cl=getPrimitiveClass(targetType))!=null || (cl=getRelatedPrimitiveClass(targetType))!=null)
293           {
294             if(cl==Byte.TYPE)
295                 return new Byte JavaDoc(value);
296             if(cl==Character.TYPE)
297                 return new Character JavaDoc(value.charAt(0));
298             if(cl==Double.TYPE)
299                 return new Double JavaDoc(value);
300             if(cl==Float.TYPE)
301                 return new Float JavaDoc(value);
302             if(cl==Integer.TYPE)
303                 return new Integer JavaDoc(value);
304             if(cl==Long.TYPE)
305                 return new Long JavaDoc(value);
306             if(cl==Short.TYPE)
307                 return new Short JavaDoc(value);
308             if(cl==Boolean.TYPE)
309                 return new Boolean JavaDoc(value);
310           }
311     }
312     catch(java.lang.IllegalArgumentException JavaDoc e)
313     {
314         String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.meta.convertstringvaluetopropertype_wrong_argument_type", e.getMessage() );
315         throw new MBeanConfigException( msg );
316     }
317      //no arrays yet
318
return value;
319   }
320
321   /****************************************************************************************************************
322       Parsing the attr description string to create <code>MBeanAttributeInfo</code> object.
323       @param descrLine string with one attribute description (see constructor's comment).
324       @return MBeanAttributeInfo object for given description line.
325       @throws MBeanConfigException exception if the param parsing is not successful
326   */

327   private static MBeanAttributeInfo JavaDoc parseAttributeDescrLine(String JavaDoc beanName, String JavaDoc descrLine) throws MBeanConfigException
328   {
329     //line format: <ATTR_NAME>,<ATTR_OBJECT_CLASS>,<ATTR_RW_TYPE>,<ATTR_DESCRIPTION_RESOURCE_ID>.
330
String JavaDoc name;
331     String JavaDoc className;
332     String JavaDoc rwType;
333     String JavaDoc descr;
334     boolean bReadable = false, bWritable = false;
335        
336     if(descrLine==null) {
337       String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_exception_line_empty", EXCEPTION_LINE_EMPTY );
338       throw new MBeanConfigException( msg );
339     }
340
341     //EXTRACT line fields
342
String JavaDoc[] flds = getLineFields(descrLine, 4);
343     if(flds.length<3 || flds.length>4) {
344        String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_WRONG_LINE_FORMAT, descrLine );
345        throw new MBeanConfigException( msg );
346     }
347     
348     //NAME
349
name = flds[0];
350     if(name.length()==0) {
351       String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_NAME_FLD_EMPTY, descrLine );
352       throw new MBeanConfigException( msg );
353     }
354        
355     //OBJECT CLASS NAME
356
className = flds[1];
357     if(className.length()==0) {
358       String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_CLASSNAME_FLD_EMPTY, descrLine );
359       throw new MBeanConfigException( msg );
360     }
361     className = convertTypeToSignatureClass(className).getName();
362        
363        
364     //RWTYPE (R/W/RW)
365
rwType = flds[2];
366     if(rwType.equals("R"))
367       bReadable = true;
368     else
369       if(rwType.equals("W"))
370         bWritable = true;
371       else
372         if(rwType.equals("RW") || rwType.equals("WR"))
373           {
374             bWritable = true;
375             bReadable = true;
376           }
377         else {
378           String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_UNKNOWN_RWTYPE, descrLine );
379           throw new MBeanConfigException( msg );
380         }
381     
382     //DESCRIPTION
383
String JavaDoc defaultName = beanName+"."+name+".attribute";
384
385     if(flds.length<4 || flds[3].length()==0)
386       descr = getResourceString(defaultName, defaultName);
387     else
388       descr = getResourceString(flds[3], defaultName);
389        
390     return new MBeanAttributeInfo JavaDoc(name, className, descr, bReadable, bWritable, false);
391   }
392
393   /****************************************************************************************************************
394       Parsing the operation description string to create <code>MBeanAttributeInfo[]</code> array
395       (There is possible to have more than one resulting element even for one operation -
396       in case of the corresponding method has overloadings).
397       @param configuringMBeanClass Mbean object Class for reflection.
398       @param descrLine string with one operation description (see constructor's comment).
399       @return array of MBeanAttributeInfo for given operation.
400       @throws MBeanConfigException exception if the param parsing is not successful
401   */

402   private static MBeanOperationInfo JavaDoc[] parseOperationDescrLine(Class JavaDoc configuringMBeanClass, String JavaDoc descrLine) throws MBeanConfigException
403   {
404     //line format: <OP_NAME>,<OP_TYPE>,<OP_DESCRIPTION>
405
String JavaDoc name;
406     String JavaDoc typeName;
407     int type;
408     String JavaDoc descr;
409        
410     if(descrLine==null) {
411       String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_exception_line_empty", EXCEPTION_LINE_EMPTY );
412       throw new MBeanConfigException( msg );
413     }
414     
415     //EXTRACT method_name(param1, param2, ...) part
416
int idx1 = descrLine.indexOf('(');
417     if(idx1<=0 || idx1==descrLine.length()-1) {
418        String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_WRONG_PARAM_FORMAT, descrLine );
419        throw new MBeanConfigException( msg );
420     }
421     name = descrLine.substring(0, idx1).trim();
422     if(name.length()==0) {
423       String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_NAME_FLD_EMPTY, descrLine );
424       throw new MBeanConfigException( msg );
425     }
426     int idx2 = descrLine.indexOf(')', idx1+1);
427     if(idx2<=0) {
428       String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_WRONG_PARAM_FORMAT, descrLine );
429       throw new MBeanConfigException( msg );
430     }
431     
432     Object JavaDoc[] params = decomposeParametersDescription(descrLine.substring(idx1+1,idx2));
433     
434     //EXTRACT line fields
435
String JavaDoc[] flds = getLineFields(descrLine.substring(idx2), 3);
436     if(flds.length<2) {
437        String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_WRONG_LINE_FORMAT, descrLine );
438        throw new MBeanConfigException( msg );
439     }
440     
441     
442     //TYPE (INFO/ACTION...)
443
typeName = flds[1];
444     if(typeName.equals(TYPE_INFO))
445       type = MBeanOperationInfo.INFO;
446     else
447       if(typeName.equals(TYPE_ACTION))
448         type = MBeanOperationInfo.ACTION;
449       else
450         if(typeName.equals(TYPE_ACTION_INFO))
451           type = MBeanOperationInfo.ACTION_INFO;
452         else
453           if(typeName.equals(TYPE_UNKNOWN))
454             type = MBeanOperationInfo.UNKNOWN;
455           else {
456             String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_UNKNOWN_IMPACT_TYPE, descrLine );
457             throw new MBeanConfigException( msg );
458           }
459       
460     //DESCRIPTION
461
String JavaDoc beanName = getPureClassName(configuringMBeanClass);
462     String JavaDoc defaultName = beanName+"."+name+".operation";
463     if(flds.length<3 || flds[2].length()==0)
464       descr = getResourceString(defaultName, defaultName);
465     else
466       descr = getResourceString(flds[2], defaultName);
467        
468
469     //REFLECTION
470
Class JavaDoc[] signature = new Class JavaDoc[params.length/3];
471     for(int i=0; i<signature.length; i++)
472       signature[i] = (Class JavaDoc)params[i*3];
473     Method JavaDoc method;
474     try {
475       method = configuringMBeanClass.getMethod(name, signature);
476     } catch (NoSuchMethodException JavaDoc e)
477     {
478       String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.meta.parseoperationdescrline_wrong_exception_format", EXCEPTION_OPER_NOT_FOUND, descrLine );
479       throw new MBeanConfigException( msg );
480     }
481     MBeanParameterInfo JavaDoc[] infos = new MBeanParameterInfo JavaDoc[signature.length];
482     for(int i=0; i<signature.length; i++)
483       {
484         String JavaDoc pName = (String JavaDoc)params[i*3+1];
485         if(pName==null)
486            pName = ""; //get default name ?
487
String JavaDoc pDescr = (String JavaDoc)params[i*3+2];
488         if(pDescr==null) //get default descr ?
489
{
490               if(pName.length()>0)
491                  pDescr = beanName+"."+name+"."+pName+".parameter";
492               else
493                  pDescr = "";
494            }
495         infos[i] = new MBeanParameterInfo JavaDoc(pName, signature[i].getName(), pDescr);
496       }
497     return new MBeanOperationInfo JavaDoc[] {new MBeanOperationInfo JavaDoc(name, descr, infos, method.getReturnType().getName(), type)};
498     
499
500 // Method[] methods = getMethodsForName(configuringMBeanClass, name);
501
// MBeanOperationInfo[] infos = new MBeanOperationInfo[methods.length];
502
// for(int i=0; i<methods.length; i++)
503
// infos[i] = new MBeanOperationInfo(descr, methods[i]);
504
//NOTE: ACTION/INFO operation type is not used by this constructor. To do later.
505
// return infos;
506
}
507
508   //****************************************************************************************************************
509
// extracts up to <nMAx> fields separated by line delimiter
510
// only nMax-th field can contain unmasked delimiters
511
static private String JavaDoc[] getLineFields(String JavaDoc line, int nMax)
512   {
513     ArrayList JavaDoc flds = new ArrayList JavaDoc();
514     int idx1=0, idx2=100;
515     if(line==null || line.length()==0 || nMax==0)
516       return null;
517     while(idx2>0 && nMax>flds.size() && idx1<line.length())
518       {
519         idx2 = line.indexOf(DESCR_LINE_DELIMITER, idx1);
520         if(idx2<0)
521           flds.add(line.substring(idx1).trim());
522         else
523           flds.add(line.substring(idx1, idx2).trim());
524         idx1 = idx2 + 1;
525         if(idx2==line.length())
526            flds.add("");
527       }
528     String JavaDoc[] strs = new String JavaDoc[flds.size()];
529     for(int i=0; i<strs.length; i++)
530       {
531         strs[i] = (String JavaDoc)flds.get(i);
532       }
533     return strs;
534   }
535
536   //********************************************************************************************************************
537
static String JavaDoc getResourceString(String JavaDoc resourceID, String JavaDoc nullReplacer)
538   {
539     return resourceID; //TEMPORARY FOP TEST
540
/* should be discussed with Kedar
541        String res = null;
542        try {
543          PropertiesStringSource propSource = new PropertiesStringSource(STRINGSOUREC_FILENAME);
544          res = propSource.getString(resourceID);
545          if(res!=null)
546             return res;
547          if(nullReplacer!=null)
548             return nullReplacer;
549          else
550             return resourceID;
551        } catch(IOException e) {
552          if(nullReplacer!=null)
553             return nullReplacer;
554          else
555             return resourceID;
556        }
557   */

558   }
559
560   //*********************************************************************************************************************
561
static private Method JavaDoc[] getMethodsForName(Class JavaDoc configuringMBeanClass, String JavaDoc name)
562   {
563     ArrayList JavaDoc overloads = new ArrayList JavaDoc();
564     Method JavaDoc[] all = configuringMBeanClass.getMethods();
565     if(all==null)
566       return null;
567     for(int i=0; i<all.length; i++)
568       {
569         if(name.equals(all[i].getName()))
570           overloads.add(all[i]);
571       }
572     if(overloads.size()==0)
573       return null;
574     Method JavaDoc[] ret = new Method JavaDoc[overloads.size()];
575     for(int i=0; i<ret.length; i++)
576       ret[i] = (Method JavaDoc)overloads.get(i);
577     return ret;
578   }
579
580
581   //****************************************************************************************************************
582
// converts type from description to java signature style
583
//
584
static private Class JavaDoc convertTypeToSignatureClass(String JavaDoc type) throws MBeanConfigException
585   {
586     type = type.trim();
587     int idx = type.indexOf("[");
588     if(idx==0 || type.length()==0) {
589        String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.meta.converttypetosignatureclass_wrong_exception_format", EXCEPTION_EMPTY_PARAM_TYPE, type );
590        throw new MBeanConfigException( msg );
591     }
592     try
593     {
594       if(idx>0)
595         { // array
596
String JavaDoc name = type.substring(0,idx).trim();
597           if(name.length()==0) {
598             String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.meta.converttypetosignatureclass_wrong_exception_format", EXCEPTION_EMPTY_PARAM_TYPE, type );
599             throw new MBeanConfigException( msg );
600           }
601           String JavaDoc code = getPrimitiveCode(name);
602           if(code==null)
603             {
604               if(name.indexOf('.')<0)
605                  name = "java.lang."+name;
606               return Class.forName(getArrayPrefix(type, idx) + "L" + name + ";");
607             }
608           else
609             return Class.forName(getArrayPrefix(type, idx) + code) ;
610         }
611       else
612         {
613           Class JavaDoc cl = getPrimitiveClass(type);
614           if(cl!=null)
615             return cl;
616           //non-primitive plain class
617

618           try {
619             return Class.forName(type);
620           }
621           catch(ClassNotFoundException JavaDoc e)
622           {
623              if(type.indexOf('.')>0) {
624                 String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.meta.converttypetosignatureclass_wrong_exception_format", EXCEPTION_WRONG_PARAM_CLASS, type );
625                 throw new MBeanConfigException( msg );
626              }
627           }
628           //here we are if no . in name
629
return Class.forName("java.lang."+type);
630         }
631     }
632     catch(ClassNotFoundException JavaDoc e)
633     {
634       String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.meta.converttypetosignatureclass_wrong_exception_format", EXCEPTION_WRONG_PARAM_CLASS, type );
635       throw new MBeanConfigException( msg );
636     }
637     
638   }
639
640   //****************************************************************************************************************
641
static private String JavaDoc getArrayPrefix(String JavaDoc line, int idx) throws MBeanConfigException
642   {
643     String JavaDoc pref = "";
644     int dimCheck = 0;
645     for( ; idx<line.length(); idx++)
646       {
647         if(line.charAt(idx)=='[')
648           pref = pref + "[";
649         else
650           if(line.charAt(idx)==']')
651             dimCheck++;
652       }
653     if(pref.length()!=dimCheck) {
654        String JavaDoc msg = localStrings.getString( "admin.server.core.mbean.meta.getarrayprefix_wrong_exception_format", EXCEPTION_WRONG_PARAM_DIM, line );
655        throw new MBeanConfigException( msg );
656     }
657     return pref;
658   }
659
660   //****************************************************************************************************************
661
static Object JavaDoc[][] convTable = new Object JavaDoc[][] {
662     {"byte", "B", Byte.TYPE, "java.lang.Byte"},
663     {"char", "C", Character.TYPE, "java.lang.Character"},
664     {"double", "D", Double.TYPE, "java.lang.Double"},
665     {"float", "F", Float.TYPE, "java.lang.Float"},
666     {"int", "I", Integer.TYPE, "java.lang.Integer"},
667     {"long", "J", Long.TYPE, "java.lang.Long"},
668     {"short", "S", Short.TYPE, "java.lang.Short"},
669     {"boolean", "Z", Boolean.TYPE, "java.lang.Boolean"},
670     {"void", "V", Void.TYPE, "?"} };
671     
672   //****************************************************************************************************************
673
static private Object JavaDoc findConvTableElemByType(String JavaDoc type, int idx)
674   {
675      for(int i=0; i<convTable.length; i++)
676         {
677            if(type.equals((String JavaDoc)convTable[i][0]))
678               return convTable[i][idx];
679         }
680      return null;
681   }
682
683   //****************************************************************************************************************
684
static private Object JavaDoc findConvTableElemByAssoClassName(String JavaDoc assoClass, int idx)
685   {
686      for(int i=0; i<convTable.length; i++)
687         {
688            if(assoClass.equals((String JavaDoc)convTable[i][3]))
689               return convTable[i][idx];
690         }
691      return null;
692   }
693
694   //****************************************************************************************************************
695
static private String JavaDoc getPrimitiveCode(String JavaDoc type)
696   {
697      return (String JavaDoc)findConvTableElemByType(type, 1);
698   }
699       
700   //****************************************************************************************************************
701
static private Class JavaDoc getPrimitiveClass(String JavaDoc type)
702   {
703      return (Class JavaDoc)findConvTableElemByType(type, 2);
704   }
705       
706   //****************************************************************************************************************
707
static private Class JavaDoc getRelatedPrimitiveClass(String JavaDoc className)
708   {
709      return (Class JavaDoc)findConvTableElemByAssoClassName(className, 2);
710   }
711      
712   //****************************************************************************************************************
713
// extracts parameters description
714
//
715
static private Object JavaDoc[] decomposeParametersDescription(String JavaDoc line) throws MBeanConfigException
716   {
717     ArrayList JavaDoc flds = new ArrayList JavaDoc();
718     int idx1=0, idx2=100;
719     line = line.trim();
720     int len = line.length();
721     int parmEnd;
722     if(len==0)
723       return new Object JavaDoc[0];
724     String JavaDoc type;
725     while(idx1<len)
726       {
727         if((parmEnd=line.indexOf(',', idx1))<0)
728            parmEnd = len;
729         //TYPE
730
idx2 = line.indexOf(' ', idx1);
731         if(idx2>parmEnd)
732            idx2=parmEnd;
733         if(idx2<0)
734           {
735             flds.add(convertTypeToSignatureClass(line.substring(idx1)));
736             idx2 = parmEnd;
737           }
738         else
739           flds.add(convertTypeToSignatureClass(line.substring(idx1,idx2)));
740         idx1 = idx2 + 1;
741         while(idx1<parmEnd && line.charAt(idx1)==' ')
742           idx1++;
743         if(idx1>=parmEnd)
744           {
745             flds.add(null); //name
746
flds.add(null); //descr
747
if(idx1==parmEnd)
748                idx1++;
749             while(idx1<len && line.charAt(idx1)==' ')
750               idx1++;
751             continue;
752           }
753         
754         //NAME
755
idx2 = line.indexOf(' ', idx1);
756         if(idx2>parmEnd)
757            idx2=parmEnd;
758         if(idx2<0)
759           {
760             flds.add(line.substring(idx1).trim());
761             idx2 = parmEnd;
762           }
763         else
764           flds.add(line.substring(idx1,idx2).trim());
765         idx1 = idx2 + 1;
766         while(idx1<parmEnd && line.charAt(idx1)==' ')
767           idx1++;
768         if(idx1>=parmEnd)
769           {
770             flds.add(null); //descr
771
if(idx1==parmEnd)
772                idx1++;
773             while(idx1<len && line.charAt(idx1)==' ')
774               idx1++;
775             continue;
776           }
777         
778         // DESCRIPTION
779
idx2 = line.indexOf(',', idx1);
780         if(idx2<0)
781           {
782             flds.add(line.substring(idx1).trim());
783             idx2 = len;
784           }
785         else
786           flds.add(line.substring(idx1,idx2).trim());
787         idx1 = idx2 + 1;
788         while(idx1<len && line.charAt(idx1)==' ')
789           idx1++;
790       }
791
792     Object JavaDoc[] strs = new Object JavaDoc[flds.size()];
793     for(int i=0; i<strs.length; i++)
794       {
795         strs[i] = flds.get(i);
796       }
797     return strs;
798   }
799   //****************************************************************************************************************
800
private static String JavaDoc getPureClassName(Class JavaDoc configuringMBeanClass)
801   {
802      if(configuringMBeanClass==null)
803         return null;
804      String JavaDoc className = configuringMBeanClass.getName();
805      int idx = className.lastIndexOf('.');
806      if(idx>=0)
807         return className.substring(idx+1);
808      return className;
809   }
810 }
811
812
813
Popular Tags