1 23 package com.sun.enterprise.management.support; 24 25 import java.util.List ; 26 import java.util.ArrayList ; 27 import java.util.Set ; 28 import java.util.HashSet ; 29 import java.util.Iterator ; 30 import java.util.Collections ; 31 32 import javax.management.ObjectName ; 33 34 import com.sun.appserv.management.base.AllTypesMapper; 35 import com.sun.appserv.management.base.Util; 36 import com.sun.appserv.management.base.AMX; 37 import com.sun.appserv.management.util.misc.ClassUtil; 38 import com.sun.appserv.management.util.misc.GSetUtil; 39 import com.sun.appserv.management.util.misc.ListUtil; 40 import com.sun.appserv.management.util.misc.ObjectUtil; 41 import com.sun.appserv.management.util.jmx.JMXUtil; 42 43 import com.sun.appserv.management.util.stringifier.SmartStringifier; 44 45 48 public final class TypeInfo 49 { 50 private final TypeData mTypeData; 51 private final Set <String > mChildJ2EETypes; 52 private final Set <String > mNonChildJ2EETypes; 53 54 private final Class mInterface; 55 private final Class mImplClass; 56 57 public 58 TypeInfo( final TypeData typeData ) 59 throws ClassNotFoundException 60 { 61 mTypeData = typeData; 62 mInterface = deriveInterface( typeData.getJ2EEType() ); 63 mImplClass = deriveImplClass( mInterface ); 64 65 mChildJ2EETypes = new HashSet <String >(); 66 mNonChildJ2EETypes = new HashSet <String >(); 67 } 68 69 public String 70 toString() 71 { 72 final StringBuffer buf = new StringBuffer (); 73 74 final String PR = "\n "; 75 76 buf.append( getJ2EEType() + ": " ); 77 buf.append( PR + "parent type = " + SmartStringifier.toString( getLegalParentJ2EETypes() ) ); 78 buf.append( PR + "child types = " + SmartStringifier.toString( mChildJ2EETypes ) ); 79 buf.append( PR + "mbean = " + mInterface.getName() ); 80 buf.append( PR + "impl = " + mImplClass.getName() ); 81 buf.append( "\n" ); 82 83 return( buf.toString() ); 84 } 85 86 87 private static Class 88 deriveInterface( final String j2eeType ) 89 throws ClassNotFoundException 90 { 91 return( AllTypesMapper.getInstance().getInterfaceForType( j2eeType ) ); 92 } 93 94 private static String 95 getBaseName( String mbeanClassName ) 96 { 97 final String classname = ClassUtil.stripPackageName( mbeanClassName ); 98 final String baseName = classname; 99 100 return( baseName ); 101 } 102 103 private static Class 104 deriveImplClass( final Class mbeanInterface ) 105 throws ClassNotFoundException 106 { 107 final String baseName = getBaseName( mbeanInterface.getName() ); 108 109 final String implClass = baseName; 110 111 return( locateImplClass( implClass ) ); 112 } 113 114 115 private static final String [] IMPL_PACKAGES = new String [] 116 { 117 "com.sun.enterprise.management.ext.lb", 118 "com.sun.enterprise.management.ext.wsmgmt", 119 "com.sun.enterprise.management.ext.logging", 120 "com.sun.enterprise.management.config", 121 "com.sun.enterprise.management.j2ee", 122 "com.sun.enterprise.management.monitor", 123 "com.sun.enterprise.management.support", 124 "com.sun.enterprise.management.deploy", 125 "com.sun.enterprise.management.ext", 126 "com.sun.enterprise.management", 127 }; 128 129 private final static String IMPL = "Impl"; 130 131 private static Class 132 locateImplClass( 133 final String packageName, 134 final String baseName ) 135 { 136 if ( ! packageName.startsWith( "com.sun.enterprise.management" ) ) 137 { 138 throw new RuntimeException ( "Illegal implementation package for AMX" ); 139 } 140 141 final String implClassname = packageName + "." + baseName + IMPL; 142 143 Class implClass = null; 144 try 145 { 146 implClass = ClassUtil.getClassFromName( implClassname ); 147 } 148 catch( ClassNotFoundException e ) 149 { 150 } 152 153 return( implClass ); 154 } 155 156 157 private static Class 158 locateImplClass( 159 final String classname ) 160 throws ClassNotFoundException 161 { 162 Class implClass = null; 163 164 for( int i = 0; i < IMPL_PACKAGES.length; ++i ) 165 { 166 implClass = locateImplClass( IMPL_PACKAGES[ i ], classname ); 167 168 if ( implClass != null ) 169 { 170 break; 171 } 172 } 173 174 if ( implClass == null ) 175 { 176 throw new ClassNotFoundException ( 177 "Excpected to find implementation class " + classname + IMPL ); 178 } 179 180 return( implClass ); 181 } 182 183 184 public void 185 addChildJ2EEType( final String j2eeType ) 186 { 187 assert( ! j2eeType.equals( getJ2EEType() ) ); 188 assert( ! mNonChildJ2EETypes.contains( j2eeType ) ); 189 190 mChildJ2EETypes.add( j2eeType ); 191 } 192 193 public void 194 addContaineeJ2EEType( final String j2eeType ) 195 { 196 assert( ! j2eeType.equals( getJ2EEType() ) ); 197 assert( ! mChildJ2EETypes.contains( j2eeType ) ); 198 199 mNonChildJ2EETypes.add( j2eeType ); 200 } 201 202 public String 203 getJ2EEType() 204 { 205 return( mTypeData.getJ2EEType() ); 206 } 207 208 public Set <String > 209 getLegalParentJ2EETypes() 210 { 211 return( mTypeData.getLegalParentJ2EETypes() ); 212 } 213 214 public String 215 getContainedByJ2EEType() 216 { 217 return( mTypeData.getContaineeByJ2EEType() ); 218 } 219 220 public boolean 221 isSubType() 222 { 223 return( mTypeData.isSubType() ); 224 } 225 226 public Class 227 getInterface() 228 { 229 return( mInterface ); 230 } 231 232 public Class 233 getImplClass() 234 { 235 return( mImplClass ); 236 } 237 238 public String 239 getParentJ2EEType() 240 { 241 final Set <String > legalParentJ2EETypes = getLegalParentJ2EETypes(); 242 243 if ( legalParentJ2EETypes == null ) 244 { 245 throw new IllegalArgumentException ( "no legal parent types for: " + getJ2EEType() ); 246 } 247 248 if ( legalParentJ2EETypes.size() != 1 ) 249 { 250 throw new IllegalArgumentException ( "expecting single parent for " + 251 getJ2EEType() + ", have: " + toString( legalParentJ2EETypes ) ); 252 } 253 254 return( GSetUtil.getSingleton( legalParentJ2EETypes ) ); 255 } 256 257 258 public Set <String > 259 getChildJ2EETypes() 260 { 261 return( Collections.unmodifiableSet( mChildJ2EETypes ) ); 262 } 263 264 public Set <String > 265 getNonChildJ2EETypes() 266 { 267 return( Collections.unmodifiableSet( mNonChildJ2EETypes ) ); 268 } 269 270 public Set <String > 271 getContaineeJ2EETypes() 272 { 273 final Set <String > all = 274 GSetUtil.newSet( mChildJ2EETypes, mNonChildJ2EETypes ); 275 276 return( Collections.unmodifiableSet( all ) ); 277 } 278 279 public int 280 hashCode() 281 { 282 return ObjectUtil.hashCode( 283 mTypeData, mInterface, mChildJ2EETypes, mNonChildJ2EETypes ); 284 } 285 286 public boolean 287 equals( final Object o ) 288 { 289 if ( o == this ) 290 { 291 return( true ); 292 } 293 else if ( ! (o instanceof TypeInfo ) ) 294 { 295 return( false ); 296 } 297 298 final TypeInfo rhs = (TypeInfo)o; 299 boolean equals = false; 300 if ( mTypeData.equals( rhs.mTypeData ) && 301 mInterface == rhs.mInterface && 302 mImplClass == rhs.mImplClass && 303 mChildJ2EETypes.equals( rhs.mChildJ2EETypes ) && 304 getLegalParentJ2EETypes().equals( rhs.getLegalParentJ2EETypes() ) 305 ) 306 { 307 equals = true; 308 } 309 310 return( equals ); 311 } 312 313 314 private static String 315 toString( final Object o ) 316 { 317 return( SmartStringifier.toString( o ) ); 318 } 319 320 } 321 322 323 324 325 326 327 328 329 | Popular Tags |