KickJava   Java API By Example, From Geeks To Geeks.

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


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  * JMXBeanManager.java
22  *
23  * The JMX Bean manager is responsible for loading and unloading of jmx beans.
24  * It is controlled by the DeploymentManager.
25  */

26
27 package com.rift.coad.lib.deployment.jmxbean;
28
29 // java imports
30
import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Set JavaDoc;
34 import javax.management.MBeanServer JavaDoc;
35 import java.lang.management.ManagementFactory JavaDoc;
36 import java.rmi.Remote JavaDoc;
37 import javax.naming.Context JavaDoc;
38 import javax.naming.InitialContext JavaDoc;
39 import javax.naming.Name JavaDoc;
40 import javax.rmi.PortableRemoteObject JavaDoc;
41
42 // logging import
43
import org.apache.log4j.Logger;
44
45 // coadunation imports
46
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 /**
57  * The JMX Bean manager is responsible for loading and unloading of jmx beans.
58  * It is controlled by the DeploymentManager.
59  *
60  * @author Brett Chaldecott
61  */

62 public class JMXBeanManager {
63     
64     /**
65      * This object maintains the beans in memory.
66      */

67     public class BeanList {
68         // class constants
69
private final static String JavaDoc DEFAULT_PATH = "java:comp/env/jmx";
70         private final static String JavaDoc JMX_CTX = "jmxctx";
71         
72         // the list of beans
73
private Map JavaDoc objectList = null;
74         private Map JavaDoc bindList = null;
75         private Configuration config = null;
76         private Context JavaDoc context = null;
77         private ContextManager contextManager = null;
78         
79         
80         /**
81          * The default constructor of the bean list.
82          *
83          * @exception JMXException
84          */

85         public BeanList() throws JMXException {
86             objectList = new HashMap JavaDoc();
87             bindList = new HashMap JavaDoc();
88             try {
89                 config = ConfigurationFactory.getInstance().
90                         getConfig(this.getClass());
91                 context = new InitialContext JavaDoc();
92                 contextManager = new ContextManager(
93                         config.getString(JMX_CTX,DEFAULT_PATH));
94             } catch (Exception JavaDoc ex) {
95                 throw new JMXException("Failed to instanciate the BeanList : "
96                         + ex.getMessage(),ex);
97             }
98         }
99         
100         
101         /**
102          * This method will add the jmx bean to the object and bind list.
103          *
104          * @param objectName The object name to use in the object list.
105          * @param bean The reference to the bean to add.
106          */

107         public synchronized void addBean(String JavaDoc objectName, Object JavaDoc 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                 // Add to local context
118
contextManager.bind(wrapper.getBindName(),wrapper.getProxy());
119                 
120                 // add to object lists
121
objectList.put(objectName,bean);
122                 bindList.put(wrapper.getBindName(),bean);
123                 
124             } catch (Exception JavaDoc 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         /**
135          * This method removed the bean from the lists.
136          *
137          * @param objectName The name of the object to remove from the list.
138          */

139         public synchronized void removeBean(String JavaDoc objectName) throws
140                 JMXException{
141             try {
142                 BeanWrapper wrapper = (BeanWrapper)objectList.get(objectName);
143                 if (wrapper != null) {
144                     // we do not need to remove the local reference as
145
// this gets removed when the context gets released
146
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 JavaDoc 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 JavaDoc ex) {
162                 throw new JMXException(
163                         "Failed to remove the jmx bean from the list : " +
164                         ex.getMessage(),ex);
165             }
166         }
167         
168         
169         /**
170          * This method returns the list of keys identifying the objects.
171          *
172          * @return The keys identifying the objects in the system.
173          */

174         public synchronized Set JavaDoc getObjectKeys() {
175             return objectList.keySet();
176         }
177         
178         
179         /**
180          * This method returns the object reference identified by the JMX object
181          * name.
182          *
183          * @return The object identified by the JMX object name, or null if it
184          * is not found.
185          * @param key The key identifying the object.
186          */

187         public synchronized Object JavaDoc getObject(String JavaDoc key) {
188             return objectList.get(key);
189         }
190         
191         
192         /**
193          * This method returns true if the object list contains the specified
194          * key.
195          *
196          * @return TRUE if the key is found FALSE if not.
197          * @param key The key identifying the object.
198          */

199         public synchronized boolean containsObject(String JavaDoc key) {
200             return objectList.containsKey(key);
201         }
202         
203         
204         /**
205          * This method returns the list of bind keys.
206          *
207          * @return The key set containing the bind keys.
208          */

209         public synchronized Set JavaDoc getBindKeys() {
210             return bindList.keySet();
211         }
212         
213         
214         /**
215          * Thist method returns the bind information.
216          *
217          * @return The bind information.
218          * @param key The bind key identifying the object.
219          */

220         public synchronized Object JavaDoc getBindObject(String JavaDoc key) {
221             return bindList.get(key);
222         }
223         
224         
225         /**
226          * This method returns true if the list contains the bind object.
227          *
228          * @return TRUE if found FALSE if not.
229          * @param key The key to find.
230          */

231         public synchronized boolean containsBindObject(String JavaDoc key) {
232             return bindList.containsKey(key);
233         }
234     }
235     
236     // class constants
237
private final static String JavaDoc USERNAME = "jmxbean_user";
238     
239     // the class log variable
240
protected Logger log =
241             Logger.getLogger(JMXBeanManager.class.getName());
242     
243     
244     // class member variables
245
private final MBeanServer JavaDoc mbs = ManagementFactory.getPlatformMBeanServer();
246     private ThreadsPermissionContainer permissions = null;
247     private Map JavaDoc loaders = null;
248     private BeanList beanList = null;
249     private CoadunationThreadGroup threadGroup = null;
250     private String JavaDoc username = null;
251     
252     /**
253      * Creates a new instance of JMXBeanManager
254      *
255      * @param permissions The reference thread permissions.
256      * @param threadGroup The group of threads.
257      * @exception Exception
258      */

259     public JMXBeanManager(
260             ThreadsPermissionContainer permissions,
261             CoadunationThreadGroup threadGroup) throws Exception JavaDoc {
262         this.permissions = permissions;
263         loaders = new HashMap JavaDoc();
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 JavaDoc ex) {
271             throw new JMXException("Failed to start the JMX Bean manager [" +
272                     ex.getMessage() + "]",ex);
273         }
274     }
275     
276     
277     /**
278      * This method loads the JMX bean from the deployment file using the jmx
279      * bean deployment loader.
280      *
281      * @param loader The reference to the loader object.
282      * @exception JMXException
283      * @exception Exception
284      */

285     public void load(DeploymentLoader loader) throws JMXException, Exception JavaDoc {
286         if (loaders.containsKey(loader)) {
287             throw new JMXException("This entries has been loaded before.");
288         }
289         
290         // check for clashes
291
beanClash(loader.getDeploymentInfo().getJmxBeans());
292         
293         // load the beans
294
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         // add the beans to the bean list
305
Map JavaDoc beans = jmxBeanLoader.getBeans();
306         for (Iterator JavaDoc iter = beans.keySet().iterator(); iter.hasNext();) {
307             String JavaDoc key = (String JavaDoc)iter.next();
308             log.info("Load bean [" + key + "]");
309             beanList.addBean(key,beans.get(key));
310         }
311         
312         // add the beans
313
loaders.put(loader,jmxBeanLoader);
314     }
315     
316     
317     /**
318      * This method unloads the deployment loader.
319      *
320      * @param loader The loader to unload.
321      * @exception JMXException
322      */

323     public void unLoad(DeploymentLoader loader) throws JMXException {
324         if (false == loaders.containsKey(loader)) {
325             // do nothing there is nothing known about this entry
326
return;
327         }
328         // remove all the bean references.
329
JMXBeanLoader jmxBeanLoader = (JMXBeanLoader)loaders.get(loader);
330         Map JavaDoc beans = jmxBeanLoader.getBeans();
331         for (Iterator JavaDoc iter = beans.keySet().iterator(); iter.hasNext();) {
332             String JavaDoc key = (String JavaDoc)iter.next();
333             log.info("Unload bean [" + key + "]");
334             beanList.removeBean(key);
335         }
336         
337         // remove the bean
338
jmxBeanLoader.unRegisterBeans();
339         jmxBeanLoader.stopThreads();
340         loaders.remove(loader);
341     }
342     
343     
344     /**
345      * Retrieve the objects keys. These are the standard lookup keys.
346      *
347      * @return The list of beans managed by this object.
348      */

349     public Set JavaDoc getObjectKeys() {
350         return beanList.getObjectKeys();
351     }
352     
353     
354     /**
355      * This method will return the reference to the wrapper object using the
356      * bind key identifier.
357      *
358      * @return The object identified by the bind key.
359      * @param key The bind key to retrieve the object for.
360      */

361     public Object JavaDoc getObject(String JavaDoc key) {
362         return beanList.getObject(key);
363     }
364     
365     
366     /**
367      * Retrieve the bind keys. These are the standard lookup keys.
368      *
369      * @return The list of beans managed by this object.
370      */

371     public Set JavaDoc getBindKeys() {
372         return beanList.getBindKeys();
373     }
374     
375     
376     /**
377      * This method will return the reference to the wrapper object using the
378      * bind key identifier.
379      *
380      * @return The object identified by the bind key.
381      * @param key The bind key to retrieve the object for.
382      */

383     public Object JavaDoc getBindObject(String JavaDoc key) {
384         return beanList.getBindObject(key);
385     }
386     
387     
388     /**
389      * This method checks to see if there are any clashing beans.
390      *
391      * @param beans The list of beans to perform the check on.
392      * @exception JMXException
393      */

394     private void beanClash(Map JavaDoc beans) throws JMXException {
395         for (Iterator JavaDoc 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