Path: blob/master/test/jdk/javax/management/monitor/MBeanServerForwarderInvocationHandler.java
41149 views
/*1* Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24*/2526import java.lang.reflect.InvocationHandler;27import java.lang.reflect.Method;28import java.lang.reflect.Proxy;29import javax.management.MBeanServer;30import javax.management.remote.MBeanServerForwarder;3132public class MBeanServerForwarderInvocationHandler33implements InvocationHandler {3435public static MBeanServerForwarder newProxyInstance() {3637final InvocationHandler handler =38new MBeanServerForwarderInvocationHandler();3940final Class[] interfaces =41new Class[] {MBeanServerForwarder.class};4243Object proxy = Proxy.newProxyInstance(44MBeanServerForwarder.class.getClassLoader(),45interfaces,46handler);4748return MBeanServerForwarder.class.cast(proxy);49}5051public Object invoke(Object proxy, Method method, Object[] args)52throws Throwable {5354final String methodName = method.getName();5556if (methodName.equals("getMBeanServer")) {57return mbs;58}5960if (methodName.equals("setMBeanServer")) {61if (args[0] == null)62throw new IllegalArgumentException("Null MBeanServer");63if (mbs != null)64throw new IllegalArgumentException("MBeanServer object " +65"already initialized");66mbs = (MBeanServer) args[0];67return null;68}6970if (methodName.equals("getAttribute") && exception != null) {71throw exception;72}7374return method.invoke(mbs, args);75}7677public void setGetAttributeException(Exception exception) {78this.exception = exception;79}8081private Exception exception;82private MBeanServer mbs;83}848586