1 26 27 package com.rift.coad.lib.deployment.jmxbean; 28 29 import java.util.Iterator ; 31 import java.util.Map ; 32 import java.util.HashMap ; 33 import java.util.Set ; 34 import javax.management.MBeanServer ; 35 import java.lang.management.ManagementFactory ; 36 import java.rmi.Remote ; 37 import javax.naming.Context ; 38 import javax.naming.InitialContext ; 39 import javax.naming.Name ; 40 import javax.rmi.PortableRemoteObject ; 41 42 import org.apache.log4j.Logger; 44 45 import com.rift.coad.lib.bean.BeanWrapper; 47 import com.rift.coad.lib.security.ThreadsPermissionContainer; 48 import com.rift.coad.lib.deployment.DeploymentLoader; 49 import com.rift.coad.lib.thread.CoadunationThreadGroup; 50 import com.rift.coad.lib.deployment.JMXBeanInfo; 51 import com.rift.coad.lib.configuration.Configuration; 52 import com.rift.coad.lib.configuration.ConfigurationFactory; 53 import com.rift.coad.lib.naming.ContextManager; 54 import com.rift.coad.lib.bean.BeanWrapper; 55 56 62 public class JMXBeanManager { 63 64 67 public class BeanList { 68 private final static String DEFAULT_PATH = "java:comp/env/jmx"; 70 private final static String JMX_CTX = "jmxctx"; 71 72 private Map objectList = null; 74 private Map bindList = null; 75 private Configuration config = null; 76 private Context context = null; 77 private ContextManager contextManager = null; 78 79 80 85 public BeanList() throws JMXException { 86 objectList = new HashMap (); 87 bindList = new HashMap (); 88 try { 89 config = ConfigurationFactory.getInstance(). 90 getConfig(this.getClass()); 91 context = new InitialContext (); 92 contextManager = new ContextManager( 93 config.getString(JMX_CTX,DEFAULT_PATH)); 94 } catch (Exception ex) { 95 throw new JMXException("Failed to instanciate the BeanList : " 96 + ex.getMessage(),ex); 97 } 98 } 99 100 101 107 public synchronized void addBean(String objectName, Object bean) throws 108 JMXException{ 109 BeanWrapper wrapper = (BeanWrapper)bean; 110 try { 111 if (wrapper.getTie() != null) { 112 log.info("Adding the [" + wrapper.getBindName() 113 + "] to the portable remote object."); 114 javax.rmi.PortableRemoteObject.exportObject(wrapper.getTie()); 115 context.bind(wrapper.getBindName(),wrapper.getTie()); 116 } 117 contextManager.bind(wrapper.getBindName(),wrapper.getProxy()); 119 120 objectList.put(objectName,bean); 122 bindList.put(wrapper.getBindName(),bean); 123 124 } catch (Exception ex) { 125 log.error("Failed to add the bean [" + objectName + "][" + 126 wrapper.getBindName() + "]",ex); 127 throw new JMXException( 128 "Failed to add the jmx bean to the list : " + 129 ex.getMessage(),ex); 130 } 131 } 132 133 134 139 public synchronized void removeBean(String objectName) throws 140 JMXException{ 141 try { 142 BeanWrapper wrapper = (BeanWrapper)objectList.get(objectName); 143 if (wrapper != null) { 144 bindList.remove(wrapper.getBindName()); 147 if (wrapper.getTie() != null) { 148 try{ 149 log.info("Unbinding an object [" + objectName 150 + "] from the portable object"); 151 context.unbind(wrapper.getBindName()); 152 javax.rmi.PortableRemoteObject.unexportObject( 153 wrapper.getTie()); 154 } catch (org.omg.CORBA.BAD_INV_ORDER ex) { 155 log.info("Failed to unbind the object [" + objectName 156 + "] as it is not bound.",ex); 157 } 158 } 159 } 160 objectList.remove(objectName); 161 } catch (Exception ex) { 162 throw new JMXException( 163 "Failed to remove the jmx bean from the list : " + 164 ex.getMessage(),ex); 165 } 166 } 167 168 169 174 public synchronized Set getObjectKeys() { 175 return objectList.keySet(); 176 } 177 178 179 187 public synchronized Object getObject(String key) { 188 return objectList.get(key); 189 } 190 191 192 199 public synchronized boolean containsObject(String key) { 200 return objectList.containsKey(key); 201 } 202 203 204 209 public synchronized Set getBindKeys() { 210 return bindList.keySet(); 211 } 212 213 214 220 public synchronized Object getBindObject(String key) { 221 return bindList.get(key); 222 } 223 224 225 231 public synchronized boolean containsBindObject(String key) { 232 return bindList.containsKey(key); 233 } 234 } 235 236 private final static String USERNAME = "jmxbean_user"; 238 239 protected Logger log = 241 Logger.getLogger(JMXBeanManager.class.getName()); 242 243 244 private final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 246 private ThreadsPermissionContainer permissions = null; 247 private Map loaders = null; 248 private BeanList beanList = null; 249 private CoadunationThreadGroup threadGroup = null; 250 private String username = null; 251 252 259 public JMXBeanManager( 260 ThreadsPermissionContainer permissions, 261 CoadunationThreadGroup threadGroup) throws Exception { 262 this.permissions = permissions; 263 loaders = new HashMap (); 264 beanList = new BeanList(); 265 this.threadGroup = threadGroup.createThreadGroup(); 266 try { 267 Configuration config = ConfigurationFactory.getInstance(). 268 getConfig(JMXBeanManager.class); 269 username = config.getString(USERNAME); 270 } catch (Exception ex) { 271 throw new JMXException("Failed to start the JMX Bean manager [" + 272 ex.getMessage() + "]",ex); 273 } 274 } 275 276 277 285 public void load(DeploymentLoader loader) throws JMXException, Exception { 286 if (loaders.containsKey(loader)) { 287 throw new JMXException("This entries has been loaded before."); 288 } 289 290 beanClash(loader.getDeploymentInfo().getJmxBeans()); 292 293 JMXBeanLoader jmxBeanLoader = new JMXBeanLoader(mbs,loader,permissions, 295 threadGroup); 296 jmxBeanLoader.setContextClassLoader(loader.getClassLoader()); 297 threadGroup.addThread(jmxBeanLoader,username); 298 jmxBeanLoader.start(); 299 jmxBeanLoader.join(); 300 if (!jmxBeanLoader.wasSucessfull()) { 301 throw jmxBeanLoader.getException(); 302 } 303 304 Map beans = jmxBeanLoader.getBeans(); 306 for (Iterator iter = beans.keySet().iterator(); iter.hasNext();) { 307 String key = (String )iter.next(); 308 log.info("Load bean [" + key + "]"); 309 beanList.addBean(key,beans.get(key)); 310 } 311 312 loaders.put(loader,jmxBeanLoader); 314 } 315 316 317 323 public void unLoad(DeploymentLoader loader) throws JMXException { 324 if (false == loaders.containsKey(loader)) { 325 return; 327 } 328 JMXBeanLoader jmxBeanLoader = (JMXBeanLoader)loaders.get(loader); 330 Map beans = jmxBeanLoader.getBeans(); 331 for (Iterator iter = beans.keySet().iterator(); iter.hasNext();) { 332 String key = (String )iter.next(); 333 log.info("Unload bean [" + key + "]"); 334 beanList.removeBean(key); 335 } 336 337 jmxBeanLoader.unRegisterBeans(); 339 jmxBeanLoader.stopThreads(); 340 loaders.remove(loader); 341 } 342 343 344 349 public Set getObjectKeys() { 350 return beanList.getObjectKeys(); 351 } 352 353 354 361 public Object getObject(String key) { 362 return beanList.getObject(key); 363 } 364 365 366 371 public Set getBindKeys() { 372 return beanList.getBindKeys(); 373 } 374 375 376 383 public Object getBindObject(String key) { 384 return beanList.getBindObject(key); 385 } 386 387 388 394 private void beanClash(Map beans) throws JMXException { 395 for (Iterator iter = beans.keySet().iterator(); iter.hasNext();) { 396 JMXBeanInfo jmxBeanInfo = (JMXBeanInfo)beans.get(iter.next()); 397 if (beanList.containsObject(jmxBeanInfo.getObjectName())) { 398 throw new JMXException("The object with the name [" + 399 jmxBeanInfo.getObjectName() + "] is already bound"); 400 } 401 if (beanList.containsBindObject(jmxBeanInfo.getBindName())) { 402 throw new JMXException("The object with the bind name [" + 403 jmxBeanInfo.getBindName() + "] is already bound"); 404 } 405 } 406 } 407 408 409 } 410 | Popular Tags |