1 23 package com.sun.enterprise.management; 24 25 import java.util.Collection ; 26 import java.util.Set ; 27 import java.util.HashSet ; 28 import java.util.SortedSet ; 29 import java.util.TreeSet ; 30 import java.util.Arrays ; 31 import java.util.Collections ; 32 33 import javax.management.ObjectName ; 34 35 import com.sun.appserv.management.DomainRoot; 36 import com.sun.appserv.management.base.Util; 37 import com.sun.appserv.management.base.AMX; 38 39 import com.sun.appserv.management.client.ConnectionSource; 40 import com.sun.appserv.management.client.ProxyFactory; 41 42 import com.sun.appserv.management.util.misc.StringUtil; 43 import com.sun.appserv.management.util.misc.GSetUtil; 44 import com.sun.appserv.management.util.misc.ListUtil; 45 import com.sun.appserv.management.util.misc.ClassUtil; 46 import com.sun.appserv.management.util.misc.ExceptionUtil; 47 import com.sun.appserv.management.util.misc.TypeCast; 48 49 import com.sun.appserv.management.util.jmx.ObjectNameComparator; 50 51 import com.sun.enterprise.management.support.AMXDebugStuff; 52 53 56 public final class TestUtil 57 { 58 private final DomainRoot mDomainRoot; 59 private final String NEWLINE; 60 61 public 62 TestUtil( final DomainRoot domainRoot ) 63 { 64 mDomainRoot = domainRoot; 65 NEWLINE = System.getProperty( "line.separator" ); 66 } 67 68 private void 69 trace( final Object o) 70 { 71 System.out.println( "" + o ); 72 } 73 74 public AMXDebugStuff 75 asAMXDebugStuff( final AMX amx ) 76 { 77 final String [] attrNames = Util.getExtra( amx ).getAttributeNames(); 78 79 AMXDebugStuff result = null; 80 if ( GSetUtil.newUnmodifiableStringSet( attrNames ).contains( "AMXDebug" ) ) 81 { 82 final ProxyFactory factory = Util.getExtra( amx ).getProxyFactory(); 83 84 try 85 { 86 final Class amxClass = 87 ClassUtil.getClassFromName( Util.getExtra( amx ).getInterfaceName() ); 88 final Class [] interfaces = new Class [] { amxClass, AMXDebugStuff.class }; 89 90 final ObjectName objectName = Util.getObjectName( amx ); 91 92 return (AMXDebugStuff) 93 factory.newProxyInstance( objectName, interfaces ); 94 } 95 catch( Exception e ) 96 { 97 trace( ExceptionUtil.toString( e ) ); 98 throw new RuntimeException ( e ); 99 } 100 } 101 102 return result; 103 } 104 105 106 109 public Set <String > 110 getJ2EETypes( final Set <AMX> amxs ) 111 { 112 final Set <String > registered = new HashSet <String >(); 113 114 for( final AMX amx : amxs ) 115 { 116 registered.add( amx.getJ2EEType() ); 117 } 118 119 return registered; 120 } 121 122 125 public Set <String > 126 findRegisteredJ2EETypes() 127 { 128 return getJ2EETypes( mDomainRoot.getQueryMgr().queryAllSet() ); 129 } 130 131 public String 132 setToSortedString( final Set <String > s, final String delim ) 133 { 134 final String [] a = GSetUtil.toStringArray( s ); 135 Arrays.sort( a ); 136 137 return StringUtil.toString( NEWLINE, (Object [])a ); 138 } 139 140 141 public static SortedSet <ObjectName > 142 newSortedSet( final ObjectName [] objectNames ) 143 { 144 final SortedSet <ObjectName > s = new TreeSet <ObjectName >( ObjectNameComparator.INSTANCE ); 145 146 for( final ObjectName objectName : objectNames ) 147 { 148 s.add( objectName ); 149 } 150 151 return s; 152 } 153 154 public static SortedSet <ObjectName > 155 newSortedSet( final Collection <ObjectName > c ) 156 { 157 final ObjectName [] objectNames = new ObjectName [ c.size() ]; 158 c.toArray( objectNames ); 159 160 return newSortedSet( objectNames ); 161 } 162 163 167 public SortedSet <ObjectName > 168 getAllObjectNames() 169 { 170 final Set <ObjectName > s = 171 Observer.getInstance().getCurrentlyRegisteredAMX(); 172 173 return newSortedSet( s ); 174 } 175 176 177 180 public SortedSet <AMX> 181 getAllAMX() 182 { 183 final SortedSet <ObjectName > all = getAllObjectNames(); 184 185 final SortedSet <AMX> allAMX = new TreeSet <AMX>( new AMXComparator<AMX>() ); 186 final ProxyFactory proxyFactory = Util.getExtra( mDomainRoot ).getProxyFactory(); 187 for( final ObjectName objectName : all ) 188 { 189 try 190 { 191 final AMX amx = proxyFactory.getProxy( objectName, AMX.class ); 192 193 allAMX.add( amx ); 194 } 195 catch( Exception e ) 196 { 197 trace( ExceptionUtil.toString( e ) ); 198 } 199 } 200 201 return allAMX; 202 } 203 204 public <T> SortedSet <T> 205 getAllAMX( final Class <T> theInterface ) 206 { 207 final SortedSet <AMX> all = getAllAMX(); 208 final TreeSet <AMX> allOfInterface = new TreeSet <AMX>( new AMXComparator<AMX>() ); 209 210 for( final AMX amx : all ) 211 { 212 if ( theInterface.isAssignableFrom( amx.getClass() ) ) 213 { 214 allOfInterface.add( amx ); 215 } 216 } 217 218 return TypeCast.asSortedSet( allOfInterface ); 219 220 } 221 222 public ObjectName [] 223 getAllAMXArray() 224 { 225 final SortedSet <ObjectName > s = getAllObjectNames(); 226 final ObjectName [] objectNames = new ObjectName [ s.size() ]; 227 s.toArray( objectNames ); 228 229 return( objectNames ); 230 } 231 232 public Set <String > 233 getAvailJ2EETypes() 234 { 235 final SortedSet <ObjectName > allObjectNames = getAllObjectNames(); 236 final Set <String > j2eeTypes = new HashSet <String >(); 237 238 for( final ObjectName objectName : allObjectNames ) 239 { 240 final String value = Util.getJ2EEType( objectName ); 241 242 j2eeTypes.add( value ); 243 } 244 return( j2eeTypes ); 245 } 246 247 } 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | Popular Tags |