FileDocCategorySizeDatePackage
DynamicInterceptor.javaAPI DocGlassfish v2 API23238Fri May 04 22:25:40 BST 2007com.sun.enterprise.interceptor

DynamicInterceptor

public class DynamicInterceptor extends Object implements MBeanServer
This Interceptor wraps the real MBeanServer so that additional interceptor code can be "turned on" at a later point. However, it must be possible to start the MBeanServer even before the JVM calls main(). Therefore, This class must not depend on anything that can't initialize before the JVM calls main(). This includes things like logging which is not happy being invoked that early.

When instantiated at startup, the instance of this class that wraps the real MBeanServer is termed the "Primary Interceptor". There can only be one such Interceptor for each real* MBeanServer. MBeanServer #0 is the Platform MBeanServer, and this class must be used for GlassFish. Additional MBeanServers can be created if desired.

This class can also be used to implement an Interceptor which can be set for use by the Primary Interceptor. Such interceptors are used only for get/setAttribute(s) and invoke(), though the use of them could be expanded for other methods.

Note that many methods are declared 'final' for efficiency. If a subclass needs to override a method, remove 'final'. Until that time, we might as well remain efficient, since most methods won't be overridden.

Fields Summary
private volatile MBeanServer
mDelegateMBeanServer
protected final com.sun.appserv.management.helper.AMXDebugHelper
mDebug
protected final Map
mHooks
private static final boolean
SPECIAL_CHECKS
Constructors Summary
public DynamicInterceptor()

        mDelegateMBeanServer    = null;
        
        mDebug  = new AMXDebugHelper( "__DynamicInterceptor__" );
        mDebug.setEchoToStdOut( false );    // must not print to stdout, or a infinite recursion might result
        debug( "DynamicInterceptor.DynamicInterceptor" );
        
        mHooks = Collections.synchronizedMap( new HashMap<String,DynamicInterceptorHook>() );
    
Methods Summary
public synchronized voidaddHook(java.lang.String jmxDomain, DynamicInterceptorHook hook)
Add a per-domain hook which will be called for all invocations on MBeans.

Not supported: interceptors on calls that are not specific to an MBean eg countMBeans().

Works only for methods in {@link DynamicInterceptorHook).

        if ( mHooks.containsKey( jmxDomain ) )
        {
            throw new IllegalStateException();
        }
        
        debug( "Added hook for JMX domain ", jmxDomain,
            " using hook of class ", hook.getClass().getName() );
        
        mHooks.put( jmxDomain, hook );
    
public final voidaddNotificationListener(javax.management.ObjectName objectName, javax.management.NotificationListener notificationListener, javax.management.NotificationFilter notificationFilter, java.lang.Object obj)

        debug( "DynamicInterceptor.addNotificationListener: ", objectName, notificationListener.getClass().getName() );
        getDelegateMBeanServer().addNotificationListener(objectName, 
        notificationListener, notificationFilter, obj);
    
public final voidaddNotificationListener(javax.management.ObjectName objectName, javax.management.ObjectName objectName1, javax.management.NotificationFilter notificationFilter, java.lang.Object obj)

        debug( "DynamicInterceptor.addNotificationListener: ", objectName, objectName1 );
        getDelegateMBeanServer().addNotificationListener(objectName, objectName1, notificationFilter, obj);
    
private voidcheckForIllegalMBean(javax.management.ObjectName objectName)

         
        
    
        if ( SPECIAL_CHECKS )
        {
            final String jmxDomain = objectName.getDomain();
            if ( jmxDomain.equals( "ias" ) )
            {
                final String msg = "JMX domain 'ias' may not be used: " + JMXUtil.toString(objectName);
                final IllegalArgumentException e    = new IllegalArgumentException( msg );
                debug( msg, "\n", e );
                throw e;
            }
            
            /*
            if ( "server-instance".equals( objectName.getKeyProperty( "type" ) ) )
            {
                final String msg = "MBean " + JMXUtil.toString(objectName) + " is used, why?";
                final IllegalArgumentException e    = new IllegalArgumentException( msg );
                debug( msg, "\n", e );
                throw e;
            }
            */
        }
    
