1 8 9 package mx4j.server.interceptor; 10 11 import java.lang.reflect.Constructor ; 12 import java.lang.reflect.InvocationTargetException ; 13 import javax.management.Attribute ; 14 import javax.management.AttributeList ; 15 import javax.management.AttributeNotFoundException ; 16 import javax.management.DynamicMBean ; 17 import javax.management.InvalidAttributeValueException ; 18 import javax.management.JMRuntimeException ; 19 import javax.management.ListenerNotFoundException ; 20 import javax.management.MBeanException ; 21 import javax.management.MBeanInfo ; 22 import javax.management.MBeanRegistration ; 23 import javax.management.MBeanRegistrationException ; 24 import javax.management.MBeanServer ; 25 import javax.management.NotificationBroadcaster ; 26 import javax.management.NotificationEmitter ; 27 import javax.management.NotificationFilter ; 28 import javax.management.NotificationListener ; 29 import javax.management.ObjectName ; 30 import javax.management.ReflectionException ; 31 import javax.management.RuntimeErrorException ; 32 import javax.management.RuntimeMBeanException ; 33 34 import mx4j.ImplementationException; 35 import mx4j.log.Logger; 36 import mx4j.server.MBeanMetaData; 37 import mx4j.util.Utils; 38 39 46 public class InvokerMBeanServerInterceptor extends DefaultMBeanServerInterceptor implements InvokerMBeanServerInterceptorMBean 47 { 48 private MBeanServer outerServer; 49 50 56 public InvokerMBeanServerInterceptor(MBeanServer outerServer) 57 { 58 this.outerServer = outerServer; 59 } 60 61 64 public String getType() 65 { 66 return "invoker"; 67 } 68 69 72 public boolean isEnabled() 73 { 74 return true; 75 } 76 77 public void addNotificationListener(MBeanMetaData metadata, NotificationListener listener, NotificationFilter filter, Object handback) 78 { 79 ((NotificationBroadcaster )metadata.getMBean()).addNotificationListener(listener, filter, handback); 80 } 81 82 public void removeNotificationListener(MBeanMetaData metadata, NotificationListener listener) throws ListenerNotFoundException 83 { 84 ((NotificationBroadcaster )metadata.getMBean()).removeNotificationListener(listener); 85 } 86 87 public void removeNotificationListener(MBeanMetaData metadata, NotificationListener listener, NotificationFilter filter, Object handback) 88 throws ListenerNotFoundException 89 { 90 ((NotificationEmitter )metadata.getMBean()).removeNotificationListener(listener, filter, handback); 91 } 92 93 public void instantiate(MBeanMetaData metadata, String className, String [] params, Object [] args) throws ReflectionException , MBeanException 94 { 95 try 96 { 97 ClassLoader loader = metadata.getClassLoader(); 98 if (loader == null) loader = Thread.currentThread().getContextClassLoader(); 99 Class cls = loader.loadClass(className); 100 101 Class [] signature = Utils.loadClasses(loader, params); 102 103 Constructor ctor = cls.getConstructor(signature); 104 105 metadata.setMBean(ctor.newInstance(args)); 106 } 107 catch (ClassNotFoundException x) 108 { 109 throw new ReflectionException (x); 110 } 111 catch (NoSuchMethodException x) 112 { 113 throw new ReflectionException (x); 114 } 115 catch (InstantiationException x) 116 { 117 throw new ReflectionException (x); 118 } 119 catch (IllegalAccessException x) 120 { 121 throw new ReflectionException (x); 122 } 123 catch (IllegalArgumentException x) 124 { 125 throw new ReflectionException (x); 126 } 127 catch (InvocationTargetException x) 128 { 129 Throwable t = x.getTargetException(); 130 if (t instanceof Error ) 131 { 132 throw new RuntimeErrorException ((Error )t); 133 } 134 else if (t instanceof RuntimeException ) 135 { 136 throw new RuntimeMBeanException ((RuntimeException )t); 137 } 138 else 139 { 140 throw new MBeanException ((Exception )t); 141 } 142 } 143 } 144 145 public void registration(MBeanMetaData metadata, int operation) throws MBeanRegistrationException 146 { 147 Object mbean = metadata.getMBean(); 148 if (!(mbean instanceof MBeanRegistration )) return; 149 150 MBeanRegistration registrable = (MBeanRegistration )mbean; 151 152 try 153 { 154 switch (operation) 155 { 156 case PRE_REGISTER: 157 ObjectName objName = registrable.preRegister(outerServer, metadata.getObjectName()); 158 metadata.setObjectName(objName); 159 break; 160 case POST_REGISTER_TRUE: 161 registrable.postRegister(Boolean.TRUE); 162 break; 163 case POST_REGISTER_FALSE: 164 registrable.postRegister(Boolean.FALSE); 165 break; 166 case PRE_DEREGISTER: 167 registrable.preDeregister(); 168 break; 169 case POST_DEREGISTER: 170 registrable.postDeregister(); 171 break; 172 default: 173 throw new ImplementationException(); 174 } 175 } 176 catch (RuntimeException x) 177 { 178 throw new RuntimeMBeanException (x); 179 } 180 catch (Exception x) 181 { 182 if (x instanceof MBeanRegistrationException ) 183 { 184 throw (MBeanRegistrationException )x; 185 } 186 throw new MBeanRegistrationException (x); 187 } 188 catch (Error x) 189 { 190 throw new RuntimeErrorException (x); 191 } 192 } 193 194 public MBeanInfo getMBeanInfo(MBeanMetaData metadata) 195 { 196 if (metadata.isMBeanDynamic()) 197 { 198 MBeanInfo info = null; 200 try 201 { 202 info = ((DynamicMBean )metadata.getMBean()).getMBeanInfo(); 203 } 204 catch (RuntimeException x) 205 { 206 throw new RuntimeMBeanException (x); 207 } 208 if (info == null) return null; 209 metadata.setMBeanInfo(info); 210 } 211 212 return (MBeanInfo )metadata.getMBeanInfo().clone(); 213 } 214 215 public Object invoke(MBeanMetaData metadata, String method, String [] params, Object [] args) throws MBeanException , ReflectionException 216 { 217 if (metadata.isMBeanDynamic()) 218 { 219 try 220 { 221 return ((DynamicMBean )metadata.getMBean()).invoke(method, args, params); 222 } 223 catch (JMRuntimeException x) 224 { 225 throw x; 226 } 227 catch (RuntimeException x) 228 { 229 throw new RuntimeMBeanException (x); 230 } 231 catch (Error x) 232 { 233 throw new RuntimeErrorException (x); 234 } 235 } 236 else 237 { 238 return metadata.getMBeanInvoker().invoke(metadata, method, params, args); 239 } 240 } 241 242 public Object getAttribute(MBeanMetaData metadata, String attribute) throws MBeanException , AttributeNotFoundException , ReflectionException 243 { 244 if (metadata.isMBeanDynamic()) 245 { 246 try 247 { 248 return ((DynamicMBean )metadata.getMBean()).getAttribute(attribute); 249 } 250 catch (JMRuntimeException x) 251 { 252 throw x; 253 } 254 catch (RuntimeException x) 255 { 256 throw new RuntimeMBeanException (x); 257 } 258 catch (Error x) 259 { 260 throw new RuntimeErrorException (x); 261 } 262 } 263 else 264 { 265 return metadata.getMBeanInvoker().getAttribute(metadata, attribute); 266 } 267 } 268 269 public void setAttribute(MBeanMetaData metadata, Attribute attribute) throws MBeanException , AttributeNotFoundException , InvalidAttributeValueException , ReflectionException 270 { 271 if (metadata.isMBeanDynamic()) 272 { 273 try 274 { 275 ((DynamicMBean )metadata.getMBean()).setAttribute(attribute); 276 } 277 catch (JMRuntimeException x) 278 { 279 throw x; 280 } 281 catch (RuntimeException x) 282 { 283 throw new RuntimeMBeanException (x); 284 } 285 catch (Error x) 286 { 287 throw new RuntimeErrorException (x); 288 } 289 } 290 else 291 { 292 metadata.getMBeanInvoker().setAttribute(metadata, attribute); 293 } 294 } 295 296 public AttributeList getAttributes(MBeanMetaData metadata, String [] attributes) 297 { 298 if (metadata.isMBeanDynamic()) 299 { 300 try 301 { 302 return ((DynamicMBean )metadata.getMBean()).getAttributes(attributes); 303 } 304 catch (JMRuntimeException x) 305 { 306 throw x; 307 } 308 catch (RuntimeException x) 309 { 310 throw new RuntimeMBeanException (x); 311 } 312 catch (Error x) 313 { 314 throw new RuntimeErrorException (x); 315 } 316 } 317 else 318 { 319 AttributeList list = new AttributeList (); 320 for (int i = 0; i < attributes.length; ++i) 321 { 322 String name = attributes[i]; 323 try 324 { 325 Object value = getAttribute(metadata, name); 326 Attribute attr = new Attribute (name, value); 327 list.add(attr); 328 } 329 catch (Exception ignored) 330 { 331 Logger logger = getLogger(); 332 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Exception caught from getAttributes(), ignoring attribute " + name); 333 } 334 } 335 return list; 336 } 337 } 338 339 public AttributeList setAttributes(MBeanMetaData metadata, AttributeList attributes) 340 { 341 if (metadata.isMBeanDynamic()) 342 { 343 try 344 { 345 return ((DynamicMBean )metadata.getMBean()).setAttributes(attributes); 346 } 347 catch (JMRuntimeException x) 348 { 349 throw x; 350 } 351 catch (RuntimeException x) 352 { 353 throw new RuntimeMBeanException (x); 354 } 355 catch (Error x) 356 { 357 throw new RuntimeErrorException (x); 358 } 359 } 360 else 361 { 362 AttributeList list = new AttributeList (); 363 for (int i = 0; i < attributes.size(); ++i) 364 { 365 Attribute attr = (Attribute )attributes.get(i); 366 try 367 { 368 setAttribute(metadata, attr); 369 list.add(attr); 370 } 371 catch (Exception ignored) 372 { 373 Logger logger = getLogger(); 374 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Exception caught from setAttributes(), ignoring attribute " + attr, ignored); 375 } 376 } 377 return list; 378 } 379 } 380 } 381 | Popular Tags |