1 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 ; 25 import java.util.List ; 26 27 30 public class InterceptorContext 31 implements Serializable 32 { 33 private static final Logger _log = Logger.getInstance( InterceptorContext.class ); 34 35 private Object _resultOverride; 36 private Interceptor _overridingInterceptor; 37 38 public void setResultOverride( Object 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 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 interceptorsList, Class 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 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 94 protected static Interceptor addInterceptor( InterceptorConfig config, Class baseClassOrInterface, 95 List interceptors ) 96 { 97 Interceptor interceptor = createInterceptor( config, baseClassOrInterface ); 98 if ( interceptor != null ) interceptors.add( interceptor ); 99 return interceptor; 100 } 101 102 109 protected static Interceptor createInterceptor( InterceptorConfig config, Class baseClassOrInterface ) 110 { 111 assert Interceptor.class.isAssignableFrom( baseClassOrInterface ) 112 : baseClassOrInterface.getName() + " cannot be assigned to " + Interceptor.class.getName(); 113 114 ClassLoader cl = DiscoveryUtils.getClassLoader(); 115 String className = config.getInterceptorClass(); 116 117 try 118 { 119 Class 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 e ) 133 { 134 _log.error( "Could not find interceptor class " + className, e ); 135 } 136 catch ( InstantiationException e ) 137 { 138 _log.error( "Could not instantiate interceptor class " + className, e ); 139 } 140 catch ( IllegalAccessException e ) 141 { 142 _log.error( "Could not instantiate interceptor class " + className, e ); 143 } 144 145 return null; 146 } 147 148 } 149 | Popular Tags |