MBeanEasyConfigpublic class MBeanEasyConfig extends Object This class is for holding and providing MBeanInfo data
for Dynamic MBeans (operations,attributes,MBean description...). |
Fields Summary |
---|
private static com.sun.enterprise.util.i18n.StringManager | localStrings | static final String | DESCR_LINE_DELIMITER | static final String | STRINGSOUREC_FILENAME | static final String | EXCEPTION_LINE_EMPTY | static final String | EXCEPTION_WRONG_LINE_FORMAT | static final String | EXCEPTION_NAME_FLD_EMPTY | static final String | EXCEPTION_CLASSNAME_FLD_EMPTY | static final String | EXCEPTION_UNKNOWN_RWTYPE | static final String | EXCEPTION_UNKNOWN_IMPACT_TYPE | static final String | EXCEPTION_OPER_NOT_FOUND | static final String | EXCEPTION_WRONG_PARAM_FORMAT | static final String | EXCEPTION_WRONG_PARAM_DIM | static final String | EXCEPTION_WRONG_PARAM_CLASS | static final String | EXCEPTION_EMPTY_PARAM_TYPE | static final String | TYPE_INFO | static final String | TYPE_ACTION | static final String | TYPE_ACTION_INFO | static final String | TYPE_UNKNOWN | private MBeanAttributeInfo[] | m_attrs | private MBeanConstructorInfo[] | m_cstrs | private MBeanOperationInfo[] | m_opers | private MBeanNotificationInfo[] | m_notfs | private Class | m_beanClass | private String | m_descr | static Object[] | convTable |
Constructors Summary |
---|
public MBeanEasyConfig(Class configuringMBeanClass, String[] attrDescr, String[] operDescr, String descr)Constructs of MBeansEasyConfig from provided
text descriptions of MBean components (operations, attributes,...). //MBean dscription
m_beanClass = configuringMBeanClass; // configuring MBean save
m_descr = descr; //save MBean descriptor
if(m_descr==null || m_descr.length()==0)
m_descr = ""+getPureClassName(configuringMBeanClass)+".mbean";
// create INFOs
m_attrs = createAttributesInfo(m_beanClass, attrDescr);
m_cstrs = createConstructorsInfo(m_beanClass, null);
m_opers = createOperationsInfo(m_beanClass, operDescr);
//m_notfs = createNotificationsInfo(notfsDescr);
|
Methods Summary |
---|
public static java.lang.Object | convertStringValueToProperType(java.lang.String value, java.lang.String targetType)Converts string value presentation to target type (esp. useful for CLI).
if(value==null)
return null;
Class cl;
try
{
if((cl=getPrimitiveClass(targetType))!=null || (cl=getRelatedPrimitiveClass(targetType))!=null)
{
if(cl==Byte.TYPE)
return new Byte(value);
if(cl==Character.TYPE)
return new Character(value.charAt(0));
if(cl==Double.TYPE)
return new Double(value);
if(cl==Float.TYPE)
return new Float(value);
if(cl==Integer.TYPE)
return new Integer(value);
if(cl==Long.TYPE)
return new Long(value);
if(cl==Short.TYPE)
return new Short(value);
if(cl==Boolean.TYPE)
return new Boolean(value);
}
}
catch(java.lang.IllegalArgumentException e)
{
String msg = localStrings.getString( "admin.server.core.mbean.meta.convertstringvaluetopropertype_wrong_argument_type", e.getMessage() );
throw new MBeanConfigException( msg );
}
//no arrays yet
return value;
| private static java.lang.Class | convertTypeToSignatureClass(java.lang.String type)
type = type.trim();
int idx = type.indexOf("[");
if(idx==0 || type.length()==0) {
String msg = localStrings.getString( "admin.server.core.mbean.meta.converttypetosignatureclass_wrong_exception_format", EXCEPTION_EMPTY_PARAM_TYPE, type );
throw new MBeanConfigException( msg );
}
try
{
if(idx>0)
{ // array
String name = type.substring(0,idx).trim();
if(name.length()==0) {
String msg = localStrings.getString( "admin.server.core.mbean.meta.converttypetosignatureclass_wrong_exception_format", EXCEPTION_EMPTY_PARAM_TYPE, type );
throw new MBeanConfigException( msg );
}
String code = getPrimitiveCode(name);
if(code==null)
{
if(name.indexOf('.")<0)
name = "java.lang."+name;
return Class.forName(getArrayPrefix(type, idx) + "L" + name + ";");
}
else
return Class.forName(getArrayPrefix(type, idx) + code) ;
}
else
{
Class cl = getPrimitiveClass(type);
if(cl!=null)
return cl;
//non-primitive plain class
try {
return Class.forName(type);
}
catch(ClassNotFoundException e)
{
if(type.indexOf('.")>0) {
String msg = localStrings.getString( "admin.server.core.mbean.meta.converttypetosignatureclass_wrong_exception_format", EXCEPTION_WRONG_PARAM_CLASS, type );
throw new MBeanConfigException( msg );
}
}
//here we are if no . in name
return Class.forName("java.lang."+type);
}
}
catch(ClassNotFoundException e)
{
String msg = localStrings.getString( "admin.server.core.mbean.meta.converttypetosignatureclass_wrong_exception_format", EXCEPTION_WRONG_PARAM_CLASS, type );
throw new MBeanConfigException( msg );
}
| public static javax.management.MBeanAttributeInfo[] | createAttributesInfo(java.lang.Class configuringMBeanClass, java.lang.String[] attrDescr)Creates MBeanAttributeInfo[] array according to descriptions
given in text format (see constructor comments).
if(attrDescr==null || attrDescr.length<1)
return new MBeanAttributeInfo[0];
MBeanAttributeInfo[] infos = new MBeanAttributeInfo[attrDescr.length];
String beanName = getPureClassName(configuringMBeanClass);
// the weak restriction - all descr strings should be non-empty
for(int i=0; i<attrDescr.length; i++)
{
infos[i] = parseAttributeDescrLine(beanName, attrDescr[i]);
}
return infos;
| public static javax.management.MBeanConstructorInfo[] | createConstructorsInfo(java.lang.Class configuringMBeanClass, java.lang.String constructorDescr)Creates MBeanConstructorInfo[] array according to configuring
MBean class reflection.
if(configuringMBeanClass==null)
return new MBeanConstructorInfo[0];
if(constructorDescr==null)
{
String nameDescr = getPureClassName(configuringMBeanClass)+".constructor";
constructorDescr = getResourceString(nameDescr, nameDescr);
}
//enumerate all constructors
Constructor[] ctrs = configuringMBeanClass.getConstructors();
MBeanConstructorInfo[] infos = new MBeanConstructorInfo[ctrs.length];
for(int i=0; i<ctrs.length; i++)
{
infos[i] = new MBeanConstructorInfo(constructorDescr, ctrs[i]);
}
return infos;
| public static javax.management.MBeanOperationInfo[] | createOperationsInfo(java.lang.Class configuringMBeanClass, java.lang.String[] operDescr)Creates MBeanOperationInfo[] array according to descriptions
given in text format (see constructor comments) and to configuring
MBean class reflection.
if(configuringMBeanClass==null)
return new MBeanOperationInfo[0];
if(operDescr==null || operDescr.length<1)
return new MBeanOperationInfo[0];
ArrayList arr = new ArrayList();
for(int i=0; i<operDescr.length; i++)
{
MBeanOperationInfo[] overloads = parseOperationDescrLine(configuringMBeanClass, operDescr[i]);
for(int j=0; j<overloads.length; j++)
arr.add(overloads[j]);
}
MBeanOperationInfo[] infos = new MBeanOperationInfo[arr.size()];
// the weak restriction - all descr strings should be non-empty
for(int i=0; i<infos.length; i++)
{
infos[i] = (MBeanOperationInfo)arr.get(i);
}
return infos;
| private static java.lang.Object[] | decomposeParametersDescription(java.lang.String line)
ArrayList flds = new ArrayList();
int idx1=0, idx2=100;
line = line.trim();
int len = line.length();
int parmEnd;
if(len==0)
return new Object[0];
String type;
while(idx1<len)
{
if((parmEnd=line.indexOf(',", idx1))<0)
parmEnd = len;
//TYPE
idx2 = line.indexOf(' ", idx1);
if(idx2>parmEnd)
idx2=parmEnd;
if(idx2<0)
{
flds.add(convertTypeToSignatureClass(line.substring(idx1)));
idx2 = parmEnd;
}
else
flds.add(convertTypeToSignatureClass(line.substring(idx1,idx2)));
idx1 = idx2 + 1;
while(idx1<parmEnd && line.charAt(idx1)==' ")
idx1++;
if(idx1>=parmEnd)
{
flds.add(null); //name
flds.add(null); //descr
if(idx1==parmEnd)
idx1++;
while(idx1<len && line.charAt(idx1)==' ")
idx1++;
continue;
}
//NAME
idx2 = line.indexOf(' ", idx1);
if(idx2>parmEnd)
idx2=parmEnd;
if(idx2<0)
{
flds.add(line.substring(idx1).trim());
idx2 = parmEnd;
}
else
flds.add(line.substring(idx1,idx2).trim());
idx1 = idx2 + 1;
while(idx1<parmEnd && line.charAt(idx1)==' ")
idx1++;
if(idx1>=parmEnd)
{
flds.add(null); //descr
if(idx1==parmEnd)
idx1++;
while(idx1<len && line.charAt(idx1)==' ")
idx1++;
continue;
}
// DESCRIPTION
idx2 = line.indexOf(',", idx1);
if(idx2<0)
{
flds.add(line.substring(idx1).trim());
idx2 = len;
}
else
flds.add(line.substring(idx1,idx2).trim());
idx1 = idx2 + 1;
while(idx1<len && line.charAt(idx1)==' ")
idx1++;
}
Object[] strs = new Object[flds.size()];
for(int i=0; i<strs.length; i++)
{
strs[i] = flds.get(i);
}
return strs;
| private static java.lang.Object | findConvTableElemByAssoClassName(java.lang.String assoClass, int idx)
for(int i=0; i<convTable.length; i++)
{
if(assoClass.equals((String)convTable[i][3]))
return convTable[i][idx];
}
return null;
| private static java.lang.Object | findConvTableElemByType(java.lang.String type, int idx)
//****************************************************************************************************************
for(int i=0; i<convTable.length; i++)
{
if(type.equals((String)convTable[i][0]))
return convTable[i][idx];
}
return null;
| private static java.lang.String | getArrayPrefix(java.lang.String line, int idx)
String pref = "";
int dimCheck = 0;
for( ; idx<line.length(); idx++)
{
if(line.charAt(idx)=='[")
pref = pref + "[";
else
if(line.charAt(idx)==']")
dimCheck++;
}
if(pref.length()!=dimCheck) {
String msg = localStrings.getString( "admin.server.core.mbean.meta.getarrayprefix_wrong_exception_format", EXCEPTION_WRONG_PARAM_DIM, line );
throw new MBeanConfigException( msg );
}
return pref;
| private static java.lang.String[] | getLineFields(java.lang.String line, int nMax)
ArrayList flds = new ArrayList();
int idx1=0, idx2=100;
if(line==null || line.length()==0 || nMax==0)
return null;
while(idx2>0 && nMax>flds.size() && idx1<line.length())
{
idx2 = line.indexOf(DESCR_LINE_DELIMITER, idx1);
if(idx2<0)
flds.add(line.substring(idx1).trim());
else
flds.add(line.substring(idx1, idx2).trim());
idx1 = idx2 + 1;
if(idx2==line.length())
flds.add("");
}
String[] strs = new String[flds.size()];
for(int i=0; i<strs.length; i++)
{
strs[i] = (String)flds.get(i);
}
return strs;
| public javax.management.MBeanInfo | getMBeanInfo()Get MBeanInfo description object for configurable MBean.
if(m_beanClass==null)
return null;
else
return new MBeanInfo(m_beanClass.getName(), m_descr,
m_attrs, m_cstrs, m_opers, m_notfs);
| private static java.lang.reflect.Method[] | getMethodsForName(java.lang.Class configuringMBeanClass, java.lang.String name)
ArrayList overloads = new ArrayList();
Method[] all = configuringMBeanClass.getMethods();
if(all==null)
return null;
for(int i=0; i<all.length; i++)
{
if(name.equals(all[i].getName()))
overloads.add(all[i]);
}
if(overloads.size()==0)
return null;
Method[] ret = new Method[overloads.size()];
for(int i=0; i<ret.length; i++)
ret[i] = (Method)overloads.get(i);
return ret;
| private static java.lang.Class | getPrimitiveClass(java.lang.String type)
return (Class)findConvTableElemByType(type, 2);
| private static java.lang.String | getPrimitiveCode(java.lang.String type)
return (String)findConvTableElemByType(type, 1);
| private static java.lang.String | getPureClassName(java.lang.Class configuringMBeanClass)
if(configuringMBeanClass==null)
return null;
String className = configuringMBeanClass.getName();
int idx = className.lastIndexOf('.");
if(idx>=0)
return className.substring(idx+1);
return className;
| private static java.lang.Class | getRelatedPrimitiveClass(java.lang.String className)
return (Class)findConvTableElemByAssoClassName(className, 2);
| static java.lang.String | getResourceString(java.lang.String resourceID, java.lang.String nullReplacer)
return resourceID; //TEMPORARY FOP TEST
/* should be discussed with Kedar
String res = null;
try {
PropertiesStringSource propSource = new PropertiesStringSource(STRINGSOUREC_FILENAME);
res = propSource.getString(resourceID);
if(res!=null)
return res;
if(nullReplacer!=null)
return nullReplacer;
else
return resourceID;
} catch(IOException e) {
if(nullReplacer!=null)
return nullReplacer;
else
return resourceID;
}
*/
| private static javax.management.MBeanAttributeInfo | parseAttributeDescrLine(java.lang.String beanName, java.lang.String descrLine)Parsing the attr description string to create MBeanAttributeInfo object.
//line format: <ATTR_NAME>,<ATTR_OBJECT_CLASS>,<ATTR_RW_TYPE>,<ATTR_DESCRIPTION_RESOURCE_ID>.
String name;
String className;
String rwType;
String descr;
boolean bReadable = false, bWritable = false;
if(descrLine==null) {
String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_exception_line_empty", EXCEPTION_LINE_EMPTY );
throw new MBeanConfigException( msg );
}
//EXTRACT line fields
String[] flds = getLineFields(descrLine, 4);
if(flds.length<3 || flds.length>4) {
String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_WRONG_LINE_FORMAT, descrLine );
throw new MBeanConfigException( msg );
}
//NAME
name = flds[0];
if(name.length()==0) {
String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_NAME_FLD_EMPTY, descrLine );
throw new MBeanConfigException( msg );
}
//OBJECT CLASS NAME
className = flds[1];
if(className.length()==0) {
String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_CLASSNAME_FLD_EMPTY, descrLine );
throw new MBeanConfigException( msg );
}
className = convertTypeToSignatureClass(className).getName();
//RWTYPE (R/W/RW)
rwType = flds[2];
if(rwType.equals("R"))
bReadable = true;
else
if(rwType.equals("W"))
bWritable = true;
else
if(rwType.equals("RW") || rwType.equals("WR"))
{
bWritable = true;
bReadable = true;
}
else {
String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_UNKNOWN_RWTYPE, descrLine );
throw new MBeanConfigException( msg );
}
//DESCRIPTION
String defaultName = beanName+"."+name+".attribute";
if(flds.length<4 || flds[3].length()==0)
descr = getResourceString(defaultName, defaultName);
else
descr = getResourceString(flds[3], defaultName);
return new MBeanAttributeInfo(name, className, descr, bReadable, bWritable, false);
| private static javax.management.MBeanOperationInfo[] | parseOperationDescrLine(java.lang.Class configuringMBeanClass, java.lang.String descrLine)Parsing the operation description string to create MBeanAttributeInfo[] array
(There is possible to have more than one resulting element even for one operation -
in case of the corresponding method has overloadings).
//line format: <OP_NAME>,<OP_TYPE>,<OP_DESCRIPTION>
String name;
String typeName;
int type;
String descr;
if(descrLine==null) {
String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_exception_line_empty", EXCEPTION_LINE_EMPTY );
throw new MBeanConfigException( msg );
}
//EXTRACT method_name(param1, param2, ...) part
int idx1 = descrLine.indexOf('(");
if(idx1<=0 || idx1==descrLine.length()-1) {
String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_WRONG_PARAM_FORMAT, descrLine );
throw new MBeanConfigException( msg );
}
name = descrLine.substring(0, idx1).trim();
if(name.length()==0) {
String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_NAME_FLD_EMPTY, descrLine );
throw new MBeanConfigException( msg );
}
int idx2 = descrLine.indexOf(')", idx1+1);
if(idx2<=0) {
String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_WRONG_PARAM_FORMAT, descrLine );
throw new MBeanConfigException( msg );
}
Object[] params = decomposeParametersDescription(descrLine.substring(idx1+1,idx2));
//EXTRACT line fields
String[] flds = getLineFields(descrLine.substring(idx2), 3);
if(flds.length<2) {
String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_WRONG_LINE_FORMAT, descrLine );
throw new MBeanConfigException( msg );
}
//TYPE (INFO/ACTION...)
typeName = flds[1];
if(typeName.equals(TYPE_INFO))
type = MBeanOperationInfo.INFO;
else
if(typeName.equals(TYPE_ACTION))
type = MBeanOperationInfo.ACTION;
else
if(typeName.equals(TYPE_ACTION_INFO))
type = MBeanOperationInfo.ACTION_INFO;
else
if(typeName.equals(TYPE_UNKNOWN))
type = MBeanOperationInfo.UNKNOWN;
else {
String msg = localStrings.getString( "admin.server.core.mbean.meta.parseattributedescrline_wrong_exception_format", EXCEPTION_UNKNOWN_IMPACT_TYPE, descrLine );
throw new MBeanConfigException( msg );
}
//DESCRIPTION
String beanName = getPureClassName(configuringMBeanClass);
String defaultName = beanName+"."+name+".operation";
if(flds.length<3 || flds[2].length()==0)
descr = getResourceString(defaultName, defaultName);
else
descr = getResourceString(flds[2], defaultName);
//REFLECTION
Class[] signature = new Class[params.length/3];
for(int i=0; i<signature.length; i++)
signature[i] = (Class)params[i*3];
Method method;
try {
method = configuringMBeanClass.getMethod(name, signature);
} catch (NoSuchMethodException e)
{
String msg = localStrings.getString( "admin.server.core.mbean.meta.parseoperationdescrline_wrong_exception_format", EXCEPTION_OPER_NOT_FOUND, descrLine );
throw new MBeanConfigException( msg );
}
MBeanParameterInfo[] infos = new MBeanParameterInfo[signature.length];
for(int i=0; i<signature.length; i++)
{
String pName = (String)params[i*3+1];
if(pName==null)
pName = ""; //get default name ?
String pDescr = (String)params[i*3+2];
if(pDescr==null) //get default descr ?
{
if(pName.length()>0)
pDescr = beanName+"."+name+"."+pName+".parameter";
else
pDescr = "";
}
infos[i] = new MBeanParameterInfo(pName, signature[i].getName(), pDescr);
}
return new MBeanOperationInfo[] {new MBeanOperationInfo(name, descr, infos, method.getReturnType().getName(), type)};
// Method[] methods = getMethodsForName(configuringMBeanClass, name);
// MBeanOperationInfo[] infos = new MBeanOperationInfo[methods.length];
// for(int i=0; i<methods.length; i++)
// infos[i] = new MBeanOperationInfo(descr, methods[i]);
//NOTE: ACTION/INFO operation type is not used by this constructor. To do later.
// return infos;
| public void | setAttributesInfo(javax.management.MBeanAttributeInfo[] attributesInfo)Sets (updates) MBeanAttributeInfo[] component of contained MBeanInfo
if(attributesInfo==null)
attributesInfo = new MBeanAttributeInfo[0];
m_attrs = attributesInfo;
| public void | setMBeanDescription(java.lang.String beanDescription)Sets (updates) description of configured MBean
m_descr = beanDescription; //set MBean descriptor
| public void | setNotificationsInfo(javax.management.MBeanNotificationInfo[] notificationsInfo)Sets ( updates) MBeanNotificationInfo[] component of contained MBeanInfo
if(notificationsInfo==null)
notificationsInfo = new MBeanNotificationInfo[0];
m_notfs = notificationsInfo;
| public void | setOperationsInfo(javax.management.MBeanConstructorInfo[] constructorInfo)Sets (updates) MBeanConstructorInfo[] component of contained MBeanInfo
if(constructorInfo ==null)
constructorInfo = new MBeanConstructorInfo[0];
m_cstrs = constructorInfo;
| public void | setOperationsInfo(javax.management.MBeanOperationInfo[] operationsInfo)Sets (updates) MBeanOperationInfo[] component of contained MBeanInfo
if(operationsInfo ==null)
operationsInfo = new MBeanOperationInfo[0];
m_opers = operationsInfo;
|
|