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.source;
16:
17: import java.lang.annotation.Annotation;
18:
19: import org.strecks.controller.BeanSourceAware;
20: import org.strecks.controller.internal.ActionBeanAnnotationReader;
21: import org.strecks.exceptions.ApplicationConfigurationException;
22: import org.strecks.source.annotation.BeanSourceReaderClass;
23: import org.strecks.util.ReflectHelper;
24:
25: /**
26: * <code>ActionBeanAnnotationReader</code> which locates annotations which use the marker
27: * annotation <code>BeanSourceReaderClass</code>, and use the declared class to create a
28: * <code>BeanSourceReader</code>, which itself creates a <code>BeanSource</code> implementation
29: * @author Phil Zoio
30: */
31: public class BeanSourceAnnotationReader implements
32: ActionBeanAnnotationReader<BeanSourceAware> {
33:
34: private ActionBeanSource beanSource;
35:
36: public void populateController(BeanSourceAware controller) {
37: controller.setBeanSource(beanSource);
38: }
39:
40: /*
41: * public ControllerAction controller(ControllerAction controllerAction) { return
42: * controllerAction; }
43: */
44:
45: /**
46: * Locate annotations which use the marker annotation <code>BeanSourceReaderClass</code>, and
47: * use the declared class to create a <code>BeanSourceReader</code>, which itself
48: */
49: public boolean readAnnotations(Class actionClass) {
50:
51: Annotation[] annotations = actionClass.getAnnotations();
52:
53: boolean found = false;
54:
55: for (Annotation annotation : annotations) {
56: Class<? extends Annotation> annotationType = annotation
57: .annotationType();
58: BeanSourceReaderClass beanSourceReaderClass = annotationType
59: .getAnnotation(BeanSourceReaderClass.class);
60:
61: if (beanSourceReaderClass != null) {
62: if (found == true) {
63: throw new ApplicationConfigurationException(
64: "Action bean class "
65: + actionClass.getName()
66: + " has more than one bean source annotation, that is, annotations which themselves use the annotation "
67: + BeanSourceReaderClass.class
68: .getName());
69: }
70:
71: BeanSourceReader beanSourceReader = ReflectHelper
72: .createInstance(beanSourceReaderClass.value(),
73: BeanSourceReader.class);
74: beanSource = beanSourceReader.readBeanSource(
75: actionClass, annotation);
76:
77: found = true;
78: }
79:
80: }
81:
82: if (beanSource != null) {
83: return true;
84: } else {
85: beanSource = new DefaultActionBeanSource(actionClass);
86: return false;
87: }
88:
89: }
90:
91: ActionBeanSource getBeanSource() {
92: return beanSource;
93: }
94:
95: }
|