01: /*
02: * Copyright 2005-2006 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
05: * in compliance with the License. You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software distributed under the License
10: * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11: * or implied. See the License for the specific language governing permissions and limitations under
12: * the License.
13: */
14:
15: package org.strecks.interceptor.internal;
16:
17: import java.lang.annotation.Annotation;
18: import java.util.ArrayList;
19: import java.util.List;
20:
21: import org.strecks.interceptor.AfterInterceptor;
22: import org.strecks.interceptor.BeforeInterceptor;
23: import org.strecks.interceptor.annotation.AfterInterceptors;
24: import org.strecks.interceptor.annotation.BeforeInterceptors;
25: import org.strecks.util.ReflectHelper;
26:
27: /**
28: * Implements default strategy for reading and instantiating interceptors. Looks first for
29: * <code>BeforeInterceptors</code> annotation, then for <code>AfterInterceptors</code>
30: * annotation
31: * @author Phil Zoio
32: */
33: public class DefaultInterceptorReader implements InterceptorReader {
34:
35: private List<BeforeInterceptor> beforeInterceptors = null;
36:
37: private List<AfterInterceptor> afterInterceptors = null;
38:
39: public void readInterceptors(Annotation annotation,
40: Class actionBeanClass) {
41: if (annotation instanceof BeforeInterceptors) {
42: readBeforeInterceptors((BeforeInterceptors) annotation,
43: actionBeanClass);
44: } else if (annotation instanceof AfterInterceptors) {
45: readAfterInterceptors((AfterInterceptors) annotation,
46: actionBeanClass);
47: }
48: }
49:
50: void readBeforeInterceptors(BeforeInterceptors interceptors,
51: Class actionBeanClass) {
52: Class[] classes = interceptors.classes();
53: for (Class clazz : classes) {
54: BeforeInterceptor instance = ReflectHelper.createInstance(
55: clazz, BeforeInterceptor.class);
56: addBeforeInterceptor(instance);
57: }
58: }
59:
60: void readAfterInterceptors(AfterInterceptors interceptors,
61: Class actionBeanClass) {
62: Class[] classes = interceptors.classes();
63: for (Class clazz : classes) {
64: AfterInterceptor instance = ReflectHelper.createInstance(
65: clazz, AfterInterceptor.class);
66: addAfterInterceptor(instance);
67: }
68: }
69:
70: void addBeforeInterceptor(BeforeInterceptor instance) {
71: if (instance != null) {
72: if (beforeInterceptors == null)
73: beforeInterceptors = new ArrayList<BeforeInterceptor>();
74: beforeInterceptors.add(instance);
75: }
76: }
77:
78: void addAfterInterceptor(AfterInterceptor instance) {
79: if (instance != null) {
80: if (afterInterceptors == null)
81: afterInterceptors = new ArrayList<AfterInterceptor>();
82: afterInterceptors.add(instance);
83: }
84: }
85:
86: public List<? extends BeforeInterceptor> getBeforeInterceptors() {
87: return beforeInterceptors;
88: }
89:
90: public List<? extends AfterInterceptor> getAfterInterceptors() {
91: return afterInterceptors;
92: }
93:
94: }
|