jdk/src/share/classes/com/sun/jmx/namespace/HandlerInterceptor.java
changeset 4159 9e3aae7675f1
parent 4158 0b4d21bc8b5c
parent 4156 acaa49a2768a
child 4160 bda0a85afcb7
equal deleted inserted replaced
4158:0b4d21bc8b5c 4159:9e3aae7675f1
     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.namespace;
       
    27 
       
    28 
       
    29 import com.sun.jmx.defaults.JmxProperties;
       
    30 import com.sun.jmx.interceptor.MBeanServerInterceptor;
       
    31 
       
    32 import com.sun.jmx.mbeanserver.Util;
       
    33 import java.io.IOException;
       
    34 import java.io.ObjectInputStream;
       
    35 import java.util.Set;
       
    36 import java.util.logging.Level;
       
    37 import java.util.logging.Logger;
       
    38 
       
    39 import javax.management.Attribute;
       
    40 import javax.management.AttributeList;
       
    41 import javax.management.AttributeNotFoundException;
       
    42 import javax.management.InstanceAlreadyExistsException;
       
    43 import javax.management.InstanceNotFoundException;
       
    44 import javax.management.IntrospectionException;
       
    45 import javax.management.InvalidAttributeValueException;
       
    46 import javax.management.ListenerNotFoundException;
       
    47 import javax.management.MBeanException;
       
    48 import javax.management.MBeanInfo;
       
    49 import javax.management.MBeanRegistrationException;
       
    50 import javax.management.MBeanServer;
       
    51 import javax.management.NotCompliantMBeanException;
       
    52 import javax.management.NotificationFilter;
       
    53 import javax.management.NotificationListener;
       
    54 import javax.management.ObjectInstance;
       
    55 import javax.management.ObjectName;
       
    56 import javax.management.OperationsException;
       
    57 import javax.management.QueryExp;
       
    58 import javax.management.ReflectionException;
       
    59 import javax.management.RuntimeOperationsException;
       
    60 import javax.management.loading.ClassLoaderRepository;
       
    61 import javax.management.namespace.JMXNamespace;
       
    62 
       
    63 /**
       
    64  * This interceptor wraps a JMXNamespace, and performs
       
    65  * {@code ObjectName} rewriting. {@code HandlerInterceptor} are
       
    66  * created and managed by a {@link NamespaceDispatchInterceptor} or a
       
    67  * {@link DomainDispatchInterceptor}.
       
    68  * <p><b>
       
    69  * This API is a Sun internal API and is subject to changes without notice.
       
    70  * </b></p>
       
    71  * @since 1.7
       
    72  */
       
    73 public abstract class HandlerInterceptor<T extends JMXNamespace>
       
    74         extends RoutingMBeanServerConnection<MBeanServer>
       
    75         implements MBeanServerInterceptor {
       
    76 
       
    77     /**
       
    78      * A logger for this class.
       
    79      **/
       
    80     private static final Logger LOG = JmxProperties.NAMESPACE_LOGGER;
       
    81 
       
    82     // The wrapped JMXNamespace
       
    83     private final T handler;
       
    84 
       
    85     /**
       
    86      * Creates a new instance of HandlerInterceptor
       
    87      */
       
    88     public HandlerInterceptor(T handler) {
       
    89         if (handler == null) throw new IllegalArgumentException("null");
       
    90         this.handler = handler;
       
    91     }
       
    92 
       
    93     //
       
    94     // The {@code source} connection is a connection to the MBeanServer
       
    95     // that contains the actual MBeans.
       
    96     // In the case of cascading, that would be a connection to the sub
       
    97     // agent. Practically, this is JMXNamespace.getSourceServer();
       
    98     //
       
    99     @Override
       
   100     protected MBeanServer source() {
       
   101          return handler.getSourceServer();
       
   102     }
       
   103 
       
   104     // The MBeanServer on which getClassLoader / getClassLoaderFor
       
   105     // will be called.
       
   106     // The NamespaceInterceptor overrides this method - so that it
       
   107     // getClassLoader / getClassLoaderFor don't trigger the loop
       
   108     // detection mechanism.
       
   109     //
       
   110     MBeanServer getServerForLoading() {
       
   111          return source();
       
   112     }
       
   113 
       
   114     // The namespace or domain handler - this either a JMXNamespace or a
       
   115     // a JMXDomain
       
   116     T getHandlerInterceptorMBean() {
       
   117         return handler;
       
   118     }
       
   119 
       
   120     // If the underlying JMXNamespace throws an IO, the IO will be
       
   121     // wrapped in a RuntimeOperationsException.
       
   122     RuntimeException handleIOException(IOException x,String fromMethodName,
       
   123             Object... params) {
       
   124             // Must do something here?
       
   125         if (LOG.isLoggable(Level.FINEST)) {
       
   126             LOG.finest("IO Exception in "+fromMethodName+": "+x+
       
   127                     " - "+" rethrowing as RuntimeOperationsException.");
       
   128         }
       
   129         throw new RuntimeOperationsException(
       
   130                     Util.newRuntimeIOException(x));
       
   131     }
       
   132 
       
   133     // From MBeanServerConnection: catch & handles IOException
       
   134     @Override
       
   135     public AttributeList getAttributes(ObjectName name, String[] attributes)
       
   136         throws InstanceNotFoundException, ReflectionException {
       
   137         try {
       
   138             final String[] authorized =
       
   139                     checkAttributes(name,attributes,"getAttribute");
       
   140             final AttributeList attrList =
       
   141                     super.getAttributes(name,authorized);
       
   142             return attrList;
       
   143         } catch (IOException ex) {
       
   144             throw handleIOException(ex,"getAttributes",name,attributes);
       
   145         }
       
   146     }
       
   147 
       
   148     // From MBeanServer
       
   149     public ClassLoader getClassLoaderFor(ObjectName mbeanName)
       
   150         throws InstanceNotFoundException {
       
   151         final ObjectName sourceName = toSourceOrRuntime(mbeanName);
       
   152         try {
       
   153             check(mbeanName,null,"getClassLoaderFor");
       
   154             return getServerForLoading().getClassLoaderFor(sourceName);
       
   155         } catch (RuntimeException ex) {
       
   156             throw makeCompliantRuntimeException(ex);
       
   157         }
       
   158     }
       
   159 
       
   160 
       
   161     // From MBeanServer
       
   162     public ClassLoader getClassLoader(ObjectName loaderName)
       
   163         throws InstanceNotFoundException {
       
   164         final ObjectName sourceName = toSourceOrRuntime(loaderName);
       
   165         try {
       
   166             check(loaderName,null,"getClassLoader");
       
   167             return getServerForLoading().getClassLoader(sourceName);
       
   168         } catch (RuntimeException ex) {
       
   169             throw makeCompliantRuntimeException(ex);
       
   170         }
       
   171     }
       
   172 
       
   173     // From MBeanServer
       
   174     public ObjectInstance registerMBean(Object object, ObjectName name)
       
   175         throws InstanceAlreadyExistsException, MBeanRegistrationException,
       
   176             NotCompliantMBeanException {
       
   177         final ObjectName sourceName = newSourceMBeanName(name);
       
   178         try {
       
   179             checkCreate(name,object.getClass().getName(),"registerMBean");
       
   180             return processOutputInstance(
       
   181                     source().registerMBean(object,sourceName));
       
   182         } catch (RuntimeException ex) {
       
   183             throw makeCompliantRuntimeException(ex);
       
   184         }
       
   185     }
       
   186 
       
   187     // From MBeanServerConnection: catch & handles IOException
       
   188     @Override
       
   189     public void removeNotificationListener(ObjectName name, ObjectName listener)
       
   190         throws InstanceNotFoundException, ListenerNotFoundException {
       
   191         try {
       
   192             check(name,null,"removeNotificationListener");
       
   193             super.removeNotificationListener(name,listener);
       
   194         } catch (IOException ex) {
       
   195             throw handleIOException(ex,"removeNotificationListener",name,listener);
       
   196         }
       
   197     }
       
   198 
       
   199     // From MBeanServerConnection: catch & handles IOException
       
   200     @Override
       
   201     public String getDefaultDomain() {
       
   202         try {
       
   203             return super.getDefaultDomain();
       
   204         } catch (IOException ex) {
       
   205             throw handleIOException(ex,"getDefaultDomain");
       
   206         }
       
   207     }
       
   208 
       
   209     // From MBeanServerConnection: catch & handles IOException
       
   210     @Override
       
   211     public String[] getDomains() {
       
   212         try {
       
   213             check(null,null,"getDomains");
       
   214             final String[] domains = super.getDomains();
       
   215             return checkDomains(domains,"getDomains");
       
   216         } catch (IOException ex) {
       
   217             throw handleIOException(ex,"getDomains");
       
   218         }
       
   219     }
       
   220 
       
   221     // From MBeanServerConnection: catch & handles IOException
       
   222     @Override
       
   223     public Integer getMBeanCount() {
       
   224         try {
       
   225             return super.getMBeanCount();
       
   226         } catch (IOException ex) {
       
   227             throw handleIOException(ex,"getMBeanCount");
       
   228         }
       
   229     }
       
   230 
       
   231     // From MBeanServerConnection: catch & handles IOException
       
   232     @Override
       
   233     public void setAttribute(ObjectName name, Attribute attribute)
       
   234         throws InstanceNotFoundException, AttributeNotFoundException,
       
   235             InvalidAttributeValueException, MBeanException,
       
   236             ReflectionException {
       
   237         try {
       
   238             check(name,
       
   239                   (attribute==null?null:attribute.getName()),
       
   240                   "setAttribute");
       
   241             super.setAttribute(name,attribute);
       
   242         } catch (IOException ex) {
       
   243             throw handleIOException(ex,"setAttribute",name, attribute);
       
   244         }
       
   245     }
       
   246 
       
   247     // From MBeanServerConnection: catch & handles IOException
       
   248     @Override
       
   249     public Set<ObjectName> queryNames(ObjectName name, QueryExp query) {
       
   250         if (name == null) name=ObjectName.WILDCARD;
       
   251         try {
       
   252             checkPattern(name,null,"queryNames");
       
   253             return super.queryNames(name,query);
       
   254         } catch (IOException ex) {
       
   255             throw handleIOException(ex,"queryNames",name, query);
       
   256         }
       
   257     }
       
   258 
       
   259     // From MBeanServerConnection: catch & handles IOException
       
   260     @Override
       
   261     public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) {
       
   262         if (name == null) name=ObjectName.WILDCARD;
       
   263         try {
       
   264             checkPattern(name,null,"queryMBeans");
       
   265             return super.queryMBeans(name,query);
       
   266         } catch (IOException ex) {
       
   267             throw handleIOException(ex,"queryMBeans",name, query);
       
   268         }
       
   269     }
       
   270 
       
   271     // From MBeanServerConnection: catch & handles IOException
       
   272     @Override
       
   273     public boolean isInstanceOf(ObjectName name, String className)
       
   274         throws InstanceNotFoundException {
       
   275         try {
       
   276             check(name, null, "isInstanceOf");
       
   277             return super.isInstanceOf(name, className);
       
   278         } catch (IOException ex) {
       
   279             throw handleIOException(ex,"isInstanceOf",name, className);
       
   280         }
       
   281     }
       
   282 
       
   283     // From MBeanServerConnection: catch & handles IOException
       
   284     @Override
       
   285     public ObjectInstance createMBean(String className, ObjectName name)
       
   286         throws ReflectionException, InstanceAlreadyExistsException,
       
   287             MBeanRegistrationException, MBeanException,
       
   288             NotCompliantMBeanException {
       
   289         try {
       
   290             checkCreate(name, className, "instantiate");
       
   291             checkCreate(name, className, "registerMBean");
       
   292             return super.createMBean(className, name);
       
   293         } catch (IOException ex) {
       
   294             throw handleIOException(ex,"createMBean",className, name);
       
   295         }
       
   296     }
       
   297 
       
   298     // From MBeanServerConnection: catch & handles IOException
       
   299     @Override
       
   300     public ObjectInstance createMBean(String className, ObjectName name,
       
   301                         ObjectName loaderName)
       
   302         throws ReflectionException, InstanceAlreadyExistsException,
       
   303                 MBeanRegistrationException, MBeanException,
       
   304                 NotCompliantMBeanException, InstanceNotFoundException {
       
   305         try {
       
   306             checkCreate(name, className, "instantiate");
       
   307             checkCreate(name, className, "registerMBean");
       
   308             return super.createMBean(className, name, loaderName);
       
   309         } catch (IOException ex) {
       
   310             throw handleIOException(ex,"createMBean",className, name, loaderName);
       
   311         }
       
   312     }
       
   313 
       
   314     // From MBeanServerConnection: catch & handles IOException
       
   315     @Override
       
   316     public Object getAttribute(ObjectName name, String attribute)
       
   317         throws MBeanException, AttributeNotFoundException,
       
   318             InstanceNotFoundException, ReflectionException {
       
   319         try {
       
   320             check(name, attribute, "getAttribute");
       
   321             return super.getAttribute(name, attribute);
       
   322         } catch (IOException ex) {
       
   323             throw handleIOException(ex,"getAttribute",name, attribute);
       
   324         }
       
   325     }
       
   326 
       
   327     // From MBeanServerConnection: catch & handles IOException
       
   328     @Override
       
   329     public void removeNotificationListener(ObjectName name, ObjectName listener,
       
   330                             NotificationFilter filter, Object handback)
       
   331         throws InstanceNotFoundException, ListenerNotFoundException {
       
   332         try {
       
   333             check(name,null,"removeNotificationListener");
       
   334             super.removeNotificationListener(name, listener, filter, handback);
       
   335         } catch (IOException ex) {
       
   336             throw handleIOException(ex,"removeNotificationListener",name,
       
   337                     listener, filter, handback);
       
   338         }
       
   339     }
       
   340 
       
   341     // From MBeanServerConnection: catch & handles IOException
       
   342     @Override
       
   343     public void removeNotificationListener(ObjectName name,
       
   344                       NotificationListener listener, NotificationFilter filter,
       
   345                       Object handback)
       
   346         throws InstanceNotFoundException, ListenerNotFoundException {
       
   347         try {
       
   348             check(name,null,"removeNotificationListener");
       
   349             super.removeNotificationListener(name, listener, filter, handback);
       
   350         } catch (IOException ex) {
       
   351             throw handleIOException(ex,"removeNotificationListener",name,
       
   352                     listener, filter, handback);
       
   353         }
       
   354     }
       
   355 
       
   356     // From MBeanServerConnection: catch & handles IOException
       
   357     @Override
       
   358     public void removeNotificationListener(ObjectName name,
       
   359                 NotificationListener listener)
       
   360         throws InstanceNotFoundException, ListenerNotFoundException {
       
   361         try {
       
   362             check(name,null,"removeNotificationListener");
       
   363             super.removeNotificationListener(name, listener);
       
   364         } catch (IOException ex) {
       
   365             throw handleIOException(ex,"removeNotificationListener",name,
       
   366                     listener);
       
   367         }
       
   368     }
       
   369 
       
   370     // From MBeanServerConnection: catch & handles IOException
       
   371     @Override
       
   372     public void addNotificationListener(ObjectName name,
       
   373                     NotificationListener listener, NotificationFilter filter,
       
   374                     Object handback) throws InstanceNotFoundException {
       
   375         try {
       
   376             check(name,null,"addNotificationListener");
       
   377             super.addNotificationListener(name, listener, filter, handback);
       
   378         } catch (IOException ex) {
       
   379             throw handleIOException(ex,"addNotificationListener",name,
       
   380                     listener, filter, handback);
       
   381         }
       
   382     }
       
   383 
       
   384     // From MBeanServerConnection: catch & handles IOException
       
   385     @Override
       
   386     public void addNotificationListener(ObjectName name, ObjectName listener,
       
   387                 NotificationFilter filter, Object handback)
       
   388         throws InstanceNotFoundException {
       
   389         try {
       
   390             check(name,null,"addNotificationListener");
       
   391             super.addNotificationListener(name, listener, filter, handback);
       
   392         } catch (IOException ex) {
       
   393             throw handleIOException(ex,"addNotificationListener",name,
       
   394                     listener, filter, handback);
       
   395         }
       
   396     }
       
   397 
       
   398     // From MBeanServerConnection: catch & handles IOException
       
   399     @Override
       
   400     public boolean isRegistered(ObjectName name) {
       
   401         try {
       
   402             return super.isRegistered(name);
       
   403         } catch (IOException ex) {
       
   404             throw handleIOException(ex,"isRegistered",name);
       
   405         }
       
   406     }
       
   407 
       
   408     // From MBeanServerConnection: catch & handles IOException
       
   409     @Override
       
   410     public void unregisterMBean(ObjectName name)
       
   411         throws InstanceNotFoundException, MBeanRegistrationException {
       
   412         try {
       
   413             check(name, null, "unregisterMBean");
       
   414             super.unregisterMBean(name);
       
   415         } catch (IOException ex) {
       
   416             throw handleIOException(ex,"unregisterMBean",name);
       
   417         }
       
   418     }
       
   419 
       
   420     // From MBeanServerConnection: catch & handles IOException
       
   421     @Override
       
   422     public MBeanInfo getMBeanInfo(ObjectName name)
       
   423         throws InstanceNotFoundException, IntrospectionException,
       
   424             ReflectionException {
       
   425         try {
       
   426             check(name, null, "getMBeanInfo");
       
   427             return super.getMBeanInfo(name);
       
   428         } catch (IOException ex) {
       
   429             throw handleIOException(ex,"getMBeanInfo",name);
       
   430         }
       
   431     }
       
   432 
       
   433     // From MBeanServerConnection: catch & handles IOException
       
   434     @Override
       
   435     public ObjectInstance getObjectInstance(ObjectName name)
       
   436         throws InstanceNotFoundException {
       
   437         try {
       
   438             check(name, null, "getObjectInstance");
       
   439             return super.getObjectInstance(name);
       
   440         } catch (IOException ex) {
       
   441             throw handleIOException(ex,"getObjectInstance",name);
       
   442         }
       
   443     }
       
   444 
       
   445     // From MBeanServerConnection: catch & handles IOException
       
   446     @Override
       
   447     public ObjectInstance createMBean(String className, ObjectName name,
       
   448                 Object[] params, String[] signature)
       
   449         throws ReflectionException, InstanceAlreadyExistsException,
       
   450             MBeanRegistrationException, MBeanException,
       
   451             NotCompliantMBeanException {
       
   452         try {
       
   453             checkCreate(name, className, "instantiate");
       
   454             checkCreate(name, className, "registerMBean");
       
   455             return super.createMBean(className, name, params, signature);
       
   456         } catch (IOException ex) {
       
   457             throw handleIOException(ex,"createMBean",className, name,
       
   458                     params, signature);
       
   459         }
       
   460     }
       
   461 
       
   462     // From MBeanServerConnection: catch & handles IOException
       
   463     @Override
       
   464     public ObjectInstance createMBean(String className, ObjectName name,
       
   465                 ObjectName loaderName, Object[] params, String[] signature)
       
   466         throws ReflectionException, InstanceAlreadyExistsException,
       
   467             MBeanRegistrationException, MBeanException,
       
   468             NotCompliantMBeanException, InstanceNotFoundException {
       
   469         try {
       
   470             checkCreate(name, className, "instantiate");
       
   471             checkCreate(name, className, "registerMBean");
       
   472             return super.createMBean(className, name, loaderName, params,
       
   473                     signature);
       
   474         } catch (IOException ex) {
       
   475             throw handleIOException(ex,"createMBean",className, name,loaderName,
       
   476                     params, signature);
       
   477         }
       
   478     }
       
   479 
       
   480     // From MBeanServerConnection: catch & handles IOException
       
   481     @Override
       
   482     public AttributeList setAttributes(ObjectName name,AttributeList attributes)
       
   483     throws InstanceNotFoundException, ReflectionException {
       
   484         try {
       
   485             final AttributeList authorized =
       
   486                     checkAttributes(name, attributes, "setAttribute");
       
   487             return super.setAttributes(name, authorized);
       
   488         } catch (IOException ex) {
       
   489             throw handleIOException(ex,"setAttributes",name, attributes);
       
   490         }
       
   491     }
       
   492 
       
   493     // From MBeanServerConnection: catch & handles IOException
       
   494     @Override
       
   495     public Object invoke(ObjectName name, String operationName, Object[] params,
       
   496                 String[] signature)
       
   497         throws InstanceNotFoundException, MBeanException, ReflectionException {
       
   498         try {
       
   499             check(name, operationName, "invoke");
       
   500             return super.invoke(name, operationName, params, signature);
       
   501         } catch (IOException ex) {
       
   502             throw handleIOException(ex,"invoke",name, operationName,
       
   503                     params, signature);
       
   504         }
       
   505     }
       
   506 
       
   507     //
       
   508     //  These methods are inherited from MBeanServer....
       
   509     //
       
   510 
       
   511     /**
       
   512      * This method should never be called.
       
   513      * Throws UnsupportedOperationException.
       
   514      */
       
   515     public Object instantiate(String className)
       
   516             throws ReflectionException, MBeanException {
       
   517         if (LOG.isLoggable(Level.FINE))
       
   518             LOG.fine("call to unsupported instantiate method: " +
       
   519                     "trowing UnsupportedOperationException");
       
   520         throw new UnsupportedOperationException("Not applicable.");
       
   521     }
       
   522 
       
   523     /**
       
   524      * This method should never be called.
       
   525      * Throws UnsupportedOperationException.
       
   526      */
       
   527     public Object instantiate(String className, ObjectName loaderName)
       
   528             throws ReflectionException, MBeanException,
       
   529             InstanceNotFoundException {
       
   530         if (LOG.isLoggable(Level.FINE))
       
   531             LOG.fine("call to unsupported method: instantiate(...) -" +
       
   532                     "throwing UnsupportedOperationException");
       
   533         throw new UnsupportedOperationException("Not applicable.");
       
   534     }
       
   535 
       
   536     /**
       
   537      * This method should never be called.
       
   538      * Throws UnsupportedOperationException.
       
   539      */
       
   540     public Object instantiate(String className, Object[] params,
       
   541             String[] signature) throws ReflectionException, MBeanException {
       
   542         if (LOG.isLoggable(Level.FINE))
       
   543             LOG.fine("call to unsupported method: instantiate(...) -" +
       
   544                     "throwing UnsupportedOperationException");
       
   545         throw new UnsupportedOperationException("Not applicable.");
       
   546     }
       
   547 
       
   548     /**
       
   549      * This method should never be called.
       
   550      * Throws UnsupportedOperationException.
       
   551      */
       
   552     public Object instantiate(String className, ObjectName loaderName,
       
   553             Object[] params, String[] signature)
       
   554             throws ReflectionException, MBeanException,
       
   555             InstanceNotFoundException {
       
   556         if (LOG.isLoggable(Level.FINE))
       
   557             LOG.fine("call to unsupported method: instantiate(...) -" +
       
   558                     "throwing UnsupportedOperationException");
       
   559         throw new UnsupportedOperationException("Not applicable.");
       
   560     }
       
   561 
       
   562     /**
       
   563      * This method should never be called.
       
   564      * Throws UnsupportedOperationException.
       
   565      */
       
   566     @Deprecated
       
   567     public ObjectInputStream deserialize(ObjectName name, byte[] data)
       
   568             throws InstanceNotFoundException, OperationsException {
       
   569         if (LOG.isLoggable(Level.FINE))
       
   570             LOG.fine("call to unsupported method: deserialize(...) -" +
       
   571                     "throwing UnsupportedOperationException");
       
   572         throw new UnsupportedOperationException("Not applicable.");
       
   573     }
       
   574 
       
   575     /**
       
   576      * This method should never be called.
       
   577      * Throws UnsupportedOperationException.
       
   578      */
       
   579     @Deprecated
       
   580     public ObjectInputStream deserialize(String className, byte[] data)
       
   581             throws OperationsException, ReflectionException {
       
   582         if (LOG.isLoggable(Level.FINE))
       
   583             LOG.fine("call to unsupported method: deserialize(...) -" +
       
   584                     "throwing UnsupportedOperationException");
       
   585         throw new UnsupportedOperationException("Not applicable.");
       
   586     }
       
   587 
       
   588     /**
       
   589      * This method should never be called.
       
   590      * Throws UnsupportedOperationException.
       
   591      */
       
   592     @Deprecated
       
   593     public ObjectInputStream deserialize(String className,
       
   594             ObjectName loaderName, byte[] data)
       
   595             throws InstanceNotFoundException, OperationsException,
       
   596             ReflectionException {
       
   597         if (LOG.isLoggable(Level.FINE))
       
   598             LOG.fine("call to unsupported method: deserialize(...) -" +
       
   599                     "throwing UnsupportedOperationException");
       
   600         throw new UnsupportedOperationException("Not applicable.");
       
   601     }
       
   602 
       
   603     /**
       
   604      * This method should never be called.
       
   605      * Throws UnsupportedOperationException.
       
   606      */
       
   607     public ClassLoaderRepository getClassLoaderRepository() {
       
   608         if (LOG.isLoggable(Level.FINE))
       
   609             LOG.fine("call to unsupported method: getClassLoaderRepository() -" +
       
   610                     "throwing UnsupportedOperationException");
       
   611         throw new UnsupportedOperationException("Not applicable.");
       
   612     }
       
   613 
       
   614     static RuntimeException newUnsupportedException(String namespace) {
       
   615         return new RuntimeOperationsException(
       
   616             new UnsupportedOperationException(
       
   617                 "Not supported in this namespace: "+namespace));
       
   618     }
       
   619 
       
   620     /**
       
   621      * A result might be excluded for security reasons.
       
   622      */
       
   623     @Override
       
   624     boolean excludesFromResult(ObjectName targetName, String queryMethod) {
       
   625         return !checkQuery(targetName, queryMethod);
       
   626     }
       
   627 
       
   628 
       
   629     //----------------------------------------------------------------------
       
   630     // Hooks for checking permissions
       
   631     //----------------------------------------------------------------------
       
   632 
       
   633    /**
       
   634      * This method is a hook to implement permission checking in subclasses.
       
   635      * A subclass may override this method and throw a {@link
       
   636      * SecurityException} if the permission is denied.
       
   637      *
       
   638      * @param routingName The name of the MBean in the enclosing context.
       
   639      *        This is of the form {@code <namespace>//<ObjectName>}.
       
   640      * @param member The {@link
       
   641      *  javax.management.namespace.JMXNamespacePermission#getMember member}
       
   642      *  name.
       
   643      * @param action The {@link
       
   644      *  javax.management.namespace.JMXNamespacePermission#getActions action}
       
   645      *  name.
       
   646      * @throws SecurityException if the caller doesn't have the permission
       
   647      *         to perform the given action on the MBean pointed to
       
   648      *         by routingName.
       
   649      */
       
   650     abstract void check(ObjectName routingName,
       
   651                         String member, String action);
       
   652 
       
   653     // called in createMBean and registerMBean
       
   654     abstract void checkCreate(ObjectName routingName, String className,
       
   655                                 String action);
       
   656 
       
   657     /**
       
   658      * This is a hook to implement permission checking in subclasses.
       
   659      *
       
   660      * Checks that the caller has sufficient permission for returning
       
   661      * information about {@code sourceName} in {@code action}.
       
   662      *
       
   663      * Subclass may override this method and return false if the caller
       
   664      * doesn't have sufficient permissions.
       
   665      *
       
   666      * @param routingName The name of the MBean to include or exclude from
       
   667      *        the query, expressed in the enclosing context.
       
   668      *        This is of the form {@code <namespace>//<ObjectName>}.
       
   669      * @param action one of "queryNames" or "queryMBeans"
       
   670      * @return true if {@code sourceName} can be returned.
       
   671      */
       
   672     abstract boolean checkQuery(ObjectName routingName, String action);
       
   673 
       
   674     /**
       
   675      * This method is a hook to implement permission checking in subclasses.
       
   676      *
       
   677      * @param routingName The name of the MBean in the enclosing context.
       
   678      *        This is of the form {@code <namespace>//<ObjectName>}.
       
   679      * @param attributes  The list of attributes to check permission for.
       
   680      * @param action one of "getAttribute" or "setAttribute"
       
   681      * @return The list of attributes for which the callers has the
       
   682      *         appropriate {@link
       
   683      *         javax.management.namespace.JMXNamespacePermission}.
       
   684      * @throws SecurityException if the caller doesn't have the permission
       
   685      *         to perform {@code action} on the MBean pointed to by routingName.
       
   686      */
       
   687     abstract String[] checkAttributes(ObjectName routingName,
       
   688             String[] attributes, String action);
       
   689 
       
   690     /**
       
   691      * This method is a hook to implement permission checking in subclasses.
       
   692      *
       
   693      * @param routingName The name of the MBean in the enclosing context.
       
   694      *        This is of the form {@code <namespace>//<ObjectName>}.
       
   695      * @param attributes The list of attributes to check permission for.
       
   696      * @param action one of "getAttribute" or "setAttribute"
       
   697      * @return The list of attributes for which the callers has the
       
   698      *         appropriate {@link
       
   699      *         javax.management.namespace.JMXNamespacePermission}.
       
   700      * @throws SecurityException if the caller doesn't have the permission
       
   701      *         to perform {@code action} on the MBean pointed to by routingName.
       
   702      */
       
   703     abstract AttributeList checkAttributes(ObjectName routingName,
       
   704             AttributeList attributes, String action);
       
   705 
       
   706     /**
       
   707      * This method is a hook to implement permission checking in subclasses.
       
   708      * Checks that the caller as the necessary permissions to view the
       
   709      * given domain. If not remove the domains for which the caller doesn't
       
   710      * have permission from the list.
       
   711      * <p>
       
   712      * By default, this method always returns {@code domains}
       
   713      *
       
   714      * @param domains The domains to return.
       
   715      * @param action  "getDomains"
       
   716      * @return a filtered list of domains.
       
   717      */
       
   718     String[] checkDomains(String[] domains, String action) {
       
   719         return domains;
       
   720     }
       
   721 
       
   722     // A priori check for queryNames/queryMBeans/
       
   723     void checkPattern(ObjectName routingPattern,
       
   724                String member, String action) {
       
   725         // pattern is checked only at posteriori by checkQuery.
       
   726         // checking it a priori usually doesn't work, because ObjectName.apply
       
   727         // does not work between two patterns.
       
   728         // We only check that we have the permission requested for 'action'.
       
   729         check(null,null,action);
       
   730     }
       
   731 
       
   732 
       
   733 
       
   734 }