jdk/src/share/classes/com/sun/jmx/namespace/HandlerInterceptor.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.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  * usually created and managed by a {@link NamespaceDispatcher} or
       
    67  * {@link DomainDispatcher}.
       
    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     @Override
       
    94     protected MBeanServer source() {
       
    95          return handler.getSourceServer();
       
    96     }
       
    97 
       
    98     // The MBeanServer on which getClassLoader / getClassLoaderFor
       
    99     // will be called.
       
   100     // The NamespaceInterceptor overrides this method - so that it
       
   101     // getClassLoader / getClassLoaderFor don't trigger the loop
       
   102     // detection mechanism.
       
   103     //
       
   104     MBeanServer getServerForLoading() {
       
   105          return source();
       
   106     }
       
   107 
       
   108     T getNamespace() {
       
   109         return handler;
       
   110     }
       
   111 
       
   112     // If the underlying JMXNamespace throws an IO, the IO will be
       
   113     // wrapped in a RuntimeOperationsException.
       
   114     RuntimeException handleIOException(IOException x,String fromMethodName,
       
   115             Object... params) {
       
   116             // Must do something here?
       
   117         if (LOG.isLoggable(Level.FINEST)) {
       
   118             LOG.finest("IO Exception in "+fromMethodName+": "+x+
       
   119                     " - "+" rethrowing as RuntimeOperationsException.");
       
   120         }
       
   121         throw new RuntimeOperationsException(
       
   122                     Util.newRuntimeIOException(x));
       
   123     }
       
   124 
       
   125     // From MBeanServer: catch & handles IOException
       
   126     @Override
       
   127     public AttributeList getAttributes(ObjectName name, String[] attributes)
       
   128         throws InstanceNotFoundException, ReflectionException {
       
   129         try {
       
   130             return super.getAttributes(name, attributes);
       
   131         } catch (IOException ex) {
       
   132             throw handleIOException(ex,"getAttributes",name,attributes);
       
   133         }
       
   134     }
       
   135 
       
   136     // From MBeanServer
       
   137     public ClassLoader getClassLoaderFor(ObjectName mbeanName)
       
   138         throws InstanceNotFoundException {
       
   139         final ObjectName sourceName = toSourceOrRuntime(mbeanName);
       
   140         try {
       
   141             check(mbeanName,null,"getClassLoaderFor");
       
   142             return getServerForLoading().getClassLoaderFor(sourceName);
       
   143         } catch (RuntimeException ex) {
       
   144             throw makeCompliantRuntimeException(ex);
       
   145         }
       
   146     }
       
   147 
       
   148 
       
   149     // From MBeanServer
       
   150     public ClassLoader getClassLoader(ObjectName loaderName)
       
   151         throws InstanceNotFoundException {
       
   152         final ObjectName sourceName = toSourceOrRuntime(loaderName);
       
   153         try {
       
   154             check(loaderName,null,"getClassLoader");
       
   155             return getServerForLoading().getClassLoader(sourceName);
       
   156         } catch (RuntimeException ex) {
       
   157             throw makeCompliantRuntimeException(ex);
       
   158         }
       
   159     }
       
   160 
       
   161     // From MBeanServer
       
   162     public ObjectInstance registerMBean(Object object, ObjectName name)
       
   163         throws InstanceAlreadyExistsException, MBeanRegistrationException,
       
   164             NotCompliantMBeanException {
       
   165         final ObjectName sourceName = newSourceMBeanName(name);
       
   166         try {
       
   167             checkCreate(name,object.getClass().getName(),"registerMBean");
       
   168             return processOutputInstance(
       
   169                     source().registerMBean(object,sourceName));
       
   170         } catch (RuntimeException ex) {
       
   171             throw makeCompliantRuntimeException(ex);
       
   172         }
       
   173     }
       
   174 
       
   175     // From MBeanServer: catch & handles IOException
       
   176     @Override
       
   177     public void removeNotificationListener(ObjectName name, ObjectName listener)
       
   178         throws InstanceNotFoundException, ListenerNotFoundException {
       
   179         try {
       
   180             super.removeNotificationListener(name, listener);
       
   181         } catch (IOException ex) {
       
   182             throw handleIOException(ex,"removeNotificationListener",name,listener);
       
   183         }
       
   184     }
       
   185 
       
   186     // From MBeanServer: catch & handles IOException
       
   187     @Override
       
   188     public String getDefaultDomain() {
       
   189         try {
       
   190             return super.getDefaultDomain();
       
   191         } catch (IOException ex) {
       
   192             throw handleIOException(ex,"getDefaultDomain");
       
   193         }
       
   194     }
       
   195 
       
   196     // From MBeanServer: catch & handles IOException
       
   197     @Override
       
   198     public String[] getDomains() {
       
   199         try {
       
   200             return super.getDomains();
       
   201         } catch (IOException ex) {
       
   202             throw handleIOException(ex,"getDomains");
       
   203         }
       
   204     }
       
   205 
       
   206     // From MBeanServer: catch & handles IOException
       
   207     @Override
       
   208     public Integer getMBeanCount() {
       
   209         try {
       
   210             return super.getMBeanCount();
       
   211         } catch (IOException ex) {
       
   212             throw handleIOException(ex,"getMBeanCount");
       
   213         }
       
   214     }
       
   215 
       
   216     // From MBeanServer: catch & handles IOException
       
   217     @Override
       
   218     public void setAttribute(ObjectName name, Attribute attribute)
       
   219         throws InstanceNotFoundException, AttributeNotFoundException,
       
   220             InvalidAttributeValueException, MBeanException,
       
   221             ReflectionException {
       
   222         try {
       
   223             super.setAttribute(name, attribute);
       
   224         } catch (IOException ex) {
       
   225             throw handleIOException(ex,"setAttribute",name, attribute);
       
   226         }
       
   227     }
       
   228 
       
   229     // From MBeanServer: catch & handles IOException
       
   230     @Override
       
   231     public Set<ObjectName> queryNames(ObjectName name, QueryExp query) {
       
   232         try {
       
   233             return super.queryNames(name, query);
       
   234         } catch (IOException ex) {
       
   235             throw handleIOException(ex,"queryNames",name, query);
       
   236         }
       
   237     }
       
   238 
       
   239     // From MBeanServer: catch & handles IOException
       
   240     @Override
       
   241     public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) {
       
   242         try {
       
   243             return super.queryMBeans(name, query);
       
   244         } catch (IOException ex) {
       
   245             throw handleIOException(ex,"queryMBeans",name, query);
       
   246         }
       
   247     }
       
   248 
       
   249     // From MBeanServer: catch & handles IOException
       
   250     @Override
       
   251     public boolean isInstanceOf(ObjectName name, String className)
       
   252         throws InstanceNotFoundException {
       
   253         try {
       
   254             return super.isInstanceOf(name, className);
       
   255         } catch (IOException ex) {
       
   256             throw handleIOException(ex,"isInstanceOf",name, className);
       
   257         }
       
   258     }
       
   259 
       
   260     // From MBeanServer: catch & handles IOException
       
   261     @Override
       
   262     public ObjectInstance createMBean(String className, ObjectName name)
       
   263         throws ReflectionException, InstanceAlreadyExistsException,
       
   264             MBeanRegistrationException, MBeanException,
       
   265             NotCompliantMBeanException {
       
   266         try {
       
   267             return super.createMBean(className, name);
       
   268         } catch (IOException ex) {
       
   269             throw handleIOException(ex,"createMBean",className, name);
       
   270         }
       
   271     }
       
   272 
       
   273     // From MBeanServer: catch & handles IOException
       
   274     @Override
       
   275     public ObjectInstance createMBean(String className, ObjectName name,
       
   276                         ObjectName loaderName)
       
   277         throws ReflectionException, InstanceAlreadyExistsException,
       
   278                 MBeanRegistrationException, MBeanException,
       
   279                 NotCompliantMBeanException, InstanceNotFoundException {
       
   280         try {
       
   281             return super.createMBean(className, name, loaderName);
       
   282         } catch (IOException ex) {
       
   283             throw handleIOException(ex,"createMBean",className, name, loaderName);
       
   284         }
       
   285     }
       
   286 
       
   287     // From MBeanServer: catch & handles IOException
       
   288     @Override
       
   289     public Object getAttribute(ObjectName name, String attribute)
       
   290         throws MBeanException, AttributeNotFoundException,
       
   291             InstanceNotFoundException, ReflectionException {
       
   292         try {
       
   293             return super.getAttribute(name, attribute);
       
   294         } catch (IOException ex) {
       
   295             throw handleIOException(ex,"getAttribute",name, attribute);
       
   296         }
       
   297     }
       
   298 
       
   299     // From MBeanServer: catch & handles IOException
       
   300     @Override
       
   301     public void removeNotificationListener(ObjectName name, ObjectName listener,
       
   302                             NotificationFilter filter, Object handback)
       
   303         throws InstanceNotFoundException, ListenerNotFoundException {
       
   304         try {
       
   305             super.removeNotificationListener(name, listener, filter, handback);
       
   306         } catch (IOException ex) {
       
   307             throw handleIOException(ex,"removeNotificationListener",name,
       
   308                     listener, filter, handback);
       
   309         }
       
   310     }
       
   311 
       
   312     // From MBeanServer: catch & handles IOException
       
   313     @Override
       
   314     public void removeNotificationListener(ObjectName name,
       
   315                       NotificationListener listener, NotificationFilter filter,
       
   316                       Object handback)
       
   317         throws InstanceNotFoundException, ListenerNotFoundException {
       
   318         try {
       
   319             super.removeNotificationListener(name, listener, filter, handback);
       
   320         } catch (IOException ex) {
       
   321             throw handleIOException(ex,"removeNotificationListener",name,
       
   322                     listener, filter, handback);
       
   323         }
       
   324     }
       
   325 
       
   326     // From MBeanServer: catch & handles IOException
       
   327     @Override
       
   328     public void removeNotificationListener(ObjectName name,
       
   329                 NotificationListener listener)
       
   330         throws InstanceNotFoundException, ListenerNotFoundException {
       
   331         try {
       
   332             super.removeNotificationListener(name, listener);
       
   333         } catch (IOException ex) {
       
   334             throw handleIOException(ex,"removeNotificationListener",name,
       
   335                     listener);
       
   336         }
       
   337     }
       
   338 
       
   339     // From MBeanServer: catch & handles IOException
       
   340     @Override
       
   341     public void addNotificationListener(ObjectName name,
       
   342                     NotificationListener listener, NotificationFilter filter,
       
   343                     Object handback) throws InstanceNotFoundException {
       
   344         try {
       
   345             super.addNotificationListener(name, listener, filter, handback);
       
   346         } catch (IOException ex) {
       
   347             throw handleIOException(ex,"addNotificationListener",name,
       
   348                     listener, filter, handback);
       
   349         }
       
   350     }
       
   351 
       
   352     // From MBeanServer: catch & handles IOException
       
   353     @Override
       
   354     public void addNotificationListener(ObjectName name, ObjectName listener,
       
   355                 NotificationFilter filter, Object handback)
       
   356         throws InstanceNotFoundException {
       
   357         try {
       
   358             super.addNotificationListener(name, listener, filter, handback);
       
   359         } catch (IOException ex) {
       
   360             throw handleIOException(ex,"addNotificationListener",name,
       
   361                     listener, filter, handback);
       
   362         }
       
   363     }
       
   364 
       
   365     // From MBeanServer: catch & handles IOException
       
   366     @Override
       
   367     public boolean isRegistered(ObjectName name) {
       
   368         try {
       
   369             return super.isRegistered(name);
       
   370         } catch (IOException ex) {
       
   371             throw handleIOException(ex,"isRegistered",name);
       
   372         }
       
   373     }
       
   374 
       
   375     // From MBeanServer: catch & handles IOException
       
   376     @Override
       
   377     public void unregisterMBean(ObjectName name)
       
   378         throws InstanceNotFoundException, MBeanRegistrationException {
       
   379         try {
       
   380             super.unregisterMBean(name);
       
   381         } catch (IOException ex) {
       
   382             throw handleIOException(ex,"unregisterMBean",name);
       
   383         }
       
   384     }
       
   385 
       
   386     // From MBeanServer: catch & handles IOException
       
   387     @Override
       
   388     public MBeanInfo getMBeanInfo(ObjectName name)
       
   389         throws InstanceNotFoundException, IntrospectionException,
       
   390             ReflectionException {
       
   391         try {
       
   392             return super.getMBeanInfo(name);
       
   393         } catch (IOException ex) {
       
   394             throw handleIOException(ex,"getMBeanInfo",name);
       
   395         }
       
   396     }
       
   397 
       
   398     // From MBeanServer: catch & handles IOException
       
   399     @Override
       
   400     public ObjectInstance getObjectInstance(ObjectName name)
       
   401         throws InstanceNotFoundException {
       
   402         try {
       
   403             return super.getObjectInstance(name);
       
   404         } catch (IOException ex) {
       
   405             throw handleIOException(ex,"getObjectInstance",name);
       
   406         }
       
   407     }
       
   408 
       
   409     // From MBeanServer: catch & handles IOException
       
   410     @Override
       
   411     public ObjectInstance createMBean(String className, ObjectName name,
       
   412                 Object[] params, String[] signature)
       
   413         throws ReflectionException, InstanceAlreadyExistsException,
       
   414             MBeanRegistrationException, MBeanException,
       
   415             NotCompliantMBeanException {
       
   416         try {
       
   417             return super.createMBean(className, name, params, signature);
       
   418         } catch (IOException ex) {
       
   419             throw handleIOException(ex,"createMBean",className, name,
       
   420                     params, signature);
       
   421         }
       
   422     }
       
   423 
       
   424     // From MBeanServer: catch & handles IOException
       
   425     @Override
       
   426     public ObjectInstance createMBean(String className, ObjectName name,
       
   427                 ObjectName loaderName, Object[] params, String[] signature)
       
   428         throws ReflectionException, InstanceAlreadyExistsException,
       
   429             MBeanRegistrationException, MBeanException,
       
   430             NotCompliantMBeanException, InstanceNotFoundException {
       
   431         try {
       
   432             return super.createMBean(className, name, loaderName, params,
       
   433                     signature);
       
   434         } catch (IOException ex) {
       
   435             throw handleIOException(ex,"createMBean",className, name,loaderName,
       
   436                     params, signature);
       
   437         }
       
   438     }
       
   439 
       
   440     // From MBeanServer: catch & handles IOException
       
   441     @Override
       
   442     public AttributeList setAttributes(ObjectName name,AttributeList attributes)
       
   443     throws InstanceNotFoundException, ReflectionException {
       
   444         try {
       
   445             return super.setAttributes(name, attributes);
       
   446         } catch (IOException ex) {
       
   447             throw handleIOException(ex,"setAttributes",name, attributes);
       
   448         }
       
   449     }
       
   450 
       
   451     // From MBeanServer: catch & handles IOException
       
   452     @Override
       
   453     public Object invoke(ObjectName name, String operationName, Object[] params,
       
   454                 String[] signature)
       
   455         throws InstanceNotFoundException, MBeanException, ReflectionException {
       
   456         try {
       
   457             return super.invoke(name, operationName, params, signature);
       
   458         } catch (IOException ex) {
       
   459             throw handleIOException(ex,"invoke",name, operationName,
       
   460                     params, signature);
       
   461         }
       
   462     }
       
   463 
       
   464     //
       
   465     //  These methods are inherited from MBeanServer....
       
   466     //
       
   467 
       
   468     /**
       
   469      * This method should never be called.
       
   470      * Throws UnsupportedOperationException.
       
   471      */
       
   472     public Object instantiate(String className)
       
   473             throws ReflectionException, MBeanException {
       
   474         if (LOG.isLoggable(Level.FINE))
       
   475             LOG.fine("call to unsupported instantiate method: " +
       
   476                     "trowing UnsupportedOperationException");
       
   477         throw new UnsupportedOperationException("Not applicable.");
       
   478     }
       
   479 
       
   480     /**
       
   481      * This method should never be called.
       
   482      * Throws UnsupportedOperationException.
       
   483      */
       
   484     public Object instantiate(String className, ObjectName loaderName)
       
   485             throws ReflectionException, MBeanException,
       
   486             InstanceNotFoundException {
       
   487         if (LOG.isLoggable(Level.FINE))
       
   488             LOG.fine("call to unsupported method: instantiate(...) -" +
       
   489                     "throwing UnsupportedOperationException");
       
   490         throw new UnsupportedOperationException("Not applicable.");
       
   491     }
       
   492 
       
   493     /**
       
   494      * This method should never be called.
       
   495      * Throws UnsupportedOperationException.
       
   496      */
       
   497     public Object instantiate(String className, Object[] params,
       
   498             String[] signature) throws ReflectionException, MBeanException {
       
   499         if (LOG.isLoggable(Level.FINE))
       
   500             LOG.fine("call to unsupported method: instantiate(...) -" +
       
   501                     "throwing UnsupportedOperationException");
       
   502         throw new UnsupportedOperationException("Not applicable.");
       
   503     }
       
   504 
       
   505     /**
       
   506      * This method should never be called.
       
   507      * Throws UnsupportedOperationException.
       
   508      */
       
   509     public Object instantiate(String className, ObjectName loaderName,
       
   510             Object[] params, String[] signature)
       
   511             throws ReflectionException, MBeanException,
       
   512             InstanceNotFoundException {
       
   513         if (LOG.isLoggable(Level.FINE))
       
   514             LOG.fine("call to unsupported method: instantiate(...) -" +
       
   515                     "throwing UnsupportedOperationException");
       
   516         throw new UnsupportedOperationException("Not applicable.");
       
   517     }
       
   518 
       
   519     /**
       
   520      * This method should never be called.
       
   521      * Throws UnsupportedOperationException.
       
   522      */
       
   523     @Deprecated
       
   524     public ObjectInputStream deserialize(ObjectName name, byte[] data)
       
   525             throws InstanceNotFoundException, OperationsException {
       
   526         if (LOG.isLoggable(Level.FINE))
       
   527             LOG.fine("call to unsupported method: deserialize(...) -" +
       
   528                     "throwing UnsupportedOperationException");
       
   529         throw new UnsupportedOperationException("Not applicable.");
       
   530     }
       
   531 
       
   532     /**
       
   533      * This method should never be called.
       
   534      * Throws UnsupportedOperationException.
       
   535      */
       
   536     @Deprecated
       
   537     public ObjectInputStream deserialize(String className, byte[] data)
       
   538             throws OperationsException, ReflectionException {
       
   539         if (LOG.isLoggable(Level.FINE))
       
   540             LOG.fine("call to unsupported method: deserialize(...) -" +
       
   541                     "throwing UnsupportedOperationException");
       
   542         throw new UnsupportedOperationException("Not applicable.");
       
   543     }
       
   544 
       
   545     /**
       
   546      * This method should never be called.
       
   547      * Throws UnsupportedOperationException.
       
   548      */
       
   549     @Deprecated
       
   550     public ObjectInputStream deserialize(String className,
       
   551             ObjectName loaderName, byte[] data)
       
   552             throws InstanceNotFoundException, OperationsException,
       
   553             ReflectionException {
       
   554         if (LOG.isLoggable(Level.FINE))
       
   555             LOG.fine("call to unsupported method: deserialize(...) -" +
       
   556                     "throwing UnsupportedOperationException");
       
   557         throw new UnsupportedOperationException("Not applicable.");
       
   558     }
       
   559 
       
   560     /**
       
   561      * This method should never be called.
       
   562      * Throws UnsupportedOperationException.
       
   563      */
       
   564     public ClassLoaderRepository getClassLoaderRepository() {
       
   565         if (LOG.isLoggable(Level.FINE))
       
   566             LOG.fine("call to unsupported method: getClassLoaderRepository() -" +
       
   567                     "throwing UnsupportedOperationException");
       
   568         throw new UnsupportedOperationException("Not applicable.");
       
   569     }
       
   570 
       
   571     static RuntimeException newUnsupportedException(String namespace) {
       
   572         return new RuntimeOperationsException(
       
   573             new UnsupportedOperationException(
       
   574                 "Not supported in this namespace: "+namespace));
       
   575     }
       
   576 
       
   577 }