KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > core > cmc > MantaJMXManagement


1 package org.mr.core.cmc;
2
3  /*
4  * Copyright 2002 by
5  * <a HREF="http://www.coridan.com">Coridan</a>
6  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
7  *
8  * The contents of this file are subject to the Mozilla Public License Version
9  * 1.1 (the "License"); you may not use this file except in compliance with the
10  * License. You may obtain a copy of the License at
11  * http://www.mozilla.org/MPL/
12  *
13  * Software distributed under the License is distributed on an "AS IS" basis,
14  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
15  * for the specific language governing rights and limitations under the
16  * License.
17  *
18  * The Original Code is "MantaRay" (TM).
19  *
20  * The Initial Developer of the Original Code is lital kasif.
21  * Portions created by the Initial Developer are Copyright (C) 2006
22  * Coridan Inc. All Rights Reserved.
23  *
24  * Contributor(s): all the names of the contributors are added in the source
25  * code where applicable.
26  *
27  * Alternatively, the contents of this file may be used under the terms of the
28  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
29  * provisions of LGPL are applicable instead of those above. If you wish to
30  * allow use of your version of this file only under the terms of the LGPL
31  * License and not to allow others to use your version of this file under
32  * the MPL, indicate your decision by deleting the provisions above and
33  * replace them with the notice and other provisions required by the LGPL.
34  * If you do not delete the provisions above, a recipient may use your version
35  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
36  
37  *
38  * This library is free software; you can redistribute it and/or modify it
39  * under the terms of the MPL as stated above or under the terms of the GNU
40  * Lesser General Public License as published by the Free Software Foundation;
41  * either version 2.1 of the License, or any later version.
42  *
43  * This library is distributed in the hope that it will be useful, but WITHOUT
44  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
45  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
46  * License for more details.
47  *
48  * Created on Sep 8, 2004
49  */

50
51 import com.sun.jdmk.comm.HtmlAdaptorServer;
52 import org.apache.commons.logging.Log;
53 import org.apache.commons.logging.LogFactory;
54 import org.mr.MantaAgent;
55 import org.mr.MantaException;
56 import org.mr.core.net.cmc.GetConnectionsJMX;
57 import org.mr.core.stats.cmc.GetStatsJMX;
58 import org.mr.kernel.world.cmc.GetServiceParticipationJMX;
59 import org.mr.kernel.world.cmc.GetWorldModelerVersionJMX;
60 import org.mr.plugins.discovery.ADControlSender;
61
62 import javax.management.*;
63 import javax.management.remote.JMXConnectorServer JavaDoc;
64 import javax.management.remote.JMXConnectorServerFactory JavaDoc;
65 import javax.management.remote.JMXServiceURL JavaDoc;
66 import java.io.IOException JavaDoc;
67 import java.net.MalformedURLException JavaDoc;
68 import java.rmi.registry.LocateRegistry JavaDoc;
69 import java.util.Set JavaDoc;
70
71 /**
72  * This class manages the mantary as it creats a management server and enables to add,
73  * remove and get ManagedObjects from that server.
74  * the delegeted object underlying this object is the JMX implamentation of MX4J
75  *
76  * @author lital kasif
77  */