public final javax.management.ObjectInstancecreateMBean(java.lang.String str, javax.management.ObjectName objectName)

        debug( "DynamicInterceptor.createMBean: ", str, objectName );
        return getDelegateMBeanServer().createMBean (str, objectName);
    
public final javax.management.ObjectInstancecreateMBean(java.lang.String str, javax.management.ObjectName objectName, javax.management.ObjectName objectName2)

        debug( "DynamicInterceptor.createMBean: ", str, objectName, objectName2 );
        return getDelegateMBeanServer().createMBean (str, objectName, objectName2);
    
public final javax.management.ObjectInstancecreateMBean(java.lang.String str, javax.management.ObjectName objectName, java.lang.Object[] obj, java.lang.String[] str3)

        debug( "DynamicInterceptor.createMBean: ", str, objectName, obj, str3);
        return getDelegateMBeanServer().createMBean (str, objectName, obj, str3);
    
public final javax.management.ObjectInstancecreateMBean(java.lang.String str, javax.management.ObjectName objectName, javax.management.ObjectName objectName2, java.lang.Object[] obj, java.lang.String[] str4)

       debug( "DynamicInterceptor.createMBean: ", str, objectName, objectName2, obj, str4);
       return getDelegateMBeanServer().createMBean (str, objectName, objectName2, obj, str4);
    
protected final voiddebug(java.lang.Object args)

        mDebug.println( args );
    
public final java.io.ObjectInputStreamdeserialize(java.lang.String str, byte[] values)

       debug( "DynamicInterceptor.deserialize: ", str, values);
        return getDelegateMBeanServer().deserialize (str, values);
    
public final java.io.ObjectInputStreamdeserialize(javax.management.ObjectName objectName, byte[] values)

        debug( "DynamicInterceptor.deserialize: ", objectName, values);
        return getDelegateMBeanServer().deserialize (objectName, values);
    
public final java.io.ObjectInputStreamdeserialize(java.lang.String str, javax.management.ObjectName objectName, byte[] values)

        debug( "DynamicInterceptor.deserialize: ", str, objectName, values);
        return getDelegateMBeanServer().deserialize (str, objectName, values);
    
public final java.lang.ObjectgetAttribute(javax.management.ObjectName objectName, java.lang.String attributeName)

        Object result = null;
        final DynamicInterceptorHook    hook    = getHook(objectName);
        if ( hook != null )
        {
            result  = hook.getAttribute( objectName, attributeName );
        }
        else
        {
            result  = getDelegateMBeanServer().getAttribute( objectName, attributeName );
        }
        debug( "DynamicInterceptor.getAttribute: ", objectName, attributeName, result );
        return result;
    
public final javax.management.AttributeListgetAttributes(javax.management.ObjectName objectName, java.lang.String[] attrNames)

        AttributeList result = null;
        final DynamicInterceptorHook    hook    = getHook(objectName);
        if ( hook != null )
        {
            result  = hook.getAttributes( objectName, attrNames );
        }
        else
        {
            result  = getDelegateMBeanServer().getAttributes( objectName, attrNames );
        }
        debug( "DynamicInterceptor.getAttributes: ", objectName, attrNames, result );
        return result;
    
public final java.lang.ClassLoadergetClassLoader(javax.management.ObjectName objectName)

        debug( "DynamicInterceptor.getClassLoader: ", objectName);
            
        ClassLoader result = null;
        
        final DynamicInterceptorHook    hook    = getHook(objectName);
        if ( hook != null )
        {
        debug( "calling hook" );
            result  = hook.getClassLoader( objectName );
        }
        else
        {
            result  = getDelegateMBeanServer().getClassLoader( objectName );
        }
        return result;
    
public final java.lang.ClassLoadergetClassLoaderFor(javax.management.ObjectName objectName)

        debug( "DynamicInterceptor.getClassLoaderFor: ", objectName);
        ClassLoader result = null;
        
        final DynamicInterceptorHook    hook    = getHook(objectName);
        if ( hook != null )
        {
        debug( "calling hook" );
            result  = hook.getClassLoaderFor( objectName );
        }
        else
        {
            result  = getDelegateMBeanServer().getClassLoaderFor( objectName );
        }
        return result;
    
