KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jmx > ManagementReprImplJSR160


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: ManagementReprImplJSR160.java,v 1.2 2005/04/28 08:43:24 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.jonas.jmx;
26
27 import java.util.HashSet JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.util.Set JavaDoc;
30
31 import javax.management.Attribute JavaDoc;
32 import javax.management.AttributeList JavaDoc;
33 import javax.management.MBeanException JavaDoc;
34 import javax.management.MBeanInfo JavaDoc;
35 import javax.management.MBeanServerConnection JavaDoc;
36 import javax.management.ObjectName JavaDoc;
37 import javax.management.ReflectionException JavaDoc;
38 import javax.management.RuntimeErrorException JavaDoc;
39 import javax.management.RuntimeMBeanException JavaDoc;
40 import javax.management.RuntimeOperationsException JavaDoc;
41 import javax.naming.Context JavaDoc;
42
43 import org.objectweb.jonas.common.Log;
44 import org.objectweb.util.monolog.api.BasicLevel;
45 import org.objectweb.util.monolog.api.Logger;
46
47 /**
48  * Provides a management representative to be used by JMX based management applications.
49  * @author Adriana Danes
50  */

51 public class ManagementReprImplJSR160 implements ManagementRepr {
52
53     /**
54      * JOnAS <code>logger</code>
55      */

56     private static Logger logger = null;
57     /**
58      * <code>mMBeanServerConnection</code> object representing the MBeanServer
59      * on the server side.
60      */

61     private MBeanServerConnection JavaDoc mbeanServerConnection = null;
62
63     /**
64      * ManagementRepr object constructor
65      */

66     protected ManagementReprImplJSR160() {
67         logger = Log.getLogger("org.objectweb.jonas.jmx");
68         if (logger.isLoggable(BasicLevel.DEBUG)) {
69             logger.log(BasicLevel.DEBUG, "Management Representativ based on JSR160 created for jonasAdmin");
70         }
71     }
72
73     /**
74      * @param on The object name of the MBean to be checked
75      * @return True if the MBean is already registered in the MBean server, false otherwise or if an exception is catched.
76      */

77     public boolean isRegistered(ObjectName JavaDoc on) {
78         try {
79             return mbeanServerConnection.isRegistered(on);
80         } catch (Exception JavaDoc e) {
81             return false;
82         }
83     }
84
85     /**
86      * @param on The ObjectName of the MBean from which the attribute is to be retrieved.
87      * @param attribute A String specifying the name of the attribute to be retrieve.
88      * @return The value of the attribute.
89      * @throws ManagementException management operation failed
90      */

91     public Object JavaDoc getAttribute(ObjectName JavaDoc on, String JavaDoc attribute)
92     throws ManagementException {
93
94         try {
95             return mbeanServerConnection.getAttribute(on, attribute);
96         } catch (Exception JavaDoc e) {
97             throw new ManagementException("Error while getting attribute " + attribute + ": "
98                     + e.getClass().getName(), e);
99         }
100     }
101
102     /**
103      * @param on The ObjectName of the MBean of which attribute are to be retrieved.
104      * @param attributes A list of the attributes to be retrieved.
105      * @return Thelist of retrieved attributes.
106      * @throws ManagementException management operation failed
107      */

108     public AttributeList JavaDoc getAttributes(ObjectName JavaDoc on, String JavaDoc[] attributes)
109     throws ManagementException {
110
111         try {
112             return mbeanServerConnection.getAttributes(on, attributes);
113         } catch (Exception JavaDoc e) {
114             throw new ManagementException("Error while getting attributes: "
115                     + e.getClass().getName(), e);
116         }
117     }
118
119     /**
120      * @param on The ObjectName of the MBean within which the attribute is to be set.
121      * @param attribute A String specifying the name of the attribute to be retrieve.
122      * @param value The value to set to the attribute.
123      * @throws ManagementException management operation failed
124      */

125     public void setAttribute(ObjectName JavaDoc on, String JavaDoc attribute, Object JavaDoc value)
126     throws ManagementException {
127         if(logger.isLoggable(BasicLevel.DEBUG)) {
128             logger.log(BasicLevel.DEBUG
129                     , "Set Attribute called, on " + on.toString() + " to change attribute " + attribute
130                     + " value to " + (String JavaDoc) value.toString());
131         }
132
133         try {
134             mbeanServerConnection.setAttribute(on, new Attribute JavaDoc(attribute, value));
135         } catch (Exception JavaDoc e) {
136             throw new ManagementException("Error while setting attribute " + attribute + ": "
137                     + e.getClass().getName(), e);
138         }
139     }
140
141     /**
142      * @param on The ObjectName of the MBean of which the attribute is to be set.
143      * @param attributes A list of attributes: The identification of the attribute to be set and the value it is to be set to
144      * @return The list of attributes that were set, with their new values.
145      * @throws ManagementException management operation failed
146      */

147     public AttributeList JavaDoc setAttributes(ObjectName JavaDoc on, AttributeList JavaDoc attributes)
148     throws ManagementException {
149
150         try {
151             return mbeanServerConnection.setAttributes(on, attributes);
152         } catch (Exception JavaDoc e) {
153             throw new ManagementException("Error while setting attributes: "
154                     + e.getClass().getName(), e);
155         }
156     }
157
158     /**
159      * @param on
160      * @throws ManagementException management operation failed
161      */

162     public Object JavaDoc invoke(ObjectName JavaDoc on, String JavaDoc operation, Object JavaDoc[] param, String JavaDoc[] signature)
163     throws ManagementException {
164
165         try {
166             return mbeanServerConnection.invoke(on, operation, param, signature);
167         } catch (Exception JavaDoc e) {
168             String JavaDoc message = "";
169             String JavaDoc targetExcName = null;
170             Throwable JavaDoc exc = null;
171             if (e instanceof MBeanException JavaDoc ||
172                     e instanceof ReflectionException JavaDoc ||
173                     e instanceof RuntimeMBeanException JavaDoc ||
174                     e instanceof RuntimeOperationsException JavaDoc ||
175                     e instanceof RuntimeErrorException JavaDoc) {
176
177                 Exception JavaDoc targetExc = null;
178                 if (e instanceof MBeanException JavaDoc) {
179                     targetExc = ((MBeanException JavaDoc) e).getTargetException();
180                 }
181                 else if (e instanceof ReflectionException JavaDoc) {
182                     targetExc = ((ReflectionException JavaDoc) e).getTargetException();
183                 }
184                 else if (e instanceof RuntimeMBeanException JavaDoc) {
185                     targetExc = ((RuntimeMBeanException JavaDoc) e).getTargetException();
186                 }
187                 else if (e instanceof RuntimeOperationsException JavaDoc) {
188                     targetExc = ((RuntimeOperationsException JavaDoc) e).getTargetException();
189                 }
190                 else if (e instanceof RuntimeErrorException JavaDoc) {
191                     Error JavaDoc atargetExc = ((RuntimeErrorException JavaDoc) e).getTargetError();
192                     targetExc = new Exception JavaDoc(atargetExc.getMessage());
193                 }
194                 targetExcName = targetExc.toString();
195                 exc = targetExc;
196             }
197             else {
198                 exc = e;
199             }
200             if(logger.isLoggable(BasicLevel.DEBUG)) {
201                 logger.log(BasicLevel.DEBUG
202                         , "Exception ----[ " + e.toString() + " while invoking operation " + operation +
203                         " on MBean " + on.toString() + " ]-------");
204                 if (targetExcName != null) {
205                     logger.log(BasicLevel.DEBUG, "-------[ Embedded error : ]-------");
206                     logger.log(BasicLevel.DEBUG, "-------[ " + targetExcName + " ]-------");
207                 }
208             }
209
210             throw new ManagementException(message, exc);
211         }
212     }
213
214     /**
215      * @return A set containing the ObjectNames for the MBeans selected.
216      * @throws ManagementException management operation failed
217      */

218     public java.util.Set JavaDoc queryNames(ObjectName JavaDoc on)
219     throws ManagementException {
220         try {
221             return (java.util.Set JavaDoc) mbeanServerConnection.queryNames(on, null);
222         } catch (Exception JavaDoc e) {
223             throw new ManagementException("Error while getting MBeans names: " + e.getClass().getName()
224                     , e);
225         }
226     }
227
228     /**
229      * @return An instance of MBeanInfo allowing the retrieval of all
230      * attributes and operations of this MBean.
231      * @throws ManagementException management operation failed
232      */

233     public MBeanInfo JavaDoc getMBeanInfo(ObjectName JavaDoc name)
234     throws ManagementException {
235         try {
236             return (MBeanInfo JavaDoc) mbeanServerConnection.getMBeanInfo(name);
237         } catch (Exception JavaDoc e) {
238             throw new ManagementException("Error while getting MBean info: " + e.getClass().getName()
239                     , e);
240         }
241     }
242
243     /**
244      * This method is no more necessary with JSR160 support
245      * @return null
246      * @throws javax.naming.NamingException not possible anymore
247      */

248     public Context JavaDoc getContext() throws javax.naming.NamingException JavaDoc {
249         return null;
250     }
251
252     /**
253      * This method is no more necessary with JSR160 support
254      * @return a non null String
255      */

256     public String JavaDoc getCurrentRMIConnectorName() {
257         return "jonas";
258     }
259
260     /**
261      * This method is no more necessary with JSR160 support
262      */

263     public void setCurrentRMIConnectorName(String JavaDoc name) throws Exception JavaDoc {
264         ;
265     }
266
267     /**
268      * This method is no more necessary with JSR160 support
269      */

270     public void resetCurrentRMIConnectorName() {
271         ;
272     }
273
274     /**
275      * This method is no more necessary with JSR160 support
276      * @return null
277      * @throws javax.naming.NamingException not possible anymore
278      */

279     public Set JavaDoc getRMIConnectorsNames() throws javax.naming.NamingException JavaDoc {
280         HashSet JavaDoc res = new HashSet JavaDoc();
281         res.add("jonas");
282         return res;
283     }
284
285     /**
286      * This method is no more necessary with JSR160 support
287      * @return null
288      */

289     public String JavaDoc getJonasNamingServiceURL() {
290         return "url";
291     }
292
293     /**
294      * This method is no more necessary with JSR160 support
295      * @throws javax.naming.NamingException not possible anymore
296      */

297     public void setJonasNamingServiceURL(String JavaDoc url) throws javax.naming.NamingException JavaDoc {
298         ;
299     }
300
301     /**
302      * This method is no more necessary with JSR160 support
303      * @throws javax.naming.NamingException not possible anymore
304      */

305     public void setNamingEnvCtx(Properties JavaDoc env) throws javax.naming.NamingException JavaDoc {
306         ;
307     }
308     /**
309      * @return Returns the mbeanServerConnection.
310      */

311     public MBeanServerConnection JavaDoc getMBeanServerConnection() {
312         return mbeanServerConnection;
313     }
314     /**
315      * @param mbeanServerConnection The mbeanServerConnection to set.
316      */

317     public void setMBeanServerConnection(
318             MBeanServerConnection JavaDoc mbeanServerConnection) {
319         this.mbeanServerConnection = mbeanServerConnection;
320     }
321 }
322
Popular Tags