KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > pageflow > interceptor > InterceptorContext


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.pageflow.interceptor;
19
20 import org.apache.beehive.netui.util.config.bean.CustomProperty;
21 import org.apache.beehive.netui.util.internal.DiscoveryUtils;
22 import org.apache.beehive.netui.util.logging.Logger;
23
24 import java.io.Serializable JavaDoc;
25 import java.util.List JavaDoc;
26
27 /**
28  * Base context for callbacks on {@link Interceptor}s.
29  */

30 public class InterceptorContext
31         implements Serializable JavaDoc
32 {
33     private static final Logger _log = Logger.getInstance( InterceptorContext.class );
34     
35     private Object JavaDoc _resultOverride;
36     private Interceptor _overridingInterceptor;
37
38     public void setResultOverride( Object JavaDoc newResult, Interceptor interceptor )
39     {
40         _resultOverride = newResult;
41         _overridingInterceptor = interceptor;
42     }
43     
44     public boolean hasResultOverride()
45     {
46         return _overridingInterceptor != null;
47     }
48
49     public Object JavaDoc getResultOverride()
50     {
51         return _resultOverride;
52     }
53     
54     public Interceptor getOverridingInterceptor()
55     {
56         return _overridingInterceptor;
57     }
58     
59     protected static void addInterceptors( org.apache.beehive.netui.util.config.bean.Interceptor[] configBeans,
60                                            List JavaDoc/*< Interceptor >*/ interceptorsList, Class JavaDoc baseClassOrInterface )
61     {
62         if ( configBeans != null )
63         {
64             for ( int i = 0; i < configBeans.length; i++ )
65             {
66                 org.apache.beehive.netui.util.config.bean.Interceptor configBean = configBeans[i];
67                 String JavaDoc className = configBean.getInterceptorClass();
68                 InterceptorConfig config = new InterceptorConfig( className );
69                 CustomProperty[] customProps = configBean.getCustomPropertyArray();
70                 
71                 if ( customProps != null )
72                 {
73                     for ( int j = 0; j < customProps.length; j++ )
74                     {
75                         CustomProperty customProp = customProps[j];
76                         config.addCustomProperty( customProp.getName(), customProp.getValue() );
77                     }
78                 }
79                 
80                 addInterceptor( config, baseClassOrInterface, interceptorsList );
81             }
82         }
83     }
84     
85     /**
86      * Instantiates an interceptor, based on the class name in the given InterceptorConfig, and adds it to the
87      * given collection of interceptors.
88      *
89      * @param config the InterceptorConfig used to determine the interceptor class.
90      * @param baseClassOrInterface the required base class or interface. May be <code>null</code>.
91      * @param interceptors the List of interceptors to which to add.
92      * @return an initialized Interceptor, or <code>null</code> if an error occurred.
93      */

94     protected static Interceptor addInterceptor( InterceptorConfig config, Class JavaDoc baseClassOrInterface,
95                                                  List JavaDoc/*< Interceptor >*/ interceptors )
96     {
97         Interceptor interceptor = createInterceptor( config, baseClassOrInterface );
98         if ( interceptor != null ) interceptors.add( interceptor );
99         return interceptor;
100     }
101     
102     /**
103      * Instantiates an interceptor, based on the class name in the given InterceptorConfig.
104      *
105      * @param config the InterceptorConfig used to determine the interceptor class.
106      * @param baseClassOrInterface the required base class or interface. May be <code>null</code>.
107      * @return an initialized Interceptor, or <code>null</code> if an error occurred.
108      */

109     protected static Interceptor createInterceptor( InterceptorConfig config, Class JavaDoc baseClassOrInterface )
110     {
111         assert Interceptor.class.isAssignableFrom( baseClassOrInterface )
112                 : baseClassOrInterface.getName() + " cannot be assigned to " + Interceptor.class.getName();
113         
114         ClassLoader JavaDoc cl = DiscoveryUtils.getClassLoader();
115         String JavaDoc className = config.getInterceptorClass();
116         
117         try
118         {
119             Class JavaDoc interceptorClass = cl.loadClass( className );
120             
121             if ( ! baseClassOrInterface.isAssignableFrom( interceptorClass ) )
122             {
123                 _log.error( "Interceptor " + interceptorClass.getName() + " does not implement or extend "
124                             + baseClassOrInterface.getName() );
125                 return null;
126             }
127             
128             Interceptor interceptor = ( Interceptor ) interceptorClass.newInstance();
129             interceptor.init( config );
130             return interceptor;
131         }
132         catch ( ClassNotFoundException JavaDoc e )
133         {
134             _log.error( "Could not find interceptor class " + className, e );
135         }
136         catch ( InstantiationException JavaDoc e )
137         {
138             _log.error( "Could not instantiate interceptor class " + className, e );
139         }
140         catch ( IllegalAccessException JavaDoc e )
141         {
142             _log.error( "Could not instantiate interceptor class " + className, e );
143         }
144         
145         return null;
146     }
147     
148 }
149
Popular Tags