1 7 8 package com.sun.jmx.mbeanserver; 9 10 11 12 import java.util.Iterator ; 14 import java.io.PrintWriter ; 15 import java.io.StringWriter ; 16 17 import javax.management.* ; 19 import com.sun.jmx.trace.Trace; 20 21 29 abstract class BaseMetaDataImpl implements MetaData { 30 31 32 private final static String dbgTag = "BaseMetaDataImpl"; 33 34 35 38 BaseMetaDataImpl() { 39 40 } 43 44 45 51 public abstract MBeanInfo getMBeanInfo(Object moi) 52 throws IntrospectionException; 53 54 public abstract Object getAttribute(Object instance, String attribute) 55 throws MBeanException, AttributeNotFoundException, 56 ReflectionException; 57 58 public abstract AttributeList getAttributes(Object instance, 59 String [] attributes) 60 throws ReflectionException; 61 62 public abstract AttributeList setAttributes(Object instance, 63 AttributeList attributes) 64 throws ReflectionException; 65 66 public abstract Object setAttribute(Object instance, Attribute attribute) 67 throws AttributeNotFoundException, InvalidAttributeValueException, 68 MBeanException, ReflectionException; 69 70 public abstract Object invoke(Object instance, String operationName, 71 Object params[], String signature[]) 72 throws MBeanException, ReflectionException; 73 74 public ObjectName preRegisterInvoker(Object moi, ObjectName name, 75 MBeanServer mbs) 76 throws InstanceAlreadyExistsException, MBeanRegistrationException { 77 78 if (!(moi instanceof MBeanRegistration)) return name; 79 80 final ObjectName newName; 81 82 try { 83 newName=(ObjectName) 84 ((MBeanRegistration)moi).preRegister(mbs, name); 85 } catch (RuntimeException e) { 86 throw new RuntimeMBeanException((RuntimeException )e, 87 "RuntimeException thrown in preRegister method"); 88 } catch (Error er) { 89 throw new RuntimeErrorException((Error ) er, 90 "Error thrown in preRegister method"); 91 } catch (MBeanRegistrationException r) { 92 throw (MBeanRegistrationException)r; 93 } catch (Exception ex) { 94 throw new MBeanRegistrationException((Exception ) ex, 95 "Exception thrown in preRegister method"); 96 } 97 98 if (newName != null) return newName; 99 else return name; 100 } 101 102 public void postRegisterInvoker(Object moi, boolean registrationDone) { 103 if (!(moi instanceof MBeanRegistration)) return; 104 105 try { 106 ((MBeanRegistration)moi). 107 postRegister(new Boolean (registrationDone)); 108 } catch (RuntimeException e) { 109 throw new RuntimeMBeanException((RuntimeException )e, 110 "RuntimeException thrown in postRegister method"); 111 } catch (Error er) { 112 throw new RuntimeErrorException((Error ) er, 113 "Error thrown in postRegister method"); 114 } 115 } 116 117 public void preDeregisterInvoker(Object moi) 118 throws MBeanRegistrationException { 119 if (!(moi instanceof MBeanRegistration)) return; 120 121 try { 122 ((MBeanRegistration)moi).preDeregister(); 123 } catch (RuntimeException e) { 124 throw new RuntimeMBeanException((RuntimeException ) e, 125 "RuntimeException thrown in preDeregister method"); 126 } catch (Error er) { 127 throw new RuntimeErrorException((Error ) er, 128 "Error thrown in preDeregister method"); 129 } catch (MBeanRegistrationException t) { 130 throw (MBeanRegistrationException)t; 131 } catch (Exception ex) { 132 throw new MBeanRegistrationException((Exception )ex, 133 "Exception thrown in preDeregister method"); 134 } 135 } 136 137 public void postDeregisterInvoker(Object moi) { 138 if (!(moi instanceof MBeanRegistration)) return; 139 140 try { 141 ((MBeanRegistration)moi).postDeregister(); 142 } catch (RuntimeException e) { 143 throw new RuntimeMBeanException((RuntimeException )e, 144 "RuntimeException thrown in postDeregister method"); 145 } catch (Error er) { 146 throw new RuntimeErrorException((Error ) er, 147 "Error thrown in postDeregister method"); 148 } 149 } 150 151 public String getMBeanClassName(Object moi) 152 throws IntrospectionException, NotCompliantMBeanException { 153 154 final MBeanInfo mbi; 155 try { 156 mbi = getMBeanInfo(moi); 158 } catch (SecurityException x) { 159 throw x; 160 } catch (IntrospectionException x) { 161 throw x; 162 } catch (Exception x) { 163 throw new NotCompliantMBeanException("Can't obtain MBeanInfo " + 164 "from DynamicMBean: " + x); 165 } 166 167 if (mbi == null) { 168 throw new 169 NotCompliantMBeanException("The MBeanInfo returned is null"); 170 } 171 172 final String className = mbi.getClassName(); 173 174 if (className == null) { 175 throw new 176 IntrospectionException("The class Name returned is null"); 177 } 178 179 return className; 180 } 181 182 public boolean isInstanceOf(Object instance, String className) 183 throws ReflectionException { 184 185 try { 186 final String cn = getMBeanClassName(instance); 187 if (cn.equals(className)) 188 return true; 189 try { 190 final ClassLoader cl = instance.getClass().getClassLoader(); 191 192 final Class classNameClass = findClass(className,cl); 193 if (classNameClass == null) return false; 194 if (classNameClass.isInstance(instance)) return true; 195 196 final Class instanceClass = findClass(cn,cl); 197 if (instanceClass == null) return false; 198 return classNameClass.isAssignableFrom(instanceClass); 199 } catch (Exception x) { 200 201 debugX("isInstanceOf",x); 202 return false; 203 } 204 } catch (IntrospectionException x) { 205 debugX("isInstanceOf",x); 206 throw new ReflectionException(x,x.getMessage()); 207 } catch (NotCompliantMBeanException x) { 208 debugX("isInstanceOf",x); 209 throw new ReflectionException(x,x.getMessage()); 210 } 211 } 212 213 Class findClass(String className, ClassLoader loader) 214 throws ReflectionException { 215 try { 216 if (loader == null) 217 return Class.forName(className); 218 else 219 return loader.loadClass(className); 220 } catch (ClassNotFoundException x) { 221 throw new ReflectionException(x,x.getMessage()); 222 } 223 } 224 225 228 private static boolean isTraceOn() { 229 return Trace.isSelected(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER); 230 } 231 232 private static void trace(String clz, String func, String info) { 233 Trace.send(Trace.LEVEL_TRACE, Trace.INFO_MBEANSERVER, clz, func, info); 234 } 235 236 private static void trace(String func, String info) { 237 trace(dbgTag, func, info); 238 } 239 240 private static boolean isDebugOn() { 241 return Trace.isSelected(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER); 242 } 243 244 private static void debug(String clz, String func, String info) { 245 Trace.send(Trace.LEVEL_DEBUG, Trace.INFO_MBEANSERVER, clz, func, info); 246 } 247 248 private static void debug(String func, String info) { 249 debug(dbgTag, func, info); 250 } 251 252 private static void debugX(String func,Throwable e) { 253 if (isDebugOn()) { 254 final StringWriter s = new StringWriter (); 255 e.printStackTrace(new PrintWriter (s)); 256 final String stack = s.toString(); 257 258 debug(dbgTag,func,"Exception caught in "+ func+"(): "+e); 259 debug(dbgTag,func,stack); 260 } 264 } 265 } 266 | Popular Tags |