KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.objectweb.petals.tools.petalsadmin.utils;
2
3 import java.io.IOException JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.Hashtable JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.Properties JavaDoc;
8 import java.util.Set JavaDoc;
9
10 import javax.management.MBeanServerConnection JavaDoc;
11 import javax.management.MalformedObjectNameException JavaDoc;
12 import javax.management.ObjectName JavaDoc;
13 import javax.management.remote.JMXConnector JavaDoc;
14 import javax.management.remote.JMXConnectorFactory JavaDoc;
15 import javax.management.remote.JMXServiceURL JavaDoc;
16
17 /**
18  * Class used to get a connector to the JMX server
19  *
20  * @author ddesjardins - eBMWebsourcing
21  */

22 public final class JBIJMXConnectorUtil {
23     
24     private final static String JavaDoc PETALS_DOMAIN = "Petals";
25     
26     private final static String JavaDoc TYPE = "type";
27     
28     private final static String JavaDoc NAME = "name";
29     
30     private final static String JavaDoc SERVICE_TYPE = "service";
31     
32     private final static String JavaDoc ADMIN_NAME = "Admin";
33     
34     private final static String JavaDoc INSTALLATION_NAME = "Installation";
35     
36     private final static String JavaDoc DEPLOYMENT_NAME = "Deployment";
37
38     private JBIJMXConnectorUtil() {
39         // Do nothing
40
}
41
42     /**
43      * Retreive the object name of the admin service
44      *
45      * @param mBeanServer
46      * mbean server
47      * @return object name
48      * @throws IOException
49      * @throws NullPointerException
50      * @throws MalformedObjectNameException
51      */

52     public static ObjectName JavaDoc getAdminServiceMBeanName(
53             MBeanServerConnection JavaDoc mBeanServer) throws IOException JavaDoc,
54         MalformedObjectNameException JavaDoc, NullPointerException JavaDoc {
55         ObjectName JavaDoc result = null;
56         Set JavaDoc objNames;
57         Hashtable JavaDoc<String JavaDoc, String JavaDoc> attributes = new Hashtable JavaDoc<String JavaDoc, String JavaDoc>();
58         attributes.put(NAME, ADMIN_NAME);
59         attributes.put(TYPE, SERVICE_TYPE);
60         ObjectName JavaDoc objName = new ObjectName JavaDoc(PETALS_DOMAIN, attributes);
61         objNames = mBeanServer.queryNames(objName, null);
62         if (objNames != null && objNames.size() == 1) {
63             result = (ObjectName JavaDoc) objNames.iterator().next();
64         }
65         return result;
66     }
67
68     /**
69      * Retreive the object name of a component
70      *
71      * @param mBeanServer
72      * mbean server
73      * @return object name
74      * @throws IOException
75      * @throws NullPointerException
76      * @throws MalformedObjectNameException
77      */

78     public static ObjectName JavaDoc getComponentMBeanName(
79             MBeanServerConnection JavaDoc mBeanServer, String JavaDoc name) throws IOException JavaDoc,
80         MalformedObjectNameException JavaDoc, NullPointerException JavaDoc {
81         ObjectName JavaDoc result = null;
82         String JavaDoc pattern = "*:type=engine,name=" + name;
83         Set JavaDoc objNames;
84         ObjectName JavaDoc objName = new ObjectName JavaDoc(pattern);
85         objNames = mBeanServer.queryNames(objName, null);
86         if (objNames != null && objNames.size() == 1) {
87             result = (ObjectName JavaDoc) objNames.iterator().next();
88         }
89         if (result == null) {
90             pattern = "*:type=binding,name=" + name;
91             objName = new ObjectName JavaDoc(pattern);
92             objNames = mBeanServer.queryNames(objName, null);
93             if (objNames != null && objNames.size() == 1) {
94                 result = (ObjectName JavaDoc) objNames.iterator().next();
95             }
96         }
97         return result;
98     }
99
100     /**
101      * Get a JMX connector
102      *
103      * @param host
104      * host
105      * @param port
106      * port
107      * @param username
108      * username
109      * @param password
110      * password
111      * @return jmx connector
112      * @throws IOException
113      */

114     public static JMXConnector JavaDoc getConnection(String JavaDoc host, String JavaDoc port,
115             String JavaDoc username, String JavaDoc password) throws IOException JavaDoc {
116         JMXConnector JavaDoc connector = null;
117         Properties JavaDoc env = System.getProperties();
118         env.put("java.naming.factory.initial",
119                 "com.sun.jndi.rmi.registry.RegistryContextFactory");
120         Map JavaDoc<String JavaDoc, Object JavaDoc> args = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
121         if (!"".equals(username)) {
122             if (password == null) {
123                 password = "";
124             }
125             String JavaDoc[] credentials = new String JavaDoc[] {username, password};
126             args.put("jmx.remote.credentials", credentials);
127         }
128         String JavaDoc urlStr = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port
129                 + "/management/rmi-jmx-connector";
130         JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc(urlStr);
131         connector = JMXConnectorFactory.connect(url, args);
132         return connector;
133     }
134
135     /**
136      * Retreive the object name of the deployment service
137      *
138      * @param mBeanServer
139      * mbean server
140      * @return object name
141      * @throws IOException
142      * @throws NullPointerException
143      * @throws MalformedObjectNameException
144      */

145     public static ObjectName JavaDoc getDeploymentServiceMBeanName(
146             MBeanServerConnection JavaDoc mBeanServer) throws IOException JavaDoc,
147         MalformedObjectNameException JavaDoc, NullPointerException JavaDoc {
148         ObjectName JavaDoc result = null;
149         Set JavaDoc objNames;
150         Hashtable JavaDoc<String JavaDoc, String JavaDoc> attributes = new Hashtable JavaDoc<String JavaDoc, String JavaDoc>();
151         attributes.put(NAME, DEPLOYMENT_NAME);
152         attributes.put(TYPE, SERVICE_TYPE);
153         ObjectName JavaDoc objName = new ObjectName JavaDoc(PETALS_DOMAIN, attributes);
154         objNames = mBeanServer.queryNames(objName, null);
155         if (objNames != null && objNames.size() == 1) {
156             result = (ObjectName JavaDoc) objNames.iterator().next();
157         }
158         return result;
159     }
160
161     /**
162      * Retreive the object name of the installation service
163      *
164      * @param mBeanServer
165      * mbean server
166      * @return object name
167      * @throws IOException
168      * @throws NullPointerException
169      * @throws MalformedObjectNameException
170      */

171     public static ObjectName JavaDoc getInstallationServiceMBeanName(
172             MBeanServerConnection JavaDoc mBeanServer) throws IOException JavaDoc,
173         MalformedObjectNameException JavaDoc, NullPointerException JavaDoc {
174         ObjectName JavaDoc result = null;
175         Set JavaDoc objNames;
176         Hashtable JavaDoc<String JavaDoc, String JavaDoc> attributes = new Hashtable JavaDoc<String JavaDoc, String JavaDoc>();
177         attributes.put(NAME, INSTALLATION_NAME);
178         attributes.put(TYPE, SERVICE_TYPE);
179         ObjectName JavaDoc objName = new ObjectName JavaDoc(PETALS_DOMAIN, attributes);
180         objNames = mBeanServer.queryNames(objName, null);
181         if (objNames != null && objNames.size() == 1) {
182             result = (ObjectName JavaDoc) objNames.iterator().next();
183         }
184         return result;
185     }
186
187 }
188
Popular Tags