1 29 30 package com.caucho.management.server; 31 32 import com.caucho.jmx.Description; 33 import com.caucho.jmx.Jmx; 34 35 import javax.management.MalformedObjectNameException ; 36 import javax.management.ObjectName ; 37 import java.util.Map ; 38 import java.util.logging.Level ; 39 import java.util.logging.Logger ; 40 41 44 abstract public class AbstractManagedObject implements ManagedObjectMXBean { 45 private static final Logger log 46 = Logger.getLogger(AbstractManagedObject.class.getName()); 47 48 private ClassLoader _classLoader; 49 private ObjectName _objectName; 50 51 protected AbstractManagedObject() 52 { 53 this(Thread.currentThread().getContextClassLoader()); 54 } 55 56 protected AbstractManagedObject(ClassLoader loader) 57 { 58 _classLoader = loader; 59 } 60 61 64 @Description("The JMX ObjectName for the MBean") 65 public ObjectName getObjectName() 66 { 67 if (_objectName == null) { 68 69 try { 70 Map <String ,String > props = Jmx.copyContextProperties(_classLoader); 71 72 props.put("type", getType()); 73 74 String name = getName(); 75 if (name != null) { 76 if (name.indexOf(':') >= 0) 77 name = ObjectName.quote(name); 78 79 props.put("name", name); 80 } 81 _objectName = Jmx.getObjectName("resin", props); 83 } catch (MalformedObjectNameException e) { 84 throw new RuntimeException (e); 85 } 86 } 87 88 return _objectName; 89 } 90 91 94 abstract public String getName(); 95 96 99 public String getType() 100 { 101 Class []interfaces = getClass().getInterfaces(); 102 103 for (int i = 0; i < interfaces.length; i++) { 104 String className = interfaces[i].getName(); 105 106 if (className.endsWith("MXBean")) { 107 int p = className.lastIndexOf('.'); 108 int q = className.indexOf("MXBean"); 109 110 return className.substring(p + 1, q); 111 } 112 } 113 114 int p = getClass().getName().lastIndexOf('.'); 115 116 return getClass().getName().substring(p + 1); 117 } 118 119 122 protected boolean registerSelf() 123 { 124 try { 125 Jmx.register(this, getObjectName(), _classLoader); 126 127 return true; 128 } catch (RuntimeException e) { 129 throw e; 130 } catch (Exception e) { 131 log.log(Level.FINE, e.toString(), e); 132 133 return false; 134 } 135 } 136 137 140 protected boolean unregisterSelf() 141 { 142 try { 143 Jmx.unregister(getObjectName(), _classLoader); 144 145 return true; 146 } catch (Throwable e) { 147 log.log(Level.FINE, e.toString(), e); 148 149 return false; 150 } 151 } 152 } 153 | Popular Tags |