1 23 24 29 30 31 package com.sun.enterprise.admin.dottedname.valueaccessor; 32 33 import java.util.Set ; 34 import java.util.HashSet ; 35 36 import javax.management.MBeanServerConnection ; 37 import javax.management.IntrospectionException ; 38 import javax.management.ReflectionException ; 39 import javax.management.InstanceNotFoundException ; 40 import javax.management.AttributeNotFoundException ; 41 import javax.management.InvalidAttributeValueException ; 42 import javax.management.MBeanException ; 43 import javax.management.AttributeList ; 44 import javax.management.MBeanInfo ; 45 import javax.management.MBeanAttributeInfo ; 46 import javax.management.ObjectName ; 47 import javax.management.Attribute ; 48 49 50 import com.sun.enterprise.admin.dottedname.DottedNameStrings; 51 52 import com.sun.enterprise.admin.util.ClassUtil; 53 54 public class AttributeValueAccessor extends ValueAccessorBase 55 { 56 public 57 AttributeValueAccessor( final MBeanServerConnection conn ) 58 { 59 super( conn ); 60 } 61 62 65 public static Set 66 getAllAttributeNames( final MBeanServerConnection conn, final ObjectName objectName ) 67 throws java.io.IOException , ReflectionException , InstanceNotFoundException , IntrospectionException 68 { 69 final Set allNames = new HashSet (); 70 71 final MBeanInfo info = conn.getMBeanInfo( objectName ); 73 final MBeanAttributeInfo [] attrsInfo = info.getAttributes(); 74 if ( attrsInfo != null ) 75 { 76 for( int i = 0; i < attrsInfo.length; ++i ) 77 { 78 allNames.add( attrsInfo[ i ].getName() ); 79 } 80 } 81 82 return( allNames ); 83 } 84 85 86 public Attribute 87 getValue( final ObjectName objectName, final String valueName ) 88 throws MBeanException , AttributeNotFoundException , InstanceNotFoundException , 89 ReflectionException , java.io.IOException 90 { 91 final Object value = getMBS().getAttribute( objectName, valueName); 92 93 return( new Attribute ( valueName, value ) ); 94 } 95 96 Class 97 getAttributeClass( final ObjectName objectName, final String attributeName ) 98 throws IntrospectionException , java.io.IOException , ReflectionException , 99 InstanceNotFoundException , ClassNotFoundException 100 { 101 final MBeanInfo info = getMBS().getMBeanInfo( objectName ); 102 final MBeanAttributeInfo [] attrsInfo = info.getAttributes(); 103 Class theClass = null; 104 105 for( int i = 0; i < attrsInfo.length; ++i ) 106 { 107 final String testName = attrsInfo[ i ].getName(); 108 109 if ( testName.equals( attributeName ) ) 110 { 111 theClass = ClassUtil.getClassFromName( attrsInfo[ i ].getType() ); 112 break; 113 } 114 } 115 return( theClass ); 116 } 117 118 public Attribute 119 setValue( final ObjectName objectName, final Attribute attr ) 120 throws Exception 121 { 122 if ( attr.getValue() == null ) 123 { 124 final String msg = DottedNameStrings.getString( 125 DottedNameStrings.ILLEGAL_TO_SET_NULL_KEY, 126 attr.getName( ) ); 127 128 throw new IllegalArgumentException ( msg ); 129 } 130 131 Attribute actualAttr = null; 132 133 Object value = attr.getValue(); 134 if ( value instanceof String ) 135 { 136 final Class attrClass = getAttributeClass( objectName, attr.getName() ); 137 if ( attrClass == null ) 138 { 139 final String msg = DottedNameStrings.getString( 140 DottedNameStrings.ATTRIBUTE_NOT_FOUND_KEY, 141 attr.getName( ) ); 142 143 throw new AttributeNotFoundException ( attr.getName() ); 144 } 145 146 value = coerceToClass( attrClass, (String )value ); 147 148 actualAttr = new Attribute ( attr.getName(), value ); 149 } 150 else 151 { 152 actualAttr = attr; 154 } 155 156 getMBS().setAttribute( objectName, actualAttr); 157 158 return( actualAttr ); 159 } 160 } 161 162 | Popular Tags |