jdk/test/javax/management/Introspector/ObjectNameTemplateTest.java
changeset 4156 acaa49a2768a
parent 4155 460e37d40f12
child 4159 9e3aae7675f1
equal deleted inserted replaced
4155:460e37d40f12 4156:acaa49a2768a
     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 %M% %I%
       
    26  * @bug 6675526
       
    27  * @summary Test MBeans named with @ObjectNameTemplate
       
    28  * @author Jean-Francois Denise
       
    29  * @run main/othervm ObjectNameTemplateTest
       
    30  */
       
    31 import java.lang.management.ManagementFactory;
       
    32 import java.lang.reflect.Constructor;
       
    33 import java.util.ArrayList;
       
    34 import java.util.List;
       
    35 import javax.management.Attribute;
       
    36 import javax.management.AttributeList;
       
    37 import javax.management.AttributeNotFoundException;
       
    38 import javax.management.DynamicMBean;
       
    39 import javax.management.ImmutableDescriptor;
       
    40 import javax.management.InvalidAttributeValueException;
       
    41 import javax.management.JMX;
       
    42 import javax.management.MBeanException;
       
    43 import javax.management.MBeanInfo;
       
    44 import javax.management.MBeanServer;
       
    45 import javax.management.MXBean;
       
    46 import javax.management.MBean;
       
    47 import javax.management.ManagedAttribute;
       
    48 import javax.management.NotCompliantMBeanException;
       
    49 import javax.management.ObjectName;
       
    50 import javax.management.ObjectNameTemplate;
       
    51 import javax.management.ReflectionException;
       
    52 import javax.management.StandardMBean;
       
    53 
       
    54 public class ObjectNameTemplateTest {
       
    55 
       
    56     private static MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
       
    57     private static final String NAME_TEMPLATE_MULTI =
       
    58             "com.example:type=MultiStdCache,name={Name}";
       
    59     private static final String NAME_TEMPLATE_MONO =
       
    60             "com.example:{Type}={TypeValue}";
       
    61     private static final String NAME_TEMPLATE_QUOTED =
       
    62             "com.example:type=Quotted,name=\"{Name}\"";
       
    63     private static final String NAME_TEMPLATE_WRAPPED =
       
    64             "com.example:type=MgtInterface,id={Id}";
       
    65     private static final String NAME_TEMPLATE_FULL =
       
    66             "{Naming}";
       
    67     private static final String FULL_NAME = "com.example:type=NotAdvised";
       
    68     private static final String NAME1 = "toto1";
       
    69     private static final String NAME2 = "toto2";
       
    70     private static final String TYPE_KEY = "thisIsTheType";
       
    71     private static final String TYPE_VALUE = "aTypeValue";
       
    72     private static final String INVALID_NAME = "?,=*,\n, ";
       
    73     private static final int ID = 999;
       
    74     private static Object[] EMPTY_PARAMS = {};
       
    75     private static String[] EMPTY_SIGNATURE = {};
       
    76     private static final ObjectName OBJECTNAME_CACHE =
       
    77             ObjectName.valueOf("com.example:type=Cache");
       
    78     private static final ObjectName OBJECTNAME_SUBCACHE =
       
    79             ObjectName.valueOf("com.example:type=SubCache");
       
    80     private static final ObjectName OBJECTNAME_CACHEMX =
       
    81             ObjectName.valueOf("com.example:type=CacheMX");
       
    82     private static final ObjectName OBJECTNAME_SUBCACHEMX =
       
    83             ObjectName.valueOf("com.example:type=SubCacheMX");
       
    84     private static final ObjectName OBJECTNAME_DYNACACHE =
       
    85             ObjectName.valueOf("com.example:type=DynaCache");
       
    86     private static final ObjectName OBJECTNAME_STDCACHE =
       
    87             ObjectName.valueOf("com.example:type=StdCache");
       
    88     private static final ObjectName OBJECTNAME_STDCACHEMX =
       
    89             ObjectName.valueOf("com.example:type=StdCacheMX");
       
    90     private static final ObjectName OBJECTNAME_MULTI_1 =
       
    91             ObjectName.valueOf("com.example:" +
       
    92             "type=MultiStdCache,name=" + NAME1);
       
    93     private static final ObjectName OBJECTNAME_MULTI_2 =
       
    94             ObjectName.valueOf("com.example:" +
       
    95             "type=MultiStdCache,name=" + NAME2);
       
    96     private static final ObjectName OBJECTNAME_MONO =
       
    97             ObjectName.valueOf("com.example:" + TYPE_KEY + "=" +
       
    98             TYPE_VALUE);
       
    99     private static final ObjectName OBJECTNAME_QUOTED =
       
   100             ObjectName.valueOf("com.example:type=Quotted," +
       
   101             "name="+ObjectName.quote(INVALID_NAME));
       
   102     private static final ObjectName OBJECTNAME_WRAPPED_RESOURCE =
       
   103             ObjectName.valueOf("com.example:type=MgtInterface,id=" + ID);
       
   104     private static final ObjectName OBJECTNAME_FULL =
       
   105             ObjectName.valueOf(FULL_NAME);
       
   106 
       
   107     private static void test(Class<?> mbean, Object[] params,
       
   108             String[] signature, ObjectName name, String template)
       
   109             throws Exception {
       
   110         mbs.createMBean(mbean.getName(), null, params, signature);
       
   111         test(name, template);
       
   112         List<Class<?>> parameters = new ArrayList<Class<?>>();
       
   113         for (String sig : signature) {
       
   114             parameters.add(Class.forName(sig));
       
   115         }
       
   116         Class<?> classes[] = new Class<?>[parameters.size()];
       
   117         Constructor ctr = mbean.getConstructor(parameters.toArray(classes));
       
   118         Object inst = ctr.newInstance(params);
       
   119         test(inst, name, template);
       
   120     }
       
   121 
       
   122     private static void test(Object obj, ObjectName name, String template)
       
   123             throws Exception {
       
   124         mbs.registerMBean(obj, null);
       
   125         test(name, template);
       
   126     }
       
   127 
       
   128     private static void test(ObjectName name, String template)
       
   129             throws Exception {
       
   130         if (!mbs.isRegistered(name)) {
       
   131             throw new Exception("Wrong " + name + " name");
       
   132         }
       
   133         if (template != null && !mbs.getMBeanInfo(name).getDescriptor().
       
   134                 getFieldValue("objectNameTemplate").equals(template)) {
       
   135             throw new Exception("Invalid Derscriptor");
       
   136         }
       
   137         mbs.unregisterMBean(name);
       
   138     }
       
   139 
       
   140     public static void main(String[] args) throws Exception {
       
   141         test(Cache.class, EMPTY_PARAMS, EMPTY_SIGNATURE, OBJECTNAME_CACHE,
       
   142                 OBJECTNAME_CACHE.toString());
       
   143 
       
   144         test(CacheMX.class, EMPTY_PARAMS, EMPTY_SIGNATURE, OBJECTNAME_CACHEMX,
       
   145                 OBJECTNAME_CACHEMX.toString());
       
   146 
       
   147         test(SubCache.class, EMPTY_PARAMS, EMPTY_SIGNATURE, OBJECTNAME_SUBCACHE,
       
   148                 OBJECTNAME_SUBCACHE.toString());
       
   149 
       
   150         test(SubCacheMX.class, EMPTY_PARAMS, EMPTY_SIGNATURE, OBJECTNAME_SUBCACHEMX,
       
   151                 OBJECTNAME_SUBCACHEMX.toString());
       
   152 
       
   153         test(DynaCache.class, EMPTY_PARAMS, EMPTY_SIGNATURE, OBJECTNAME_DYNACACHE,
       
   154                 null);
       
   155 
       
   156         test(StdCacheMX.class, EMPTY_PARAMS, EMPTY_SIGNATURE, OBJECTNAME_STDCACHEMX,
       
   157                 OBJECTNAME_STDCACHEMX.toString());
       
   158 
       
   159         test(StdCache.class, EMPTY_PARAMS, EMPTY_SIGNATURE, OBJECTNAME_STDCACHE,
       
   160                 OBJECTNAME_STDCACHE.toString());
       
   161         String[] sig = {String.class.getName()};
       
   162         Object[] params = {NAME1};
       
   163         test(MultiStdCache.class, params, sig, OBJECTNAME_MULTI_1,
       
   164                 NAME_TEMPLATE_MULTI);
       
   165         Object[] params2 = {NAME2};
       
   166         test(MultiStdCache.class, params2, sig, OBJECTNAME_MULTI_2,
       
   167                 NAME_TEMPLATE_MULTI);
       
   168 
       
   169         test(MonoStdCache.class, EMPTY_PARAMS, EMPTY_SIGNATURE, OBJECTNAME_MONO,
       
   170                 NAME_TEMPLATE_MONO);
       
   171 
       
   172         test(Quoted.class, EMPTY_PARAMS, EMPTY_SIGNATURE, OBJECTNAME_QUOTED,
       
   173                 NAME_TEMPLATE_QUOTED);
       
   174 
       
   175         test(new StandardMBean(new WrappedResource(), MgtInterface.class),
       
   176                 OBJECTNAME_WRAPPED_RESOURCE, NAME_TEMPLATE_WRAPPED);
       
   177 
       
   178         test(FullName.class, EMPTY_PARAMS, EMPTY_SIGNATURE, OBJECTNAME_FULL,
       
   179                 NAME_TEMPLATE_FULL);
       
   180         try {
       
   181             test(Wrong.class, EMPTY_PARAMS, EMPTY_SIGNATURE, null, null);
       
   182             throw new Exception("No treceived expected Exception");
       
   183         } catch (NotCompliantMBeanException ncex) {
       
   184             if (!(ncex.getCause() instanceof AttributeNotFoundException)) {
       
   185                 throw new Exception("Invalid initCause");
       
   186             }
       
   187         }
       
   188     }
       
   189 
       
   190     @MBean
       
   191     @ObjectNameTemplate("{Naming}")
       
   192     public static class FullName {
       
   193 
       
   194         @ManagedAttribute
       
   195         public String getNaming() {
       
   196             return FULL_NAME;
       
   197         }
       
   198     }
       
   199 
       
   200     @ObjectNameTemplate("com.example:type=MgtInterface,id={Id}")
       
   201     public interface MgtInterface {
       
   202 
       
   203         public int getId();
       
   204     }
       
   205 
       
   206     public static class WrappedResource implements MgtInterface {
       
   207 
       
   208         public int getId() {
       
   209             return ID;
       
   210         }
       
   211     }
       
   212 
       
   213     @MBean
       
   214     @ObjectNameTemplate("com.example:type=Cache")
       
   215     public static class Cache {
       
   216     }
       
   217 
       
   218     @ObjectNameTemplate("com.example:type=SubCache")
       
   219     public static class SubCache extends Cache {
       
   220     }
       
   221 
       
   222     @MXBean
       
   223     @ObjectNameTemplate("com.example:type=CacheMX")
       
   224     public static class CacheMX {
       
   225     }
       
   226 
       
   227     @ObjectNameTemplate("com.example:type=SubCacheMX")
       
   228     public static class SubCacheMX extends CacheMX {
       
   229     }
       
   230 
       
   231     @ObjectNameTemplate("com.example:type=StdCache")
       
   232     public interface StdCacheMBean {
       
   233     }
       
   234 
       
   235     public static class StdCache implements StdCacheMBean {
       
   236     }
       
   237 
       
   238     @ObjectNameTemplate("com.example:type=StdCacheMX")
       
   239     public interface StdCacheMXBean {
       
   240     }
       
   241 
       
   242     public static class StdCacheMX implements StdCacheMXBean {
       
   243     }
       
   244 
       
   245     public static class DynaCache implements DynamicMBean {
       
   246 
       
   247         public Object getAttribute(String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException {
       
   248             throw new UnsupportedOperationException("Not supported yet.");
       
   249         }
       
   250 
       
   251         public void setAttribute(Attribute attribute) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
       
   252             throw new UnsupportedOperationException("Not supported yet.");
       
   253         }
       
   254 
       
   255         public AttributeList getAttributes(String[] attributes) {
       
   256             throw new UnsupportedOperationException("Not supported yet.");
       
   257         }
       
   258 
       
   259         public AttributeList setAttributes(AttributeList attributes) {
       
   260             throw new UnsupportedOperationException("Not supported yet.");
       
   261         }
       
   262 
       
   263         public Object invoke(String actionName, Object[] params, String[] signature) throws MBeanException, ReflectionException {
       
   264             throw new UnsupportedOperationException("Not supported yet.");
       
   265         }
       
   266 
       
   267         public MBeanInfo getMBeanInfo() {
       
   268             ImmutableDescriptor d = new ImmutableDescriptor(JMX.OBJECT_NAME_TEMPLATE + "=com.example:type=DynaCache");
       
   269 
       
   270             return new MBeanInfo("DynaCache", "Description", null, null, null, null, d);
       
   271         }
       
   272     }
       
   273 
       
   274     @ObjectNameTemplate("com.example:type=MultiStdCache,name={Name}")
       
   275     public interface MultiStdCacheMXBean {
       
   276 
       
   277         public String getName();
       
   278     }
       
   279 
       
   280     public static class MultiStdCache implements MultiStdCacheMXBean {
       
   281 
       
   282         private String name;
       
   283 
       
   284         public MultiStdCache(String name) {
       
   285             this.name = name;
       
   286         }
       
   287 
       
   288         public String getName() {
       
   289             return name;
       
   290         }
       
   291     }
       
   292 
       
   293     @ObjectNameTemplate("com.example:{Type}={TypeValue}")
       
   294     public interface MonoStdCacheMXBean {
       
   295 
       
   296         public String getTypeValue();
       
   297 
       
   298         public String getType();
       
   299     }
       
   300 
       
   301     public static class MonoStdCache implements MonoStdCacheMXBean {
       
   302 
       
   303         public String getTypeValue() {
       
   304             return TYPE_VALUE;
       
   305         }
       
   306 
       
   307         public String getType() {
       
   308             return TYPE_KEY;
       
   309         }
       
   310     }
       
   311 
       
   312     @ObjectNameTemplate("com.example:type=Quotted,name=\"{Name}\"")
       
   313     public interface QuottedMXBean {
       
   314 
       
   315         public String getName();
       
   316     }
       
   317 
       
   318     public static class Quoted implements QuottedMXBean {
       
   319 
       
   320         public String getName() {
       
   321             return INVALID_NAME;
       
   322         }
       
   323     }
       
   324 
       
   325     @ObjectNameTemplate("com.example:{Type}={TypeValue}, name={Name}")
       
   326     public interface WrongMXBean {
       
   327 
       
   328         public String getTypeValue();
       
   329 
       
   330         public String getType();
       
   331     }
       
   332 
       
   333     public static class Wrong implements WrongMXBean {
       
   334 
       
   335         public String getTypeValue() {
       
   336             return TYPE_VALUE;
       
   337         }
       
   338 
       
   339         public String getType() {
       
   340             return TYPE_KEY;
       
   341         }
       
   342     }
       
   343 }