KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > interceptors > InterceptorList


1 /*
2  * @(#)InterceptorList.java 1.18 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.interceptors;
9
10 import org.omg.PortableInterceptor.Interceptor JavaDoc;
11 import org.omg.PortableInterceptor.ORBInitInfo JavaDoc;
12 import org.omg.PortableInterceptor.ORBInitInfoPackage.DuplicateName JavaDoc;
13
14 import org.omg.CORBA.INTERNAL JavaDoc;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Collections JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.lang.reflect.Array JavaDoc;
22
23 import com.sun.corba.se.impl.logging.InterceptorsSystemException ;
24
25 /**
26  * Provides a repository of registered Portable Interceptors, organized
27  * by type. This list is designed to be accessed as efficiently as
28  * possible during runtime, with the expense of added complexity during
29  * initialization and interceptor registration. The class is designed
30  * to easily allow for the addition of new interceptor types.
31  */

32 public class InterceptorList {
33
34     // Interceptor type list. If additional interceptors are needed,
35
// add additional types in numerical order (do not skip numbers),
36
// and update NUM_INTERCEPTOR_TYPES and classTypes accordingly.
37
// NUM_INTERCEPTOR_TYPES represents the number of interceptor
38
// types, so we know how many lists to maintain.
39
static final int INTERCEPTOR_TYPE_CLIENT = 0;
40     static final int INTERCEPTOR_TYPE_SERVER = 1;
41     static final int INTERCEPTOR_TYPE_IOR = 2;
42     
43     static final int NUM_INTERCEPTOR_TYPES = 3;
44     
45     // Array of class types for interceptors. This is used to create the
46
// appropriate array type for each interceptor type. These must
47
// match the indices of the constants declared above.
48
static final Class JavaDoc[] classTypes = {
49         org.omg.PortableInterceptor.ClientRequestInterceptor JavaDoc.class,
50         org.omg.PortableInterceptor.ServerRequestInterceptor JavaDoc.class,
51         org.omg.PortableInterceptor.IORInterceptor JavaDoc.class
52     };
53     
54     // True if no further interceptors may be registered with this list.
55
private boolean locked = false;
56     private InterceptorsSystemException wrapper ;
57
58     // List of interceptors currently registered. There are
59
// NUM_INTERCEPTOR_TYPES lists of registered interceptors.
60
// For example, interceptors[INTERCEPTOR_TYPE_CLIENT] contains an array
61
// of objects of type ClientRequestInterceptor.
62
private Interceptor JavaDoc[][] interceptors =
63         new Interceptor JavaDoc[NUM_INTERCEPTOR_TYPES][];
64    
65     /**
66      * Creates a new Interceptor List. Constructor is package scope so
67      * only the ORB can create it.
68      */

69     InterceptorList( InterceptorsSystemException wrapper ) {
70     this.wrapper = wrapper ;
71         // Create empty interceptors arrays for each type:
72
initInterceptorArrays();
73     }
74
75     /**
76      * Registers an interceptor of the given type into the interceptor list.
77      * The type is one of:
78      * <ul>
79      * <li>INTERCEPTOR_TYPE_CLIENT - ClientRequestInterceptor
80      * <li>INTERCEPTOR_TYPE_SERVER - ServerRequestInterceptor
81      * <li>INTERCEPTOR_TYPE_IOR - IORInterceptor
82      * </ul>
83      *
84      * @exception DuplicateName Thrown if an interceptor of the given
85      * name already exists for the given type.
86      */

87     void register_interceptor( Interceptor JavaDoc interceptor, int type )
88     throws DuplicateName JavaDoc
89     {
90         // If locked, deny any further addition of interceptors.
91
if( locked ) {
92         throw wrapper.interceptorListLocked() ;
93         }
94         
95     // Cache interceptor name:
96
String JavaDoc interceptorName = interceptor.name();
97     boolean anonymous = interceptorName.equals( "" );
98     boolean foundDuplicate = false;
99     Interceptor JavaDoc[] interceptorList = interceptors[type];
100
101     // If this is not an anonymous interceptor,
102
// search for an interceptor of the same name in this category:
103
if( !anonymous ) {
104         int size = interceptorList.length;
105
106         // An O(n) search will suffice because register_interceptor is not
107
// likely to be called often.
108
for( int i = 0; i < size; i++ ) {
109         Interceptor JavaDoc in = (Interceptor JavaDoc)interceptorList[i];
110         if( in.name().equals( interceptorName ) ) {
111             foundDuplicate = true;
112             break;
113         }
114         }
115     }
116
117     if( !foundDuplicate ) {
118             growInterceptorArray( type );
119             interceptors[type][interceptors[type].length-1] = interceptor;
120     }
121     else {
122         throw new DuplicateName JavaDoc( interceptorName );
123     }
124     }
125
126     /**
127      * Locks this interceptor list so that no more interceptors may be
128      * registered. This method is called after all interceptors are
129      * registered for security reasons.
130      */

131     void lock() {
132         locked = true;
133     }
134     
135     /**
136      * Retrieves an array of interceptors of the given type. For efficiency,
137      * the type parameter is assumed to be valid.
138      */

139     Interceptor JavaDoc[] getInterceptors( int type ) {
140         return interceptors[type];
141     }
142
143     /**
144      * Returns true if there is at least one interceptor of the given type,
145      * or false if not.
146      */

147     boolean hasInterceptorsOfType( int type ) {
148     return interceptors[type].length > 0;
149     }
150     
151     /**
152      * Initializes all interceptors arrays to zero-length arrays of the
153      * correct type, based on the classTypes list.
154      */

155     private void initInterceptorArrays() {
156         for( int type = 0; type < NUM_INTERCEPTOR_TYPES; type++ ) {
157             Class JavaDoc classType = classTypes[type];
158             
159             // Create a zero-length array for each type:
160
interceptors[type] =
161                 (Interceptor JavaDoc[])Array.newInstance( classType, 0 );
162         }
163     }
164     
165     /**
166      * Grows the given interceptor array by one:
167      */

168     private void growInterceptorArray( int type ) {
169         Class JavaDoc classType = classTypes[type];
170         int currentLength = interceptors[type].length;
171         Interceptor JavaDoc[] replacementArray;
172         
173         // Create new array to replace the old one. The new array will be
174
// one element larger but have the same type as the old one.
175
replacementArray = (Interceptor JavaDoc[])
176             Array.newInstance( classType, currentLength + 1 );
177         System.arraycopy( interceptors[type], 0,
178                           replacementArray, 0, currentLength );
179         interceptors[type] = replacementArray;
180     }
181
182     /**
183      * Destroys all interceptors in this list by invoking their destroy()
184      * method.
185      */

186     void destroyAll() {
187     int numTypes = interceptors.length;
188
189     for( int i = 0; i < numTypes; i++ ) {
190         int numInterceptors = interceptors[i].length;
191         for( int j = 0; j < numInterceptors; j++ ) {
192         interceptors[i][j].destroy();
193         }
194     }
195     }
196
197     /**
198      * Sort interceptors.
199      */

200     void sortInterceptors() {
201     List JavaDoc sorted = null;
202     List JavaDoc unsorted = null;
203
204     int numTypes = interceptors.length;
205
206     for( int i = 0; i < numTypes; i++ ) {
207         int numInterceptors = interceptors[i].length;
208         if (numInterceptors > 0) {
209         // Get fresh sorting bins for each non empty type.
210
sorted = new ArrayList JavaDoc(); // not synchronized like we want.
211
unsorted = new ArrayList JavaDoc();
212         }
213         for( int j = 0; j < numInterceptors; j++ ) {
214         Interceptor JavaDoc interceptor = interceptors[i][j];
215         if (interceptor instanceof Comparable JavaDoc) {
216             sorted.add(interceptor);
217         } else {
218             unsorted.add(interceptor);
219         }
220         }
221         if (numInterceptors > 0 && sorted.size() > 0) {
222         // Let the RuntimeExceptions thrown by sort
223
// (i.e., ClassCastException and UnsupportedOperationException)
224
// flow back to the user.
225
Collections.sort(sorted);
226         Iterator JavaDoc sortedIterator = sorted.iterator();
227         Iterator JavaDoc unsortedIterator = unsorted.iterator();
228         for( int j = 0; j < numInterceptors; j++ ) {
229             if (sortedIterator.hasNext()) {
230             interceptors[i][j] =
231                 (Interceptor JavaDoc) sortedIterator.next();
232             } else if (unsortedIterator.hasNext()) {
233             interceptors[i][j] =
234                 (Interceptor JavaDoc) unsortedIterator.next();
235             } else {
236             throw wrapper.sortSizeMismatch() ;
237             }
238         }
239         }
240     }
241     }
242 }
243
244
Popular Tags