1 /*
   2  * Copyright (c) 2005, 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 6331783
  27  * @summary Test that MBeanServer.queryNames doesn't call getMBeanInfo on every
  28  *          resultant MBean when there is no security manager installed.
  29  * @author Luis-Miguel Alventosa
  30  * @run clean AvoidGetMBeanInfoCallsTest
  31  * @run build AvoidGetMBeanInfoCallsTest
  32  * @run main AvoidGetMBeanInfoCallsTest
  33  */
  34 
  35 import java.util.Set;
  36 import javax.management.Attribute;
  37 import javax.management.AttributeList;
  38 import javax.management.AttributeNotFoundException;
  39 import javax.management.DynamicMBean;
  40 import javax.management.InvalidAttributeValueException;
  41 import javax.management.MBeanException;
  42 import javax.management.MBeanInfo;
  43 import javax.management.MBeanServer;
  44 import javax.management.MBeanServerFactory;
  45 import javax.management.ObjectName;
  46 import javax.management.ReflectionException;
  47 
  48 public class AvoidGetMBeanInfoCallsTest {
  49 
  50     /**
  51      * Test DynamicMBean class
  52      */
  53     public static class Test implements DynamicMBean {
  54 
  55         public Object getAttribute(String attribute)
  56             throws AttributeNotFoundException,
  57                    MBeanException,
  58                    ReflectionException {
  59             return null;
  60         }
  61 
  62         public void setAttribute(Attribute attribute)
  63             throws AttributeNotFoundException,
  64                    InvalidAttributeValueException,
  65                    MBeanException,
  66                    ReflectionException {
  67         }
  68 
  69         public AttributeList getAttributes(String[] attributes) {
  70             return null;
  71         }
  72 
  73         public AttributeList setAttributes(AttributeList attributes) {
  74             return null;
  75         }
  76 
  77         public Object invoke(String actionName,
  78                              Object params[],
  79                              String signature[])
  80             throws MBeanException,
  81                    ReflectionException {
  82             return null;
  83         }
  84 
  85         public MBeanInfo getMBeanInfo() {
  86             entered = true;
  87             return new MBeanInfo(Test.class.getName(),
  88                                  "Test description",
  89                                  null, null, null, null);
  90         }
  91 
  92         public boolean entered;
  93     }
  94 
  95     /*
  96      * Print message
  97      */
  98     private static void echo(String message) {
  99         System.out.println(message);
 100     }
 101 
 102     /**
 103      * Standalone entry point.
 104      *
 105      * Run the test and report to stdout.
 106      */
 107     public static void main(String args[]) throws Exception {
 108 
 109         echo(">>> Create MBeanServer");
 110         MBeanServer server = MBeanServerFactory.newMBeanServer();
 111 
 112         echo(">>> Default Domain: " + server.getDefaultDomain());
 113 
 114         echo(">>> Create and register Test MBean");
 115         Test mbean = new Test();
 116         ObjectName name = ObjectName.getInstance(":type=Test");
 117         server.registerMBean(mbean, name);
 118 
 119         echo(">>> Set entered flag to false in Test MBean");
 120         mbean.entered = false;
 121 
 122         echo(">>> Query Names:");
 123         Set<ObjectName> names = server.queryNames(null, null);
 124         for (ObjectName on : names) {
 125             echo("\t" + on.toString());
 126         }
 127 
 128         echo(">>> Entered flag = " + mbean.entered);
 129 
 130         if (mbean.entered) {
 131             echo(">>> Test FAILED!");
 132             throw new IllegalArgumentException("getMBeanInfo got called");
 133         } else {
 134             echo(">>> Test PASSED!");
 135         }
 136     }
 137 }