1/*
2 * Copyright (c) 2006, 2016, 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 * @test
26 * @bug 6403794
27 * @summary Test that all the platform MXBeans are wrapped in StandardMBean so
28 *          an MBeanServer which does not have support for MXBeans can be used.
29 * @author Luis-Miguel Alventosa
30 *
31 * @run clean MBeanServerMXBeanUnsupportedTest
32 * @run build MBeanServerMXBeanUnsupportedTest
33 * @run main/othervm MBeanServerMXBeanUnsupportedTest
34 */
35
36import java.lang.management.ManagementFactory;
37import java.lang.reflect.InvocationHandler;
38import java.lang.reflect.Method;
39import java.lang.reflect.Proxy;
40import java.util.Arrays;
41import java.util.HashSet;
42import javax.management.MBeanServer;
43import javax.management.MBeanServerBuilder;
44import javax.management.MBeanServerDelegate;
45import javax.management.ObjectName;
46import javax.management.StandardMBean;
47import javax.management.remote.MBeanServerForwarder;
48
49public class MBeanServerMXBeanUnsupportedTest {
50
51    /**
52     * An MBeanServerBuilder that returns an MBeanServer which throws a
53     * RuntimeException if MXBeans are not converted into StandardMBean.
54     */
55    public static class MBeanServerBuilderImpl extends MBeanServerBuilder {
56
57        private final MBeanServerBuilder inner;
58
59        public MBeanServerBuilderImpl() {
60            inner = new MBeanServerBuilder();
61        }
62
63        public MBeanServer newMBeanServer(
64                String defaultDomain,
65                MBeanServer outer,
66                MBeanServerDelegate delegate) {
67            final MBeanServerForwarder mbsf =
68                    MBeanServerForwarderInvocationHandler.newProxyInstance();
69
70            final MBeanServer innerMBeanServer =
71                    inner.newMBeanServer(defaultDomain,
72                    (outer == null ? mbsf : outer),
73                    delegate);
74
75            mbsf.setMBeanServer(innerMBeanServer);
76            return mbsf;
77        }
78    }
79
80    /**
81     * An MBeanServerForwarderInvocationHandler that throws a
82     * RuntimeException if we try to register a non StandardMBean.
83     */
84    public static class MBeanServerForwarderInvocationHandler
85            implements InvocationHandler {
86
87        public static final HashSet<String> excludeList = new HashSet<String>(
88            Arrays.asList("com.sun.management:type=DiagnosticCommand"));
89
90        public static MBeanServerForwarder newProxyInstance() {
91
92            final InvocationHandler handler =
93                    new MBeanServerForwarderInvocationHandler();
94
95            final Class[] interfaces =
96                    new Class[] {MBeanServerForwarder.class};
97
98            Object proxy = Proxy.newProxyInstance(
99                    MBeanServerForwarder.class.getClassLoader(),
100                    interfaces,
101                    handler);
102
103            return MBeanServerForwarder.class.cast(proxy);
104        }
105
106        public Object invoke(Object proxy, Method method, Object[] args)
107                throws Throwable {
108
109            final String methodName = method.getName();
110
111            if (methodName.equals("getMBeanServer")) {
112                return mbs;
113            }
114
115            if (methodName.equals("setMBeanServer")) {
116                if (args[0] == null)
117                    throw new IllegalArgumentException("Null MBeanServer");
118                if (mbs != null)
119                    throw new IllegalArgumentException("MBeanServer object " +
120                            "already initialized");
121                mbs = (MBeanServer) args[0];
122                return null;
123            }
124
125            if (methodName.equals("registerMBean")) {
126                Object mbean = args[0];
127                ObjectName name = (ObjectName) args[1];
128                String domain = name.getDomain();
129                System.out.println("registerMBean: class=" +
130                        mbean.getClass().getName() + "\tname=" + name);
131                Object result = method.invoke(mbs, args);
132                if (domain.equals("java.lang") ||
133                    domain.equals("java.util.logging") ||
134                    domain.equals("com.sun.management")) {
135                    if(!excludeList.contains(name.getCanonicalName())) {
136                        String mxbean = (String)
137                            mbs.getMBeanInfo(name).getDescriptor().getFieldValue("mxbean");
138                        if (mxbean == null || !mxbean.equals("true")) {
139                            throw new RuntimeException(
140                                "Platform MBeans must be MXBeans!");
141                        }
142                        if (!(mbean instanceof StandardMBean)) {
143                            throw new RuntimeException(
144                                "MXBeans must be wrapped in StandardMBean!");
145                        }
146                    }
147                }
148                return result;
149            }
150
151            return method.invoke(mbs, args);
152        }
153
154        private MBeanServer mbs;
155    }
156
157    /*
158     * Standalone entry point.
159     *
160     * Run the test and report to stdout.
161     */
162    public static void main(String args[]) throws Exception {
163        System.setProperty("javax.management.builder.initial",
164                MBeanServerBuilderImpl.class.getName());
165        try {
166            ManagementFactory.getPlatformMBeanServer();
167        } catch (RuntimeException e) {
168            System.out.println(">>> Unhappy Bye, Bye!");
169            throw e;
170        }
171        System.out.println(">>> Happy Bye, Bye!");
172    }
173}
174