1 23 24 119 120 package com.sun.enterprise.admin; 121 122 import java.util.ArrayList ; 123 import java.lang.reflect.Method ; 124 import java.lang.reflect.InvocationTargetException ; 125 126 import com.sun.enterprise.admin.meta.MBeanMetaConstants; 127 import com.sun.enterprise.admin.meta.MBeanRegistryFactory; 128 import com.sun.enterprise.admin.meta.MBeanRegistry; 129 import com.sun.enterprise.admin.meta.MBeanRegistryEntry; 130 131 import com.sun.enterprise.admin.meta.naming.MBeanNamingDescriptor; 132 import com.sun.enterprise.admin.meta.naming.MBeanNamingInfo; 133 134 135 137 import javax.management.MBeanException ; 139 import javax.management.Descriptor ; 140 import javax.management.ObjectName ; 141 import javax.management.MBeanInfo ; 142 import javax.management.MBeanOperationInfo ; 143 import javax.management.MBeanAttributeInfo ; 144 import javax.management.MBeanParameterInfo ; 145 import javax.management.modelmbean.ModelMBeanInfo ; 146 147 import java.util.logging.Logger ; 149 import java.util.logging.Level ; 150 import com.sun.logging.LogDomains; 151 import com.sun.enterprise.util.i18n.StringManager; 152 153 156 public class MBeanHelper implements MBeanMetaConstants 158 { 159 static protected Logger _sLogger = LogDomains.getLogger(LogDomains.ADMIN_LOGGER); 161 static protected StringManager _localStrings = StringManager.getManager( BaseAdminMBean.class ); 162 163 public static Object INVOKE_ERROR_SIGNAL_OBJECT = new Object (); 164 165 public static Object getDescriptorFieldValue(ModelMBeanInfo info, String name) throws MBeanException 168 { 169 Descriptor descr = info.getMBeanDescriptor(); 170 return descr.getFieldValue(name); 171 172 } 173 174 public static String [] getLocation(ModelMBeanInfo info) throws MBeanException 177 { 178 return (String [])getDescriptorFieldValue(info, NMLOCATION_FIELD_NAME); 179 180 } 181 182 public static String getXPathPattern(ModelMBeanInfo info) throws MBeanException 185 { 186 return (String )getDescriptorFieldValue(info, XPATH_FIELD_NAME); 187 } 188 189 190 240 private static Class getAttributeClass(String signature) throws Exception 242 { 243 if (signature.equals(Boolean.TYPE.getName())) 244 return Boolean.TYPE; 245 else if (signature.equals(Byte.TYPE.getName())) 246 return Byte.TYPE; 247 else if (signature.equals(Character.TYPE.getName())) 248 return Character.TYPE; 249 else if (signature.equals(Double.TYPE.getName())) 250 return Double.TYPE; 251 else if (signature.equals(Float.TYPE.getName())) 252 return Float.TYPE; 253 else if (signature.equals(Integer.TYPE.getName())) 254 return Integer.TYPE; 255 else if (signature.equals(Long.TYPE.getName())) 256 return Long.TYPE; 257 else if (signature.equals(Short.TYPE.getName())) 258 return Short.TYPE; 259 else { 260 try { 261 ClassLoader cl=Thread.currentThread().getContextClassLoader(); 262 if( cl!=null ) 263 return cl.loadClass(signature); 264 } catch( ClassNotFoundException e ) { 265 } 266 return Class.forName(signature); 268 } 273 } 274 public static String [] getParamTypesFromOperationInfo(MBeanOperationInfo opInfo) 276 { 277 MBeanParameterInfo [] params = opInfo.getSignature(); 278 if(params==null) 279 return new String [0]; 280 ArrayList signature = new ArrayList (); 281 for(int i=0; i<params.length; i++) 282 signature.add(params[i].getType()); 283 return (String [])signature.toArray(new String [signature.size()]); 284 } 285 public static Class [] getSignatureFromOperationInfo(MBeanOperationInfo opInfo) throws Exception 287 { 288 MBeanParameterInfo [] params = opInfo.getSignature(); 289 if(params==null) 290 return new Class [0]; 291 ArrayList signature = new ArrayList (); 292 for(int i=0; i<params.length; i++) 293 signature.add(getAttributeClass(params[i].getType())); 294 return (Class [])signature.toArray(new Class [signature.size()]); 295 } 296 297 public static MBeanOperationInfo findMatchingOperationInfo(MBeanInfo mbeanInfo, String name, String signature[]) 299 { 300 MBeanOperationInfo [] opInfos = mbeanInfo.getOperations(); 301 if(opInfos==null) 302 return null; 303 boolean bMatch; 304 for(int i=0; i<opInfos.length; i++) 305 { 306 if(name.equals(opInfos[i].getName())) 307 { 308 String sign[] = getParamTypesFromOperationInfo(opInfos[i]); 309 if(isSignaturesEqual(sign, signature)) 310 return opInfos[i]; 311 } 312 } 313 return null; 314 } 315 316 public static MBeanAttributeInfo findMatchingAttributeInfo(MBeanInfo mbeanInfo, String name) 318 { 319 MBeanAttributeInfo [] attrInfos = mbeanInfo.getAttributes(); 320 if(attrInfos==null) 321 return null; 322 boolean bMatch; 323 for(int i=0; i<attrInfos.length; i++) 324 { 325 if(name.equals(attrInfos[i].getName())) 326 { 327 return attrInfos[i]; 328 } 329 } 330 return null; 331 } 332 333 public static Object invokeOperationInBean(MBeanOperationInfo opInfo, Object bean, Object [] params) throws Exception 335 { 336 return invokeOperationInBean(opInfo.getName(), opInfo, bean, params); 337 } 338 public static Object invokeOperationInBean(String opName, MBeanOperationInfo opInfo, Object bean, Object [] params) throws Exception 340 { 341 Method method = null; 342 try 343 { 344 method = findMatchingOperationMethod(opName, opInfo, bean); 345 } 346 catch (Exception e) 347 { 348 if(e instanceof SecurityException ) 349 _sLogger.log(Level.FINEST, "invokeOperationInBean() failed", e); 350 } 351 if(method==null) 352 return INVOKE_ERROR_SIGNAL_OBJECT; 353 return method.invoke(bean, params); 354 } 355 356 public static Method findMatchingOperationMethod(String opName, MBeanOperationInfo opInfo, Object bean) throws Exception 358 { 359 { 361 Class signature[] = getSignatureFromOperationInfo(opInfo); 362 return bean.getClass().getMethod(opName, signature); 363 } 364 372 } 373 private static boolean isSignaturesEqual(String sign[], String signature[]) 375 { 376 if ((signature==null||signature.length==0) && 378 (sign==null||sign.length==0) ) 379 return true; 380 if((signature==null && sign!=null) || 381 (signature!=null && sign==null) ) 382 return false; 383 if(signature.length != sign.length) 384 return false; 385 boolean bMatch = true; 386 for(int j=0; j<sign.length; j++) 387 { 388 if(!sign[j].equals(signature[j])) 389 { 390 bMatch = false; 391 break; 392 } 393 } 394 return bMatch; 395 } 396 public static MBeanException extractAndWrapTargetException(Exception e, String wrapMsg) 398 { 399 while(e instanceof InvocationTargetException || 400 e instanceof MBeanException ) 401 { 402 if(e instanceof InvocationTargetException ) 403 { 404 Throwable t = ((InvocationTargetException )e).getTargetException(); 405 if (t instanceof Exception ) 406 e = (Exception )t; 407 else 408 e = new Exception (t.getMessage()); 409 } 410 else 411 if(e instanceof MBeanException ) 412 { 413 e = ((MBeanException )e).getTargetException(); 414 } 415 } 416 String msg = e.getMessage(); 417 String targetMsgPref = _localStrings.getString( "admin.server.core.mbean.target_exception_prefix"); 418 if(msg!=null) 419 return new MBeanException (e, wrapMsg + "\n"+ targetMsgPref + ": " + e.getMessage()); 420 else 421 return new MBeanException (e, wrapMsg); 422 } 423 } 424 | Popular Tags |