001: /*
002: * CoadunationAdmin: The admin frontend for coadunation.
003: * Copyright (C) 2007 - 2008 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * MXManagerImpl.java
020: */
021:
022: // package path
023: package com.rift.coad.web.admin.server;
024:
025: // imports
026: import java.lang.annotation.Annotation;
027: import java.util.Set;
028: import java.util.Iterator;
029: import java.util.Arrays;
030: import java.util.Vector;
031: import java.lang.reflect.Method;
032: import java.io.ByteArrayOutputStream;
033: import java.io.CharArrayWriter;
034: import java.io.PrintWriter;
035: import java.io.PrintStream;
036:
037: // import log4j
038: import org.apache.log4j.Logger;
039:
040: // gwt imports
041: import com.google.gwt.user.server.rpc.RemoteServiceServlet;
042:
043: // coadunation imports
044: import com.rift.coad.lib.deployment.jmxbean.JMXBeanConnector;
045: import com.rift.coad.lib.deployment.jmxbean.JMXBeanManager;
046: import com.rift.coad.web.admin.client.MXManager;
047: import com.rift.coad.web.admin.client.MXException;
048: import com.rift.coad.web.admin.client.MethodDef;
049: import com.rift.coad.web.admin.client.VariableDef;
050:
051: /**
052: *
053: * @author brett
054: */
055: public class MXManagerImpl extends RemoteServiceServlet implements
056: MXManager {
057:
058: // private member variables
059: private static Logger log = Logger.getLogger(MXManagerImpl.class);
060:
061: /**
062: * This method returns the list of daemons.
063: *
064: * @return A list of daemons.
065: */
066: public String[] getMXBeans() throws MXException {
067: try {
068: Set entries = JMXBeanConnector.getInstance()
069: .getJMXBeanKeys();
070: String[] result = new String[entries.size()];
071: int index = 0;
072: for (Iterator iter = entries.iterator(); iter.hasNext(); index++) {
073: result[index] = (String) iter.next();
074: }
075: Arrays.sort(result);
076: return result;
077: } catch (Exception ex) {
078: log.error("Failed to retrieve the daemon list : "
079: + ex.getMessage(), ex);
080: throw new MXException(
081: "Failed to retrieve the daemon list : "
082: + ex.getMessage());
083: }
084:
085: }
086:
087: /**
088: * This method returns the list of daemons
089: *
090: * @return The list of methods.
091: * @param name The name of the method.
092: * @exception MXException
093: */
094: public String[] getMethods(String name) throws MXException {
095: try {
096: Object bean = JMXBeanConnector.getInstance().getJMXBean(
097: name);
098: Class[] interfaces = bean.getClass().getInterfaces();
099:
100: Vector methodVec = new Vector();
101: for (int interfaceIndex = 0; interfaceIndex < interfaces.length; interfaceIndex++) {
102: Method[] methods = interfaces[interfaceIndex]
103: .getDeclaredMethods();
104: for (int index = 0; index < methods.length; index++) {
105: Method method = methods[index];
106: String methodDec = method.getName();
107: methodDec += "(";
108: String sep = "";
109: for (int param = 0; param < method
110: .getParameterTypes().length; param++) {
111: methodDec += sep
112: + method.getParameterTypes()[param]
113: .getName();
114: sep = ",";
115: }
116: methodDec += ")";
117: methodVec.add(methodDec);
118: }
119: }
120: String[] result = (String[]) methodVec
121: .toArray(new String[0]);
122: Arrays.sort(result);
123: return result;
124: } catch (Exception ex) {
125: log.error("Failed to retrieve the method list : "
126: + ex.getMessage(), ex);
127: throw new MXException(
128: "Failed to retrieve the method list : "
129: + ex.getMessage());
130: }
131: }
132:
133: /**
134: * This method returns definition of the method.
135: *
136: * @return A list of methods.
137: * @param The name of the method.
138: * @exception MXException
139: */
140: public MethodDef getMethod(String objectName, String methodName)
141: throws MXException {
142: try {
143: Object bean = JMXBeanConnector.getInstance().getJMXBean(
144: objectName);
145: String name = methodName.substring(0, methodName
146: .indexOf("("));
147: String paramTypes = methodName.substring(
148: methodName.indexOf("(") + 1,
149: methodName.indexOf(")")).trim();
150: Class[] types = null;
151: log.debug("Object name " + bean.getClass().getName());
152: if (paramTypes.length() != 0) {
153: String[] params = paramTypes.split("[,]");
154: types = new Class[params.length];
155: log.debug("There are :" + params.length + ": params : "
156: + paramTypes);
157: for (int param = 0; param < params.length; param++) {
158: log.debug("Set the type :" + params[param]);
159: if (ObjectTypeUtil.isPrimitive(params[param])) {
160: types[param] = ObjectTypeUtil
161: .getPrimitive(params[param]);
162: } else {
163: types[param] = Class
164: .forName(params[param], false, bean
165: .getClass().getClassLoader());
166: }
167: }
168: } else {
169: types = new Class[0];
170: }
171: Method method = ObjectTypeUtil.getMethod(bean.getClass()
172: .getInterfaces(), name, types);
173: VariableDef result = new VariableDef("Result", method
174: .getReturnType().getName(), "");
175: MethodDef methodDef = new MethodDef(method.getName(),
176: method.toGenericString(), result, null);
177: VariableDef[] parameters = new VariableDef[method
178: .getParameterTypes().length];
179: for (int index = 0; index < method.getParameterTypes().length; index++) {
180: parameters[index] = new VariableDef("p" + index, method
181: .getParameterTypes()[index].getName(), method
182: .getParameterTypes()[index].getSimpleName());
183: }
184: methodDef.setParameters(parameters);
185:
186: return methodDef;
187: } catch (Exception ex) {
188: log.error("Failed to retrieve the method info : "
189: + ex.getMessage(), ex);
190: throw new MXException(
191: "Failed to retrieve the method info : "
192: + ex.getMessage());
193: }
194: }
195:
196: /**
197: * This method is called to invoke the daemon.
198: *
199: * @return The result of the method call.
200: * @param method The method to call.
201: * @exception MXException
202: */
203: public String invokeMethod(String objectName, MethodDef methodDef)
204: throws MXException {
205: try {
206: Object bean = JMXBeanConnector.getInstance().getJMXBean(
207: objectName);
208: VariableDef[] parameters = methodDef.getParameters();
209: Class[] types = new Class[parameters.length];
210: Object[] arguments = new Object[parameters.length];
211: for (int index = 0; index < parameters.length; index++) {
212: if (ObjectTypeUtil.isPrimitive(parameters[index]
213: .getType())) {
214: types[index] = ObjectTypeUtil
215: .getPrimitive(parameters[index].getType());
216: } else {
217: types[index] = Class.forName(parameters[index]
218: .getType(), false, bean.getClass()
219: .getClassLoader());
220: }
221: arguments[index] = ObjectTypeUtil.getValue(
222: types[index], parameters[index].getValue());
223: }
224: Method method = bean.getClass().getMethod(
225: methodDef.getName(), types);
226:
227: // invoke the method
228: ClassLoader currentLoader = Thread.currentThread()
229: .getContextClassLoader();
230: try {
231: Thread.currentThread().setContextClassLoader(
232: bean.getClass().getClassLoader());
233: Object result = method.invoke(bean, arguments);
234: if (result == null) {
235: return "null";
236: }
237: return result.toString();
238: } finally {
239: // reset the class loader
240: Thread.currentThread().setContextClassLoader(
241: currentLoader);
242: }
243: } catch (Exception ex) {
244: log.error("Failed to invoke the method : "
245: + ex.getMessage(), ex);
246: String result = "Failed to invoke the method : "
247: + ex.getMessage() + "\n";
248: ByteArrayOutputStream output = new ByteArrayOutputStream();
249: PrintStream outStream = new PrintStream(output);
250: ex.printStackTrace(outStream);
251: outStream.flush();
252: result += output.toString();
253:
254: return result;
255: }
256: }
257: }
|