KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > tools > petalsadmin > utils > JMXUtils


1 package org.objectweb.petals.tools.petalsadmin.utils;
2
3 import java.io.IOException JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import javax.management.Attribute JavaDoc;
12 import javax.management.AttributeNotFoundException JavaDoc;
13 import javax.management.MBeanServerConnection JavaDoc;
14 import javax.management.ObjectName JavaDoc;
15 import javax.management.remote.JMXConnector JavaDoc;
16
17 public class JMXUtils {
18
19     /**
20      * Host for the JMX connection
21      */

22     private String JavaDoc host = "";
23
24     /**
25      * Password the JMX connection
26      */

27     private String JavaDoc password = "";
28
29     /**
30      * Port for the JMX connection
31      */

32     private String JavaDoc port = "";
33
34     /**
35      * Username for the JMX connection
36      */

37     private String JavaDoc username = "";
38
39     public JMXUtils(String JavaDoc serverHost,String JavaDoc jmxPort, String JavaDoc jmxUser, String JavaDoc jmxPasswd) {
40         this.host = serverHost;
41         this.port=jmxPort;
42         this.username=jmxUser;
43         this.password=jmxPasswd;
44     }
45
46     /**
47      * Get a JMX connector
48      *
49      * @param host
50      * host
51      * @param port
52      * port
53      * @param username
54      * username
55      * @param password
56      * password
57      * @return jmx connector
58      * @throws IOException
59      */

60     /*public static JMXConnector getConnection(String host, String port,
61             String username, String password) throws IOException {
62         JMXConnector connector = null;
63         Properties env = System.getProperties();
64         env.put("java.naming.factory.initial",
65                 "com.sun.jndi.rmi.registry.RegistryContextFactory");
66         Map<String, Object> args = new HashMap<String, Object>();
67         if (!"".equals(username)) {
68             if (password == null) {
69                 password = "";
70             }
71             String[] credentials = new String[] { username, password };
72             args.put("jmx.remote.credentials", credentials);
73         }
74         String urlStr = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port
75                 + "/management/rmi-jmx-connector";
76         JMXServiceURL url = new JMXServiceURL(urlStr);
77         connector = JMXConnectorFactory.connect(url, args);
78         return connector;
79     }*/

80
81     /**
82      * List the deployed JBI components
83      *
84      * @return map of components
85      * @throws Exception
86      */

87     public Map JavaDoc<String JavaDoc, JBIEntity> listComponents(String JavaDoc phost, String JavaDoc pport, String JavaDoc pusername, String JavaDoc ppassword)
88         throws Exception JavaDoc {
89         Map JavaDoc<String JavaDoc, JBIEntity> components = new HashMap JavaDoc<String JavaDoc, JBIEntity>();
90         JMXConnector JavaDoc connector = null;
91         try {
92             connector = retrieveConnector(phost, pport, pusername, ppassword);
93             MBeanServerConnection JavaDoc connection = connector
94                     .getMBeanServerConnection();
95             Object JavaDoc result = connection.getAttribute(JBIJMXConnectorUtil
96                     .getAdminServiceMBeanName(connection), "BindingComponents");
97             if (result instanceof ObjectName JavaDoc[]) {
98                 ObjectName JavaDoc[] engines = (ObjectName JavaDoc[]) result;
99                 for (ObjectName JavaDoc objectName : engines) {
100                     JBIEntity component = new JBIEntity();
101                     String JavaDoc objState = (String JavaDoc) connection.getAttribute(
102                             objectName, "CurrentState");
103                     String JavaDoc name = objectName.getKeyProperty("name");
104                     component.setName(name);
105                     component.setState(objState);
106                     component.setObjectName(objectName);
107                     // TODO find a way to get the description of a component
108
component.setDescription("");
109                     component.setType(JBIEntity.BINDING_COMPONENT);
110                     components.put(component.getName(), component);
111                 }
112             }
113             result = connection.getAttribute(JBIJMXConnectorUtil
114                     .getAdminServiceMBeanName(connection), "EngineComponents");
115             if (result instanceof ObjectName JavaDoc[]) {
116                 ObjectName JavaDoc[] engines = (ObjectName JavaDoc[]) result;
117                 for (ObjectName JavaDoc objectName : engines) {
118                     JBIEntity component = new JBIEntity();
119                     String JavaDoc objState = (String JavaDoc) connection.getAttribute(
120                             objectName, "CurrentState");
121                     String JavaDoc name = objectName.getKeyProperty("name");
122                     component.setName(name);
123                     component.setState(objState);
124                     component.setObjectName(objectName);
125                     // TODO find a way to get the description of a component
126
component.setDescription("");
127                     component.setType(JBIEntity.SERVICE_ENGINE);
128                     components.put(component.getName(), component);
129                 }
130             }
131             connector.close();
132         } catch (Exception JavaDoc e) {
133             try {
134                 if (connector != null) {
135                     connector.close();
136                 }
137             } catch (IOException JavaDoc e1) {
138                 // Do nothing
139
}
140             throw new Exception JavaDoc(e);
141         }
142         return components;
143     }
144
145     /**
146      * Retrieve a JMX connector
147      *
148      * @return a JMXConnector
149      * @throws IOException
150      */

151     public JMXConnector JavaDoc retrieveConnector() throws IOException JavaDoc {
152         return JBIJMXConnectorUtil
153                 .getConnection(host, port, username, password);
154     }
155
156     /**
157      * Retrieve a JMX connector
158      *
159      * @return a JMXConnector
160      * @throws IOException
161      */

162     public JMXConnector JavaDoc retrieveConnector(String JavaDoc phost, String JavaDoc pport, String JavaDoc pusername, String JavaDoc ppassword)
163         throws IOException JavaDoc {
164         return JBIJMXConnectorUtil
165                 .getConnection(phost, pport, pusername, ppassword);
166     }
167
168     @SuppressWarnings JavaDoc("unchecked")
169     public List JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> getStartedServers() throws Exception JavaDoc {
170         //list of the servers name to return
171
List JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> serversList= new ArrayList JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>>();
172         JMXConnector JavaDoc connector = null;
173         try {
174             connector = retrieveConnector();
175             MBeanServerConnection JavaDoc connection = connector
176                     .getMBeanServerConnection();
177             ObjectName JavaDoc petalsAdminName = new ObjectName JavaDoc(
178                     "Petals:name=PetalsAdmin,type=service");
179             @SuppressWarnings JavaDoc("unused")
180             Collection JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> serversConfigurations = (Collection JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>>) connection
181                     .getAttribute(petalsAdminName,"AllStartedServersConfiguration");
182             /* donnees des Map de serversConfigurations
183             String CONF_HOST="host";
184             String CONF_HTML_PORT="htmlPort";
185             String CONF_JMX_LOGIN="jmxLogin";
186             String CONF_JMX_PASSWORD="jmxPassword";
187             String CONF_JMX_PORT="jmxPort";
188             String CONF_NAME="name";
189              */

190             Iterator JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> parcServers = serversConfigurations.iterator();
191             while(parcServers.hasNext())
192             {
193                 Map JavaDoc<String JavaDoc, String JavaDoc> currentServer = parcServers.next();
194                 serversList.add(currentServer);
195             }
196             
197         } catch (Exception JavaDoc e) {
198             throw new Exception JavaDoc(e);
199         }
200         return serversList;
201     }
202     
203     @SuppressWarnings JavaDoc("unchecked")
204     public List JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> getAllServers() throws Exception JavaDoc {
205         //list of the servers name to return
206
List JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> serversList= new ArrayList JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>>();
207         JMXConnector JavaDoc connector = null;
208         try {
209             connector = retrieveConnector();
210             MBeanServerConnection JavaDoc connection = connector
211                     .getMBeanServerConnection();
212             ObjectName JavaDoc petalsAdminName = new ObjectName JavaDoc(
213                     "Petals:name=PetalsAdmin,type=service");
214             Collection JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> serversConfigurations = (Collection JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>>) connection
215                     .getAttribute(petalsAdminName,"AllServersConfiguration");
216             /* donnees des Map de serversConfigurations
217             String CONF_HOST="host";
218             String CONF_HTML_PORT="htmlPort";
219             String CONF_JMX_LOGIN="jmxLogin";
220             String CONF_JMX_PASSWORD="jmxPassword";
221             String CONF_JMX_PORT="jmxPort";
222             String CONF_NAME="name";
223              */

224             Iterator JavaDoc<Map JavaDoc<String JavaDoc, String JavaDoc>> parcServers = serversConfigurations.iterator();
225             while(parcServers.hasNext())
226             {
227                 Map JavaDoc<String JavaDoc, String JavaDoc> currentServer = parcServers.next();
228                 serversList.add(currentServer);
229             }
230             
231         } catch (Exception JavaDoc e) {
232             throw new Exception JavaDoc(e);
233         }
234         return serversList;
235     }
236     
237     @SuppressWarnings JavaDoc("unchecked")
238     public List JavaDoc<List JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>>> bulkData(Map JavaDoc<String JavaDoc, String JavaDoc> serverConf, long sinceTime)
239         throws Exception JavaDoc
240     {
241         List JavaDoc<List JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>>> list_exchanges = new ArrayList JavaDoc<List JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>>>();
242         JMXConnector JavaDoc connector = null;
243         try {
244             String JavaDoc serverHost = serverConf.get("host");
245             String JavaDoc serverPort = serverConf.get("jmxPort");
246             String JavaDoc serverLogin = serverConf.get("jmxLogin");
247             String JavaDoc serverPasswd = serverConf.get("jmxPassword");
248
249             connector = retrieveConnector(serverHost,serverPort,serverLogin,serverPasswd);
250             MBeanServerConnection JavaDoc connection = connector
251                     .getMBeanServerConnection();
252             ObjectName JavaDoc petalsAdminName = new ObjectName JavaDoc(
253             "Petals:name=PetalsAdmin,type=service");
254             list_exchanges = (List JavaDoc<List JavaDoc<Map JavaDoc<String JavaDoc,Object JavaDoc>>>) connection.invoke(petalsAdminName,"bulkData", new Object JavaDoc[]{sinceTime}, new String JavaDoc[]{Long.TYPE.getCanonicalName()});
255         }
256         catch (Exception JavaDoc e)
257         {
258             throw new Exception JavaDoc(e);
259         }
260         return list_exchanges;
261     }
262     
263     /**
264      * start or stop monitoring on a specific server
265      * @param serverHost ip address of the server to manage
266      * @param action : "start" to start monitoring, "stop" to stop monitoring
267      * @return
268      */

269     public boolean monitor(String JavaDoc serverHost, String JavaDoc serverPort, String JavaDoc serverLogin, String JavaDoc serverPasswd, boolean action)
270         throws Exception JavaDoc, AttributeNotFoundException JavaDoc
271     {
272         JMXConnector JavaDoc connector = null;
273         try {
274             connector = retrieveConnector(serverHost,serverPort,serverLogin,serverPasswd);
275             MBeanServerConnection JavaDoc connection = connector
276                     .getMBeanServerConnection();
277             ObjectName JavaDoc petalsAdminName = new ObjectName JavaDoc(
278                     "Petals:name=PetalsAdmin,type=service");
279             Attribute JavaDoc attAction = new Attribute JavaDoc("Monitoring",action);
280             connection.setAttribute(petalsAdminName, attAction);
281             return true;
282         }
283         catch (AttributeNotFoundException JavaDoc anf) {
284             return true;
285         }
286         catch (Exception JavaDoc e) {
287             throw new Exception JavaDoc(e);
288         }
289     }
290
291     /**
292      * start or stop monitoring on a specific server
293      * @param serverHost ip address of the server to manage
294      * @param action : "start" to start monitoring, "stop" to stop monitoring
295      * @return
296      */

297     public String JavaDoc getAttribute(String JavaDoc serverHost, String JavaDoc serverPort, String JavaDoc serverLogin, String JavaDoc serverPasswd, String JavaDoc attributeName)
298         throws Exception JavaDoc, AttributeNotFoundException JavaDoc
299     {
300         JMXConnector JavaDoc connector = null;
301         try {
302             connector = retrieveConnector(serverHost,serverPort,serverLogin,serverPasswd);
303             MBeanServerConnection JavaDoc connection = connector
304                     .getMBeanServerConnection();
305             ObjectName JavaDoc petalsAdminName = new ObjectName JavaDoc(
306                     "Petals:name=PetalsAdmin,type=service");
307             return connection.getAttribute(petalsAdminName,"Monitoring").toString();
308         }
309         catch (AttributeNotFoundException JavaDoc anf) {
310             return "not found";
311         }
312         catch (Exception JavaDoc e) {
313             throw new Exception JavaDoc(e);
314         }
315     }
316
317     public String JavaDoc getHost() {
318         return host;
319     }
320
321     public void setHost(String JavaDoc host) {
322         this.host = host;
323     }
324
325     public String JavaDoc getPassword() {
326         return password;
327     }
328
329     public void setPassword(String JavaDoc password) {
330         this.password = password;
331     }
332
333     public String JavaDoc getPort() {
334         return port;
335     }
336
337     public void setPort(String JavaDoc port) {
338         this.port = port;
339     }
340
341     public String JavaDoc getUsername() {
342         return username;
343     }
344
345     public void setUsername(String JavaDoc username) {
346         this.username = username;
347     }
348
349 }
350
Popular Tags