1 24 25 package org.objectweb.cjdbc.controller.jmx; 26 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import javax.management.MBeanServer ; 31 import javax.management.MBeanServerFactory ; 32 import javax.management.ObjectInstance ; 33 import javax.management.ObjectName ; 34 35 import org.objectweb.cjdbc.common.i18n.Translate; 36 import org.objectweb.cjdbc.common.jmx.JmxException; 37 import org.objectweb.cjdbc.common.log.Trace; 38 39 49 public class MBeanServerManager 50 { 51 52 static Trace logger = Trace 53 .getLogger("org.objectweb.cjdbc.controller.jmx.MBeanServer"); 54 55 private static MBeanServer mbs; 56 private static boolean isJmxEnabled = true; 57 58 64 public static synchronized MBeanServer getInstance() 65 { 66 67 if (!isJmxEnabled) 68 { 69 return null; 70 } 71 72 if (mbs != null) 73 { 74 return mbs; 75 } 76 77 String defaultServerBuilder = System 78 .getProperty("javax.management.builder.initial"); 79 80 if (!MBeanServerBuilder.class.getName().equals(defaultServerBuilder)) 81 { 82 if (defaultServerBuilder != null) 83 logger.error("property javax.management.builder.initial was " 84 + defaultServerBuilder); 85 86 logger.debug("setting property javax.management.builder.initial"); 87 System 88 .setProperty("javax.management.builder.initial", 89 org.objectweb.cjdbc.controller.jmx.MBeanServerBuilder.class 90 .getName()); 91 92 } 93 94 mbs = MBeanServerFactory.createMBeanServer(); 95 return mbs; 96 } 97 98 103 public static boolean isJmxEnabled() 104 { 105 return isJmxEnabled; 106 } 107 108 114 public static void setJmxEnabled(boolean isJmxEnabled) throws JmxException 115 { 116 if (MBeanServerManager.isJmxEnabled != isJmxEnabled && !isJmxEnabled && mbs!=null) 117 { 118 List list = RmiConnector.getRmiConnectors(); 120 for (Iterator it = list.iterator(); it.hasNext();) 121 { 122 RmiConnector rmi = (RmiConnector) it.next(); 123 rmi.stop(); 124 } 125 126 list = HttpAdaptor.getHttpAdaptors(); 128 for (Iterator it = list.iterator(); it.hasNext();) 129 { 130 HttpAdaptor http = (HttpAdaptor) it.next(); 131 http.stop(); 132 } 133 MBeanServerFactory.releaseMBeanServer(mbs); 135 mbs = null; 136 } 137 MBeanServerManager.isJmxEnabled = isJmxEnabled; 139 } 140 141 163 public static ObjectInstance registerMBean(Object object, ObjectName name) 164 throws JmxException 165 { 166 MBeanServer server = getInstance(); 167 try 168 { 169 170 if (server != null) 171 { 172 logger.debug(Translate.get("jmx.register.mbean", new String []{ 173 object.getClass().toString(), name.getCanonicalName()})); 174 175 ObjectInstance objInstance = null; 176 if (!server.isRegistered(name)) 177 { 178 objInstance = server.registerMBean(object, name); 179 } 180 else 181 { 182 logger.error(Translate.get("jmx.register.mbean.already.exist", 183 new String []{name.getCanonicalName()})); 184 try 185 { 186 server.unregisterMBean(name); 187 } 188 catch (Exception e) 189 { 190 logger.error(Translate.get("jmx.delete.mbean.failed", new String []{ 191 name.toString(), e.getMessage()})); 192 } 193 objInstance = server.registerMBean(object, name); 194 } 195 196 logger.debug(Translate.get("jmx.server.mbean.count", "" 197 + server.getMBeanCount())); 198 return objInstance; 199 } 200 return null; 201 } 202 catch (Exception e) 203 { 204 logger.error(Translate.get("jmx.register.mbean.failed", 205 new String []{object.getClass().toString(), e.getMessage(), 206 e.getClass().toString()})); 207 e.printStackTrace(); 208 e.getCause().printStackTrace(); 209 throw new JmxException(e); 210 } 211 } 212 213 219 public static void unregister(ObjectName name) throws JmxException 220 { 221 MBeanServer server = getInstance(); 222 if (server != null) 223 { 224 try 225 { 226 server.unregisterMBean(name); 228 logger.debug(Translate.get("jmx.server.mbean.count", "" 229 + server.getMBeanCount())); 230 231 } 232 catch (Exception e) 233 { 234 logger.error(Translate.get("jmx.register.mbean.failed", new String []{ 235 name.getCanonicalName(), e.getMessage()})); 236 throw new JmxException(e); 237 } 238 } 239 } 240 241 } | Popular Tags |