KickJava   Java API By Example, From Geeks To Geeks.

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


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: ManagementReprImpl.java,v 1.9 2005/04/28 08:43:24 benoitf Exp $
23  * --------------------------------------------------------------------------
24  */

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

49 public class ManagementReprImpl implements ManagementRepr
50 {
51
52     static private Logger logger = null;
53
54     protected ManagementReprImpl()
55     {
56         logger = Log.getLogger("org.objectweb.jonas.jmx");
57         if (logger.isLoggable(BasicLevel.DEBUG)) {
58             logger.log(BasicLevel.DEBUG, "Management Representativ created for jonasAdmin");
59         }
60     }
61
62     /**
63      * @return True if the MBean is already registered in the MBean server, false otherwise or if an exception is catched.
64      */

65     public boolean isRegistered(ObjectName JavaDoc on) {
66         try {
67             return ConnectorFactory.getRMIConnector().isRegistered(on);
68         } catch (Exception JavaDoc e) {
69             return false;
70         }
71     }
72
73     /**
74      * @param on The ObjectName of the MBean from which the attribute is to be retrieved.
75      * @param attribute A String specifying the name of the attribute to be retrieve.
76      * @return The value of the attribute.
77      */

78     public Object JavaDoc getAttribute(ObjectName JavaDoc on, String JavaDoc attribute)
79         throws ManagementException {
80
81         try {
82             return ConnectorFactory.getRMIConnector().getAttribute(on, attribute);
83         } catch (Exception JavaDoc e) {
84             throw new ManagementException("Error while getting attribute " + attribute + " from " + on + ": "
85                                               + e.getClass().getName(), e);
86         }
87     }
88
89     /**
90      * @param on The ObjectName of the MBean of which attribute are to be retrieved.
91      * @param attributes A list of the attributes to be retrieved.
92      * @return Thelist of retrieved attributes.
93      */

94     public AttributeList JavaDoc getAttributes(ObjectName JavaDoc on, String JavaDoc[] attributes)
95         throws ManagementException {
96
97         try {
98             return ConnectorFactory.getRMIConnector().getAttributes(on, attributes);
99         } catch (Exception JavaDoc e) {
100             throw new ManagementException("Error while getting attributes: "
101                                           + e.getClass().getName(), e);
102         }
103     }
104
105         /**
106          * @param on The ObjectName of the MBean within which the attribute is to be set.
107          * @param attribute A String specifying the name of the attribute to be retrieve.
108          * @param value The value to set to the attribute.
109          */

110         public void setAttribute(ObjectName JavaDoc on, String JavaDoc attribute, Object JavaDoc value)
111             throws ManagementException {
112
113             //Logger logger = Log.getLogger("org.objectweb.jonas.jmx");
114
if(logger.isLoggable(BasicLevel.DEBUG))
115                 logger.log(BasicLevel.DEBUG
116                            , "Set Attribute called, on " + on.toString() + " to change attribute " + attribute
117                            + " value to " + (String JavaDoc) value.toString());
118             try {
119                 ConnectorFactory.getRMIConnector().setAttribute(on, new Attribute JavaDoc(attribute, value));
120             } catch (Exception JavaDoc e) {
121                 throw new ManagementException("Error while setting attribute " + attribute + ": "
122                                                       + e.getClass().getName(), e);
123             }
124         }
125
126     /**
127      * @param on The ObjectName of the MBean of which the attribute is to be set.
128      * @param attributes A list of attributes: The identification of the attribute to be set and the value it is to be set to
129      * @return The list of attributes that were set, with their new values.
130      */

131     public AttributeList JavaDoc setAttributes(ObjectName JavaDoc on, AttributeList JavaDoc attributes)
132         throws ManagementException {
133
134         try {
135             return ConnectorFactory.getRMIConnector().setAttributes(on, attributes);
136         } catch (Exception JavaDoc e) {
137             throw new ManagementException("Error while setting attributes: "
138                                           + e.getClass().getName(), e);
139         }
140     }
141
142     /**
143      * @param on
144      */

145     public Object JavaDoc invoke(ObjectName JavaDoc on, String JavaDoc operation, Object JavaDoc[] param, String JavaDoc[] signature)
146             throws ManagementException {
147
148         try {
149             return ConnectorFactory.getRMIConnector().invoke(on, operation, param, signature);
150         }
151         catch (Exception JavaDoc e) {
152             String JavaDoc message = "";
153             String JavaDoc targetExcName = null;
154             Throwable JavaDoc exc = null;
155             if (e instanceof MBeanException JavaDoc ||
156                 e instanceof ReflectionException JavaDoc ||
157                 e instanceof RuntimeMBeanException JavaDoc ||
158                 e instanceof RuntimeOperationsException JavaDoc ||
159                 e instanceof RuntimeErrorException JavaDoc) {
160
161                 Exception JavaDoc targetExc = null;
162                 if (e instanceof MBeanException JavaDoc) {
163                     targetExc = ((MBeanException JavaDoc) e).getTargetException();
164                 }
165                 else if (e instanceof ReflectionException JavaDoc) {
166                     targetExc = ((ReflectionException JavaDoc) e).getTargetException();
167                 }
168                 else if (e instanceof RuntimeMBeanException JavaDoc) {
169                     targetExc = ((RuntimeMBeanException JavaDoc) e).getTargetException();
170                 }
171                 else if (e instanceof RuntimeOperationsException JavaDoc) {
172                     targetExc = ((RuntimeOperationsException JavaDoc) e).getTargetException();
173                 }
174                 else if (e instanceof RuntimeErrorException JavaDoc) {
175                     Error JavaDoc atargetExc = ((RuntimeErrorException JavaDoc) e).getTargetError();
176                     targetExc = new Exception JavaDoc(atargetExc.getMessage());
177                 }
178                 targetExcName = targetExc.toString();
179                 exc = targetExc;
180             }
181             else {
182                 exc = e;
183             }
184             if(logger.isLoggable(BasicLevel.DEBUG)) {
185                 logger.log(BasicLevel.DEBUG
186                     , "Exception ----[ " + e.toString() + " while invoking operation " + operation +
187                     " on MBean " + on.toString() + " ]-------");
188                 if (targetExcName != null) {
189                     logger.log(BasicLevel.DEBUG, "-------[ Embedded error : ]-------");
190                     logger.log(BasicLevel.DEBUG, "-------[ " + targetExcName + " ]-------");
191                 }
192             }
193
194             throw new ManagementException(message, exc);
195         }
196     }
197
198         /**
199          * @return A set containing the ObjectNames for the MBeans selected.
200          */

201         public java.util.Set JavaDoc queryNames(ObjectName JavaDoc on)
202             throws ManagementException
203             {
204                 try
205                     {
206                         return (java.util.Set JavaDoc) ConnectorFactory.getRMIConnector().queryNames(on, null);
207                     }
208                 catch (Exception JavaDoc e)
209                     {
210                         throw new ManagementException("Error while getting MBeans names: " + e.getClass().getName()
211                                                       , e);
212                     }
213             }
214
215         /**
216          * @return An instance of MBeanInfo allowing the retrieval of all
217          * attributes and operations of this MBean.
218          */

219         public MBeanInfo JavaDoc getMBeanInfo(ObjectName JavaDoc name)
220             throws ManagementException
221             {
222                 try
223                     {
224                         return (MBeanInfo JavaDoc) ConnectorFactory.getRMIConnector().getMBeanInfo(name);
225                     }
226                 catch (Exception JavaDoc e)
227                     {
228                         throw new ManagementException("Error while getting MBean info: " + e.getClass().getName()
229                                                       , e);
230                     }
231             }
232
233         /**
234          * @return Context the current application context, create an initial context
235          * if there is no current context.
236          */

237         public Context JavaDoc getContext()
238             throws javax.naming.NamingException JavaDoc
239             {
240                 return ConnectorFactory.getContext();
241             }
242
243         /**
244          * @return String the name of the current RMI connector.
245          * Return null if no RMI connector is available.
246          */

247         public String JavaDoc getCurrentRMIConnectorName()
248             {
249                 return ConnectorFactory.getCurrentRMIConnectorName();
250             }
251
252         /**
253          * Set the currentRMIConnectorName to the specified value
254          */

255         public void setCurrentRMIConnectorName(String JavaDoc name)
256             throws Exception JavaDoc
257             {
258                 ConnectorFactory.setCurrentRMIConnectorName(name);
259             }
260
261         /**
262          * Set the currentRMIConnectorName to null
263          */

264         public void resetCurrentRMIConnectorName()
265             {
266                 ConnectorFactory.resetCurrentRMIConnectorName();
267             }
268
269         /**
270          * @return Set a set containning all RMI connector names available in the current context.
271          */

272         public Set JavaDoc getRMIConnectorsNames()
273             throws javax.naming.NamingException JavaDoc
274             {
275                 return ConnectorFactory.getRMIConnectorsNames();
276             }
277
278         /**
279          * @return String the value of the PROVIDER_URL property in the current context.
280          */

281         public String JavaDoc getJonasNamingServiceURL()
282             {
283                 return ConnectorFactory.getJonasNamingServiceURL();
284             }
285
286         /**
287          * Sets the PROVIDER_URL property to the specified value.
288          */

289         public void setJonasNamingServiceURL(String JavaDoc url)
290             throws javax.naming.NamingException JavaDoc
291             {
292                 ConnectorFactory.setJonasNamingServiceURL(url);
293             }
294
295         /**
296          * Create a new naming context based on the given env. properties
297          * @param env properties to create a new naming context
298          */

299         public void setNamingEnvCtx(Properties JavaDoc env) throws javax.naming.NamingException JavaDoc {
300             ConnectorFactory.setNamingEnvCtx(env);
301         }
302 }
303
Popular Tags