KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > tools > jbiplugin > util > JBIJMXConnectorUtil


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
20 package org.objectweb.petals.tools.jbiplugin.util;
21
22 import java.io.IOException JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Properties JavaDoc;
27 import java.util.Set JavaDoc;
28
29 import javax.management.MBeanServerConnection JavaDoc;
30 import javax.management.MalformedObjectNameException JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.management.remote.JMXConnector JavaDoc;
33 import javax.management.remote.JMXConnectorFactory JavaDoc;
34 import javax.management.remote.JMXServiceURL JavaDoc;
35
36 /**
37  * Class used to get a connector to the JMX server
38  *
39  * @author ddesjardins - eBMWebsourcing
40  */

41 public final class JBIJMXConnectorUtil {
42     
43     private final static String JavaDoc PETALS_DOMAIN = "Petals";
44     
45     private final static String JavaDoc TYPE = "type";
46     
47     private final static String JavaDoc NAME = "name";
48     
49     private final static String JavaDoc SERVICE_TYPE = "service";
50     
51     private final static String JavaDoc ADMIN_NAME = "Admin";
52     
53     private final static String JavaDoc INSTALLATION_NAME = "Installation";
54     
55     private final static String JavaDoc DEPLOYMENT_NAME = "Deployment";
56
57     private JBIJMXConnectorUtil() {
58         // Do nothing
59
}
60
61     /**
62      * Retreive the object name of the admin service
63      *
64      * @param mBeanServer
65      * mbean server
66      * @return object name
67      * @throws IOException
68      * @throws NullPointerException
69      * @throws MalformedObjectNameException
70      */

71     public static ObjectName JavaDoc getAdminServiceMBeanName(
72             MBeanServerConnection JavaDoc mBeanServer) throws IOException JavaDoc,
73         MalformedObjectNameException JavaDoc, NullPointerException JavaDoc {
74         ObjectName JavaDoc result = null;
75         Set JavaDoc objNames;
76         Hashtable JavaDoc<String JavaDoc, String JavaDoc> attributes = new Hashtable JavaDoc<String JavaDoc, String JavaDoc>();
77         attributes.put(NAME, ADMIN_NAME);
78         attributes.put(TYPE, SERVICE_TYPE);
79         ObjectName JavaDoc objName = new ObjectName JavaDoc(PETALS_DOMAIN, attributes);
80         objNames = mBeanServer.queryNames(objName, null);
81         if (objNames != null && objNames.size() == 1) {
82             result = (ObjectName JavaDoc) objNames.iterator().next();
83         }
84         return result;
85     }
86
87     /**
88      * Retreive the installer object name of a component
89      *
90      * @param mBeanServer
91      * mbean server
92      * @return object name
93      * @throws IOException
94      * @throws NullPointerException
95      * @throws MalformedObjectNameException
96      */

97     public static ObjectName JavaDoc getInstallerComponentMBeanName(
98             MBeanServerConnection JavaDoc mBeanServer, String JavaDoc name) throws IOException JavaDoc,
99         MalformedObjectNameException JavaDoc, NullPointerException JavaDoc {
100         ObjectName JavaDoc result = null;
101         String JavaDoc pattern = "*:type=installer,name=" + name;
102         Set JavaDoc objNames;
103         ObjectName JavaDoc objName = new ObjectName JavaDoc(pattern);
104         objNames = mBeanServer.queryNames(objName, null);
105         if (objNames != null && objNames.size() == 1) {
106             result = (ObjectName JavaDoc) objNames.iterator().next();
107         }
108         return result;
109     }
110
111     /**
112      * Retreive the object name of a component
113      *
114      * @param mBeanServer
115      * mbean server
116      * @return object name
117      * @throws IOException
118      * @throws NullPointerException
119      * @throws MalformedObjectNameException
120      */

121     public static ObjectName JavaDoc getComponentMBeanName(
122             MBeanServerConnection JavaDoc mBeanServer, String JavaDoc name) throws IOException JavaDoc,
123         MalformedObjectNameException JavaDoc, NullPointerException JavaDoc {
124         ObjectName JavaDoc result = null;
125         String JavaDoc pattern = "*:type=engine,name=" + name;
126         Set JavaDoc objNames;
127         ObjectName JavaDoc objName = new ObjectName JavaDoc(pattern);
128         objNames = mBeanServer.queryNames(objName, null);
129         if (objNames != null && objNames.size() == 1) {
130             result = (ObjectName JavaDoc) objNames.iterator().next();
131         }
132         if (result == null) {
133             pattern = "*:type=binding,name=" + name;
134             objName = new ObjectName JavaDoc(pattern);
135             objNames = mBeanServer.queryNames(objName, null);
136             if (objNames != null && objNames.size() == 1) {
137                 result = (ObjectName JavaDoc) objNames.iterator().next();
138             }
139         }
140         return result;
141     }
142
143     /**
144      * Get a JMX connector
145      *
146      * @param host
147      * host
148      * @param port
149      * port
150      * @param username
151      * username
152      * @param password
153      * password
154      * @return jmx connector
155      * @throws IOException
156      */

157     public static JMXConnector JavaDoc getConnection(String JavaDoc host, String JavaDoc port,
158             String JavaDoc username, String JavaDoc password) throws IOException JavaDoc {
159         JMXConnector JavaDoc connector = null;
160         Properties JavaDoc env = System.getProperties();
161         env.put("java.naming.factory.initial",
162                 "com.sun.jndi.rmi.registry.RegistryContextFactory");
163         Map JavaDoc<String JavaDoc, Object JavaDoc> args = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
164         if (!"".equals(username)) {
165             if (password == null) {
166                 password = "";
167             }
168             String JavaDoc[] credentials = new String JavaDoc[] {username, password};
169             args.put("jmx.remote.credentials", credentials);
170         }
171         String JavaDoc urlStr = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port
172                 + "/management/rmi-jmx-connector";
173         JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc(urlStr);
174         connector = JMXConnectorFactory.connect(url, args);
175         return connector;
176     }
177
178     /**
179      * Retreive the object name of the deployment service
180      *
181      * @param mBeanServer
182      * mbean server
183      * @return object name
184      * @throws IOException
185      * @throws NullPointerException
186      * @throws MalformedObjectNameException
187      */

188     public static ObjectName JavaDoc getDeploymentServiceMBeanName(
189             MBeanServerConnection JavaDoc mBeanServer) throws IOException JavaDoc,
190         MalformedObjectNameException JavaDoc, NullPointerException JavaDoc {
191         ObjectName JavaDoc result = null;
192         Set JavaDoc objNames;
193         Hashtable JavaDoc<String JavaDoc, String JavaDoc> attributes = new Hashtable JavaDoc<String JavaDoc, String JavaDoc>();
194         attributes.put(NAME, DEPLOYMENT_NAME);
195         attributes.put(TYPE, SERVICE_TYPE);
196         ObjectName JavaDoc objName = new ObjectName JavaDoc(PETALS_DOMAIN, attributes);
197         objNames = mBeanServer.queryNames(objName, null);
198         if (objNames != null && objNames.size() == 1) {
199             result = (ObjectName JavaDoc) objNames.iterator().next();
200         }
201         return result;
202     }
203
204     /**
205      * Retreive the object name of the installation service
206      *
207      * @param mBeanServer
208      * mbean server
209      * @return object name
210      * @throws IOException
211      * @throws NullPointerException
212      * @throws MalformedObjectNameException
213      */

214     public static ObjectName JavaDoc getInstallationServiceMBeanName(
215             MBeanServerConnection JavaDoc mBeanServer) throws IOException JavaDoc,
216         MalformedObjectNameException JavaDoc, NullPointerException JavaDoc {
217         ObjectName JavaDoc result = null;
218         Set JavaDoc objNames;
219         Hashtable JavaDoc<String JavaDoc, String JavaDoc> attributes = new Hashtable JavaDoc<String JavaDoc, String JavaDoc>();
220         attributes.put(NAME, INSTALLATION_NAME);
221         attributes.put(TYPE, SERVICE_TYPE);
222         ObjectName JavaDoc objName = new ObjectName JavaDoc(PETALS_DOMAIN, attributes);
223         objNames = mBeanServer.queryNames(objName, null);
224         if (objNames != null && objNames.size() == 1) {
225             result = (ObjectName JavaDoc) objNames.iterator().next();
226         }
227         return result;
228     }
229
230 }
231
Popular Tags