jdk/src/share/classes/com/sun/jmx/interceptor/DomainDispatchInterceptor.java
changeset 1222 78e3d021d528
parent 1156 bbc2d15aaf7a
child 1709 392dd6db361a
--- a/jdk/src/share/classes/com/sun/jmx/interceptor/DomainDispatchInterceptor.java	Tue Sep 09 14:57:30 2008 +0200
+++ b/jdk/src/share/classes/com/sun/jmx/interceptor/DomainDispatchInterceptor.java	Tue Sep 09 17:01:45 2008 +0200
@@ -75,7 +75,7 @@
 
         private final DomainDispatchInterceptor parent;
         AggregatingQueryInterceptor(DomainDispatchInterceptor dispatcher) {
-            super(dispatcher.localNamespace);
+            super(dispatcher.nextInterceptor);
             parent = dispatcher;
         }
 
@@ -91,9 +91,8 @@
             // Add all matching MBeans from local namespace.
             final Set<T> res = Util.cloneSet(local);
 
-            final boolean all = (pattern == null ||
-                    pattern.getDomain().equals("*"));
             if (pattern == null) pattern = ObjectName.WILDCARD;
+            final boolean all = pattern.getDomain().equals("*");
 
             final String domain = pattern.getDomain();
 
@@ -142,7 +141,7 @@
         }
     }
 
-    private final DefaultMBeanServerInterceptor localNamespace;
+    private final DefaultMBeanServerInterceptor nextInterceptor;
     private final String mbeanServerName;
     private final MBeanServerDelegate delegate;
 
@@ -165,7 +164,7 @@
                             MBeanInstantiator   instantiator,
                             Repository          repository,
                             NamespaceDispatchInterceptor namespaces)  {
-           localNamespace = new DefaultMBeanServerInterceptor(outer,
+           nextInterceptor = new DefaultMBeanServerInterceptor(outer,
                    delegate, instantiator,repository,namespaces);
            mbeanServerName = Util.getMBeanServerSecurityName(delegate);
            this.delegate = delegate;
@@ -182,7 +181,7 @@
     @Override
     void validateHandlerNameFor(String key, ObjectName name) {
         super.validateHandlerNameFor(key,name);
-        final String[] domains = localNamespace.getDomains();
+        final String[] domains = nextInterceptor.getDomains();
         for (int i=0;i<domains.length;i++) {
             if (domains[i].equals(key))
                 throw new IllegalArgumentException("domain "+key+
@@ -192,37 +191,72 @@
 
     @Override
     final MBeanServer getInterceptorOrNullFor(ObjectName name) {
-        if (name == null) return localNamespace;
+
+        if (name == null) return nextInterceptor;
+
         final String domain = name.getDomain();
-        if (domain.endsWith(NAMESPACE_SEPARATOR)) return localNamespace;
-        if (domain.contains(NAMESPACE_SEPARATOR)) return null;
-        final String localDomain = domain;
-        if (isLocalHandlerNameFor(localDomain,name)) {
+        if (domain.endsWith(NAMESPACE_SEPARATOR))
+            return nextInterceptor; // This can be a namespace handler.
+        if (domain.contains(NAMESPACE_SEPARATOR))
+            return null; // shouldn't reach here.
+        if (isLocalHandlerNameFor(domain,name)) {
+            // This is the name of a JMXDomain MBean. Return nextInterceptor.
             LOG.finer("dispatching to local namespace");
-            return localNamespace;
+            return nextInterceptor;
         }
-        final DomainInterceptor ns = getInterceptor(localDomain);
+
+        final DomainInterceptor ns = getInterceptor(domain);
         if (ns == null) {
+            // no JMXDomain found for that domain - return nextInterceptor.
             if (LOG.isLoggable(Level.FINER)) {
-                LOG.finer("dispatching to local namespace: " + localDomain);
+                LOG.finer("dispatching to local namespace: " + domain);
             }
             return getNextInterceptor();
         }
+
         if (LOG.isLoggable(Level.FINER)) {
-            LOG.finer("dispatching to domain: " + localDomain);
+            LOG.finer("dispatching to domain: " + domain);
         }
         return ns;
     }
 
+    // This method returns true if the given pattern must be evaluated against
+    // several interceptors. This happens when either:
+    //
+    //   a) the pattern can select several domains (it's null, or it's a
+    //        domain pattern)
+    //   or b) it's not a domain pattern, but it might select the name of a
+    //        JMXDomain MBean in charge of that domain. Since the JMXDomain
+    //        MBean is located in the nextInterceptor, the pattern might need
+    //        to be evaluated on two interceptors.
+    //
+    // 1. When this method returns false, the query is evaluated on a single
+    // interceptor:
+    //    The interceptor for pattern.getDomain(), if there is one,
+    //    or the next interceptor, if there is none.
+    //
+    // 2. When this method returns true, we loop over all the domain
+    // interceptors:
+    //    in the list, and if the domain pattern matches the interceptor domain
+    //    we evaluate the query on that interceptor and aggregate the results.
+    //    Eventually we also evaluate the pattern against the next interceptor.
+    //
+    // See getInterceptorForQuery below.
+    //
     private boolean multipleQuery(ObjectName pattern) {
+        // case a) above
         if (pattern == null) return true;
         if (pattern.isDomainPattern()) return true;
 
         try {
+            // case b) above.
+            //
             // This is a bit of a hack. If there's any chance that a JMXDomain
             // MBean name is selected by the given pattern then we must include
             // the local namespace in our search.
-            // Returning true will have this effect.
+            //
+            // Returning true will have this effect. see 2. above.
+            //
             if (pattern.apply(ALL_DOMAINS.withDomain(pattern.getDomain())))
                 return true;
         } catch (MalformedObjectNameException x) {
@@ -253,7 +287,7 @@
         // We don't have a virtual domain. Send to local domains.
         if (LOG.isLoggable(Level.FINER))
              LOG.finer("dispatching to local namespace: " + domain);
-        return new QueryInterceptor(localNamespace);
+        return new QueryInterceptor(nextInterceptor);
     }
 
     @Override
@@ -288,7 +322,7 @@
 
     @Override
     final DefaultMBeanServerInterceptor getNextInterceptor() {
-        return localNamespace;
+        return nextInterceptor;
     }
 
     /**
@@ -298,11 +332,11 @@
     @Override
     public String[] getDomains() {
         // A JMXDomain is registered in its own domain.
-        // Therefore, localNamespace.getDomains() contains all domains.
-        // In addition, localNamespace will perform the necessary
+        // Therefore, nextInterceptor.getDomains() contains all domains.
+        // In addition, nextInterceptor will perform the necessary
         // MBeanPermission checks for getDomains().
         //
-        return localNamespace.getDomains();
+        return nextInterceptor.getDomains();
     }
 
     /**
@@ -310,13 +344,13 @@
      */
     @Override
     public Integer getMBeanCount() {
-        int count = getNextInterceptor().getMBeanCount().intValue();
+        int count = getNextInterceptor().getMBeanCount();
         final String[] keys = getKeys();
         for (String key:keys) {
             final MBeanServer mbs = getInterceptor(key);
             if (mbs == null) continue;
-            count += mbs.getMBeanCount().intValue();
+            count += mbs.getMBeanCount();
         }
-        return Integer.valueOf(count);
+        return count;
     }
 }