test/java/lang/management/ManagementFactory/MBeanServerMXBeanUnsupportedTest.java

Print this page


   1 /*
   2  * Copyright (c) 2006, 2011, 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  * @run clean MBeanServerMXBeanUnsupportedTest
  31  * @run build MBeanServerMXBeanUnsupportedTest
  32  * @run main/othervm MBeanServerMXBeanUnsupportedTest
  33  */
  34 
  35 import java.lang.management.ManagementFactory;
  36 import java.lang.reflect.InvocationHandler;
  37 import java.lang.reflect.Method;
  38 import java.lang.reflect.Proxy;


  39 import javax.management.MBeanServer;
  40 import javax.management.MBeanServerBuilder;
  41 import javax.management.MBeanServerDelegate;
  42 import javax.management.ObjectName;
  43 import javax.management.StandardMBean;
  44 import javax.management.remote.MBeanServerForwarder;
  45 
  46 public class MBeanServerMXBeanUnsupportedTest {
  47 
  48     /**
  49      * An MBeanServerBuilder that returns an MBeanServer which throws a
  50      * RuntimeException if MXBeans are not converted into StandardMBean.
  51      */
  52     public static class MBeanServerBuilderImpl extends MBeanServerBuilder {
  53 
  54         private final MBeanServerBuilder inner;
  55 
  56         public MBeanServerBuilderImpl() {
  57             inner = new MBeanServerBuilder();
  58         }


  64             final MBeanServerForwarder mbsf =
  65                     MBeanServerForwarderInvocationHandler.newProxyInstance();
  66 
  67             final MBeanServer innerMBeanServer =
  68                     inner.newMBeanServer(defaultDomain,
  69                     (outer == null ? mbsf : outer),
  70                     delegate);
  71 
  72             mbsf.setMBeanServer(innerMBeanServer);
  73             return mbsf;
  74         }
  75     }
  76 
  77     /**
  78      * An MBeanServerForwarderInvocationHandler that throws a
  79      * RuntimeException if we try to register a non StandardMBean.
  80      */
  81     public static class MBeanServerForwarderInvocationHandler
  82             implements InvocationHandler {
  83 



  84         public static MBeanServerForwarder newProxyInstance() {
  85 
  86             final InvocationHandler handler =
  87                     new MBeanServerForwarderInvocationHandler();
  88 
  89             final Class[] interfaces =
  90                     new Class[] {MBeanServerForwarder.class};
  91 
  92             Object proxy = Proxy.newProxyInstance(
  93                     MBeanServerForwarder.class.getClassLoader(),
  94                     interfaces,
  95                     handler);
  96 
  97             return MBeanServerForwarder.class.cast(proxy);
  98         }
  99 
 100         public Object invoke(Object proxy, Method method, Object[] args)
 101                 throws Throwable {
 102 
 103             final String methodName = method.getName();


 109             if (methodName.equals("setMBeanServer")) {
 110                 if (args[0] == null)
 111                     throw new IllegalArgumentException("Null MBeanServer");
 112                 if (mbs != null)
 113                     throw new IllegalArgumentException("MBeanServer object " +
 114                             "already initialized");
 115                 mbs = (MBeanServer) args[0];
 116                 return null;
 117             }
 118 
 119             if (methodName.equals("registerMBean")) {
 120                 Object mbean = args[0];
 121                 ObjectName name = (ObjectName) args[1];
 122                 String domain = name.getDomain();
 123                 System.out.println("registerMBean: class=" +
 124                         mbean.getClass().getName() + "\tname=" + name);
 125                 Object result = method.invoke(mbs, args);
 126                 if (domain.equals("java.lang") ||
 127                     domain.equals("java.util.logging") ||
 128                     domain.equals("com.sun.management")) {

 129                     String mxbean = (String)
 130                         mbs.getMBeanInfo(name).getDescriptor().getFieldValue("mxbean");
 131                     if (mxbean == null || !mxbean.equals("true")) {
 132                         throw new RuntimeException(
 133                                 "Platform MBeans must be MXBeans!");
 134                     }
 135                     if (!(mbean instanceof StandardMBean)) {
 136                         throw new RuntimeException(
 137                                 "MXBeans must be wrapped in StandardMBean!");
 138                     }
 139                 }

 140                 return result;
 141             }
 142 
 143             return method.invoke(mbs, args);
 144         }
 145 
 146         private MBeanServer mbs;
 147     }
 148 
 149     /*
 150      * Standalone entry point.
 151      *
 152      * Run the test and report to stdout.
 153      */
 154     public static void main(String args[]) throws Exception {
 155         System.setProperty("javax.management.builder.initial",
 156                 MBeanServerBuilderImpl.class.getName());
 157         try {
 158             ManagementFactory.getPlatformMBeanServer();
 159         } catch (RuntimeException e) {
   1 /*
   2  * Copyright (c) 2006, 2013 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  * @run clean MBeanServerMXBeanUnsupportedTest
  31  * @run build MBeanServerMXBeanUnsupportedTest
  32  * @run main/othervm MBeanServerMXBeanUnsupportedTest
  33  */
  34 
  35 import java.lang.management.ManagementFactory;
  36 import java.lang.reflect.InvocationHandler;
  37 import java.lang.reflect.Method;
  38 import java.lang.reflect.Proxy;
  39 import java.util.Arrays;
  40 import java.util.HashSet;
  41 import javax.management.MBeanServer;
  42 import javax.management.MBeanServerBuilder;
  43 import javax.management.MBeanServerDelegate;
  44 import javax.management.ObjectName;
  45 import javax.management.StandardMBean;
  46 import javax.management.remote.MBeanServerForwarder;
  47 
  48 public class MBeanServerMXBeanUnsupportedTest {
  49 
  50     /**
  51      * An MBeanServerBuilder that returns an MBeanServer which throws a
  52      * RuntimeException if MXBeans are not converted into StandardMBean.
  53      */
  54     public static class MBeanServerBuilderImpl extends MBeanServerBuilder {
  55 
  56         private final MBeanServerBuilder inner;
  57 
  58         public MBeanServerBuilderImpl() {
  59             inner = new MBeanServerBuilder();
  60         }


  66             final MBeanServerForwarder mbsf =
  67                     MBeanServerForwarderInvocationHandler.newProxyInstance();
  68 
  69             final MBeanServer innerMBeanServer =
  70                     inner.newMBeanServer(defaultDomain,
  71                     (outer == null ? mbsf : outer),
  72                     delegate);
  73 
  74             mbsf.setMBeanServer(innerMBeanServer);
  75             return mbsf;
  76         }
  77     }
  78 
  79     /**
  80      * An MBeanServerForwarderInvocationHandler that throws a
  81      * RuntimeException if we try to register a non StandardMBean.
  82      */
  83     public static class MBeanServerForwarderInvocationHandler
  84             implements InvocationHandler {
  85 
  86         public static final HashSet<String> excludeList = new HashSet<String>(
  87             Arrays.asList("com.sun.management:type=DiagnosticCommand"));
  88 
  89         public static MBeanServerForwarder newProxyInstance() {
  90 
  91             final InvocationHandler handler =
  92                     new MBeanServerForwarderInvocationHandler();
  93 
  94             final Class[] interfaces =
  95                     new Class[] {MBeanServerForwarder.class};
  96 
  97             Object proxy = Proxy.newProxyInstance(
  98                     MBeanServerForwarder.class.getClassLoader(),
  99                     interfaces,
 100                     handler);
 101 
 102             return MBeanServerForwarder.class.cast(proxy);
 103         }
 104 
 105         public Object invoke(Object proxy, Method method, Object[] args)
 106                 throws Throwable {
 107 
 108             final String methodName = method.getName();


 114             if (methodName.equals("setMBeanServer")) {
 115                 if (args[0] == null)
 116                     throw new IllegalArgumentException("Null MBeanServer");
 117                 if (mbs != null)
 118                     throw new IllegalArgumentException("MBeanServer object " +
 119                             "already initialized");
 120                 mbs = (MBeanServer) args[0];
 121                 return null;
 122             }
 123 
 124             if (methodName.equals("registerMBean")) {
 125                 Object mbean = args[0];
 126                 ObjectName name = (ObjectName) args[1];
 127                 String domain = name.getDomain();
 128                 System.out.println("registerMBean: class=" +
 129                         mbean.getClass().getName() + "\tname=" + name);
 130                 Object result = method.invoke(mbs, args);
 131                 if (domain.equals("java.lang") ||
 132                     domain.equals("java.util.logging") ||
 133                     domain.equals("com.sun.management")) {
 134                     if(!excludeList.contains(name.getCanonicalName())) {
 135                         String mxbean = (String)
 136                             mbs.getMBeanInfo(name).getDescriptor().getFieldValue("mxbean");
 137                         if (mxbean == null || !mxbean.equals("true")) {
 138                             throw new RuntimeException(
 139                                 "Platform MBeans must be MXBeans!");
 140                         }
 141                         if (!(mbean instanceof StandardMBean)) {
 142                             throw new RuntimeException(
 143                                 "MXBeans must be wrapped in StandardMBean!");
 144                         }
 145                     }
 146                 }
 147                 return result;
 148             }
 149 
 150             return method.invoke(mbs, args);
 151         }
 152 
 153         private MBeanServer mbs;
 154     }
 155 
 156     /*
 157      * Standalone entry point.
 158      *
 159      * Run the test and report to stdout.
 160      */
 161     public static void main(String args[]) throws Exception {
 162         System.setProperty("javax.management.builder.initial",
 163                 MBeanServerBuilderImpl.class.getName());
 164         try {
 165             ManagementFactory.getPlatformMBeanServer();
 166         } catch (RuntimeException e) {