1 26 27 package com.rift.coad.lib.deployment.jmxbean; 28 29 import javax.management.ObjectName ; 31 import javax.management.MBeanServer ; 32 import java.util.Map ; 33 import java.util.HashMap ; 34 import java.util.Iterator ; 35 import java.util.List ; 36 37 import org.apache.log4j.Logger; 39 40 import com.rift.coad.lib.bean.BeanWrapper; 42 import com.rift.coad.lib.bean.BeanRunnable; 43 import com.rift.coad.lib.bean.BeanThread; 44 import com.rift.coad.lib.security.ThreadsPermissionContainer; 45 import com.rift.coad.lib.deployment.DeploymentLoader; 46 import com.rift.coad.lib.deployment.JMXBeanInfo; 47 import com.rift.coad.lib.deployment.DeploymentThreadInfo; 48 import com.rift.coad.lib.thread.CoadunationThreadGroup; 49 import com.rift.coad.lib.thread.BasicThread; 50 51 57 public class JMXBeanLoader extends BasicThread { 58 59 protected Logger log = 61 Logger.getLogger(JMXBeanLoader.class.getName()); 62 63 private MBeanServer beanServer = null; 65 private DeploymentLoader deploymentLoader = null; 66 private ThreadsPermissionContainer permissions = null; 67 private Map beans = null; 68 private CoadunationThreadGroup threadGroup = null; 69 private boolean successful = true; 70 private Exception exception = null; 71 72 82 public JMXBeanLoader(MBeanServer beanServer, 83 DeploymentLoader deploymentLoader, 84 ThreadsPermissionContainer permissions, 85 CoadunationThreadGroup threadGroup) throws JMXException, Exception { 86 this.beanServer = beanServer; 87 this.deploymentLoader = deploymentLoader; 88 this.permissions = permissions; 89 this.threadGroup = threadGroup.createThreadGroup(); 90 beans = new HashMap (); 91 92 } 93 94 95 98 protected void finalize() { 99 stopThreads(); 100 } 101 102 103 106 public void process() { 107 try { 108 loadBeans(); 109 registerBeans(); 110 } catch (Exception ex) { 111 log.error("Failed to load the JMX Beans because : " + 112 ex.getMessage(),ex); 113 successful = false; 114 exception = ex; 115 } 116 } 117 118 123 private void loadBeans() throws JMXException { 124 try { 125 log.info("Load the JMX Beans for [" + 126 deploymentLoader.getFile().getPath() + "]"); 127 Map beanList = deploymentLoader.getDeploymentInfo().getJmxBeans(); 128 for (Iterator iter = beanList.keySet().iterator(); iter.hasNext();) { 129 JMXBeanInfo jmxBeanInfo = (JMXBeanInfo)beanList.get(iter.next()); 130 BeanWrapper beanWrapper = 131 new BeanWrapper(deploymentLoader, jmxBeanInfo, 132 permissions); 133 beans.put(jmxBeanInfo.getObjectName(),beanWrapper); 134 if (beanWrapper.getSubObject() instanceof BeanRunnable) { 135 BeanThread beanThread = new BeanThread( 136 (BeanRunnable)beanWrapper.getSubObject()); 137 threadGroup.addThread(beanThread,jmxBeanInfo.getUsername()); 138 beanThread.start(); 139 } 140 141 List threadList = jmxBeanInfo.getThreadInfoList(); 143 for (int index = 0; index < threadList.size(); index++) { 144 DeploymentThreadInfo threadInfo = 145 (DeploymentThreadInfo)threadList.get(index); 146 threadGroup.startThreads( 147 deploymentLoader.getClass(threadInfo.getClassName()) 148 ,threadInfo.getUsername(), 149 (int)threadInfo.getThreadNumber()); 150 } 151 } 152 } catch (Exception ex) { 153 stopThreads(); 154 throw new JMXException("Failed to load the beans : " + 155 ex.getMessage(),ex); 156 } 157 } 158 159 160 166 private void registerBeans() throws JMXException { 167 try { 168 if (log.isDebugEnabled()) { 169 log.debug("Register the Beans for [" + 170 deploymentLoader.getFile().getPath() + "]"); 171 } 172 173 for (Iterator iter = beans.keySet().iterator(); iter.hasNext();) { 174 String objectName = (String )iter.next(); 175 log.info("Register the jmx bean [" + 176 objectName + "]"); 177 BeanWrapper beanWrapper = (BeanWrapper)beans.get(objectName); 178 beanServer.registerMBean(beanWrapper.getSubObject(), 179 new ObjectName (objectName)); 180 } 181 } catch (Exception ex) { 182 throw new JMXException("Failed to register the beans : " + 183 ex.getMessage(),ex); 184 } 185 } 186 187 188 191 public void unRegisterBeans() throws JMXException { 192 try { 193 if (log.isDebugEnabled()) { 194 log.debug("Un-register the Beans for [" + 195 deploymentLoader.getFile().getPath() + "]"); 196 } 197 198 for (Iterator iter = beans.keySet().iterator(); iter.hasNext();) { 199 String objectName = (String )iter.next(); 200 log.info("Un-register the jmx bean [" + 201 objectName + "]"); 202 beanServer.unregisterMBean( 203 new ObjectName (objectName)); 204 } 205 } catch (Exception ex) { 206 throw new JMXException("Failed to un-register the beans : " + 207 ex.getMessage(),ex); 208 } 209 } 210 211 212 213 216 public void stopThreads() { 217 try { 218 if (threadGroup.isTerminated() == false) { 219 threadGroup.terminate(); 220 } 221 } catch(Exception ex) { 222 log.error("Failed to unload the beans [" + 223 deploymentLoader.getFile().getPath() + "] because : " + 224 ex.getMessage(),ex); 225 } 226 } 227 228 233 public Map getBeans() { 234 return beans; 235 } 236 237 238 242 public boolean wasSucessfull() { 243 return successful; 244 } 245 246 247 250 public Exception getException() { 251 return exception; 252 } 253 254 } 255 | Popular Tags |