costin 00/12/27 23:14:45
Modified: src/share/org/apache/tomcat/core BaseInterceptor.java
Container.java
src/share/org/apache/tomcat/util/hooks Hooks.java
Log:
Next step in refactoring the hooks. Container is now using the Hook
util.
Revision Changes Path
1.33 +8 -1
jakarta-tomcat/src/share/org/apache/tomcat/core/BaseInterceptor.java
Index: BaseInterceptor.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/BaseInterceptor.java,v
retrieving revision 1.32
retrieving revision 1.33
diff -u -r1.32 -r1.33
--- BaseInterceptor.java 2000/12/28 01:15:38 1.32
+++ BaseInterceptor.java 2000/12/28 07:14:44 1.33
@@ -513,6 +513,13 @@
public final int getDebug() {
return debug;
}
-
+
+ /** Special method for self-registered hooks, intended to support
+ * a mechanism similar with Apache2.0 and further extensibility
+ * without interface changes.
+ */
+ public int registerHooks() {
+ return DECLINED;
+ }
}
1.42 +90 -101 jakarta-tomcat/src/share/org/apache/tomcat/core/Container.java
Index: Container.java
===================================================================
RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/core/Container.java,v
retrieving revision 1.41
retrieving revision 1.42
diff -u -r1.41 -r1.42
--- Container.java 2000/12/26 23:10:55 1.41
+++ Container.java 2000/12/28 07:14:44 1.42
@@ -60,6 +60,7 @@
package org.apache.tomcat.core;
import org.apache.tomcat.util.*;
+import org.apache.tomcat.util.hooks.*;
import java.io.*;
import java.net.*;
import java.util.*;
@@ -135,6 +136,7 @@
String methods[]=null;
public Container() {
+ initHooks();
}
/** Get the context manager
@@ -374,12 +376,6 @@
}
// -------------------- Interceptors --------------------
- public static final int MAX_HOOKS=20;
-
- // Hook Ids - keep them in sync with PREDEFINED_I
- // ( static final for performance and to simplify code )
- // H_ is from "hook"
-
public static final int H_requestMap=0;
public static final int H_contextMap=1;
public static final int H_authenticate=2;
@@ -393,126 +389,119 @@
public static final int H_postRequest=10;
public static final int H_handleError=11;
public static final int H_engineInit=12;
+ public static final int H_COUNT=14;
- public static final String PREDEFINED_I[]= {
- "requestMap", "contextMap", "authenticate",
- "authorize", "preService", "beforeBody",
- "newSessionRequest", "beforeCommit",
- "afterBody", "postService", "postRequest",
- "handleError",
- // special case - all interceptors will be added to the "context"
- // chain. We plan to use a simpler Event/Listener model for
- // all context hooks, since they don't have any performance requirement
- "engineInit" };
-
- // local interceptors - all interceptors added to this
- // container
- Vector interceptors[]=new Vector[ MAX_HOOKS ];
-
- // Merged interceptors - local and global ( upper-level ) interceptors
- BaseInterceptor hooks[][]=new BaseInterceptor[MAX_HOOKS][];
-
- // used internally
- private Vector getLocalInterceptors(int hookId) {
- if( interceptors[hookId]==null )
- interceptors[hookId]=new Vector();
- return interceptors[hookId];
+ Hooks hooks=new Hooks();
+ BaseInterceptor hooksCache[][]=null;
+ BaseInterceptor allHooksCache[]=null;
+
+ private void initHooks() {
+ hooks.registerHook( "requestMap", H_requestMap );
+ hooks.registerHook( "contextMap", H_contextMap );
+ hooks.registerHook( "authenticate", H_authenticate );
+ hooks.registerHook( "authorize", H_authorize );
+ hooks.registerHook( "preService", H_preService );
+ hooks.registerHook( "beforeBody", H_beforeBody );
+ hooks.registerHook( "newSessionRequest", H_newSessionRequest );
+ hooks.registerHook( "beforeCommit", H_beforeCommit );
+ hooks.registerHook( "afterBody", H_afterBody );
+ hooks.registerHook( "postService", H_postService );
+ hooks.registerHook( "postRequest", H_postRequest );
+ hooks.registerHook( "handleError", H_handleError );
+ hooks.registerHook( "engineInit", H_handleError );
}
-
+
+ public Hooks getHooks() {
+ return hooks;
+ }
+
/** Add the interceptor to all the hook chains it's interested
- in
- */
+ * in
+ */
public void addInterceptor( BaseInterceptor bi ) {
bi.setContext( getContext() );
-
- for( int i=0; i< PREDEFINED_I.length -1 ; i++ ) {
- if( IntrospectionUtils.hasHook( bi, PREDEFINED_I[i] )) {
- if( interceptors[i]==null )
- interceptors[i]=new Vector();
- if( dL > 0 ) debug( "Adding " + PREDEFINED_I[i] + " " +bi );
- interceptors[i].addElement( bi );
- resetInterceptorCache( i );
- }
- }
- // last position just gets all interceptors
- // ( to be used for context-level hooks )
- if( interceptors[H_engineInit]==null )
- interceptors[H_engineInit]=new Vector();
- resetInterceptorCache( H_engineInit );
-
- interceptors[ H_engineInit ].addElement( bi );
- }
-
- public void removeInterceptor( BaseInterceptor bi ) {
- for( int i=0; i<PREDEFINED_I.length-1; i++ ) {
- if( interceptors[i].contains( bi )) {
- interceptors[i].removeElement( bi );
- resetInterceptorCache( i );
- }
- }
- interceptors[H_engineInit].removeElement( bi );
- resetInterceptorCache( H_engineInit );
+ hooks.addModule( bi );
+ hooksCache=null;
+ allHooksCache=null;
}
-
- // make sure we reset the cache.
- // dynamic addition of interceptors is not implemented,
- // but this is a start
- public void resetInterceptorCache( int id ) {
- hooks[id]=null;
- }
- public static int getHookId( String hookName ) {
- for( int i=0; i< PREDEFINED_I.length; i++ ) {
- if( PREDEFINED_I[i].equals(hookName))
- return i;
-
- }
- // get all interceptors for unknown hook names
- return PREDEFINED_I.length-1;
+ public void removeInterceptor( BaseInterceptor bi ) {
+ hooks.removeModule( bi );
+ hooksCache=null;
+ allHooksCache=null;
}
public BaseInterceptor[] getInterceptors( int type )
{
- if( hooks[type] != null ) {
- return hooks[type];
+ if( hooksCache != null ) {
+ return hooksCache[type];
}
- if( dL>5 )
- debug("create hooks for " + type + " " + PREDEFINED_I[type]);
-
+
+ // load the cache with all the hooks
Container globalIntContainer=getContextManager().getContainer();
- Vector globals=globalIntContainer.getLocalInterceptors( type );
- Vector locals=null;
- if( this != globalIntContainer ) {
- locals=this.getLocalInterceptors( type );
- }
+ Hooks globals=globalIntContainer.getHooks();
- int gsize=globals.size();
- int lsize=(locals==null) ? 0 : locals.size();
- hooks[type]=new BaseInterceptor[gsize+lsize];
-
- for ( int i = 0 ; i < gsize ; i++ ){
- hooks[type][i]=(BaseInterceptor)globals.elementAt(i);
- if( dL > 5 ) debug( "Add " + i + " " + hooks[type][i]);
- }
- for ( int i = 0 ; i < lsize ; i++ ){
- hooks[type][gsize+i]=(BaseInterceptor)locals.elementAt(i);
- if( dL > 5 ) debug( "Add " + i + " " + hooks[type][i+gsize]);
+ hooksCache=new BaseInterceptor[H_COUNT][];
+ for( int i=0; i<H_COUNT; i++ ) {
+ Hooks locals=null;
+ if( this != globalIntContainer ) {
+ hooksCache[i]=mergeHooks( globals.getModules(i),
+ getHooks().getModules(i));
+ } else {
+ hooksCache[i]=mergeHooks( globals.getModules(i), null);
+ }
}
-
- return hooks[type];
+ return hooksCache[type];
}
/** Get all interceptors
*/
public BaseInterceptor[] getInterceptors()
{
- // We don't check for "hasHook", so all
- // interceptors are available here
- return getInterceptors( H_engineInit );
+ if( allHooksCache != null ) {
+ return allHooksCache;
+ }
+
+ // load the cache with all the hooks
+ Container globalIntContainer=getContextManager().getContainer();
+ Hooks globals=globalIntContainer.getHooks();
+ if( this == globalIntContainer ) {
+ allHooksCache=mergeHooks( globals.getModules(), null );
+ } else {
+ allHooksCache=mergeHooks( globals.getModules(),
+ this.getHooks().getModules());
+ }
+ return allHooksCache;
}
+ private BaseInterceptor[] mergeHooks( Object globalM[], Object localM[] ) {
+ BaseInterceptor hA[]=null;
+ if( localM==null ) {
+ hA=new BaseInterceptor[ globalM.length ];
+ for( int j=0; j<globalM.length; j++ ) {
+ hA[j]=(BaseInterceptor)globalM[j];
+ }
+ } else {
+ hA=new BaseInterceptor[ globalM.length +
+ localM.length ];
+ int gsize=globalM.length;
+ for( int j=0; j<globalM.length; j++ ) {
+ hA[j]=(BaseInterceptor)globalM[j];
+ }
+ for( int j=0; j<localM.length; j++ ) {
+ hA[gsize+j]=(BaseInterceptor)localM[j];
+ }
+ }
+ return hA;
+ }
+
+ public void resetInterceptorCache( int id ) {
+ allHooksCache=null;
+ hooksCache=null;
+ }
+
// debug
public static final int dL=0;
private void debug( String s ) {
1.2 +4 -4 jakarta-tomcat/src/share/org/apache/tomcat/util/hooks/Hooks.java
Index: Hooks.java
===================================================================
RCS file:
/home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/hooks/Hooks.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- Hooks.java 2000/12/28 06:15:20 1.1
+++ Hooks.java 2000/12/28 07:14:45 1.2
@@ -86,6 +86,7 @@
* implementation
*/
public class Hooks {
+ public static final int INITIAL_HOOKS=20;
int hookCount;
String hookNames[];
Vector hooksV[];
@@ -97,8 +98,8 @@
private static final int dL=0;
- public Hooks(int hookCount ) {
- this.hookCount=hookCount;
+ public Hooks() {
+ this.hookCount=INITIAL_HOOKS; // XXX TODO: resizing
hooksV=new Vector[hookCount];
for( int i=0; i<hookCount ; i++ )
hooksV[i]=new Vector();
@@ -140,6 +141,7 @@
*/
public void addModule( Object bi ) {
for( int i=0; i< hookNames.length ; i++ ) {
+ if( hookNames[i]==null ) continue;
if( hasHook( bi, hookNames[i] )) {
if( dL > 0 ) debug( "Adding " + hookNames[i] + " " +bi );
hooksV[i].addElement( bi );
@@ -162,8 +164,6 @@
allModules=null;
}
- Hooks localHooks;
-
public Object[] getModules( int type )
{
if( hooks[type] != null ) {