KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > deployment > jmxbean > JMXBeanLoader


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * XMLConfigurationException.java
20  *
21  * JMXBeanLoader.java
22  *
23  * This object is responsible for loading the JMX Bean into memory from the
24  * deployment loader passed to it.
25  */

26
27 package com.rift.coad.lib.deployment.jmxbean;
28
29 // import
30
import javax.management.ObjectName JavaDoc;
31 import javax.management.MBeanServer JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.HashMap JavaDoc;
34 import java.util.Iterator JavaDoc;
35 import java.util.List JavaDoc;
36
37 // logging import
38
import org.apache.log4j.Logger;
39
40 // coad imports
41
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 /**
52  * The JMX Bean loader is responsibe for loading the beans contained in a
53  * Coadunation jar file.
54  *
55  * @author Brett Chaldecott
56  */

57 public class JMXBeanLoader extends BasicThread {
58     
59     // the class log variable
60
protected Logger log =
61         Logger.getLogger(JMXBeanLoader.class.getName());
62     
63     // The classes memember variables
64
private MBeanServer JavaDoc beanServer = null;
65     private DeploymentLoader deploymentLoader = null;
66     private ThreadsPermissionContainer permissions = null;
67     private Map JavaDoc beans = null;
68     private CoadunationThreadGroup threadGroup = null;
69     private boolean successful = true;
70     private Exception JavaDoc exception = null;
71     
72     /**
73      * Creates a new instance of JMXBeanLoader loader.
74      *
75      * @param beanServer The reference to the bean server.
76      * @param deploymentLoader The reference to the deployment loader.
77      * @param permissions The threads permissions container.
78      * @param threadGroup The reference to the thread group.
79      * @exception JMXException
80      * @exception Exception
81      */

82     public JMXBeanLoader(MBeanServer JavaDoc beanServer,
83             DeploymentLoader deploymentLoader,
84             ThreadsPermissionContainer permissions,
85             CoadunationThreadGroup threadGroup) throws JMXException, Exception JavaDoc {
86         this.beanServer = beanServer;
87         this.deploymentLoader = deploymentLoader;
88         this.permissions = permissions;
89         this.threadGroup = threadGroup.createThreadGroup();
90         beans = new HashMap JavaDoc();
91         
92     }
93     
94     
95     /**
96      * The finalize method for the jmx bean loader.
97      */

98     protected void finalize() {
99         stopThreads();
100     }
101     
102     
103     /**
104      * The method that process the request
105      */

106     public void process() {
107         try {
108             loadBeans();
109             registerBeans();
110         } catch (Exception JavaDoc 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     /**
119      * Load the beans into memory.
120      *
121      * @exception JMXException
122      */

123     private void loadBeans() throws JMXException {
124         try {
125             log.info("Load the JMX Beans for [" +
126                     deploymentLoader.getFile().getPath() + "]");
127             Map JavaDoc beanList = deploymentLoader.getDeploymentInfo().getJmxBeans();
128             for (Iterator JavaDoc 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                 // loop through the bean thread information
142
List JavaDoc 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 JavaDoc ex) {
153             stopThreads();
154             throw new JMXException("Failed to load the beans : " +
155                     ex.getMessage(),ex);
156         }
157     }
158     
159     
160     /**
161      * The operator method responsible for registering the beans with the JMX
162      * bean server.
163      *
164      * @exception JMXException
165      */

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 JavaDoc iter = beans.keySet().iterator(); iter.hasNext();) {
174                 String JavaDoc objectName = (String JavaDoc)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 JavaDoc(objectName));
180             }
181         } catch (Exception JavaDoc ex) {
182             throw new JMXException("Failed to register the beans : " +
183                     ex.getMessage(),ex);
184         }
185     }
186     
187     
188     /**
189      * This method is called to un-register beans from the bean server.
190      */

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 JavaDoc iter = beans.keySet().iterator(); iter.hasNext();) {
199                 String JavaDoc objectName = (String JavaDoc)iter.next();
200                 log.info("Un-register the jmx bean [" +
201                          objectName + "]");
202                 beanServer.unregisterMBean(
203                         new ObjectName JavaDoc(objectName));
204             }
205         } catch (Exception JavaDoc ex) {
206             throw new JMXException("Failed to un-register the beans : " +
207                     ex.getMessage(),ex);
208         }
209     }
210     
211     
212     
213     /**
214      * This method stops the bean processing
215      */

216     public void stopThreads() {
217         try {
218             if (threadGroup.isTerminated() == false) {
219                 threadGroup.terminate();
220             }
221         } catch(Exception JavaDoc ex) {
222             log.error("Failed to unload the beans [" +
223                     deploymentLoader.getFile().getPath() + "] because : " +
224                     ex.getMessage(),ex);
225         }
226     }
227     
228     /**
229      * This method returns the loaded beans.
230      *
231      * @return The list of loaded beans.
232      */

233     public Map JavaDoc getBeans() {
234         return beans;
235     }
236     
237     
238     /**
239      * This method returns true if the loading was successfull and false
240      * otherwise.
241      */

242     public boolean wasSucessfull() {
243         return successful;
244     }
245     
246     
247     /**
248      * The exception that caused the problem
249      */

250     public Exception JavaDoc getException() {
251         return exception;
252     }
253
254 }
255
Popular Tags