jdk/test/javax/management/namespace/QueryNamesTest.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 QueryNamesTest.java 1.4
       
    26  * @summary Test how queryNames works with Namespaces.
       
    27  * @author Daniel Fuchs
       
    28  * @run clean QueryNamesTest Wombat WombatMBean
       
    29  * @run build QueryNamesTest Wombat WombatMBean
       
    30  * @run main QueryNamesTest
       
    31  */
       
    32 
       
    33 
       
    34 import java.io.IOException;
       
    35 import java.lang.management.ManagementFactory;
       
    36 import java.util.Arrays;
       
    37 import java.util.Collections;
       
    38 import java.util.HashMap;
       
    39 import java.util.HashSet;
       
    40 import java.util.LinkedHashMap;
       
    41 import java.util.LinkedHashSet;
       
    42 import java.util.LinkedList;
       
    43 import java.util.List;
       
    44 import java.util.Map;
       
    45 import java.util.Random;
       
    46 import java.util.Set;
       
    47 import java.util.logging.Logger;
       
    48 import javax.management.InstanceNotFoundException;
       
    49 import javax.management.JMException;
       
    50 import javax.management.MBeanServer;
       
    51 import javax.management.MBeanServerConnection;
       
    52 import javax.management.MBeanServerFactory;
       
    53 import javax.management.MalformedObjectNameException;
       
    54 import javax.management.ObjectName;
       
    55 import javax.management.namespace.JMXNamespace;
       
    56 import javax.management.namespace.JMXNamespaces;
       
    57 
       
    58 /**
       
    59  * Class QueryNamesTest
       
    60  * @author Sun Microsystems, 2005 - All rights reserved.
       
    61  */
       
    62 public class QueryNamesTest {
       
    63 
       
    64     /**
       
    65      * A logger for this class.
       
    66      **/
       
    67     private static final Logger LOG =
       
    68             Logger.getLogger(QueryNamesTest.class.getName());
       
    69 
       
    70     public static class LocalNamespace
       
    71             extends JMXNamespace {
       
    72 
       
    73         private static MBeanServer check(MBeanServer server) {
       
    74             if (server == null)
       
    75                 throw new IllegalArgumentException("MBeanServer can't be null");
       
    76             return server;
       
    77         }
       
    78 
       
    79         public LocalNamespace() {
       
    80             this(MBeanServerFactory.createMBeanServer());
       
    81         }
       
    82 
       
    83         public LocalNamespace(MBeanServer server) {
       
    84             super(check(server));
       
    85         }
       
    86 
       
    87 
       
    88         public static String add(MBeanServerConnection server,
       
    89                 String nspath)
       
    90                 throws IOException, JMException {
       
    91             server.createMBean(LocalNamespace.class.getName(),
       
    92                     JMXNamespaces.getNamespaceObjectName(nspath));
       
    93             return nspath;
       
    94         }
       
    95     }
       
    96 
       
    97     /** Creates a new instance of QueryNamesTest */
       
    98     public QueryNamesTest() {
       
    99     }
       
   100 
       
   101     private static String[] namespaces = {
       
   102         "greg", "greg//chichille", "greg//chichille//petard",
       
   103         "greg//alambic", "greg//alambic//canette",
       
   104         "greg//chichille/virgule", "greg//chichille/funeste",
       
   105         "greg//chichille/virgule//bidouble",
       
   106         "greg//chichille/virgule//bi/double",
       
   107         "fran", "fran//gast", "fran//gast//gaf",
       
   108         "fran//longtar", "fran//longtar//parcmetre"
       
   109     };
       
   110 
       
   111     private static void createNamespaces(MBeanServer server) throws Exception {
       
   112         final LinkedList<String> all = new LinkedList<String>();
       
   113         try {
       
   114             for (String ns : namespaces)
       
   115                 all.addFirst(LocalNamespace.add(server,ns));
       
   116         } catch (Exception e) {
       
   117             removeNamespaces(server,all.toArray(new String[all.size()]));
       
   118             throw e;
       
   119         }
       
   120     }
       
   121 
       
   122     // Dummy test that checks that all JMXNamespaces are registered,
       
   123     // but are not returned by queryNames("*:*");
       
   124     //
       
   125     private static void checkRegistration(MBeanServer server)
       
   126         throws Exception {
       
   127         final Set<ObjectName> handlerNames = new HashSet<ObjectName>(namespaces.length);
       
   128         for (String ns : namespaces)
       
   129             handlerNames.add(JMXNamespaces.getNamespaceObjectName(ns));
       
   130         for (ObjectName nh : handlerNames) // check handler registration
       
   131             if (!server.isRegistered(nh))
       
   132                 throw new InstanceNotFoundException("handler "+nh+
       
   133                         " is not registered");
       
   134 
       
   135         // global: queryNames("*:*") from top level
       
   136         final Set<ObjectName> all1 = server.queryNames(null,null);
       
   137         final Set<ObjectName> all2 = server.queryNames(ObjectName.WILDCARD,null);
       
   138         if (!all1.equals(all2))
       
   139             throw new Exception("queryNames(*:*) != queryNames(null)");
       
   140         final Set<ObjectName> common = new HashSet<ObjectName>(all1);
       
   141         common.retainAll(handlerNames);
       
   142 
       
   143         final Set<ObjectName> ref = new HashSet<ObjectName>();
       
   144         for (String ns : namespaces) {
       
   145             if (!ns.contains(JMXNamespaces.NAMESPACE_SEPARATOR))
       
   146                 ref.add(JMXNamespaces.getNamespaceObjectName(ns));
       
   147         }
       
   148         if (!common.equals(ref)) {
       
   149             throw new Exception("some handler names were not returned by " +
       
   150                     "wildcard query - only returned: "+common+
       
   151                     ", expected: "+ref);
       
   152         }
       
   153 
       
   154         // for each namespace: queryNames("<namespace>//*:*");
       
   155         for (String ns : namespaces) {
       
   156             final ObjectName pattern = new ObjectName(ns+
       
   157                     JMXNamespaces.NAMESPACE_SEPARATOR+"*:*");
       
   158             final Set<ObjectName> all4 =
       
   159                     server.queryNames(pattern,null);
       
   160             final Set<ObjectName> common4 = new HashSet<ObjectName>(all4);
       
   161             common4.retainAll(handlerNames);
       
   162 
       
   163             final Set<ObjectName> ref4 = new HashSet<ObjectName>();
       
   164             for (String ns2 : namespaces) {
       
   165                 if (! ns2.startsWith(ns+JMXNamespaces.NAMESPACE_SEPARATOR))
       
   166                     continue;
       
   167                 if (!ns2.substring(ns.length()+
       
   168                         JMXNamespaces.NAMESPACE_SEPARATOR.length()).
       
   169                         contains(JMXNamespaces.NAMESPACE_SEPARATOR))
       
   170                     ref4.add(JMXNamespaces.getNamespaceObjectName(ns2));
       
   171             }
       
   172             if (!common4.equals(ref4)) {
       
   173                 throw new Exception("some handler names were not returned by " +
       
   174                     "wildcard query on "+pattern+" - only returned: "+common4+
       
   175                     ", expected: "+ref4);
       
   176             }
       
   177         }
       
   178     }
       
   179 
       
   180     // Make a Map<parent, direct children>
       
   181     private static Map<String,Set<String>> makeNsTree(String[] nslist) {
       
   182         final Map<String,Set<String>> nsTree =
       
   183                 new LinkedHashMap<String,Set<String>>(nslist.length);
       
   184         for (String ns : nslist) {
       
   185             if (nsTree.get(ns) == null)
       
   186                 nsTree.put(ns,new LinkedHashSet<String>());
       
   187             final String[] elts = ns.split(JMXNamespaces.NAMESPACE_SEPARATOR);
       
   188             int last = ns.lastIndexOf(JMXNamespaces.NAMESPACE_SEPARATOR);
       
   189             if (last<0) continue;
       
   190             while (last > 0 && ns.charAt(last-1) == '/') last--;
       
   191             final String parent = ns.substring(0,last);
       
   192             if (nsTree.get(parent) == null)
       
   193                 nsTree.put(parent,new LinkedHashSet<String>());
       
   194             nsTree.get(parent).add(ns);
       
   195         }
       
   196         return nsTree;
       
   197     }
       
   198 
       
   199     private static class Rigolo {
       
   200         final static String[] ones = { "a", "e", "i", "o", "u", "y", "ai", "oo",
       
   201         "ae", "ey", "ay", "oy", "au", "ou", "eu", "oi", "ei", "ea"};
       
   202         final static String[] twos = { "b", "bz", "c", "cz", "ch",
       
   203         "ct", "ck", "cs", "d", "ds", "f",  "g", "gh", "h", "j", "k", "l", "m",
       
   204         "n", "p", "ps", "q", "r", "s", "sh", "t", "v", "w", "x",
       
   205         "z"};
       
   206         final static String[] threes = {"rr","tt","pp","ss","dd","ff","ll", "mm", "nn",
       
   207         "zz", "cc", "bb"};
       
   208         final static String[] fours = {"x", "s", "ght", "cks", "rt", "rts", "ghts", "bs",
       
   209           "ts", "gg" };
       
   210         final static String[] fives = { "br", "bl", "cr", "cn", "cth", "dr",
       
   211         "fr", "fl", "cl", "chr",  "gr", "gl", "kr", "kh", "pr", "pl", "ph",
       
   212         "rh", "sr", "tr", "vr"};
       
   213 
       
   214         private Random rg = new Random();
       
   215 
       
   216         private String next(String[] table) {
       
   217             return table[rg.nextInt(table.length)];
       
   218         }
       
   219 
       
   220         public String nextName(int max) {
       
   221             final Random rg = new Random();
       
   222             final int nl = 3 + rg.nextInt(max);
       
   223             boolean begin = rg.nextBoolean();
       
   224             StringBuilder sb = new StringBuilder();
       
   225             for (int j = 0; j < nl ; j++) {
       
   226                 if (begin) {
       
   227                     sb.append(next(ones));
       
   228                 } else if (j > 0 && j < nl-1 && rg.nextInt(4)==0) {
       
   229                     sb.append(next(threes));
       
   230                 } else if (j < nl-1 && rg.nextInt(3)==0) {
       
   231                     sb.append(next(fives));
       
   232                 } else {
       
   233                     sb.append(next(twos));
       
   234                 }
       
   235                 begin = !begin;
       
   236             }
       
   237             if (!begin && rg.nextInt(2)==0)
       
   238                 sb.append(next(fours));
       
   239             return sb.toString();
       
   240         }
       
   241 
       
   242         private ObjectName getWombatName(String ns, String domain, String name)
       
   243             throws MalformedObjectNameException {
       
   244             String d = domain;
       
   245             if (ns != null && !ns.equals(""))
       
   246                 d = ns + JMXNamespaces.NAMESPACE_SEPARATOR + domain;
       
   247             return new ObjectName(d+":type=Wombat,name="+name);
       
   248         }
       
   249 
       
   250         public Set<ObjectName> nextWombats(String ns)
       
   251             throws MalformedObjectNameException  {
       
   252             final int dcount = 1 + rg.nextInt(5);
       
   253             final Set<ObjectName> wombats = new HashSet<ObjectName>();
       
   254             for (int i = 0; i < dcount ; i++) {
       
   255                 final String d = nextName(7);
       
   256                 final int ncount = 5 + rg.nextInt(20);
       
   257                 for (int j = 0 ; j<ncount; j++) {
       
   258                     final String n = nextName(5);
       
   259                     wombats.add(getWombatName(ns,d,n));
       
   260                 }
       
   261             }
       
   262             return wombats;
       
   263         }
       
   264     }
       
   265 
       
   266     public static void checkNsQuery(MBeanServer server)
       
   267         throws Exception {
       
   268         final Map<String,Set<String>> nsTree = makeNsTree(namespaces);
       
   269         final Random rg = new Random();
       
   270         final Rigolo rigolo = new Rigolo();
       
   271         for (String ns : namespaces) {
       
   272             final ObjectName name = JMXNamespaces.getNamespaceObjectName(ns);
       
   273             final String[] doms =
       
   274                     (String[])server.getAttribute(name,"Domains");
       
   275             final Set<String> subs = new HashSet<String>();
       
   276             for (String d : doms) {
       
   277                 if (d.endsWith(JMXNamespaces.NAMESPACE_SEPARATOR)) {
       
   278                     subs.add(ns+JMXNamespaces.NAMESPACE_SEPARATOR+d.substring(0,
       
   279                             d.length()-JMXNamespaces.NAMESPACE_SEPARATOR.length()));
       
   280                 }
       
   281             }
       
   282 
       
   283             final Set<String> expectNs = new HashSet<String>(nsTree.get(ns));
       
   284 
       
   285             if (! subs.containsAll(expectNs))
       
   286                 throw new Exception("getDomains didn't return all namespaces: "+
       
   287                         "returned="+subs+", expected="+expectNs);
       
   288             if (! expectNs.containsAll(subs))
       
   289                 throw new Exception("getDomains returned additional namespaces: "+
       
   290                         "returned="+subs+", expected="+expectNs);
       
   291 
       
   292             final Set<ObjectName> nsNames = server.queryNames(
       
   293                     new ObjectName(ns+
       
   294                     JMXNamespaces.NAMESPACE_SEPARATOR+"*"+
       
   295                     JMXNamespaces.NAMESPACE_SEPARATOR+":*"),null);
       
   296 
       
   297             final Set<ObjectName> expect =
       
   298                     new HashSet<ObjectName>(expectNs.size());
       
   299             for (String sub : expectNs) {
       
   300                 expect.add(JMXNamespaces.getNamespaceObjectName(sub));
       
   301             }
       
   302 
       
   303             if (! nsNames.containsAll(expect))
       
   304                 throw new Exception("queryNames didn't return all namespaces: "+
       
   305                         "returned="+nsNames+", expected="+expect);
       
   306             if (! expect.containsAll(nsNames))
       
   307                 throw new Exception("getDomains returned additional namespaces: "+
       
   308                         "returned="+nsNames+", expected="+expect);
       
   309 
       
   310         }
       
   311     }
       
   312 
       
   313     private static void addWombats(MBeanServer server, Set<ObjectName> names)
       
   314         throws Exception {
       
   315         for (ObjectName on : names) {
       
   316             if (! server.isRegistered(on)) {
       
   317                 server.createMBean(Wombat.class.getName(),on);
       
   318                 System.out.println("A new wombat is born: "+on);
       
   319             }
       
   320         }
       
   321     }
       
   322 
       
   323     private static void addWombats(MBeanServer server,
       
   324              Map<String,Set<ObjectName>> wombats)
       
   325         throws Exception {
       
   326         for (String ns : wombats.keySet()) {
       
   327             addWombats(server,wombats.get(ns));
       
   328         }
       
   329     }
       
   330 
       
   331     private static Map<String,Set<ObjectName>> nameWombats()
       
   332         throws Exception {
       
   333         final Rigolo rigolo = new Rigolo();
       
   334         final Map<String,Set<ObjectName>> wombats =
       
   335                 new HashMap<String,Set<ObjectName>>(namespaces.length);
       
   336 
       
   337         for (String ns : namespaces) {
       
   338             wombats.put(ns,rigolo.nextWombats(ns));
       
   339         }
       
   340         wombats.put("",rigolo.nextWombats(""));
       
   341         return wombats;
       
   342     }
       
   343 
       
   344     private static boolean removeWombats(MBeanServer server,
       
   345             Map<String,Set<ObjectName>> wombats) {
       
   346         boolean res = true;
       
   347         for (String ns : wombats.keySet()) {
       
   348             res = res && removeWombats(server,wombats.get(ns));
       
   349         }
       
   350         return res;
       
   351     }
       
   352 
       
   353     private static boolean removeWombats(MBeanServer server,
       
   354             Set<ObjectName> wombats) {
       
   355         boolean res = true;
       
   356         for (ObjectName on : wombats) {
       
   357             try {
       
   358                 if (server.isRegistered(on))
       
   359                     server.unregisterMBean(on);
       
   360             } catch (Exception x) {
       
   361                 res = false;
       
   362                 System.out.println("Failed to remove "+on+": "+x);
       
   363             }
       
   364         }
       
   365         return res;
       
   366     }
       
   367 
       
   368     public static void main(String[] args)
       
   369         throws Exception {
       
   370         final MBeanServer server = ManagementFactory.getPlatformMBeanServer();
       
   371         Map<String,Set<ObjectName>>  wombats = nameWombats();
       
   372         createNamespaces(server);
       
   373         try {
       
   374             addWombats(server,wombats);
       
   375             System.out.println("MBeans: " +server.getMBeanCount());
       
   376             System.out.println("Visible: " +server.queryNames(null,null).size());
       
   377             System.out.println("Domains: " +Arrays.asList(server.getDomains()));
       
   378             checkRegistration(server);
       
   379             checkNsQuery(server);
       
   380         } finally {
       
   381             boolean res = true;
       
   382             res = res && removeWombats(server, wombats);
       
   383             if (!res)
       
   384                 throw new RuntimeException("failed to cleanup some namespaces");
       
   385         }
       
   386 
       
   387     }
       
   388 
       
   389     private static boolean removeNamespaces(MBeanServer server) {
       
   390         final List<String> l = Arrays.asList(namespaces);
       
   391         Collections.reverse(l);
       
   392         return removeNamespaces(server, l.toArray(new String[namespaces.length]));
       
   393     }
       
   394 
       
   395     private static boolean removeNamespaces(MBeanServer server, String[] t) {
       
   396         boolean success = true;
       
   397         for (String ns : t) {
       
   398             try {
       
   399                 server.unregisterMBean(JMXNamespaces.getNamespaceObjectName(ns));
       
   400             } catch (Exception x) {
       
   401                 System.out.println("failed to remove namespace: "+ ns);
       
   402                 success = false;
       
   403             }
       
   404         }
       
   405         return success;
       
   406     }
       
   407 
       
   408 }