78 public class MantaJMXManagement {
79     private MBeanServer server;
80     private static MantaJMXManagement instance = null;
81     protected Log log;
82     //parameters for the RMI connection to the JMX
83
int namingPort;
84     ObjectName objectName;
85     String JavaDoc host = null;
86     ObjectInstance rmiInstance;
87     String JavaDoc jndiPath;
88     JMXServiceURL JavaDoc url;
89     JMXConnectorServer JavaDoc connectorServer;
90     //parameters for the HTTP connection to JMX
91
ObjectName serverName;
92     ObjectName processorName;
93     ObjectInstance httpInstance;
94     /*
95     * true if MantaJMXManagement has been instanced else false
96     */

97     private boolean started = false;
98     
99     /**
100      * This is the singleton entry point calling this method for the <u>first
101      * </u> time and return the instance existing the next times.
102      *
103      * @return the MantaJMXManagement object.
104      */

105     public static synchronized MantaJMXManagement getInstance() {
106         if (instance == null){
107             instance = new MantaJMXManagement();
108             instance.init();
109         }
110         return instance;
111     }//getInstance
112

113     /**
114      * initiate the management server
115      * when it finished to init the MantaJMXManagement.
116      */

117     
118     private void init() {
119         log=LogFactory.getLog("MantaJMXManagement");
120         boolean startHttpAdaptor= MantaAgent.getInstance().getSingletonRepository()
121             .getConfigManager().getBooleanProperty("management.jmx.httpAdaptor.enabled", false);
122         boolean startRMIConnector= MantaAgent.getInstance().getSingletonRepository()
123             .getConfigManager().getBooleanProperty("management.jmx.rmiConnector.enabled", false);
124         
125         
126         
127         if(startHttpAdaptor == true || startRMIConnector == true){
128             if(log.isInfoEnabled())
129                 log.info("Initializing the MantaJMXManagement component");
130             MantaJMXManagement mantaJMXManagement = getInstance();
131             mantaJMXManagement.server = MBeanServerFactory.createMBeanServer();
132             ObjectName objectName = null;
133             try {
134                 objectName = new ObjectName("JMImplementation:type=MBeanServerDelegate");
135             } catch (MalformedObjectNameException e) {
136                 if(log.isErrorEnabled()){
137                     log.error("Unknown object name 'JMImplementation:type=MBeanServerDelegate'",e);
138                     
139                 //throw new MantaException(e.getMessage(), MantaException.ID_INVALID_ARGUMENTS);
140
}
141             }
142             NotificationListener listener = new NotificationListener()
143             {
144                 public void handleNotification(Notification notification, Object JavaDoc handback)
145                 {
146                     if(log.isInfoEnabled())
147                     log.info(notification.toString());
148                 }
149             };
150             try {
151                 // The listener emits notifications about registration/unregistration of MBeans
152
mantaJMXManagement.server.addNotificationListener(objectName, listener, null, null);
153             } catch (InstanceNotFoundException e1) {
154                 if(log.isErrorEnabled()){
155                     log.error("addNotificationListener() had instance not found exception. ", e1);
156                     // throw new MantaException(e1.getMessage(), MantaException.ID_INVALID_ARGUMENTS);
157

158                 }
159             }
160             
161             
162         }
163     }//init()
164

165     public synchronized boolean startConnections() {
166         if (started) return true;
167         boolean startHttpAdaptor= MantaAgent.getInstance().getSingletonRepository()
168             .getConfigManager().getBooleanProperty("management.jmx.httpAdaptor.enabled", false);
169         boolean startRMIConnector= MantaAgent.getInstance().getSingletonRepository()
170             .getConfigManager().getBooleanProperty("management.jmx.rmiConnector.enabled", false);
171             try {
172                if(startHttpAdaptor)
173                 makeHttpAdaptor();
174                 if(startRMIConnector)
175                     makeRMIConnector();
176             } catch (MantaException e) {
177                 if (log.isErrorEnabled()){
178                     log.error("Had a problem creating an RMI connection or HTTP Adaptor: "+ e.getMessage());
179                 }
180                 return false;
181             }
182         started = true;
183         return true;
184     }//startConnections()
185

186     /**
187      * makes an rmi connector to the MantaJMXManagement on port 1099
188      * 'jmx.rmiPort' in the 'default_config' file, or if such does not exist in port 1099
189      * @throws MantaException if there is a problem creating the rmi connection
190      */

191     private void makeRMIConnector() throws MantaException{
192         //Register and start the rmiregistry MBean, needed by JSR 160 RMIConnectorServer
193
String JavaDoc host =null;
194         //gets the IP address of the host
195
host=ADControlSender.getValidLocalAddress();
196         try {
197             int namingPort= MantaAgent.getInstance().getSingletonRepository()
198                 .getConfigManager().getIntProperty("management.jmx.rmiConnector.rmiPort",1099);
199
200             jndiPath = "/jmxconnector";
201             //register the rmi port (rmiregistry)
202
LocateRegistry.createRegistry(namingPort);
203             // url = new JMXServiceURL("service:jmx:rmi://"+host+":"+ namingPort+"/jndi/rmi://"+host+":" + namingPort + jndiPath);
204
url = new JMXServiceURL JavaDoc("service:jmx:rmi:///jndi/rmi://"+host+":" + namingPort + jndiPath);
205
206             // Create and start the RMIConnectorServer
207
connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, null, server);
208             connectorServer.start();
209           
210             if (log.isInfoEnabled())
211                 log.info("Set port value for the JMX-RMI server. The given port: " + namingPort);
212         } catch (MalformedURLException JavaDoc e) {
213                 if(log.isErrorEnabled())
214                 log.error("problem with the defined jmx.rmiPort. can't create an rmi connection to management system. ", e);
215                 throw new MantaException(e.getMessage()+" The jmx.rmiPort defined might already be in use. Its value can be changed in configuration files. ", MantaException.ID_INVALID_ARGUMENTS);
216         } catch (IOException JavaDoc e) {
217             if(log.isErrorEnabled())
218             log.error("IOEception. ", e);
219             throw new MantaException(e.getMessage(), MantaException.ID_RECEIVE_GENERAL_FAIL);
220         }
221 }// makeRMIConnector
222

