FileDocCategorySizeDatePackage
InterceptorList.javaAPI DocJava SE 5 API8012Fri Aug 26 14:54:22 BST 2005com.sun.corba.se.impl.interceptors

InterceptorList

public class InterceptorList extends Object
Provides a repository of registered Portable Interceptors, organized by type. This list is designed to be accessed as efficiently as possible during runtime, with the expense of added complexity during initialization and interceptor registration. The class is designed to easily allow for the addition of new interceptor types.

Fields Summary
static final int
INTERCEPTOR_TYPE_CLIENT
static final int
INTERCEPTOR_TYPE_SERVER
static final int
INTERCEPTOR_TYPE_IOR
static final int
NUM_INTERCEPTOR_TYPES
static final Class[]
classTypes
private boolean
locked
private com.sun.corba.se.impl.logging.InterceptorsSystemException
wrapper
private org.omg.PortableInterceptor.Interceptor[]
interceptors
Constructors Summary
InterceptorList(com.sun.corba.se.impl.logging.InterceptorsSystemException wrapper)
Creates a new Interceptor List. Constructor is package scope so only the ORB can create it.

   
                          
        
	this.wrapper = wrapper ;
        // Create empty interceptors arrays for each type:
        initInterceptorArrays();
    
Methods Summary
voiddestroyAll()
Destroys all interceptors in this list by invoking their destroy() method.

	int numTypes = interceptors.length;

	for( int i = 0; i < numTypes; i++ ) {
	    int numInterceptors = interceptors[i].length;
	    for( int j = 0; j < numInterceptors; j++ ) {
		interceptors[i][j].destroy();
	    }
	}
    
org.omg.PortableInterceptor.Interceptor[]getInterceptors(int type)
Retrieves an array of interceptors of the given type. For efficiency, the type parameter is assumed to be valid.

        return interceptors[type];
    
private voidgrowInterceptorArray(int type)
Grows the given interceptor array by one:

        Class classType = classTypes[type];
        int currentLength = interceptors[type].length;
        Interceptor[] replacementArray;
        
        // Create new array to replace the old one.  The new array will be
        // one element larger but have the same type as the old one.
        replacementArray = (Interceptor[])
            Array.newInstance( classType, currentLength + 1 );
        System.arraycopy( interceptors[type], 0,
                          replacementArray, 0, currentLength );
        interceptors[type] = replacementArray;
    
booleanhasInterceptorsOfType(int type)
Returns true if there is at least one interceptor of the given type, or false if not.

	return interceptors[type].length > 0;
    
private voidinitInterceptorArrays()
Initializes all interceptors arrays to zero-length arrays of the correct type, based on the classTypes list.

        for( int type = 0; type < NUM_INTERCEPTOR_TYPES; type++ ) {
            Class classType = classTypes[type];
            
            // Create a zero-length array for each type:
            interceptors[type] = 
                (Interceptor[])Array.newInstance( classType, 0 );
        }
    
voidlock()
Locks this interceptor list so that no more interceptors may be registered. This method is called after all interceptors are registered for security reasons.

        locked = true;
    
voidregister_interceptor(org.omg.PortableInterceptor.Interceptor interceptor, int type)
Registers an interceptor of the given type into the interceptor list. The type is one of:
  • INTERCEPTOR_TYPE_CLIENT - ClientRequestInterceptor
  • INTERCEPTOR_TYPE_SERVER - ServerRequestInterceptor
  • INTERCEPTOR_TYPE_IOR - IORInterceptor

exception
DuplicateName Thrown if an interceptor of the given name already exists for the given type.

        // If locked, deny any further addition of interceptors.
        if( locked ) {
	    throw wrapper.interceptorListLocked() ;
        }
        
	// Cache interceptor name:
	String interceptorName = interceptor.name();
	boolean anonymous = interceptorName.equals( "" );
	boolean foundDuplicate = false;
	Interceptor[] interceptorList = interceptors[type];

	// If this is not an anonymous interceptor, 
	// search for an interceptor of the same name in this category:
	if( !anonymous ) {
	    int size = interceptorList.length;

	    // An O(n) search will suffice because register_interceptor is not
	    // likely to be called often.
	    for( int i = 0; i < size; i++ ) {
		Interceptor in = (Interceptor)interceptorList[i];
		if( in.name().equals( interceptorName ) ) {
		    foundDuplicate = true;
		    break;
		}
	    }
	}

	if( !foundDuplicate ) {
            growInterceptorArray( type );
            interceptors[type][interceptors[type].length-1] = interceptor;
	}
	else {
	    throw new DuplicateName( interceptorName );
	}
    
voidsortInterceptors()
Sort interceptors.

	List sorted = null;
	List unsorted = null;

	int numTypes = interceptors.length;

	for( int i = 0; i < numTypes; i++ ) {
	    int numInterceptors = interceptors[i].length;
	    if (numInterceptors > 0) {
		// Get fresh sorting bins for each non empty type.
		sorted = new ArrayList(); // not synchronized like we want.
		unsorted = new ArrayList();
	    }
	    for( int j = 0; j < numInterceptors; j++ ) {
		Interceptor interceptor = interceptors[i][j];
		if (interceptor instanceof Comparable) {
		    sorted.add(interceptor);
		} else {
		    unsorted.add(interceptor);
		}
	    }
	    if (numInterceptors > 0 && sorted.size() > 0) {
		// Let the RuntimeExceptions thrown by sort
		// (i.e., ClassCastException and UnsupportedOperationException)
		// flow back to the user.
		Collections.sort(sorted);
		Iterator sortedIterator = sorted.iterator();
		Iterator unsortedIterator = unsorted.iterator();
		for( int j = 0; j < numInterceptors; j++ ) {
		    if (sortedIterator.hasNext()) {
			interceptors[i][j] =
			    (Interceptor) sortedIterator.next();
		    } else if (unsortedIterator.hasNext()) {
			interceptors[i][j] =
			    (Interceptor) unsortedIterator.next();
		    } else {
			throw wrapper.sortSizeMismatch() ;
		    }
		}
	    }
	}