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.controller.InterceptorAware;
22: import org.strecks.controller.internal.ActionBeanAnnotationReader;
23: import org.strecks.interceptor.AfterInterceptor;
24: import org.strecks.interceptor.BeforeInterceptor;
25: import org.strecks.interceptor.annotation.InterceptorReaderInfo;
26: import org.strecks.util.ReflectHelper;
27:
28: /**
29: * Implements <code>ActionBeanAnnotationReader</code> to handle reading action bean interceptors
30: * @author Phil Zoio
31: */
32: public class ActionBeanInterceptorReader implements
33: ActionBeanAnnotationReader<InterceptorAware> {
34:
35: private List<BeforeInterceptor> beforeInterceptors = new ArrayList<BeforeInterceptor>();
36:
37: private List<AfterInterceptor> afterInterceptors = new ArrayList<AfterInterceptor>();
38:
39: public boolean readAnnotations(Class actionBeanClass) {
40:
41: boolean found = false;
42: Annotation[] annotations = actionBeanClass.getAnnotations();
43:
44: for (Annotation annotation : annotations) {
45: Class<? extends Annotation> annotationType = annotation
46: .annotationType();
47: InterceptorReaderInfo interceptorReaderInfo = annotationType
48: .getAnnotation(InterceptorReaderInfo.class);
49:
50: if (interceptorReaderInfo != null) {
51: InterceptorReader reader = ReflectHelper
52: .createInstance(interceptorReaderInfo.value(),
53: InterceptorReader.class);
54: reader.readInterceptors(annotation, actionBeanClass);
55: List<? extends BeforeInterceptor> bis = reader
56: .getBeforeInterceptors();
57: List<? extends AfterInterceptor> ais = reader
58: .getAfterInterceptors();
59: addAll(beforeInterceptors, bis);
60: addAll(afterInterceptors, ais);
61: found = true;
62: }
63: }
64:
65: return found;
66: }
67:
68: @SuppressWarnings("unchecked")
69: private static void addAll(List beforeInterceptors, List toAdd) {
70: if (toAdd != null && toAdd.size() > 0)
71: beforeInterceptors.addAll(toAdd);
72: }
73:
74: public void populateController(InterceptorAware controller) {
75: controller.setBeforeInterceptors(beforeInterceptors);
76: controller.setAfterInterceptors(afterInterceptors);
77: }
78: }
|