public final javax.management.loading.ClassLoaderRepositorygetClassLoaderRepository()

        debug( "DynamicInterceptor.getClassLoaderRepository" );
		return getDelegateMBeanServer().getClassLoaderRepository();
    
public final java.lang.StringgetDefaultDomain()

        debug( "DynamicInterceptor.getDefaultDomain: " );
        return getDelegateMBeanServer().getDefaultDomain();
    
public javax.management.MBeanServergetDelegateMBeanServer()
Get the MBeanServer to which the request can be delegated.

        return mDelegateMBeanServer;
    
public final java.lang.String[]getDomains()

        debug( "DynamicInterceptor.getDomains" );
        return getDelegateMBeanServer().getDomains();
    
protected DynamicInterceptorHookgetHook(javax.management.ObjectName objectName)

        return mHooks.get( objectName.getDomain() );
    
public final java.lang.IntegergetMBeanCount()

        debug( "DynamicInterceptor.getMBeanCount: " );
        return getDelegateMBeanServer().getMBeanCount( );
    
public final javax.management.MBeanInfogetMBeanInfo(javax.management.ObjectName objectName)

        debug( "DynamicInterceptor.getMBeanInfo: ", objectName );
        MBeanInfo result = null;
        
        final DynamicInterceptorHook    hook    = getHook(objectName);
        if ( hook != null )
        {
            result  = hook.getMBeanInfo( objectName );
        }
        else
        {
            result  = getDelegateMBeanServer().getMBeanInfo( objectName );
        }
        return result;
    
public final javax.management.ObjectInstancegetObjectInstance(javax.management.ObjectName objectName)

        debug( "DynamicInterceptor.getDefaultDomain: getObjectInstance", objectName );
        return getDelegateMBeanServer().getObjectInstance(objectName);
    
public final java.lang.Objectinstantiate(java.lang.String str)

        debug( "DynamicInterceptor.instantiate: ", str );
        return getDelegateMBeanServer().instantiate(str);
    
public final java.lang.Objectinstantiate(java.lang.String str, javax.management.ObjectName objectName)

        debug( "DynamicInterceptor.instantiate: ", str, objectName );
        return getDelegateMBeanServer().instantiate(str, objectName);
    
public final java.lang.Objectinstantiate(java.lang.String str, java.lang.Object[] obj, java.lang.String[] str2)

        debug( "DynamicInterceptor.instantiate: ", str, obj, str2 );
        return getDelegateMBeanServer().instantiate(str, obj, str2);
    
public final java.lang.Objectinstantiate(java.lang.String str, javax.management.ObjectName objectName, java.lang.Object[] obj, java.lang.String[] str3)

        debug( "DynamicInterceptor.instantiate: ", str, objectName, obj, str3 );
        return getDelegateMBeanServer().instantiate(str, objectName, obj, str3);
    
public java.lang.Objectinvoke(javax.management.ObjectName objectName, java.lang.String operationName, java.lang.Object[] params, java.lang.String[] signature)

        debug( "DynamicInterceptor.invoke(): ", objectName, ".", operationName, "{", params, "}", "{", signature, "}" );

        Object result = null;
        final DynamicInterceptorHook    hook    = getHook(objectName);
        if ( hook != null )
        {
            result  = hook.invoke( objectName, operationName, params, signature );
        }
        else
        {
            result  = getDelegateMBeanServer().invoke( objectName, operationName, params, signature );
        }
        return result;
    
public final booleanisInstanceOf(javax.management.ObjectName objectName, java.lang.String str)

        debug( "DynamicInterceptor.isInstanceOf: ", objectName, str );
        return getDelegateMBeanServer().isInstanceOf(objectName, str);
    
public final booleanisRegistered(javax.management.ObjectName objectName)

        boolean isRegistered    = getDelegateMBeanServer().isRegistered( objectName );
        
        /*
        final DynamicInterceptorHook hook = getHook(objectName);
        if ( (! isRegistered) && objectName != null && hook != null )
        {
            if ( hook.registrationHook( objectName ) )
            {
                isRegistered    = getDelegateMBeanServer().isRegistered( objectName );
            }
        }
        */

        return isRegistered;
    
