jdk/test/javax/management/namespace/DomainCreationTest.java
changeset 1156 bbc2d15aaf7a
child 1227 4546977d0d66
equal deleted inserted replaced
1155:a9a142fcf1b5 1156:bbc2d15aaf7a
       
     1 /*
       
     2  * Copyright 2008 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    21  * have any questions.
       
    22  */
       
    23 /*
       
    24  *
       
    25  * @test DomainCreationTest.java
       
    26  * @summary Test the creation and registration of JMXDomain instances.
       
    27  * @author Daniel Fuchs
       
    28  * @run clean DomainCreationTest Wombat WombatMBean
       
    29  * @run build DomainCreationTest Wombat WombatMBean
       
    30  * @run main DomainCreationTest
       
    31  */
       
    32 
       
    33 
       
    34 import java.util.Collections;
       
    35 import java.util.Map;
       
    36 import java.util.Set;
       
    37 import javax.management.Attribute;
       
    38 import javax.management.AttributeList;
       
    39 import javax.management.AttributeNotFoundException;
       
    40 import javax.management.DynamicMBean;
       
    41 import javax.management.InstanceNotFoundException;
       
    42 import javax.management.InvalidAttributeValueException;
       
    43 import javax.management.MBeanException;
       
    44 import javax.management.MBeanInfo;
       
    45 import javax.management.MBeanRegistration;
       
    46 import javax.management.MBeanServer;
       
    47 import javax.management.MBeanServerFactory;
       
    48 import javax.management.NotificationEmitter;
       
    49 import javax.management.ObjectInstance;
       
    50 import javax.management.ObjectName;
       
    51 import javax.management.ReflectionException;
       
    52 import javax.management.RuntimeMBeanException;
       
    53 import javax.management.RuntimeOperationsException;
       
    54 import javax.management.namespace.JMXDomain;
       
    55 import javax.management.namespace.MBeanServerSupport;
       
    56 
       
    57 /**
       
    58  * Test simple creation/registration of namespace.
       
    59  *
       
    60  */
       
    61 public class DomainCreationTest {
       
    62     private static Map<String,Object> emptyEnvMap() {
       
    63         return Collections.emptyMap();
       
    64     }
       
    65 
       
    66 
       
    67     public static class LocalDomainRepository
       
    68             extends MBeanServerSupport {
       
    69         private final MBeanServer server;
       
    70         private final String      domain;
       
    71 
       
    72 
       
    73         public class DynamicMBeanProxy implements DynamicMBean {
       
    74 
       
    75             private final MBeanServer server;
       
    76             private final ObjectName name;
       
    77 
       
    78             public DynamicMBeanProxy(MBeanServer s, ObjectName n) {
       
    79                 this.server = s;
       
    80                 this.name = n;
       
    81             }
       
    82 
       
    83             public Object getAttribute(String attribute)
       
    84                     throws AttributeNotFoundException,
       
    85                     MBeanException, ReflectionException {
       
    86                 try {
       
    87                     return server.getAttribute(name, attribute);
       
    88                 } catch (RuntimeException x) {
       
    89                     throw x;
       
    90                 } catch (Exception x) {
       
    91                     throw new RuntimeException(x);
       
    92                 }
       
    93             }
       
    94 
       
    95             public void setAttribute(Attribute attribute)
       
    96                     throws AttributeNotFoundException,
       
    97                     InvalidAttributeValueException, MBeanException,
       
    98                     ReflectionException {
       
    99                 try {
       
   100                     server.setAttribute(name, attribute);
       
   101                 } catch (RuntimeException x) {
       
   102                     throw x;
       
   103                 } catch (Exception x) {
       
   104                     throw new RuntimeException(x);
       
   105                 }
       
   106             }
       
   107 
       
   108             public AttributeList getAttributes(String[] attributes) {
       
   109                 try {
       
   110                     return server.getAttributes(name, attributes);
       
   111                 } catch (RuntimeException x) {
       
   112                     throw x;
       
   113                 } catch (Exception x) {
       
   114                     throw new RuntimeException(x);
       
   115                 }
       
   116             }
       
   117 
       
   118             public AttributeList setAttributes(AttributeList attributes) {
       
   119                 try {
       
   120                     return server.setAttributes(name, attributes);
       
   121                 } catch (RuntimeException x) {
       
   122                     throw x;
       
   123                 } catch (Exception x) {
       
   124                     throw new RuntimeException(x);
       
   125                 }
       
   126             }
       
   127 
       
   128             public Object invoke(String actionName, Object[] params,
       
   129                     String[] signature) throws MBeanException,
       
   130                     ReflectionException {
       
   131                 try {
       
   132                     return server.invoke(name, actionName, params, signature);
       
   133                 } catch (RuntimeException x) {
       
   134                     throw x;
       
   135                 } catch (Exception x) {
       
   136                     throw new RuntimeException(x);
       
   137                 }
       
   138             }
       
   139 
       
   140             public MBeanInfo getMBeanInfo() {
       
   141                 try {
       
   142                     return server.getMBeanInfo(name);
       
   143                 } catch (RuntimeException x) {
       
   144                     throw x;
       
   145                 } catch (Exception x) {
       
   146                     throw new RuntimeException(x);
       
   147                 }
       
   148             }
       
   149         }
       
   150 
       
   151         public LocalDomainRepository(String domain) {
       
   152             this.server = MBeanServerFactory.newMBeanServer();
       
   153             this.domain = domain;
       
   154         }
       
   155 
       
   156         @Override
       
   157         protected Set<ObjectName> getNames() {
       
   158             try {
       
   159             final ObjectName name =
       
   160                     ObjectName.getInstance(domain+":*");
       
   161             return server.queryNames(name, null);
       
   162             } catch (RuntimeException x) {
       
   163                 throw x;
       
   164             } catch (Exception x) {
       
   165                 throw new RuntimeException(x);
       
   166             }
       
   167         }
       
   168 
       
   169         @Override
       
   170         public DynamicMBean getDynamicMBeanFor(ObjectName name)
       
   171                 throws InstanceNotFoundException {
       
   172             return new DynamicMBeanProxy(server, name);
       
   173         }
       
   174 
       
   175         @Override
       
   176         public NotificationEmitter
       
   177                 getNotificationEmitterFor(ObjectName name)
       
   178                 throws InstanceNotFoundException {
       
   179             DynamicMBean mbean = getDynamicMBeanFor(name);
       
   180             if (mbean instanceof NotificationEmitter)
       
   181                 return (NotificationEmitter) mbean;
       
   182             return null;
       
   183         }
       
   184 
       
   185     }
       
   186 
       
   187     private static MBeanServer newMBeanServer() {
       
   188         return MBeanServerFactory.newMBeanServer();
       
   189     }
       
   190 
       
   191     public static interface ThingMBean {}
       
   192     public static class Thing implements ThingMBean, MBeanRegistration {
       
   193         public ObjectName preRegister(MBeanServer server, ObjectName name)
       
   194                 throws Exception {
       
   195             if (name == null) return new ObjectName(":type=Thing");
       
   196             else return name;
       
   197         }
       
   198         public void postRegister(Boolean registrationDone) {
       
   199         }
       
   200 
       
   201         public void preDeregister() throws Exception {
       
   202         }
       
   203         public void postDeregister() {
       
   204         }
       
   205     }
       
   206 
       
   207     /**
       
   208      * Test that it is possible to create a dummy MBean with a null
       
   209      * ObjectName - this is just a sanity check - as there are already
       
   210      * other JMX tests that check that.
       
   211      *
       
   212      * @throws java.lang.Exception
       
   213      */
       
   214     public static void testCreateWithNull() throws Exception {
       
   215         final MBeanServer server = newMBeanServer();
       
   216         final ObjectInstance oi = server.registerMBean(new Thing(),null);
       
   217         server.unregisterMBean(oi.getObjectName());
       
   218         System.out.println("testCreateWithNull PASSED");
       
   219     }
       
   220 
       
   221     /**
       
   222      * Check that we can register a JMXNamespace MBean, using its standard
       
   223      * ObjectName.
       
   224      * @throws java.lang.Exception
       
   225      */
       
   226     public static void testGoodObjectName() throws Exception {
       
   227         MBeanServer server = newMBeanServer();
       
   228         final ObjectName name =
       
   229                 JMXDomain.getDomainObjectName("gloups");
       
   230         final ObjectInstance oi =
       
   231                 server.registerMBean(new JMXDomain(
       
   232                 new LocalDomainRepository("gloups")),name);
       
   233         System.out.println("Succesfully registered namespace: "+name);
       
   234         try {
       
   235             if (! name.equals(oi.getObjectName()))
       
   236                 throw new RuntimeException("testGoodObjectName: TEST failed: " +
       
   237                         "namespace registered as: "+
       
   238                     oi.getObjectName()+" expected: "+name);
       
   239         } finally {
       
   240             server.unregisterMBean(oi.getObjectName());
       
   241         }
       
   242         System.out.println("Succesfully unregistered namespace: "+name);
       
   243         System.out.println("testGoodObjectName PASSED");
       
   244     }
       
   245 
       
   246     /**
       
   247      * Check that we cannot register a JMXNamespace MBean, if we don't use
       
   248      * its standard ObjectName.
       
   249      * @throws java.lang.Exception
       
   250      */
       
   251     public static void testBadObjectName() throws Exception {
       
   252         MBeanServer server = newMBeanServer();
       
   253         Throwable exp = null;
       
   254         final ObjectName name = new ObjectName("d:k=v");
       
   255         try {
       
   256             server.registerMBean(new JMXDomain(
       
   257                 new LocalDomainRepository("d")),name);
       
   258             System.out.println("testBadObjectName: " +
       
   259                     "Error: MBean registered, no exception thrown.");
       
   260         } catch(RuntimeMBeanException x) {
       
   261             exp = x.getCause();
       
   262         } catch(Exception x) {
       
   263             throw new RuntimeException("testBadObjectName: TEST failed: " +
       
   264                     "expected RuntimeMBeanException - got "+
       
   265                     x);
       
   266         }
       
   267         if (exp == null)  server.unregisterMBean(name);
       
   268         if (exp == null)
       
   269             throw new RuntimeException("testBadObjectName: TEST failed: " +
       
   270                     "expected IllegalArgumentException - got none");
       
   271         if (!(exp instanceof IllegalArgumentException))
       
   272             throw new RuntimeException("testBadObjectName: TEST failed: " +
       
   273                     "expected IllegalArgumentException - got "+
       
   274                     exp.toString(),exp);
       
   275         System.out.println("Got expected exception: "+exp);
       
   276         System.out.println("testBadObjectName PASSED");
       
   277     }
       
   278 
       
   279     /**
       
   280      * Check that we cannot register a Domain MBean in a domain that already
       
   281      * exists.
       
   282      *
       
   283      * @throws java.lang.Exception
       
   284      */
       
   285     public static void testBadDomain() throws Exception {
       
   286         MBeanServer server = newMBeanServer();
       
   287         Throwable exp = null;
       
   288         final ObjectName name = new ObjectName("glips:k=v");
       
   289         server.registerMBean(new Wombat(),name);
       
   290 
       
   291         final ObjectName dname =
       
   292                 JMXDomain.getDomainObjectName("glips");
       
   293 
       
   294         try {
       
   295             server.registerMBean(new JMXDomain(
       
   296                 new LocalDomainRepository("glips")),dname);
       
   297             System.out.println("testBadDomain: " +
       
   298                     "Error: MBean registered, no exception thrown.");
       
   299         } catch(RuntimeOperationsException x) {
       
   300             exp = x.getCause();
       
   301         } catch(Exception x) {
       
   302             throw new RuntimeException("testBadDomain: TEST failed: " +
       
   303                     "expected RuntimeOperationsException - got "+
       
   304                     x);
       
   305         } finally {
       
   306             server.unregisterMBean(name);
       
   307         }
       
   308         if (exp == null)  {
       
   309             server.unregisterMBean(dname);
       
   310         }
       
   311         if (exp == null)
       
   312             throw new RuntimeException("testBadDomain: TEST failed: " +
       
   313                     "expected IllegalArgumentException - got none");
       
   314         if (!(exp instanceof IllegalArgumentException))
       
   315             throw new RuntimeException("testBadDomain: TEST failed: " +
       
   316                     "expected IllegalArgumentException - got "+
       
   317                     exp.toString(),exp);
       
   318         System.out.println("Got expected exception: "+exp);
       
   319         System.out.println("testBadDomain PASSED");
       
   320     }
       
   321 
       
   322 
       
   323     public static void main(String... args) throws Exception {
       
   324         testCreateWithNull();
       
   325         testGoodObjectName();
       
   326         testBadObjectName();
       
   327         testBadDomain();
       
   328     }
       
   329 }