KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > system > launch > MBeanHelper


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2005 EBM Websourcing, http://www.ebmwebsourcing.com/
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  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: MBeanHelper.java 16:40:52 alouis $
20  * -------------------------------------------------------------------------
21  */

22
23 package org.objectweb.petals.system.launch;
24
25 import java.io.IOException JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import javax.jbi.management.DeploymentServiceMBean;
32 import javax.jbi.management.InstallationServiceMBean;
33 import javax.management.MBeanServer JavaDoc;
34 import javax.management.MBeanServerConnection JavaDoc;
35 import javax.management.MalformedObjectNameException JavaDoc;
36 import javax.management.ObjectName JavaDoc;
37 import javax.management.remote.JMXConnector JavaDoc;
38 import javax.management.remote.JMXConnectorFactory JavaDoc;
39 import javax.management.remote.JMXServiceURL JavaDoc;
40
41 import org.objectweb.fractal.api.Component;
42 import org.objectweb.fractal.api.control.ContentController;
43 import org.objectweb.fractal.api.control.LifeCycleController;
44 import org.objectweb.fractal.jmx.agent.AdminAttributes;
45 import org.objectweb.fractal.jmx.agent.Introspector;
46 import org.objectweb.fractal.util.Fractal;
47
48 import org.objectweb.petals.jbi.management.service.AdminServiceMBean;
49 import org.objectweb.petals.kernel.admin.service.PetalsAdminServiceMBean;
50 import org.objectweb.petals.util.SystemUtil;
51
52
53 /**
54  * This class helps the Launcher to register MBean
55  *
56  * @author alouis
57  *
58  */

59 public class MBeanHelper {
60     public static void registerMBeans(Component petalsComposite) throws Exception JavaDoc {
61
62         String JavaDoc componentName = "agent-jmx";
63         ContentController contentController = Fractal
64             .getContentController(petalsComposite);
65         Component agent = FractalHelper.getComponentByName(contentController, componentName);
66
67         String JavaDoc interfaceName = "admin";
68         Introspector.CURRENCY_TIME_LIMIT = "-1";
69         interfaceName = "attribute-controller";
70         AdminAttributes adminAttributes = (AdminAttributes) agent
71             .getFcInterface(interfaceName);
72
73         MBeanServer JavaDoc jmxServer = adminAttributes.getRawMBeanServer();
74
75         // register the fractal lifecycle controller
76
LifeCycleController lcc = Fractal
77             .getLifeCycleController(petalsComposite);
78
79         jmxServer.registerMBean(Introspector.createMBean(lcc), new ObjectName JavaDoc(
80             "FC/Petals@" + lcc.hashCode() + ":itf=lifecycle-controller"));
81
82         // register JBI AdminMBean
83
Component jbi = FractalHelper.getComponentByName(contentController, "jbi");
84         ContentController contentControllerForJBI = Fractal
85             .getContentController(jbi);
86         registerComponent(contentControllerForJBI, "admin-impl", jmxServer,
87             AdminServiceMBean.class, "Petals:name=Admin,type=service");
88
89         // register JBI DeployMBean
90
registerComponent(contentControllerForJBI, "deployment-impl",
91             jmxServer, DeploymentServiceMBean.class,
92             "Petals:name=Deployment,type=service");
93
94         // register JBI InstallMBean
95
registerComponent(contentControllerForJBI, "installation-impl",
96             jmxServer, InstallationServiceMBean.class,
97             "Petals:name=Installation,type=service");
98
99         // register Petals Admin
100
registerComponent(contentController, "petalsadmin-service", jmxServer,
101             PetalsAdminServiceMBean.class,
102             "Petals:name=PetalsAdmin,type=service");
103     }
104
105     /**
106      * Register a fractal component-service to the jmx server. Assume the
107      * interface name of the fractal component is "service"
108      *
109      * @param contentController
110      * contentcontroller where the fractal comp can be found
111      * @param fractalName
112      * the name of the fractal comp
113      * @param jmxServer
114      * the jmx server to register to
115      * @param mbeanClass
116      * the interface of the mbean
117      * @param mbeanName
118      * the name of the mbean
119      * @throws Exception
120      */

121     protected static void registerComponent(ContentController contentController,
122         String JavaDoc fractalName, MBeanServer JavaDoc jmxServer, Class JavaDoc mbeanClass,
123         String JavaDoc mbeanName) throws Exception JavaDoc {
124         Component systemState = FractalHelper.getComponentByName(contentController,
125             fractalName);
126         Object JavaDoc object = systemState.getFcInterface("service");
127
128         jmxServer.registerMBean(Introspector.createMBean(object, mbeanClass),
129             new ObjectName JavaDoc(mbeanName));
130     }
131     
132     
133     /**
134      * Return the JMX server by using the server.properties entries
135      *
136      * @return local jmx server
137      * @throws IOException
138      */

139     public static MBeanServerConnection JavaDoc retrieveJMXServer() throws IOException JavaDoc {
140         // connect to the JMX server
141
// with server.properties entries
142
String JavaDoc jmxHost = SystemUtil.getHost();
143         String JavaDoc jmxPort = SystemUtil.getJmxPort();
144         String JavaDoc username = SystemUtil.getJmxUser();
145         String JavaDoc password = SystemUtil.getJmxPassword();
146
147         JMXConnector JavaDoc connector = null;
148         Map JavaDoc<String JavaDoc, Object JavaDoc> args = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
149         if (!"".equals(username)) {
150             if (password == null) {
151                 password = "";
152             }
153             String JavaDoc[] credentials = new String JavaDoc[] {username, password};
154             args.put("jmx.remote.credentials", credentials);
155         }
156         String JavaDoc urlStr = "service:jmx:rmi:///jndi/rmi://" + jmxHost + ":"
157             + jmxPort + "/management/rmi-jmx-connector";
158         JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc(urlStr);
159         connector = retrieveJMXConnector(url, args);
160         return connector.getMBeanServerConnection();
161     }
162
163     /**
164      * Return the MBean name from the jmx server with the specified pattern
165      *
166      * @param pattern
167      * @param jmxServer
168      * @return the objectname, null if no object match
169      * @throws MalformedObjectNameException
170      * @throws NullPointerException
171      * @throws IOException
172      */

173     public static ObjectName JavaDoc retrieveServiceMBean(String JavaDoc serviceName,
174         MBeanServerConnection JavaDoc jmxServer) throws MalformedObjectNameException JavaDoc,
175         NullPointerException JavaDoc, IOException JavaDoc {
176
177         ObjectName JavaDoc result = null;
178
179         // crate the search filter
180
Hashtable JavaDoc<String JavaDoc, String JavaDoc> attributes = new Hashtable JavaDoc<String JavaDoc, String JavaDoc>();
181         attributes.put("name", serviceName);
182         attributes.put("type", "service");
183         ObjectName JavaDoc objName = new ObjectName JavaDoc("Petals", attributes);
184         
185         Set JavaDoc objNames = jmxServer.queryNames(objName, null);
186         if (objNames != null && objNames.size() == 1) {
187             result = (ObjectName JavaDoc) objNames.iterator().next();
188         }
189         return result;
190     }
191
192     /**
193      * @see JMXConnectorFactory#connect(JMXServiceURL, Map)
194      */

195     protected static JMXConnector JavaDoc retrieveJMXConnector(JMXServiceURL JavaDoc url,
196         Map JavaDoc<String JavaDoc, ?> args) throws IOException JavaDoc {
197         return JMXConnectorFactory.connect(url, args);
198     }
199
200
201    }
202
Popular Tags