223     /**
224      * makes an HTTP adaptor that enables to interface the ManagementJMX component
225      * on the port that is maches the value 'jmx.httpPort' in the 'default_config' file,
226      * or if such does not exist in port 8080
227      */

228     private void makeHttpAdaptor() throws MantaException {
229 // try {
230
int port = MantaAgent.getInstance().getSingletonRepository()
231                 .getConfigManager().getIntProperty("management.jmx.httpAdaptor.httpPort",8080);
232 // String host = MantaCoreComponent.getConfigManager().getStringProperty("jmx.httpHost", "localhost");
233

234             HtmlAdaptorServer adaptorServer = new HtmlAdaptorServer();
235
236             adaptorServer.setPort(port);
237
238             addManagedObject(adaptorServer, "HtmlAdaptor:type=htmladapter,port="+port);
239             if (log.isInfoEnabled())
240                 log.info("Set port value for the HTTP Adaptor. The given port: " + port);
241             adaptorServer.start();
242
243     }
244     
245     /**
246      * adds a new managed object to the server. the name that identify the managed object in the management system
247      * is composed in the following way: 'topic:key=value , key=value ,key=value...'
248      * at list one pair of "key=value" must be insterted. the topic, keys and values describes the
249      * function of the managed object.
250      * for example: 'MantaRay:agent=get stats'.
251      * @param managedOBJ - the object that is going to be managed. the object needs to have an interface including
252      * all the managable methods that would be called just as the object + MBean at the end. (for example if the object
253      * is called Run, the interface will be called RunMBean).
254      * @param managedObjectName - the managed object's name which should be in the format given above.
255      * @throws MantaException if the managedObjectName is ilegal or already exist, or if there is a problem in managed object,
256      * possibly it's format or interface.
257      */

258     public void addManagedObject(Object JavaDoc managedOBJ, String JavaDoc managedObjectName) throws MantaException {
259         ObjectName objectName;
260         try {
261             objectName = new ObjectName(managedObjectName);
262             if(server!=null){
263                 server.registerMBean(managedOBJ, objectName);
264             }
265         } catch (MalformedObjectNameException e) {
266             if(log.isErrorEnabled())
267                 log.error("Unkown name of the managed object. ",e);
268             throw new MantaException(e.getMessage(), MantaException.ID_INVALID_ARGUMENTS);
269         }catch (InstanceAlreadyExistsException e1) {
270             if(log.isErrorEnabled())
271                 log.error("The parameters of that managed object already exsists. ",e1);
272             throw new MantaException(e1.getMessage(), MantaException.ID_INVALID_ARGUMENTS);
273         } catch (MBeanRegistrationException e1) {
274             if(log.isErrorEnabled())
275                 log.error("Problem in registration of the new manged object. ",e1);
276             throw new MantaException(e1.getMessage(), MantaException.ID_RECEIVE_GENERAL_FAIL);
277         } catch (NotCompliantMBeanException e1) {
278             if(log.isErrorEnabled())
279                 log.error("Problem with the implementation/definations of the managed object. ",e1);
280             throw new MantaException(e1.getMessage(), MantaException.ID_RECEIVE_GENERAL_FAIL);
281         }
282     }//addManagedObject
283

284     /**
285      * returns a set of all the objects managed by the server.
286      * @return Set holding the requested managed objects.
287      */

288     public Set JavaDoc getManagedObjects(){
289         Set JavaDoc myMBeans= server.queryMBeans(null, null);
290         return myMBeans;
291     }//getManagedObjects(
292

293     /**
294      * returns a set of all the objects managed by the server that have the inserted topic
295      * as the topic of it's name.
296      * @param topic - the topic part of the name of the managed object
297      * @return Set holding the requested managed objects.
298      * @throws MantaException if the managedObjectName is ilegal.
299      */

300     public Set JavaDoc getManagedObjects(String JavaDoc topic)throws MantaException{
301         ObjectName objectName;
302         try {
303             objectName = new ObjectName(topic+":*");
304         } catch (MalformedObjectNameException e) {
305             if(log.isErrorEnabled())
306                 log.error("Unkown name of the managed object. ",e);
307             throw new MantaException(e.getMessage(), MantaException.ID_INVALID_ARGUMENTS);
308         }
309         Set JavaDoc myMBeans= server.queryMBeans(objectName, null);
310         return myMBeans;
311     }//getManagedObjects
312

313     /**
314      * returns a set holding the managed object identified by the name inerted in managedObjectName.
315      * the name should be in the format: " topic:key=value , key=value ,key=value...", where
316      * the topic, keys and values describes the
317      * function of the managed object.
318      * @param managedObjectName - the managed object's name which should be in the format given above.
319      * @return Set holding the requested managed object.
320      * @throws MantaException if the managedObjectName is ilegal.
321      */

