001: /*
002: * CoadunationLib: The coaduntion implementation library.
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * XMLConfigurationException.java
020: *
021: * JMXBeanLoader.java
022: *
023: * This object is responsible for loading the JMX Bean into memory from the
024: * deployment loader passed to it.
025: */
026:
027: package com.rift.coad.lib.deployment.jmxbean;
028:
029: // import
030: import javax.management.ObjectName;
031: import javax.management.MBeanServer;
032: import java.util.Map;
033: import java.util.HashMap;
034: import java.util.Iterator;
035: import java.util.List;
036:
037: // logging import
038: import org.apache.log4j.Logger;
039:
040: // coad imports
041: import com.rift.coad.lib.bean.BeanWrapper;
042: import com.rift.coad.lib.bean.BeanRunnable;
043: import com.rift.coad.lib.bean.BeanThread;
044: import com.rift.coad.lib.security.ThreadsPermissionContainer;
045: import com.rift.coad.lib.deployment.DeploymentLoader;
046: import com.rift.coad.lib.deployment.JMXBeanInfo;
047: import com.rift.coad.lib.deployment.DeploymentThreadInfo;
048: import com.rift.coad.lib.thread.CoadunationThreadGroup;
049: import com.rift.coad.lib.thread.BasicThread;
050:
051: /**
052: * The JMX Bean loader is responsibe for loading the beans contained in a
053: * Coadunation jar file.
054: *
055: * @author Brett Chaldecott
056: */
057: public class JMXBeanLoader extends BasicThread {
058:
059: // the class log variable
060: protected Logger log = Logger.getLogger(JMXBeanLoader.class
061: .getName());
062:
063: // The classes memember variables
064: private MBeanServer beanServer = null;
065: private DeploymentLoader deploymentLoader = null;
066: private ThreadsPermissionContainer permissions = null;
067: private Map beans = null;
068: private CoadunationThreadGroup threadGroup = null;
069: private boolean successful = true;
070: private Exception exception = null;
071:
072: /**
073: * Creates a new instance of JMXBeanLoader loader.
074: *
075: * @param beanServer The reference to the bean server.
076: * @param deploymentLoader The reference to the deployment loader.
077: * @param permissions The threads permissions container.
078: * @param threadGroup The reference to the thread group.
079: * @exception JMXException
080: * @exception Exception
081: */
082: public JMXBeanLoader(MBeanServer beanServer,
083: DeploymentLoader deploymentLoader,
084: ThreadsPermissionContainer permissions,
085: CoadunationThreadGroup threadGroup) throws JMXException,
086: Exception {
087: this .beanServer = beanServer;
088: this .deploymentLoader = deploymentLoader;
089: this .permissions = permissions;
090: this .threadGroup = threadGroup.createThreadGroup();
091: beans = new HashMap();
092:
093: }
094:
095: /**
096: * The finalize method for the jmx bean loader.
097: */
098: protected void finalize() {
099: stopThreads();
100: }
101:
102: /**
103: * The method that process the request
104: */
105: public void process() {
106: try {
107: loadBeans();
108: registerBeans();
109: } catch (Exception ex) {
110: log.error("Failed to load the JMX Beans because : "
111: + ex.getMessage(), ex);
112: successful = false;
113: exception = ex;
114: }
115: }
116:
117: /**
118: * Load the beans into memory.
119: *
120: * @exception JMXException
121: */
122: private void loadBeans() throws JMXException {
123: try {
124: log.info("Load the JMX Beans for ["
125: + deploymentLoader.getFile().getPath() + "]");
126: Map beanList = deploymentLoader.getDeploymentInfo()
127: .getJmxBeans();
128: for (Iterator iter = beanList.keySet().iterator(); iter
129: .hasNext();) {
130: JMXBeanInfo jmxBeanInfo = (JMXBeanInfo) beanList
131: .get(iter.next());
132: BeanWrapper beanWrapper = new BeanWrapper(
133: deploymentLoader, jmxBeanInfo, permissions);
134: beans.put(jmxBeanInfo.getObjectName(), beanWrapper);
135: if (beanWrapper.getSubObject() instanceof BeanRunnable) {
136: BeanThread beanThread = new BeanThread(
137: (BeanRunnable) beanWrapper.getSubObject());
138: threadGroup.addThread(beanThread, jmxBeanInfo
139: .getUsername());
140: beanThread.start();
141: }
142:
143: // loop through the bean thread information
144: List threadList = jmxBeanInfo.getThreadInfoList();
145: for (int index = 0; index < threadList.size(); index++) {
146: DeploymentThreadInfo threadInfo = (DeploymentThreadInfo) threadList
147: .get(index);
148: threadGroup.startThreads(deploymentLoader
149: .getClass(threadInfo.getClassName()),
150: threadInfo.getUsername(), (int) threadInfo
151: .getThreadNumber());
152: }
153: }
154: } catch (Exception ex) {
155: stopThreads();
156: throw new JMXException("Failed to load the beans : "
157: + ex.getMessage(), ex);
158: }
159: }
160:
161: /**
162: * The operator method responsible for registering the beans with the JMX
163: * bean server.
164: *
165: * @exception JMXException
166: */
167: private void registerBeans() throws JMXException {
168: try {
169: if (log.isDebugEnabled()) {
170: log.debug("Register the Beans for ["
171: + deploymentLoader.getFile().getPath() + "]");
172: }
173:
174: for (Iterator iter = beans.keySet().iterator(); iter
175: .hasNext();) {
176: String objectName = (String) iter.next();
177: log.info("Register the jmx bean [" + objectName + "]");
178: BeanWrapper beanWrapper = (BeanWrapper) beans
179: .get(objectName);
180: beanServer.registerMBean(beanWrapper.getSubObject(),
181: new ObjectName(objectName));
182: }
183: } catch (Exception ex) {
184: throw new JMXException("Failed to register the beans : "
185: + ex.getMessage(), ex);
186: }
187: }
188:
189: /**
190: * This method is called to un-register beans from the bean server.
191: */
192: public void unRegisterBeans() throws JMXException {
193: try {
194: if (log.isDebugEnabled()) {
195: log.debug("Un-register the Beans for ["
196: + deploymentLoader.getFile().getPath() + "]");
197: }
198:
199: for (Iterator iter = beans.keySet().iterator(); iter
200: .hasNext();) {
201: String objectName = (String) iter.next();
202: log.info("Un-register the jmx bean [" + objectName
203: + "]");
204: beanServer.unregisterMBean(new ObjectName(objectName));
205: }
206: } catch (Exception ex) {
207: throw new JMXException("Failed to un-register the beans : "
208: + ex.getMessage(), ex);
209: }
210: }
211:
212: /**
213: * This method stops the bean processing
214: */
215: public void stopThreads() {
216: try {
217: if (threadGroup.isTerminated() == false) {
218: threadGroup.terminate();
219: }
220: } catch (Exception ex) {
221: log.error("Failed to unload the beans ["
222: + deploymentLoader.getFile().getPath()
223: + "] because : " + ex.getMessage(), ex);
224: }
225: }
226:
227: /**
228: * This method returns the loaded beans.
229: *
230: * @return The list of loaded beans.
231: */
232: public Map getBeans() {
233: return beans;
234: }
235:
236: /**
237: * This method returns true if the loading was successfull and false
238: * otherwise.
239: */
240: public boolean wasSucessfull() {
241: return successful;
242: }
243:
244: /**
245: * The exception that caused the problem
246: */
247: public Exception getException() {
248: return exception;
249: }
250:
251: }
|