KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > kernel > admin > DistributedJMXServer


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: DistributedJMXServer.java 16:40:02 ddesjardins $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.kernel.admin;
23
24 import java.io.IOException JavaDoc;
25 import java.util.Hashtable JavaDoc;
26 import java.util.Set JavaDoc;
27
28 import javax.management.InstanceNotFoundException JavaDoc;
29 import javax.management.MBeanException JavaDoc;
30 import javax.management.MBeanServerConnection JavaDoc;
31 import javax.management.MalformedObjectNameException JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33 import javax.management.QueryExp JavaDoc;
34 import javax.management.ReflectionException JavaDoc;
35 import javax.management.remote.JMXConnector JavaDoc;
36
37 /**
38  * This object is a connection to a JMX server
39  *
40  * @author ddesjardins - eBMWebsourcing
41  */

42 public class DistributedJMXServer {
43     
44     /**
45      * The Petals JMX Domain
46      */

47     public static final String JavaDoc PETALS_DOMAIN = "Petals";
48
49     /**
50      * Name of the method getServiceDescription
51      */

52     public static final String JavaDoc GET_SERVICE_DESCRITION = "getServiceDescription";
53
54     /**
55      * Name of the method isExchangeWithConsumerOkayForComponent
56      */

57     public static final String JavaDoc IS_OK_WITH_CONS = "isExchangeWithConsumerOkayForComponent";
58
59     /**
60      * Name of the method isExchangeWithProviderOkayForComponent
61      */

62     public static final String JavaDoc IS_OK_WITH_PROV = "isExchangeWithProviderOkayForComponent";
63
64     /**
65      * Connection to the JMX server
66      */

67     private MBeanServerConnection JavaDoc connection;
68
69     /**
70      * JMX Connector
71      */

72     private JMXConnector JavaDoc connector;
73
74     public DistributedJMXServer(JMXConnector JavaDoc connector) {
75         this.connector = connector;
76     }
77
78     protected MBeanServerConnection JavaDoc getMBeanServerConnection() {
79         if (connection == null) {
80             try {
81                 connector.connect();
82                 connection = connector.getMBeanServerConnection();
83             } catch (IOException JavaDoc e) {
84                 e.printStackTrace();
85             }
86         }
87         return this.connection;
88     }
89
90     /**
91      * Retreive the object name of the admin service
92      *
93      * @return object name
94      * @throws IOException
95      * @throws NullPointerException
96      * @throws MalformedObjectNameException
97      */

98     public ObjectName JavaDoc getAdminServiceMBeanName() throws IOException JavaDoc,
99         MalformedObjectNameException JavaDoc, NullPointerException JavaDoc {
100         if (connection == null) {
101             connector.connect();
102             connection = connector.getMBeanServerConnection();
103         }
104         ObjectName JavaDoc result = null;
105         Set JavaDoc objNames;
106         Hashtable JavaDoc<String JavaDoc, String JavaDoc> attrs = new Hashtable JavaDoc<String JavaDoc, String JavaDoc>();
107         attrs.put("name", "Admin");
108         attrs.put("type", "service");
109         ObjectName JavaDoc objName = new ObjectName JavaDoc(PETALS_DOMAIN, attrs);
110         objNames = connection.queryNames(objName, null);
111         if (objNames != null && objNames.size() == 1) {
112             result = (ObjectName JavaDoc) objNames.iterator().next();
113         }
114         return result;
115     }
116
117     public Object JavaDoc invoke(ObjectName JavaDoc name, String JavaDoc operationName,
118             Object JavaDoc[] params, String JavaDoc[] signature)
119         throws InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc,
120         IOException JavaDoc {
121         if (connection == null) {
122             connector.connect();
123             connection = connector.getMBeanServerConnection();
124         }
125         Object JavaDoc out = null;
126         out = connection.invoke(name, operationName, params, signature);
127         return out;
128     }
129
130     public Set JavaDoc queryNames(ObjectName JavaDoc name, QueryExp JavaDoc queryExp)
131         throws InstanceNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc,
132         IOException JavaDoc {
133         if (connection == null) {
134             connector.connect();
135             connection = connector.getMBeanServerConnection();
136         }
137         Set JavaDoc out = null;
138         out = connection.queryNames(name, queryExp);
139         return out;
140     }
141
142     public void closeConnector() throws IOException JavaDoc {
143         connector.close();
144     }
145
146 }
147
Popular Tags