jdk/src/share/classes/com/sun/jmx/interceptor/NamespaceDispatchInterceptor.java
changeset 1156 bbc2d15aaf7a
child 1222 78e3d021d528
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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 
       
    26 package com.sun.jmx.interceptor;
       
    27 
       
    28 import com.sun.jmx.defaults.JmxProperties;
       
    29 import com.sun.jmx.mbeanserver.MBeanInstantiator;
       
    30 import com.sun.jmx.mbeanserver.Repository;
       
    31 import com.sun.jmx.mbeanserver.Util;
       
    32 import com.sun.jmx.namespace.NamespaceInterceptor;
       
    33 
       
    34 import java.util.Queue;
       
    35 import java.util.logging.Level;
       
    36 import java.util.logging.Logger;
       
    37 
       
    38 import javax.management.MBeanServer;
       
    39 import javax.management.MBeanServerDelegate;
       
    40 import javax.management.MalformedObjectNameException;
       
    41 import javax.management.ObjectName;
       
    42 import javax.management.namespace.JMXDomain;
       
    43 import javax.management.namespace.JMXNamespace;
       
    44 import static javax.management.namespace.JMXNamespaces.NAMESPACE_SEPARATOR;
       
    45 
       
    46 /**
       
    47  * A dispatcher that dispatches to NamespaceInterceptors.
       
    48  * <p><b>
       
    49  * This API is a Sun internal API and is subject to changes without notice.
       
    50  * </b></p>
       
    51  * @since 1.7
       
    52  */
       
    53 public class NamespaceDispatchInterceptor
       
    54         extends DispatchInterceptor<NamespaceInterceptor, JMXNamespace> {
       
    55 
       
    56     /**
       
    57      * A logger for this class.
       
    58      **/
       
    59     private static final Logger LOG = JmxProperties.NAMESPACE_LOGGER;
       
    60 
       
    61     private static final int NAMESPACE_SEPARATOR_LENGTH =
       
    62             NAMESPACE_SEPARATOR.length();
       
    63 
       
    64     private final DomainDispatchInterceptor localNamespace;
       
    65     private final String           serverName;
       
    66 
       
    67     /**
       
    68      * Creates a NamespaceDispatchInterceptor with the specified
       
    69      * repository instance.
       
    70      * <p>Do not forget to call <code>initialize(outer,delegate)</code>
       
    71      * before using this object.
       
    72      *
       
    73      * @param outer A pointer to the MBeanServer object that must be
       
    74      *        passed to the MBeans when invoking their
       
    75      *        {@link javax.management.MBeanRegistration} interface.
       
    76      * @param delegate A pointer to the MBeanServerDelegate associated
       
    77      *        with the new MBeanServer. The new MBeanServer must register
       
    78      *        this MBean in its MBean repository.
       
    79      * @param instantiator The MBeanInstantiator that will be used to
       
    80      *        instantiate MBeans and take care of class loading issues.
       
    81      * @param repository The repository to use for this MBeanServer
       
    82      */
       
    83     public NamespaceDispatchInterceptor(MBeanServer         outer,
       
    84                                MBeanServerDelegate delegate,
       
    85                                MBeanInstantiator   instantiator,
       
    86                                Repository          repository)  {
       
    87            localNamespace = new DomainDispatchInterceptor(outer,delegate,
       
    88                    instantiator,repository,this);
       
    89            serverName = Util.getMBeanServerSecurityName(delegate);
       
    90     }
       
    91 
       
    92     // TODO: Should move that to JMXNamespace? or to ObjectName?
       
    93     /**
       
    94      * Get first name space in ObjectName path. Ignore leading namespace
       
    95      * separators.
       
    96      **/
       
    97     public static String getFirstNamespace(ObjectName name) {
       
    98         if (name == null) return "";
       
    99         final String domain = name.getDomain();
       
   100         if (domain.equals("")) return "";
       
   101 
       
   102         int first = 0;
       
   103         int end = domain.indexOf(NAMESPACE_SEPARATOR,first);
       
   104         while (end == first) {
       
   105             first = end+NAMESPACE_SEPARATOR_LENGTH;
       
   106             end = domain.indexOf(NAMESPACE_SEPARATOR,first);
       
   107             if (end == -1) break;
       
   108         }
       
   109 
       
   110         if (end == -1) return "";
       
   111 
       
   112         final String namespace = domain.substring(first,end);
       
   113 
       
   114         return namespace;
       
   115     }
       
   116 
       
   117     /**
       
   118      * Called by the DefaultMBeanServerInterceptor, just before adding an
       
   119      * MBean to the repository.
       
   120      *
       
   121      * @param resource the MBean to be registered.
       
   122      * @param logicalName the name of the MBean to be registered.
       
   123      */
       
   124     final void checkLocallyRegistrable(Object resource,
       
   125             ObjectName logicalName) {
       
   126         if (!(resource instanceof JMXNamespace) &&
       
   127                 logicalName.getDomain().contains(NAMESPACE_SEPARATOR))
       
   128             throw new IllegalArgumentException(String.valueOf(logicalName)+
       
   129                     ": Invalid ObjectName for an instance of " +
       
   130                     resource.getClass().getName());
       
   131     }
       
   132 
       
   133     final boolean isLocalHandlerNameFor(String namespace,
       
   134             ObjectName handlerName) {
       
   135         return handlerName.getDomain().equals(namespace+NAMESPACE_SEPARATOR) &&
       
   136                JMXNamespace.TYPE_ASSIGNMENT.equals(
       
   137                handlerName.getKeyPropertyListString());
       
   138     }
       
   139 
       
   140     @Override
       
   141     final MBeanServer getInterceptorOrNullFor(ObjectName name) {
       
   142         final String namespace = getFirstNamespace(name);
       
   143         if (namespace.equals("") || isLocalHandlerNameFor(namespace,name) ||
       
   144             name.getDomain().equals(namespace+NAMESPACE_SEPARATOR)) {
       
   145             LOG.finer("dispatching to local name space");
       
   146             return localNamespace;
       
   147         }
       
   148         final NamespaceInterceptor ns = getInterceptor(namespace);
       
   149         if (LOG.isLoggable(Level.FINER)) {
       
   150             if (ns != null) {
       
   151                 LOG.finer("dispatching to name space: " + namespace);
       
   152             } else {
       
   153                 LOG.finer("no handler for: " + namespace);
       
   154             }
       
   155         }
       
   156         return ns;
       
   157     }
       
   158 
       
   159     @Override
       
   160     final QueryInterceptor getInterceptorForQuery(ObjectName pattern) {
       
   161         final String namespace = getFirstNamespace(pattern);
       
   162         if (namespace.equals("") || isLocalHandlerNameFor(namespace,pattern) ||
       
   163             pattern.getDomain().equals(namespace+NAMESPACE_SEPARATOR)) {
       
   164             LOG.finer("dispatching to local name space");
       
   165             return new QueryInterceptor(localNamespace);
       
   166         }
       
   167         final NamespaceInterceptor ns = getInterceptor(namespace);
       
   168         if (LOG.isLoggable(Level.FINER)) {
       
   169             if (ns != null) {
       
   170                 LOG.finer("dispatching to name space: " + namespace);
       
   171             } else {
       
   172                 LOG.finer("no handler for: " + namespace);
       
   173             }
       
   174         }
       
   175         if (ns == null) return null;
       
   176         return new QueryInterceptor(ns);
       
   177     }
       
   178 
       
   179     @Override
       
   180     final ObjectName getHandlerNameFor(String key)
       
   181         throws MalformedObjectNameException {
       
   182         return ObjectName.getInstance(key+NAMESPACE_SEPARATOR,
       
   183                     "type", JMXNamespace.TYPE);
       
   184     }
       
   185 
       
   186     @Override
       
   187     final public String getHandlerKey(ObjectName name) {
       
   188         return getFirstNamespace(name);
       
   189     }
       
   190 
       
   191     @Override
       
   192     final NamespaceInterceptor createInterceptorFor(String key,
       
   193             ObjectName name, JMXNamespace handler,
       
   194             Queue<Runnable> postRegisterQueue) {
       
   195         final NamespaceInterceptor ns =
       
   196                 new NamespaceInterceptor(serverName,handler,key);
       
   197         if (LOG.isLoggable(Level.FINER)) {
       
   198             LOG.finer("NamespaceInterceptor created: "+ns);
       
   199         }
       
   200         return ns;
       
   201     }
       
   202 
       
   203     @Override
       
   204     final DomainDispatchInterceptor getNextInterceptor() {
       
   205         return localNamespace;
       
   206     }
       
   207 
       
   208     /**
       
   209      * Returns the list of domains in which any MBean is currently
       
   210      * registered.
       
   211      */
       
   212     @Override
       
   213     public String[] getDomains() {
       
   214         return localNamespace.getDomains();
       
   215     }
       
   216 
       
   217     @Override
       
   218     public void addNamespace(ObjectName name, JMXNamespace handler,
       
   219             Queue<Runnable> postRegisterQueue) {
       
   220         if (handler instanceof JMXDomain)
       
   221             localNamespace.addNamespace(name,
       
   222                     (JMXDomain)handler,postRegisterQueue);
       
   223         else super.addNamespace(name,handler,postRegisterQueue);
       
   224     }
       
   225 
       
   226     @Override
       
   227     public void removeNamespace(ObjectName name, JMXNamespace handler,
       
   228             Queue<Runnable> postDeregisterQueue) {
       
   229         if (handler instanceof JMXDomain)
       
   230             localNamespace.removeNamespace(name,(JMXDomain)handler,
       
   231                     postDeregisterQueue);
       
   232         else super.removeNamespace(name,handler,postDeregisterQueue);
       
   233     }
       
   234 
       
   235 
       
   236 }