322     public Set JavaDoc getManagedObject(String JavaDoc managedObjectName) throws MantaException{
323         ObjectName objectName;
324         try {
325             objectName = new ObjectName(managedObjectName);
326         } catch (MalformedObjectNameException e) {
327             if(log.isErrorEnabled())
328                 log.error("Unkown name of the managed object. ",e);
329             throw new MantaException(e.getMessage(), MantaException.ID_INVALID_ARGUMENTS);
330         }
331         Set JavaDoc myMBean= server.queryMBeans(objectName, null);
332         return myMBean;
333     }// getManagedObject
334

335     /**
336      * removes from the server the managed object with the name inserted.
337      * @param managedObjectName - the managed object's name which should be in the format of:
338      * 'topic:key=value , key= value...'
339      * @throws MantaException if the managedObjectName is ilegal or already exist, or if there is a problem in managed object,
340      * possibly it's format or interface.
341      */

342     public void removeManagedObject(String JavaDoc managedObjectName) throws MantaException{
343         ObjectName objectName;
344         try {
345             objectName = new ObjectName(managedObjectName);
346             if(server!=null){
347                 server.unregisterMBean(objectName);
348                 server.unregisterMBean(objectName);
349             }
350         } catch (MalformedObjectNameException e) {
351             if(log.isErrorEnabled())
352                 log.error("Unkown name of the managed object. ",e);
353             throw new MantaException(e.getMessage(), MantaException.ID_INVALID_ARGUMENTS);
354         } catch (InstanceNotFoundException e1) {
355             if(log.isErrorEnabled())
356                 log.error("Instance not found exception. could refer to the managed object's class. ",e1);
357             throw new MantaException(e1.getMessage(), MantaException.ID_INVALID_ARGUMENTS);
358         } catch (MBeanRegistrationException e1) {
359             if(log.isErrorEnabled())
360                 log.error("Problem with the unRegistration of the new manged object. ",e1);
361             throw new MantaException(e1.getMessage(), MantaException.ID_RECEIVE_GENERAL_FAIL);
362         }
363     }//removeManagedObject
364

365     /**
366      * registers all the basic managed objects to the jmx management system.
367      * added by lital kasif
368      */

369     public static void registerJMXBeans() {
370         try {
371             boolean startHttpAdaptor= MantaAgent.getInstance().getSingletonRepository()
372                 .getConfigManager().getBooleanProperty("management.jmx.httpAdaptor.enabled", false);
373             boolean startRMIConnector= MantaAgent.getInstance().getSingletonRepository()
374                 .getConfigManager().getBooleanProperty("management.jmx.rmiConnector.enabled", false);
375             
376             if(startHttpAdaptor == true || startRMIConnector == true){
377                  MantaJMXManagement manager = MantaJMXManagement.getInstance();
378                  manager.addManagedObject(new GarbageCollectionJMX(),"MantaRay:function=garbadgeCollector");
379                  manager.addManagedObject(new GetCurrentTimeJMX(), "MantaRay:time=gets the time");
380                  manager.addManagedObject(new GetMemStatJMX(), "MantaRay:memory=gets the memory status");
381                  manager.addManagedObject(new GetConnectionsJMX(), "MantaRay:connections=get layers connected");
382                  manager.addManagedObject(new GetServiceParticipationJMX(), "MantaRay:services=services participation");
383                  manager.addManagedObject(new GetWorldModelerVersionJMX(), "MantaRay:worldModeler=get version");
384                  //manager.addManagedObject(new UpdateWorldModelerJMX(), "MantaRay:world=update version");
385
//manager.addManagedObject(new GetServiceSecurityKeyJMX(), "MantaRay:security=get service key");
386
//manager.addManagedObject(new UpdateServiceSecurityKeyJMX(), "MantaRay:security=update service key");
387
manager.addManagedObject(new GetStatsJMX(), "MantaRay:layer=get stats");
388                  manager.addManagedObject(MantaAgent.getInstance().getSingletonRepository()
389                         .getConfigManager(), "MantaRay:config=manages configuration of properties");
390                  // manager.addManagedObject(MantaAgent.getInstance().getSingletonRepository().getWorldModeler(), "MantaRay:worldModeler=manages the world modeler");
391

392             }
393            
394         } catch (Exception JavaDoc e) {
395             if(LogFactory.getLog("InitManager").isErrorEnabled())
396                 LogFactory.getLog("InitManager").error("RegisterJMXMBeans had an exception: "+e);
397         }
398     }
399 }//MantaJMXManagement
400
Popular Tags