public final java.util.SetqueryMBeans(javax.management.ObjectName objectName, javax.management.QueryExp expr)

        debug( "DynamicInterceptor.queryMBeans: ", objectName, expr );
        return getDelegateMBeanServer().queryMBeans( objectName, expr );
    
public final java.util.SetqueryNames(javax.management.ObjectName objectName, javax.management.QueryExp queryExp)

        debug( "DynamicInterceptor.queryNames: ", objectName, queryExp );
        return getDelegateMBeanServer().queryNames( objectName, queryExp);
    
public final javax.management.ObjectInstanceregisterMBean(java.lang.Object obj, javax.management.ObjectName objectName)

        checkForIllegalMBean( objectName );
        
        debug( "DynamicInterceptor.registerMBean: ", objectName, obj.getClass().getName() );
        return getDelegateMBeanServer().registerMBean( obj, objectName );
    
public final voidremoveNotificationListener(javax.management.ObjectName objectName, javax.management.ObjectName objectName1)

        debug( "DynamicInterceptor.removeNotificationListener: ", objectName, objectName1 );
        getDelegateMBeanServer().removeNotificationListener( objectName, objectName1);
    
public final voidremoveNotificationListener(javax.management.ObjectName objectName, javax.management.NotificationListener notificationListener)

        debug( "DynamicInterceptor.removeNotificationListener: ", objectName, notificationListener );
        getDelegateMBeanServer().removeNotificationListener( objectName, notificationListener);
    
public final voidremoveNotificationListener(javax.management.ObjectName objectName, javax.management.NotificationListener notificationListener, javax.management.NotificationFilter notificationFilter, java.lang.Object obj)

        debug( "DynamicInterceptor.removeNotificationListener: ",
            objectName, notificationListener, notificationFilter, obj);
        getDelegateMBeanServer().removeNotificationListener(objectName, notificationListener, notificationFilter, obj);
    
public final voidremoveNotificationListener(javax.management.ObjectName objectName, javax.management.ObjectName objectName1, javax.management.NotificationFilter notificationFilter, java.lang.Object obj)

        debug( "DynamicInterceptor.removeNotificationListener: ",
            objectName, objectName1, notificationFilter, obj);
        getDelegateMBeanServer().removeNotificationListener( objectName, objectName1, notificationFilter, obj);
    
public voidsetAttribute(javax.management.ObjectName objectName, javax.management.Attribute attribute)

        debug( "DynamicInterceptor.setAttribute: ", objectName, attribute );

        final DynamicInterceptorHook    hook    = getHook(objectName);
        if ( hook != null )
        {
            hook.setAttribute( objectName, attribute );
        }
        else
        {
            getDelegateMBeanServer().setAttribute( objectName, attribute );
        }
    
public javax.management.AttributeListsetAttributes(javax.management.ObjectName objectName, javax.management.AttributeList attributeList)

        debug( "DynamicInterceptor.setAttributes: ", objectName, attributeList );
        
        AttributeList result = null;
        final DynamicInterceptorHook    hook    = getHook(objectName);
        if ( hook != null )
        {
            result  = hook.setAttributes( objectName, attributeList );
        }
        else
        {
            result  = getDelegateMBeanServer().setAttributes( objectName, attributeList );
        }
        return result;

    
public voidsetDelegateMBeanServer(javax.management.MBeanServer server)
May/must be called once exactly once.

        debug( "DynamicInterceptor.setDelegateMBeanServer: " + server.getClass().getName() );
        if ( mDelegateMBeanServer != null )
        {
            throw new IllegalStateException();
        }
        mDelegateMBeanServer    = server;
        
        //System.out.println( "DynamicInterceptor: set mDelegateMBeanServer to " +
        //    "MBeanServer of class " + server.getClass().getName() );
    
public final voidunregisterMBean(javax.management.ObjectName objectName)

        debug( "DynamicInterceptor.unregisterMBean: ", objectName );
        getDelegateMBeanServer().unregisterMBean( objectName );