View Javadoc

1   /*
2    * Created on 17.05.2005 from Linke
3    *
4    */
5   package net.sf.crispy;
6   
7   import java.util.ArrayList;
8   import java.util.Iterator;
9   import java.util.List;
10  
11  /**
12   * Handle Interceptor- and Modify - Events.
13   * 
14   * @author Linke
15   *
16   */
17  public final class InterceptorHandler {
18  
19  	private List interceptors = new ArrayList();
20  	private Modifier modifier = null;
21  	private List interceptorFilters = new ArrayList();
22   
23  	public InterceptorHandler() { }
24  
25  	/**  
26  	 * Add Interceptor.
27  	 * @return The position of the Interceptor in the list.  
28  	 * 
29  	 */
30  	public int addInterceptor(Interceptor pvProxyInterceptor) { 
31  		interceptors.add(pvProxyInterceptor);
32  		return interceptors.indexOf(pvProxyInterceptor);
33  	}
34  	/** Get Iterator over all registered Interceptors. */
35  	public Iterator getInterceptorIterator () { return interceptors.iterator(); }
36  	/** Get Interceptor by Pos in List. */
37  	public Interceptor getInterceptorByPos (int pvPos) { return (Interceptor) interceptors.get(pvPos); }
38  	/** Get size from registered Interceptors. */
39  	public int getInterceptorSize () { return interceptors.size(); }
40  	/** Remove a registered Interceptor by position. */
41  	public Interceptor removeInterceptorByPos (int pvPos) { return (Interceptor) interceptors.remove(pvPos); }
42  	public void removeAllInterceptors () { interceptors.clear(); }
43  	
44  	public Modifier getModifier() { return modifier; }
45  	public void setModifier(Modifier pvModifier) { modifier = pvModifier; }
46  	
47  	
48  	public void addInterceptorFilter (InterceptorFilter pvFilter) {
49  		interceptorFilters.add(pvFilter);
50  	}
51  	
52  	public int getInterceptorFilterSize() { return interceptorFilters.size(); }
53  	public InterceptorFilter getInterceptorFilterByPos (int pvPos) { return (InterceptorFilter) interceptorFilters.get(pvPos); }
54  	public InterceptorFilter removeInterceptorFilter (int pvPos) { return (InterceptorFilter) interceptorFilters.remove(pvPos); }
55  	
56  	
57  	
58  	
59  	public void fireBeforeNewInstance (Class pvInterfaceClass) {
60  		if (checkInterceptorFilter(pvInterfaceClass)) {
61  			int size = interceptors.size();
62  			for (int i=0; i<size; i++) {
63  				Interceptor lvProxyInterceptor = (Interceptor) interceptors.get(i);
64  				lvProxyInterceptor.beforeNewInstance(pvInterfaceClass);
65  			}
66  		}
67  	}
68  	
69  	public void fireAfterNewInstance (Class pvInterfaceClass, Object pvProxyObject) {
70  		if (checkInterceptorFilter(pvInterfaceClass)) {
71  			int size = interceptors.size();
72  			for (int i=0; i<size; i++) {
73  				Interceptor lvProxyInterceptor = (Interceptor) interceptors.get(i);
74  				lvProxyInterceptor.afterNewInstance(pvInterfaceClass, pvProxyObject);
75  			}
76  		}
77  	}
78  	
79  	
80  	public boolean fireBeforeMethodInvocation (InterceptorContext pvInterceptorContext) {
81  		boolean lvInterruptIncocation = false;
82  		if (checkInterceptorFilter(pvInterceptorContext)) {			
83  			int size = interceptors.size();
84  			for (int i=0; i<size; i++) {
85  				Interceptor lvProxyInterceptor = (Interceptor) interceptors.get(i);
86  				lvProxyInterceptor.beforeMethodInvocation(pvInterceptorContext);
87  				if (pvInterceptorContext.getInterruptInvocation()) {
88  					lvInterruptIncocation = true;
89  				}
90  			}
91  		}
92  		return lvInterruptIncocation;
93  	}
94  	
95  	public void fireAfterMethodInvocation (InterceptorContext pvInterceptorContext) {
96  		if (checkInterceptorFilter(pvInterceptorContext)) {
97  			int size = interceptors.size();
98  			for (int i=0; i<size; i++) {
99  				Interceptor lvProxyInterceptor = (Interceptor) interceptors.get(i);
100 				lvProxyInterceptor.afterMethodInvocation(pvInterceptorContext);
101 			}
102 		}
103 	}
104 	
105 	public void fireOnError(Throwable pvThrowable) {
106 		if (checkInterceptorFilter(pvThrowable)) {		
107 			int size = interceptors.size();
108 			for (int i=0; i<size; i++) {
109 				Interceptor lvProxyInterceptor = (Interceptor) interceptors.get(i);
110 				lvProxyInterceptor.onError(pvThrowable);
111 			}
112 		}
113 	}
114 
115 	public InterceptorContext fireBeforeModifyInvocation (InterceptorContext pvInterceptorContext) {
116 		InterceptorContext lvReturn = pvInterceptorContext;
117 		if ((modifier != null) && (checkInterceptorFilter(pvInterceptorContext))) {
118 				lvReturn = modifier.modifyBeforeInvocation(lvReturn);
119 		}
120 		return lvReturn;
121 	}
122 
123 	public InterceptorContext fireAfterModifyInvocation (InterceptorContext pvInterceptorContext) {
124 		InterceptorContext lvReturn = pvInterceptorContext;
125 		if ((modifier != null) && (checkInterceptorFilter(pvInterceptorContext))) {
126 			lvReturn = modifier.modifyAfterInvocation(lvReturn);
127 		}
128 		return lvReturn;
129 	}
130 
131 	
132 	/**
133 	 * Test all accept method from registered <code>InterceptorFilter</code>.
134 	 * 
135 	 * @param pvContext The <code>InterceptorContext</code>.
136 	 * @return If all registered InterceptorFilter get <code>true</code>, than return <code>true</code>, else return <code>false</code>.
137 	 */
138 	private boolean checkInterceptorFilter(InterceptorFilterContext pvFilterContext) {
139 		for (int i=0; i<interceptorFilters.size(); i++) {
140 			InterceptorFilter lvFilter = (InterceptorFilter) interceptorFilters.get(i);
141 			if (lvFilter.accept(pvFilterContext) == false) {
142 				return false;
143 			}
144 		}
145 		return true;
146 	}
147 	
148 	private boolean checkInterceptorFilter(InterceptorContext pvContext) {
149 		InterceptorFilterContext lvFilterContext = new InterceptorFilterContext();
150 		lvFilterContext.setArgs(pvContext.getArgs());
151 		lvFilterContext.setMethod(pvContext.getMethod());
152 		return checkInterceptorFilter(lvFilterContext);
153 	}
154 	
155 	private boolean checkInterceptorFilter(Class pvClass) {
156 		InterceptorFilterContext lvFilterContext = new InterceptorFilterContext(pvClass);
157 		return checkInterceptorFilter(lvFilterContext);
158 	}
159 	
160 	private boolean checkInterceptorFilter(Throwable pvThrowable) {
161 		InterceptorFilterContext lvFilterContext = new InterceptorFilterContext(pvThrowable);
162 		return checkInterceptorFilter(lvFilterContext);
163 	}
164 
165 }