001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.jmx;
031:
032: import javax.management.Attribute;
033: import javax.management.MBeanServer;
034: import javax.management.NotificationEmitter;
035: import javax.management.NotificationFilter;
036: import javax.management.NotificationListener;
037: import javax.management.ObjectName;
038: import java.lang.reflect.Array;
039: import java.lang.reflect.InvocationHandler;
040: import java.lang.reflect.Method;
041: import java.lang.reflect.Proxy;
042:
043: /**
044: * Proxy hander for mbeans.
045: */
046: public class JmxInvocationHandler implements InvocationHandler {
047: private MBeanServer _server;
048: private ClassLoader _loader;
049: private ObjectName _name;
050:
051: /**
052: * Creates the invocation handler.
053: */
054: public JmxInvocationHandler(MBeanServer mbeanServer,
055: ClassLoader loader, ObjectName objectName) {
056: _server = mbeanServer;
057: _loader = loader;
058: _name = objectName;
059: }
060:
061: /**
062: * Creates a new proxy instance.
063: */
064: public static Object newProxyInstance(MBeanServer server,
065: ClassLoader loader, ObjectName objectName,
066: Class interfaceClass, boolean notificationBroadcaster) {
067: Class[] interfaces;
068:
069: if (notificationBroadcaster)
070: interfaces = new Class[] { interfaceClass,
071: NotificationEmitter.class };
072: else
073: interfaces = new Class[] { interfaceClass };
074:
075: JmxInvocationHandler handler;
076:
077: handler = new JmxInvocationHandler(server, loader, objectName);
078:
079: return Proxy.newProxyInstance(interfaceClass.getClassLoader(),
080: interfaces, handler);
081: }
082:
083: /**
084: * Handles the object invocation.
085: *
086: * @param proxy the proxy object to invoke
087: * @param method the method to call
088: * @param args the arguments to the proxy object
089: */
090: public Object invoke(Object proxy, Method method, Object[] args)
091: throws Throwable {
092: String methodName = method.getName();
093: Class[] params = method.getParameterTypes();
094:
095: // equals and hashCode are special cased
096: if (methodName.equals("equals") && params.length == 1
097: && params[0].equals(Object.class)) {
098: Object value = args[0];
099: if (value == null || !Proxy.isProxyClass(value.getClass()))
100: return Boolean.FALSE;
101:
102: JmxInvocationHandler handler;
103:
104: handler = (JmxInvocationHandler) Proxy
105: .getInvocationHandler(value);
106:
107: return new Boolean(_name.equals(handler._name));
108: } else if (methodName.equals("hashCode") && params.length == 0)
109: return new Integer(_name.hashCode());
110:
111: int len = methodName.length();
112: String attrName;
113:
114: Class returnType = method.getReturnType();
115:
116: if (params.length == 0 && methodName.startsWith("get")
117: && len > 3) {
118: attrName = methodName.substring(3);
119:
120: return marshall(_server.getAttribute(_name, attrName),
121: returnType);
122: } else if (params.length == 1 && returnType.equals(void.class)
123: && methodName.startsWith("set") && len > 3) {
124: attrName = methodName.substring(3);
125:
126: Attribute attr = new Attribute(attrName, args[0]);
127:
128: _server.setAttribute(_name, attr);
129:
130: return null;
131: } else if (methodName.equals("addNotificationListener")) {
132: if (args.length != 3) {
133: } else if (args[0] instanceof NotificationListener) {
134: _server.addNotificationListener(_name,
135: (NotificationListener) args[0],
136: (NotificationFilter) args[1], args[2]);
137: return null;
138: } else if (args[0] instanceof ObjectName) {
139: _server.addNotificationListener(_name,
140: (ObjectName) args[0],
141: (NotificationFilter) args[1], args[2]);
142: return null;
143: }
144: } else if (methodName.equals("removeNotificationListener")) {
145: if (args.length == 3) {
146: if (args[0] instanceof NotificationListener) {
147: _server.removeNotificationListener(_name,
148: (NotificationListener) args[0],
149: (NotificationFilter) args[1], args[2]);
150: return null;
151: } else if (args[0] instanceof ObjectName) {
152: _server.removeNotificationListener(_name,
153: (ObjectName) args[0],
154: (NotificationFilter) args[1], args[2]);
155: return null;
156: }
157: } else if (args.length == 1) {
158: if (args[0] instanceof NotificationListener) {
159: _server.removeNotificationListener(_name,
160: (NotificationListener) args[0]);
161: return null;
162: } else if (args[0] instanceof ObjectName) {
163: _server.removeNotificationListener(_name,
164: (ObjectName) args[0]);
165:
166: return null;
167: }
168: }
169: }
170:
171: String[] sig = new String[params.length];
172:
173: for (int i = 0; i < sig.length; i++)
174: sig[i] = params[i].getName();
175:
176: Object value = _server.invoke(_name, methodName, args, sig);
177:
178: return marshall(value, returnType);
179: }
180:
181: private Object marshall(Object value, Class retType) {
182: if (retType == null || value == null
183: || retType.isAssignableFrom(value.getClass()))
184: return value;
185:
186: if (value instanceof ObjectName) {
187: ObjectName name = (ObjectName) value;
188:
189: Object proxy = Jmx.find(name, _loader, _server);
190:
191: if (retType.isInstance(proxy))
192: return proxy;
193: else
194: return value;
195: } else if (value instanceof ObjectName[] && retType.isArray()) {
196: Class type = retType.getComponentType();
197: ObjectName[] names = (ObjectName[]) value;
198:
199: Object proxies = Array.newInstance(type, names.length);
200:
201: for (int i = 0; i < names.length; i++) {
202: Object proxy = Jmx.find(names[i], _loader, _server);
203:
204: if (proxy == null) {
205: Array.set(proxies, i, null);
206: continue;
207: }
208:
209: if (!type.isInstance(proxy))
210: return value;
211:
212: Array.set(proxies, i, Jmx.find(names[i], _loader,
213: _server));
214: }
215:
216: return proxies;
217: }
218:
219: return value;
220: }
221: }
|