001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: ManagementReprImplJSR160.java 9824 2006-11-09 09:30:01Z danesa $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.jmx;
025:
026: import java.util.HashSet;
027: import java.util.Properties;
028: import java.util.Set;
029:
030: import javax.management.Attribute;
031: import javax.management.AttributeList;
032: import javax.management.MBeanException;
033: import javax.management.MBeanInfo;
034: import javax.management.MBeanServerConnection;
035: import javax.management.ObjectName;
036: import javax.management.ReflectionException;
037: import javax.management.RuntimeErrorException;
038: import javax.management.RuntimeMBeanException;
039: import javax.management.RuntimeOperationsException;
040: import javax.naming.Context;
041:
042: import org.objectweb.jonas.common.Log;
043: import org.objectweb.util.monolog.api.BasicLevel;
044: import org.objectweb.util.monolog.api.Logger;
045:
046: /**
047: * Provides a management representative to be used by JMX based management applications.
048: * @author Adriana Danes
049: */
050: public class ManagementReprImplJSR160 implements ManagementRepr {
051:
052: /**
053: * JOnAS <code>logger</code>
054: */
055: private static Logger logger = null;
056: /**
057: * <code>mMBeanServerConnection</code> object representing the MBeanServer
058: * on the server side.
059: */
060: private MBeanServerConnection mbeanServerConnection = null;
061: /**
062: * sever name
063: */
064: private String serverName = null;
065:
066: /**
067: * ManagementRepr object constructor
068: */
069: protected ManagementReprImplJSR160() {
070: logger = Log.getLogger("org.objectweb.jonas.jmx");
071: if (logger.isLoggable(BasicLevel.DEBUG)) {
072: logger
073: .log(BasicLevel.DEBUG,
074: "Management Representativ based on JSR160 created for jonasAdmin");
075: }
076: }
077:
078: /**
079: * @param on The object name of the MBean to be checked
080: * @return True if the MBean is already registered in the MBean server, false otherwise or if an exception is catched.
081: */
082: public boolean isRegistered(ObjectName on) {
083: try {
084: return mbeanServerConnection.isRegistered(on);
085: } catch (Exception e) {
086: return false;
087: }
088: }
089:
090: /**
091: * @param on The ObjectName of the MBean from which the attribute is to be retrieved.
092: * @param attribute A String specifying the name of the attribute to be retrieve.
093: * @return The value of the attribute.
094: * @throws ManagementException management operation failed
095: */
096: public Object getAttribute(ObjectName on, String attribute)
097: throws ManagementException {
098: // DEBUG
099: if (mbeanServerConnection == null) {
100: logger.log(BasicLevel.ERROR,
101: "mbeanServerConnection is null");
102: throw new ManagementException(
103: "mbeanServerConnection is null");
104: }
105: try {
106: return mbeanServerConnection.getAttribute(on, attribute);
107: } catch (Exception e) {
108: logger.log(BasicLevel.WARN,
109: "Error while getting attribute " + attribute
110: + ". ObjectName = " + on);
111: throw new ManagementException(
112: "Error while getting attribute. ", e);
113: }
114: }
115:
116: /**
117: * @param on The ObjectName of the MBean of which attribute are to be retrieved.
118: * @param attributes A list of the attributes to be retrieved.
119: * @return Thelist of retrieved attributes.
120: * @throws ManagementException management operation failed
121: */
122: public AttributeList getAttributes(ObjectName on,
123: String[] attributes) throws ManagementException {
124:
125: try {
126: return mbeanServerConnection.getAttributes(on, attributes);
127: } catch (Exception e) {
128: throw new ManagementException(
129: "Error while getting attributes: "
130: + e.getClass().getName(), e);
131: }
132: }
133:
134: /**
135: * @param on The ObjectName of the MBean of which the attribute is to be set.
136: * @param attribute A String specifying the name of the attribute to be retrieve.
137: * @param value The value to set to the attribute.
138: * @throws ManagementException management operation failed
139: */
140: public void setAttribute(ObjectName on, String attribute,
141: Object value) throws ManagementException {
142: if (logger.isLoggable(BasicLevel.DEBUG)) {
143: logger.log(BasicLevel.DEBUG, "Set Attribute called, on "
144: + on.toString() + " to change attribute "
145: + attribute + " value to "
146: + (String) value.toString());
147: }
148:
149: try {
150: mbeanServerConnection.setAttribute(on, new Attribute(
151: attribute, value));
152: } catch (Exception e) {
153: throw new ManagementException(
154: "Error while setting attribute " + attribute + ": "
155: + e.getClass().getName(), e);
156: }
157: }
158:
159: /**
160: * @param on The ObjectName of the MBean of which the attribute is to be set.
161: * @param attributes A list of attributes: The identification of the attribute to be set and the value it is to be set to
162: * @return The list of attributes that were set, with their new values.
163: * @throws ManagementException management operation failed
164: */
165: public AttributeList setAttributes(ObjectName on,
166: AttributeList attributes) throws ManagementException {
167:
168: try {
169: return mbeanServerConnection.setAttributes(on, attributes);
170: } catch (Exception e) {
171: throw new ManagementException(
172: "Error while setting attributes: "
173: + e.getClass().getName(), e);
174: }
175: }
176:
177: /**
178: * @param on The ObjectName of the MBean of which an operation is to be executed
179: * @param operation name of the operation to execute
180: * @param param arguments of the operation to execute
181: * @param signature types of arguments
182: * @return return the operation result
183: * @throws ManagementException management operation failed
184: */
185: public Object invoke(ObjectName on, String operation,
186: Object[] param, String[] signature)
187: throws ManagementException {
188:
189: try {
190: return mbeanServerConnection.invoke(on, operation, param,
191: signature);
192: } catch (Exception e) {
193: String message = "";
194: String targetExcName = null;
195: Throwable exc = null;
196: if (e instanceof MBeanException
197: || e instanceof ReflectionException
198: || e instanceof RuntimeMBeanException
199: || e instanceof RuntimeOperationsException
200: || e instanceof RuntimeErrorException) {
201:
202: Exception targetExc = null;
203: if (e instanceof MBeanException) {
204: targetExc = ((MBeanException) e)
205: .getTargetException();
206: } else if (e instanceof ReflectionException) {
207: targetExc = ((ReflectionException) e)
208: .getTargetException();
209: } else if (e instanceof RuntimeMBeanException) {
210: targetExc = ((RuntimeMBeanException) e)
211: .getTargetException();
212: } else if (e instanceof RuntimeOperationsException) {
213: targetExc = ((RuntimeOperationsException) e)
214: .getTargetException();
215: } else if (e instanceof RuntimeErrorException) {
216: Error atargetExc = ((RuntimeErrorException) e)
217: .getTargetError();
218: targetExc = new Exception(atargetExc.getMessage());
219: }
220: targetExcName = targetExc.toString();
221: exc = targetExc;
222: } else {
223: exc = e;
224: }
225: if (logger.isLoggable(BasicLevel.DEBUG)) {
226: logger.log(BasicLevel.DEBUG, "Exception ----[ "
227: + e.toString() + " while invoking operation "
228: + operation + " on MBean " + on.toString()
229: + " ]-------");
230: if (targetExcName != null) {
231: logger.log(BasicLevel.DEBUG,
232: "-------[ Embedded error : ]-------");
233: logger.log(BasicLevel.DEBUG, "-------[ "
234: + targetExcName + " ]-------");
235: }
236: }
237:
238: throw new ManagementException(message, exc);
239: }
240: }
241:
242: /**
243: * @param on The ObjectName of the target MBean
244: * @return A set containing the ObjectNames for the MBeans selected.
245: * @throws ManagementException management operation failed
246: */
247: public java.util.Set queryNames(ObjectName on)
248: throws ManagementException {
249: try {
250: return (java.util.Set) mbeanServerConnection.queryNames(on,
251: null);
252: } catch (Exception e) {
253: throw new ManagementException(
254: "Error while getting MBeans names: "
255: + e.getClass().getName(), e);
256: }
257: }
258:
259: /**
260: * @return An instance of MBeanInfo allowing the retrieval of all
261: * attributes and operations of this MBean.
262: * @throws ManagementException management operation failed
263: */
264: public MBeanInfo getMBeanInfo(ObjectName name)
265: throws ManagementException {
266: try {
267: return (MBeanInfo) mbeanServerConnection.getMBeanInfo(name);
268: } catch (Exception e) {
269: throw new ManagementException(
270: "Error while getting MBean info: "
271: + e.getClass().getName(), e);
272: }
273: }
274:
275: /**
276: * This method is no more necessary with JSR160 support
277: * @return null
278: * @throws javax.naming.NamingException not possible anymore
279: */
280: public Context getContext() throws javax.naming.NamingException {
281: return null;
282: }
283:
284: /**
285: * This method is no more necessary with JSR160 support
286: * @return a non null String
287: */
288: public String getCurrentRMIConnectorName() {
289: return "jonas";
290: }
291:
292: /**
293: * This method is no more necessary with JSR160 support
294: */
295: public void setCurrentRMIConnectorName(String name)
296: throws Exception {
297: ;
298: }
299:
300: /**
301: * This method is no more necessary with JSR160 support
302: */
303: public void resetCurrentRMIConnectorName() {
304: ;
305: }
306:
307: /**
308: * This method is no more necessary with JSR160 support
309: * @return null
310: * @throws javax.naming.NamingException not possible anymore
311: */
312: public Set getRMIConnectorsNames()
313: throws javax.naming.NamingException {
314: HashSet res = new HashSet();
315: res.add("jonas");
316: return res;
317: }
318:
319: /**
320: * This method is no more necessary with JSR160 support
321: * @return null
322: */
323: public String getJonasNamingServiceURL() {
324: return "url";
325: }
326:
327: /**
328: * This method is no more necessary with JSR160 support
329: * @throws javax.naming.NamingException not possible anymore
330: */
331: public void setJonasNamingServiceURL(String url)
332: throws javax.naming.NamingException {
333: ;
334: }
335:
336: /**
337: * This method is no more necessary with JSR160 support
338: * @throws javax.naming.NamingException not possible anymore
339: */
340: public void setNamingEnvCtx(Properties env)
341: throws javax.naming.NamingException {
342: ;
343: }
344:
345: /**
346: * @return Returns the mbeanServerConnection.
347: */
348: public MBeanServerConnection getMBeanServerConnection() {
349: return mbeanServerConnection;
350: }
351:
352: /**
353: * @param mbeanServerConnection The mbeanServerConnection to set.
354: */
355: public void setMBeanServerConnection(
356: MBeanServerConnection mbeanServerConnection) {
357: this .mbeanServerConnection = mbeanServerConnection;
358: }
359:
360: public String getServerName() {
361: return serverName;
362: }
363:
364: public void setServerName(String serverName) {
365: this.serverName = serverName;
366: }
367: }
|