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: ManagementReprImpl.java 9148 2006-07-12 06:26:36Z durieuxp $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.jmx;
025:
026: import java.util.Properties;
027: import java.util.Set;
028:
029: import javax.management.Attribute;
030: import javax.management.AttributeList;
031: import javax.management.MBeanException;
032: import javax.management.MBeanInfo;
033: import javax.management.ObjectName;
034: import javax.management.ReflectionException;
035: import javax.management.RuntimeErrorException;
036: import javax.management.RuntimeMBeanException;
037: import javax.management.RuntimeOperationsException;
038: import javax.naming.Context;
039:
040: import org.objectweb.jonas.common.Log;
041: import org.objectweb.util.monolog.api.BasicLevel;
042: import org.objectweb.util.monolog.api.Logger;
043:
044: /**
045: * Provides a management representative to be used by JMX based management applications.
046: * @author Adriana Danes
047: */
048: public class ManagementReprImpl implements ManagementRepr {
049:
050: static private Logger logger = null;
051:
052: protected ManagementReprImpl() {
053: logger = Log.getLogger("org.objectweb.jonas.jmx");
054: if (logger.isLoggable(BasicLevel.DEBUG)) {
055: logger.log(BasicLevel.DEBUG,
056: "Management Representativ created for jonasAdmin");
057: }
058: }
059:
060: /**
061: * @return True if the MBean is already registered in the MBean server, false otherwise or if an exception is catched.
062: */
063: public boolean isRegistered(ObjectName on) {
064: try {
065: return ConnectorFactory.getRMIConnector().isRegistered(on);
066: } catch (Exception e) {
067: return false;
068: }
069: }
070:
071: /**
072: * @param on The ObjectName of the MBean from which the attribute is to be retrieved.
073: * @param attribute A String specifying the name of the attribute to be retrieve.
074: * @return The value of the attribute.
075: */
076: public Object getAttribute(ObjectName on, String attribute)
077: throws ManagementException {
078:
079: try {
080: // DEBUG
081: if (ConnectorFactory.getRMIConnector() == null) {
082: throw new ManagementException("Null RMIConnector");
083: }
084: return ConnectorFactory.getRMIConnector().getAttribute(on,
085: attribute);
086: } catch (Exception e) {
087: logger.log(BasicLevel.WARN,
088: "Error while getting attribute on " + on);
089: throw new ManagementException(
090: "Error while getting attribute " + attribute
091: + " from " + on + ": "
092: + e.getClass().getName(), e);
093: }
094: }
095:
096: /**
097: * @param on The ObjectName of the MBean of which attribute are to be retrieved.
098: * @param attributes A list of the attributes to be retrieved.
099: * @return Thelist of retrieved attributes.
100: */
101: public AttributeList getAttributes(ObjectName on,
102: String[] attributes) throws ManagementException {
103:
104: try {
105: return ConnectorFactory.getRMIConnector().getAttributes(on,
106: attributes);
107: } catch (Exception e) {
108: logger.log(BasicLevel.WARN,
109: "Error while getting attributes on " + on);
110: throw new ManagementException(
111: "Error while getting attributes: "
112: + e.getClass().getName(), e);
113: }
114: }
115:
116: /**
117: * @param on The ObjectName of the MBean within which the attribute is to be set.
118: * @param attribute A String specifying the name of the attribute to be retrieve.
119: * @param value The value to set to the attribute.
120: */
121: public void setAttribute(ObjectName on, String attribute,
122: Object value) throws ManagementException {
123:
124: //Logger logger = Log.getLogger("org.objectweb.jonas.jmx");
125: if (logger.isLoggable(BasicLevel.DEBUG))
126: logger.log(BasicLevel.DEBUG, "Set Attribute called, on "
127: + on.toString() + " to change attribute "
128: + attribute + " value to "
129: + (String) value.toString());
130: try {
131: ConnectorFactory.getRMIConnector().setAttribute(on,
132: new Attribute(attribute, value));
133: } catch (Exception e) {
134: throw new ManagementException(
135: "Error while setting attribute " + attribute + ": "
136: + e.getClass().getName(), e);
137: }
138: }
139:
140: /**
141: * @param on The ObjectName of the MBean of which the attribute is to be set.
142: * @param attributes A list of attributes: The identification of the attribute to be set and the value it is to be set to
143: * @return The list of attributes that were set, with their new values.
144: */
145: public AttributeList setAttributes(ObjectName on,
146: AttributeList attributes) throws ManagementException {
147:
148: try {
149: return ConnectorFactory.getRMIConnector().setAttributes(on,
150: attributes);
151: } catch (Exception e) {
152: throw new ManagementException(
153: "Error while setting attributes: "
154: + e.getClass().getName(), e);
155: }
156: }
157:
158: /**
159: * @param on
160: */
161: public Object invoke(ObjectName on, String operation,
162: Object[] param, String[] signature)
163: throws ManagementException {
164:
165: try {
166: return ConnectorFactory.getRMIConnector().invoke(on,
167: operation, param, signature);
168: } catch (Exception e) {
169: String message = "";
170: String targetExcName = null;
171: Throwable exc = null;
172: if (e instanceof MBeanException
173: || e instanceof ReflectionException
174: || e instanceof RuntimeMBeanException
175: || e instanceof RuntimeOperationsException
176: || e instanceof RuntimeErrorException) {
177:
178: Exception targetExc = null;
179: if (e instanceof MBeanException) {
180: targetExc = ((MBeanException) e)
181: .getTargetException();
182: } else if (e instanceof ReflectionException) {
183: targetExc = ((ReflectionException) e)
184: .getTargetException();
185: } else if (e instanceof RuntimeMBeanException) {
186: targetExc = ((RuntimeMBeanException) e)
187: .getTargetException();
188: } else if (e instanceof RuntimeOperationsException) {
189: targetExc = ((RuntimeOperationsException) e)
190: .getTargetException();
191: } else if (e instanceof RuntimeErrorException) {
192: Error atargetExc = ((RuntimeErrorException) e)
193: .getTargetError();
194: targetExc = new Exception(atargetExc.getMessage());
195: }
196: targetExcName = targetExc.toString();
197: exc = targetExc;
198: } else {
199: exc = e;
200: }
201: if (logger.isLoggable(BasicLevel.DEBUG)) {
202: logger.log(BasicLevel.DEBUG, "Exception ----[ "
203: + e.toString() + " while invoking operation "
204: + operation + " on MBean " + on.toString()
205: + " ]-------");
206: if (targetExcName != null) {
207: logger.log(BasicLevel.DEBUG,
208: "-------[ Embedded error : ]-------");
209: logger.log(BasicLevel.DEBUG, "-------[ "
210: + targetExcName + " ]-------");
211: }
212: }
213:
214: throw new ManagementException(message, exc);
215: }
216: }
217:
218: /**
219: * @return A set containing the ObjectNames for the MBeans selected.
220: */
221: public java.util.Set queryNames(ObjectName on)
222: throws ManagementException {
223: try {
224: return (java.util.Set) ConnectorFactory.getRMIConnector()
225: .queryNames(on, null);
226: } catch (Exception e) {
227: throw new ManagementException(
228: "Error while getting MBeans names: "
229: + e.getClass().getName(), e);
230: }
231: }
232:
233: /**
234: * @return An instance of MBeanInfo allowing the retrieval of all
235: * attributes and operations of this MBean.
236: */
237: public MBeanInfo getMBeanInfo(ObjectName name)
238: throws ManagementException {
239: try {
240: return (MBeanInfo) ConnectorFactory.getRMIConnector()
241: .getMBeanInfo(name);
242: } catch (Exception e) {
243: throw new ManagementException(
244: "Error while getting MBean info: "
245: + e.getClass().getName(), e);
246: }
247: }
248:
249: /**
250: * @return Context the current application context, create an initial context
251: * if there is no current context.
252: */
253: public Context getContext() throws javax.naming.NamingException {
254: return ConnectorFactory.getContext();
255: }
256:
257: /**
258: * @return String the name of the current RMI connector.
259: * Return null if no RMI connector is available.
260: */
261: public String getCurrentRMIConnectorName() {
262: return ConnectorFactory.getCurrentRMIConnectorName();
263: }
264:
265: /**
266: * Set the currentRMIConnectorName to the specified value
267: */
268: public void setCurrentRMIConnectorName(String name)
269: throws Exception {
270: ConnectorFactory.setCurrentRMIConnectorName(name);
271: }
272:
273: /**
274: * Set the currentRMIConnectorName to null
275: */
276: public void resetCurrentRMIConnectorName() {
277: ConnectorFactory.resetCurrentRMIConnectorName();
278: }
279:
280: /**
281: * @return Set a set containning all RMI connector names available in the current context.
282: */
283: public Set getRMIConnectorsNames()
284: throws javax.naming.NamingException {
285: return ConnectorFactory.getRMIConnectorsNames();
286: }
287:
288: /**
289: * @return String the value of the PROVIDER_URL property in the current context.
290: */
291: public String getJonasNamingServiceURL() {
292: return ConnectorFactory.getJonasNamingServiceURL();
293: }
294:
295: /**
296: * Sets the PROVIDER_URL property to the specified value.
297: */
298: public void setJonasNamingServiceURL(String url)
299: throws javax.naming.NamingException {
300: ConnectorFactory.setJonasNamingServiceURL(url);
301: }
302:
303: /**
304: * Create a new naming context based on the given env. properties
305: * @param env properties to create a new naming context
306: */
307: public void setNamingEnvCtx(Properties env)
308: throws javax.naming.NamingException {
309: ConnectorFactory.setNamingEnvCtx(env);
310: }
311: }
|