1 23 24 29 30 31 package com.sun.enterprise.admin.dottedname.valueaccessor; 32 33 import java.lang.reflect.Array ; 34 35 import javax.management.MBeanServerConnection ; 36 import javax.management.IntrospectionException ; 37 import javax.management.ReflectionException ; 38 import javax.management.InstanceNotFoundException ; 39 import javax.management.AttributeList ; 40 import javax.management.MBeanInfo ; 41 import javax.management.MBeanAttributeInfo ; 42 import javax.management.ObjectName ; 43 import javax.management.Attribute ; 44 45 import com.sun.enterprise.admin.util.ClassUtil; 46 import com.sun.enterprise.admin.util.Tokenizer; 47 import com.sun.enterprise.admin.util.TokenizerImpl; 48 49 50 abstract class ValueAccessorBase implements ValueAccessor 51 { 52 final MBeanServerConnection mConn; 53 54 55 MBeanServerConnection 56 getMBS() 57 { 58 return( mConn ); 59 } 60 61 public 62 ValueAccessorBase( MBeanServerConnection conn ) 63 { 64 mConn = conn; 65 } 66 67 final char ARRAY_ELEMENT_SEPARATOR = ','; 68 final char ESCAPE_CHAR = '\\'; 69 70 String [] 71 stringToStringArray( final String s ) 72 throws com.sun.enterprise.admin.util.TokenizerException 73 { 74 final String delimiters = "" + ARRAY_ELEMENT_SEPARATOR; 75 final String escapableChars = "" + ARRAY_ELEMENT_SEPARATOR + ESCAPE_CHAR; 76 77 final Tokenizer tok = new TokenizerImpl( s, delimiters, false, ESCAPE_CHAR, escapableChars ); 78 79 final String [] values = tok.getTokens(); 80 81 return( values ); 82 } 83 84 Object [] 85 convert( final String [] stringValues, final Class elementClass ) 86 throws Exception 87 { 88 if ( elementClass == String .class ) 89 { 90 return( stringValues ); 91 } 92 93 if ( ClassUtil.classnameIsPrimitiveArray( elementClass.getName() ) ) 94 { 95 throw new IllegalArgumentException ( ); 96 } 97 98 final Object [] values = (Object [])Array.newInstance( elementClass, stringValues.length ); 99 100 for( int i = 0; i < values.length; ++i ) 101 { 102 values[ i ] = ClassUtil.InstantiateFromString( elementClass, stringValues[ i ] ); 103 } 104 105 return( values ); 106 } 107 108 Object [] 109 stringToArray( final String s, final Class elementClass ) 110 throws Exception 111 { 112 final String [] stringValues = stringToStringArray( s ); 113 114 final Object [] values = convert( stringValues, elementClass ); 115 116 return( values ); 117 } 118 119 120 Object 121 coerceToClass( final Class theClass, String value ) 122 throws Exception 123 { 124 Object result = value; 125 126 if ( theClass != String .class ) 127 { 128 if ( ClassUtil.classIsArray( theClass ) ) 129 { 130 final String theClassName = theClass.getName(); 131 final String elementClassName = ClassUtil.getArrayMemberClassName( theClassName ); 132 133 result = stringToArray( value, ClassUtil.getClassFromName( elementClassName ) ); 135 } 136 else 137 { 138 boolean canCoerce = true; 139 140 final Class resultClass = ClassUtil.PrimitiveClassToObjectClass( theClass ); 141 142 if ( resultClass == Boolean .class ) 143 { 144 canCoerce = value.equalsIgnoreCase( "true" ) || value.equalsIgnoreCase( "false" ); 146 } 147 148 if ( canCoerce ) 149 { 150 result = ClassUtil.InstantiateFromString( resultClass, value ); 151 } 152 } 153 } 154 return( result ); 155 } 156 157 public abstract Attribute getValue( ObjectName objectName, String valueName ) throws Exception ; 158 public abstract Attribute setValue( ObjectName objectName, Attribute attr ) throws Exception ; 159 } 160 161 162 163 164 165 166 | Popular Tags |