1/* 2 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 */ 26 27import java.lang.reflect.InvocationHandler; 28import java.lang.reflect.Method; 29import java.lang.reflect.Proxy; 30import javax.management.MBeanServer; 31import javax.management.remote.MBeanServerForwarder; 32 33public class MBeanServerForwarderInvocationHandler 34 implements InvocationHandler { 35 36 public static MBeanServerForwarder newProxyInstance() { 37 38 final InvocationHandler handler = 39 new MBeanServerForwarderInvocationHandler(); 40 41 final Class[] interfaces = 42 new Class[] {MBeanServerForwarder.class}; 43 44 Object proxy = Proxy.newProxyInstance( 45 MBeanServerForwarder.class.getClassLoader(), 46 interfaces, 47 handler); 48 49 return MBeanServerForwarder.class.cast(proxy); 50 } 51 52 public Object invoke(Object proxy, Method method, Object[] args) 53 throws Throwable { 54 55 final String methodName = method.getName(); 56 57 if (methodName.equals("getMBeanServer")) { 58 return mbs; 59 } 60 61 if (methodName.equals("setMBeanServer")) { 62 if (args[0] == null) 63 throw new IllegalArgumentException("Null MBeanServer"); 64 if (mbs != null) 65 throw new IllegalArgumentException("MBeanServer object " + 66 "already initialized"); 67 mbs = (MBeanServer) args[0]; 68 return null; 69 } 70 71 if (methodName.equals("getAttribute") && exception != null) { 72 throw exception; 73 } 74 75 return method.invoke(mbs, args); 76 } 77 78 public void setGetAttributeException(Exception exception) { 79 this.exception = exception; 80 } 81 82 private Exception exception; 83 private MBeanServer mbs; 84} 85