1 29 30 package com.caucho.jmx; 31 32 import javax.management.MBeanServer ; 33 import javax.management.MBeanServerInvocationHandler ; 34 import javax.management.ObjectInstance ; 35 import javax.management.ObjectName ; 36 import javax.naming.InitialContext ; 37 import java.io.InvalidObjectException ; 38 import java.io.NotActiveException ; 39 import java.io.ObjectStreamException ; 40 import java.lang.ref.SoftReference ; 41 import java.lang.reflect.Method ; 42 import java.util.logging.Level ; 43 import java.util.logging.Logger ; 44 45 48 public class MBeanHandle implements java.io.Serializable { 49 private static final Logger log 50 = Logger.getLogger(MBeanHandle.class.getName()); 51 52 private static SoftReference <MBeanServer > _globalMBeanServer; 53 54 private final ObjectName _name; 55 56 public MBeanHandle(ObjectName name) 57 { 58 _name = name; 59 } 60 61 64 private Object readResolve() 65 throws ObjectStreamException 66 { 67 try { 68 MBeanServer server = getMBeanServer(); 69 70 ObjectInstance instance = server.getObjectInstance(_name); 71 72 String className = instance.getClassName(); 73 74 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 75 76 Class cl = Class.forName(className, false, loader); 77 78 return MBeanServerInvocationHandler.newProxyInstance(server, 79 _name, 80 cl, 81 false); 82 } catch (ObjectStreamException e) { 83 e.printStackTrace(); 84 85 throw e; 86 } catch (Exception e) { 87 e.printStackTrace(); 88 89 log.log(Level.FINE, e.toString(), e); 90 91 throw new InvalidObjectException (e.toString()); 92 } 93 } 94 95 private static MBeanServer getMBeanServer() 96 throws ObjectStreamException 97 { 98 MBeanServer server = null; 99 100 if (_globalMBeanServer != null) 101 server = _globalMBeanServer.get(); 102 103 if (server != null) 104 return server; 105 106 try { 107 InitialContext ic = new InitialContext (); 108 server = (MBeanServer ) ic.lookup("java:comp/env/jmx/GlobalMBeanServer"); 109 110 if (server != null) { 111 _globalMBeanServer = new SoftReference <MBeanServer >(server); 112 113 return server; 114 } 115 } catch (Throwable e) { 116 log.log(Level.FINER, e.toString(), e); 117 } 118 119 try { 120 Class cl = Class.forName("java.lang.Management.ManagementFactory"); 121 122 Method method = cl.getMethod("getPlatformMBeanServer", new Class [0]); 123 124 server = (MBeanServer ) method.invoke(null, new Object [0]); 125 126 if (server != null) { 127 _globalMBeanServer = new SoftReference <MBeanServer >(server); 128 129 return server; 130 } 131 } catch (Throwable e) { 132 } 133 134 log.warning("Can't load global mbean server for proxy deserialization."); 135 136 throw new NotActiveException ("Can't load global mbean server for proxy deserialization."); 137 } 138 } 139 140 | Popular Tags |