1 23 package com.sun.enterprise.management.base; 24 25 import java.util.Set ; 26 import java.util.HashSet ; 27 import java.util.Map ; 28 import java.util.HashMap ; 29 30 31 import javax.management.MBeanServerConnection ; 32 import javax.management.MBeanInfo ; 33 import javax.management.MBeanAttributeInfo ; 34 import javax.management.ObjectName ; 35 import javax.management.Attribute ; 36 import javax.management.AttributeList ; 37 38 import com.sun.appserv.management.base.AMX; 39 import com.sun.appserv.management.base.Util; 40 41 42 import com.sun.appserv.management.util.misc.MapUtil; 43 import com.sun.appserv.management.util.misc.CollectionUtil; 44 import com.sun.appserv.management.util.misc.GSetUtil; 45 import com.sun.appserv.management.util.misc.StringUtil; 46 import com.sun.appserv.management.util.jmx.JMXUtil; 47 48 import com.sun.appserv.management.config.Enabled; 49 50 import com.sun.enterprise.management.AMXTestBase; 51 52 53 55 public final class GetSetAttributeTest extends AMXTestBase 56 { 57 public GetSetAttributeTest() 58 { 59 } 60 61 62 private void 63 testGetSetAttributes(final AMX amx) 64 throws Exception 65 { 66 final ObjectName objectName = Util.getObjectName( amx ); 67 final MBeanServerConnection conn = getMBeanServerConnection(); 68 final MBeanInfo mbeanInfo = Util.getExtra( amx ).getMBeanInfo(); 69 70 final Map <String ,MBeanAttributeInfo > attrInfos = 71 JMXUtil.attributeInfosToMap( mbeanInfo.getAttributes() ); 72 final String [] attrNames = GSetUtil.toStringArray( attrInfos.keySet() ); 73 final AttributeList values = conn.getAttributes( objectName, attrNames ); 74 75 final Map <String ,Object > valuesMap = JMXUtil.attributeListToValueMap( values ); 76 77 final Set <String > getFailed = new HashSet <String >(); 78 final Map <String ,Object > setFailed = new HashMap <String ,Object >(); 79 80 for ( final MBeanAttributeInfo attrInfo : attrInfos.values() ) 81 { 82 final String name = attrInfo.getName(); 83 if ( ! valuesMap.keySet().contains( name ) ) 84 { 85 getFailed.add( name ); 86 continue; 87 } 88 89 if ( attrInfo.isReadable() ) 90 { 91 final Object value = valuesMap.get( name ); 92 93 if ( attrInfo.isWritable() ) 94 { 95 try 97 { 98 final Attribute attr = new Attribute ( name, value ); 99 conn.setAttribute( objectName, attr ); 100 } 101 catch( Exception e ) 102 { 103 setFailed.put( name, value ); 104 } 105 } 106 } 107 } 108 109 if ( getFailed.size() != 0 ) 110 { 111 warning( "Could not get Attributes for " + 112 StringUtil.quote( objectName ) + NEWLINE + 113 CollectionUtil.toString( getFailed, NEWLINE ) ); 114 } 115 116 if ( setFailed.size() != 0 ) 117 { 118 warning( "Could not identity-set Attributes for " + 119 StringUtil.quote( objectName ) + NEWLINE + 120 MapUtil.toString( setFailed, NEWLINE ) ); 121 } 122 } 123 124 125 public void 126 testGetSetAttributes() 127 throws Exception 128 { 129 final Set <AMX> all = getAllAMX(); 130 131 for( final AMX amx : all ) 132 { 133 testGetSetAttributes( amx ); 134 } 135 } 136 } 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | Popular Tags |