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: JonasManagementRepr.java 9688 2006-10-06 13:43:32Z danesa $
023: * --------------------------------------------------------------------------
024: */package org.objectweb.jonas.jmx;
025:
026: import java.util.Hashtable;
027:
028: import javax.management.MBeanInfo;
029: import javax.management.ObjectName;
030: import javax.naming.Context;
031:
032: /**
033: * Wraper class. Apply the management operations on the ManagementRep instance.
034: * @author Adriana Danes
035: */
036: public class JonasManagementRepr {
037:
038: /**
039: * Using getManagementRepr() without parameters makes creating a ManagemenrReprImpl
040: * object (an "old" style representant which uses proprietary RMIConnector object)
041: */
042: //private static ManagementRepr repr = ManagementReprFactory.getManagementRepr();
043: // Let repr null and then use setRepr method, will allow to set/change representative
044: // in order to set/change the managed server.
045: //private static ManagementRepr repr = null;
046: private static Hashtable serverReprs = new Hashtable();
047:
048: public static void addServerRepr(String serverName,
049: ManagementRepr repr) {
050: serverReprs.put(serverName, repr);
051: }
052:
053: public static boolean noServerRepr() {
054: if (serverReprs.isEmpty()) {
055: return true;
056: } else {
057: return false;
058: }
059: }
060:
061: public static boolean isServerRepr(String serverName) {
062: return serverReprs.containsKey(serverName);
063: }
064:
065: public static ManagementRepr getServerRepr(String serverName)
066: throws ManagementException {
067: if (!isServerRepr(serverName)) {
068: // try to get repr
069: ManagementReprLoader.loadServerRepr(serverName);
070: }
071: ManagementRepr repr = (ManagementRepr) serverReprs
072: .get(serverName);
073: if (repr != null) {
074: return repr;
075: } else {
076: throw new ManagementException(
077: "Can't manage server "
078: + serverName
079: + " as no ManagementRepr set (couldn't get connection)");
080: }
081: }
082:
083: /**
084: * @return True if the MBean is already registered in the MBean server, false otherwise or if an exception is catched.
085: * @param on ObjectName of the MBean we are looking for
086: * @param serverName The server name
087: */
088: public static boolean isRegistered(ObjectName on, String serverName)
089: throws ManagementException {
090: ManagementRepr repr = getServerRepr(serverName);
091: return repr.isRegistered(on);
092: }
093:
094: /**
095: * @param on The ObjectName of the MBean from which the attribute is to be retrieved.
096: * @param attribute A String specifying the name of the attribute to be retrieve.
097: * @param serverName The server name
098: * @return The value of the attribute.
099: */
100: public static Object getAttribute(ObjectName on, String attribute,
101: String serverName) throws ManagementException {
102: ManagementRepr repr = getServerRepr(serverName);
103: return repr.getAttribute(on, attribute);
104: }
105:
106: /**
107: * @param on The ObjectName of the MBean from which the attribute is to be retrieved.
108: * @param attribute A String specifying the name of the attribute to be retrieve.
109: * @param serverName The server name
110: * @return The value of the attribute.
111: */
112: public static Object getAttributes(ObjectName on, String attribute,
113: String serverName) throws ManagementException {
114: ManagementRepr repr = getServerRepr(serverName);
115: return repr.getAttribute(on, attribute);
116: }
117:
118: /**
119: * @param on The ObjectName of the MBean within which the attribute is to be set.
120: * @param serverName The server name
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: */
124: public static void setAttribute(ObjectName on, String attribute,
125: Object value, String serverName) throws ManagementException {
126: ManagementRepr repr = getServerRepr(serverName);
127: repr.setAttribute(on, attribute, value);
128: }
129:
130: /**
131: * @param on
132: * @param param invoke parameters
133: * @param signature invoke parameters signature
134: * @param serverName The server name
135: */
136: public static Object invoke(ObjectName on, String operation,
137: Object[] param, String[] signature, String serverName)
138: throws ManagementException {
139: ManagementRepr repr = getServerRepr(serverName);
140: return repr.invoke(on, operation, param, signature);
141: }
142:
143: /**
144: * @return A set containing the ObjectNames for the MBeans selected.
145: * @param serverName The server name
146: */
147: public static java.util.Set queryNames(ObjectName on,
148: String serverName) throws ManagementException {
149: ManagementRepr repr = getServerRepr(serverName);
150: return repr.queryNames(on);
151: }
152:
153: /**
154: * @return An instance of MBeanInfo allowing the retrieval of all
155: * attributes and operations of this MBean.
156: * @param name MBean's ObjectName
157: * @param serverName The server name
158: */
159: public static MBeanInfo getMBeanInfo(ObjectName name,
160: String serverName) throws ManagementException {
161: ManagementRepr repr = getServerRepr(serverName);
162: return (MBeanInfo) repr.getMBeanInfo(name);
163: }
164:
165: /**
166: * @return Context the current application context.
167: */
168: public static Context getContext(String serverName)
169: throws javax.naming.NamingException {
170: ManagementRepr repr = getServerRepr(serverName);
171: return repr.getContext();
172: }
173: }
|