KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > tools > ant > 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.ant.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 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 getComponentMBeanName(
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=engine,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         if (result == null) {
109             pattern = "*:type=binding,name=" + name;
110             objName = new ObjectName JavaDoc(pattern);
111             objNames = mBeanServer.queryNames(objName, null);
112             if (objNames != null && objNames.size() == 1) {
113                 result = (ObjectName JavaDoc) objNames.iterator().next();
114             }
115         }
116         return result;
117     }
118
119     /**
120      * Get a JMX connector
121      *
122      * @param host
123      * host
124      * @param port
125      * port
126      * @param username
127      * username
128      * @param password
129      * password
130      * @return jmx connector
131      * @throws IOException
132      */

133     public static JMXConnector JavaDoc getConnection(String JavaDoc host, String JavaDoc port,
134             String JavaDoc username, String JavaDoc password) throws IOException JavaDoc {
135         JMXConnector JavaDoc connector = null;
136         Properties JavaDoc env = System.getProperties();
137         env.put("java.naming.factory.initial",
138                 "com.sun.jndi.rmi.registry.RegistryContextFactory");
139         Map JavaDoc<String JavaDoc, Object JavaDoc> args = new HashMap JavaDoc<String JavaDoc, Object JavaDoc>();
140         if (!"".equals(username)) {
141             if (password == null) {
142                 password = "";
143             }
144             String JavaDoc[] credentials = new String JavaDoc[] {username, password};
145             args.put("jmx.remote.credentials", credentials);
146         }
147         String JavaDoc urlStr = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port
148                 + "/management/rmi-jmx-connector";
149         JMXServiceURL JavaDoc url = new JMXServiceURL JavaDoc(urlStr);
150         connector = JMXConnectorFactory.connect(url, args);
151         return connector;
152     }
153
154     /**
155      * Retreive the object name of the deployment service
156      *
157      * @param mBeanServer
158      * mbean server
159      * @return object name
160      * @throws IOException
161      * @throws NullPointerException
162      * @throws MalformedObjectNameException
163      */

164     public static ObjectName JavaDoc getDeploymentServiceMBeanName(
165             MBeanServerConnection JavaDoc mBeanServer) throws IOException JavaDoc,
166         MalformedObjectNameException JavaDoc, NullPointerException JavaDoc {
167         ObjectName JavaDoc result = null;
168         Set JavaDoc objNames;
169         Hashtable JavaDoc<String JavaDoc, String JavaDoc> attributes = new Hashtable JavaDoc<String JavaDoc, String JavaDoc>();
170         attributes.put(NAME, DEPLOYMENT_NAME);
171         attributes.put(TYPE, SERVICE_TYPE);
172         ObjectName JavaDoc objName = new ObjectName JavaDoc(PETALS_DOMAIN, attributes);
173         objNames = mBeanServer.queryNames(objName, null);
174         if (objNames != null && objNames.size() == 1) {
175             result = (ObjectName JavaDoc) objNames.iterator().next();
176         }
177         return result;
178     }
179
180     /**
181      * Retreive the object name of the installation service
182      *
183      * @param mBeanServer
184      * mbean server
185      * @return object name
186      * @throws IOException
187      * @throws NullPointerException
188      * @throws MalformedObjectNameException
189      */

190     public static ObjectName JavaDoc getInstallationServiceMBeanName(
191             MBeanServerConnection JavaDoc mBeanServer) throws IOException JavaDoc,
192         MalformedObjectNameException JavaDoc, NullPointerException JavaDoc {
193         ObjectName JavaDoc result = null;
194         Set JavaDoc objNames;
195         Hashtable JavaDoc<String JavaDoc, String JavaDoc> attributes = new Hashtable JavaDoc<String JavaDoc, String JavaDoc>();
196         attributes.put(NAME, INSTALLATION_NAME);
197         attributes.put(TYPE, SERVICE_TYPE);
198         ObjectName JavaDoc objName = new ObjectName JavaDoc(PETALS_DOMAIN, attributes);
199         objNames = mBeanServer.queryNames(objName, null);
200         if (objNames != null && objNames.size() == 1) {
201             result = (ObjectName JavaDoc) objNames.iterator().next();
202         }
203         return result;
204     }
205
206 }
207
Popular Tags