1 23 24 28 29 31 32 package com.sun.enterprise.management.support; 33 34 import java.util.Map ; 35 import java.util.HashMap ; 36 import java.util.Set ; 37 import java.util.Collections ; 38 39 import javax.management.ObjectName ; 40 import javax.management.MBeanInfo ; 41 import javax.management.MBeanOperationInfo ; 42 import javax.management.MBeanAttributeInfo ; 43 44 import com.sun.appserv.management.base.AMX; 45 import com.sun.appserv.management.base.XTypes; 46 import com.sun.appserv.management.base.SystemInfo; 47 import com.sun.appserv.management.base.Util; 48 import com.sun.appserv.management.util.misc.StringUtil; 49 import com.sun.appserv.management.util.jmx.JMXUtil; 50 51 import com.sun.appserv.management.util.misc.ClassUtil; 52 import com.sun.appserv.management.base.Container; 53 54 55 59 final class MBeanInfoConverter 60 { 61 private final Map <Class ,MBeanInfo > mConvertedInfos; 62 private static MBeanInfoConverter INSTANCE = null; 63 64 private 65 MBeanInfoConverter( ) 66 { 67 mConvertedInfos = Collections.synchronizedMap( new HashMap <Class ,MBeanInfo >() ); 68 } 69 70 public static synchronized MBeanInfoConverter 71 getInstance() 72 { 73 if ( INSTANCE == null ) 74 { 75 INSTANCE = new MBeanInfoConverter(); 76 } 77 78 return( INSTANCE ); 79 } 80 81 protected static Class 82 toClass( final String className ) 83 { 84 try 85 { 86 return( ClassUtil.getClassFromName( className ) ); 87 } 88 catch( ClassNotFoundException e ) 89 { 90 assert( false ); 91 throw new RuntimeException ( e ); 92 } 93 } 94 95 private final static String OBJECT_NAME_SUFFIX = "ObjectName"; 96 private final static String SET_SUFFIX = "Set"; 97 private final static String MAP_SUFFIX = "Map"; 98 private final static String LIST_SUFFIX = "List"; 99 private final static String OBJECT_NAME_MAP_SUFFIX = OBJECT_NAME_SUFFIX + MAP_SUFFIX; 100 private final static String OBJECT_NAME_SET_SUFFIX = OBJECT_NAME_SUFFIX + SET_SUFFIX; 101 private final static String OBJECT_NAME_LIST_SUFFIX = OBJECT_NAME_SUFFIX + LIST_SUFFIX; 102 103 104 protected void 105 trace( Object o ) 106 { 107 } 109 110 private final MBeanAttributeInfo 111 convert( final MBeanAttributeInfo info ) 112 { 113 MBeanAttributeInfo result = info; 114 final String name = info.getName(); 115 116 final Class type = toClass( info.getType() ); 117 118 String newName = null; 119 Class newType = type; 120 121 if ( AMX.class.isAssignableFrom( type ) ) 122 { 123 newName = name + OBJECT_NAME_SUFFIX; 124 newType = ObjectName .class; 125 } 126 else if ( name.endsWith( SET_SUFFIX ) && 127 ! name.endsWith( OBJECT_NAME_SET_SUFFIX ) ) 128 { 129 newName = convertMethodName( name, SET_SUFFIX, OBJECT_NAME_SET_SUFFIX ); 130 } 131 else if ( name.endsWith( MAP_SUFFIX ) && 132 ! name.endsWith( OBJECT_NAME_MAP_SUFFIX ) 133 ) 134 { 135 newName = convertMethodName( name, MAP_SUFFIX, OBJECT_NAME_MAP_SUFFIX ); 136 } 137 else if ( name.endsWith( LIST_SUFFIX ) && 138 ! name.endsWith( OBJECT_NAME_LIST_SUFFIX ) 139 ) 140 { 141 newName = convertMethodName( name, LIST_SUFFIX, OBJECT_NAME_LIST_SUFFIX ); 142 } 143 144 if ( newName != null ) 145 { 146 trace( ClassUtil.stripPackageName( type.getName() ) + " " + name + 147 " => " + ClassUtil.stripPackageName( newType.getName() ) + " " + newName ); 148 149 result = new MBeanAttributeInfo ( newName, newType.getName(), 150 info.getDescription(), info.isReadable(), info.isWritable(), info.isIs() ); 151 } 152 153 return( result ); 154 } 155 156 protected static String 157 convertMethodName( 158 final String srcName, 159 final String srcSuffix, 160 final String resultSuffix ) 161 { 162 return( StringUtil.replaceSuffix( srcName, srcSuffix, resultSuffix ) ); 163 } 164 165 private final MBeanOperationInfo 166 convert( final MBeanOperationInfo info ) 167 { 168 MBeanOperationInfo result = info; 169 final String name = info.getName(); 170 171 final Class returnClass = toClass( info.getReturnType() ); 172 173 String newName = null; 174 Class newReturnClass = returnClass; 175 176 if ( AMX.class.isAssignableFrom( returnClass ) ) 177 { 178 182 newReturnClass = ObjectName .class; 183 184 189 if ( name.startsWith( "create" ) ) 190 { 191 newName = name; 192 } 193 else 194 { 195 newName = name + OBJECT_NAME_SUFFIX; 196 } 197 } 198 else if ( Map .class.isAssignableFrom( returnClass ) && 199 name.endsWith( MAP_SUFFIX ) && 200 ! name.endsWith( OBJECT_NAME_MAP_SUFFIX ) ) 201 { 202 newName = convertMethodName( name, MAP_SUFFIX, OBJECT_NAME_MAP_SUFFIX ); 203 } 204 else if ( Set .class.isAssignableFrom( returnClass ) && 205 name.endsWith( SET_SUFFIX ) && 206 ! name.endsWith( OBJECT_NAME_SET_SUFFIX )) 207 { 208 newName = convertMethodName( name, SET_SUFFIX, OBJECT_NAME_SET_SUFFIX ); 209 } 210 else if ( Set .class.isAssignableFrom( returnClass ) && 211 name.endsWith( LIST_SUFFIX ) && 212 ! name.endsWith( OBJECT_NAME_LIST_SUFFIX )) 213 { 214 newName = convertMethodName( name, LIST_SUFFIX, OBJECT_NAME_LIST_SUFFIX ); 215 } 216 217 if ( newName != null ) 218 { 219 trace( ClassUtil.stripPackageName( returnClass.getName() ) + " " + name + "(...)" + 220 " => " + ClassUtil.stripPackageName( newReturnClass.getName() ) + " " + newName + "(...)" ); 221 222 result = new MBeanOperationInfo ( 223 newName, 224 info.getDescription(), 225 info.getSignature(), 226 newReturnClass.getName(), 227 info.getImpact() ); 228 } 229 230 return( result ); 231 } 232 233 private final MBeanAttributeInfo [] 234 convertAttributes( final MBeanAttributeInfo [] origInfos ) 235 { 236 final MBeanAttributeInfo [] infos = new MBeanAttributeInfo [ origInfos.length ]; 237 238 for( int i = 0; i < infos.length; ++i ) 239 { 240 infos[ i ] = convert( origInfos[ i ] ); 241 } 242 243 return( infos ); 244 } 245 246 private final MBeanOperationInfo [] 247 convertOperations( final MBeanOperationInfo [] origInfos ) 248 { 249 final MBeanOperationInfo [] infos = new MBeanOperationInfo [ origInfos.length ]; 250 251 for( int i = 0; i < infos.length; ++i ) 252 { 253 infos[ i ] = convert( origInfos[ i ] ); 254 } 255 256 return( infos ); 257 } 258 259 260 private final MBeanInfo 261 find( final Class theInterface ) 262 { 263 return( mConvertedInfos.get( theInterface ) ); 264 } 265 266 public final MBeanInfo 267 convert( 268 final Class theInterface, 269 final MBeanAttributeInfo [] extraAttributeInfos ) 270 { 271 MBeanInfo result = null; 272 273 synchronized( theInterface ) 275 { 276 result = find( theInterface ); 277 if ( result == null ) 278 { 279 final MBeanInfo origInfo = JMXUtil.interfaceToMBeanInfo( theInterface ); 280 281 final MBeanAttributeInfo [] origAttrInfos = origInfo.getAttributes(); 282 final MBeanAttributeInfo [] attrInfos = extraAttributeInfos == null ? 283 origAttrInfos : 284 JMXUtil.mergeMBeanAttributeInfos( origAttrInfos, extraAttributeInfos); 285 286 result = new MBeanInfo ( 287 origInfo.getClassName(), 288 origInfo.getDescription(), 289 convertAttributes( attrInfos ), 290 origInfo.getConstructors(), 291 convertOperations( origInfo.getOperations() ), 292 origInfo.getNotifications() ); 293 294 mConvertedInfos.put( theInterface, result ); 295 } 296 } 297 298 return( result ); 299 } 300 } 301 302 303 304 305 306 307 308 309 | Popular Tags |