001: package org.strecks.lifecycle.impl;
002:
003: import java.lang.reflect.Method;
004:
005: import org.strecks.controller.internal.ActionBeanAnnotationReader;
006: import org.strecks.exceptions.ApplicationConfigurationException;
007: import org.strecks.lifecycle.LifecycleMethodAware;
008: import org.strecks.lifecycle.annotation.CloseMethod;
009: import org.strecks.lifecycle.annotation.InitMethod;
010:
011: public class LifecycleMethodReader implements
012: ActionBeanAnnotationReader<LifecycleMethodAware> {
013:
014: private Method initMethod;
015:
016: private Method closeMethod;
017:
018: public boolean readAnnotations(Class actionBeanClass) {
019: Method[] methods = actionBeanClass.getMethods();
020: checkForInitMethod(actionBeanClass, methods);
021: checkForCloseMethod(actionBeanClass, methods);
022: return (initMethod != null || closeMethod != null);
023: }
024:
025: private void checkForInitMethod(Class actionBeanClass,
026: Method[] methods) {
027:
028: Method methodToReturn = null;
029: boolean found = false;
030: for (Method method : methods) {
031: InitMethod annotation = method
032: .getAnnotation(InitMethod.class);
033: if (annotation != null) {
034: found = checkAnnotation(actionBeanClass, found, method);
035: if (found) {
036: methodToReturn = method;
037: }
038: }
039: }
040: initMethod = methodToReturn;
041: }
042:
043: private void checkForCloseMethod(Class actionBeanClass,
044: Method[] methods) {
045: Method methodToReturn = null;
046: boolean found = false;
047: for (Method method : methods) {
048: CloseMethod annotation = method
049: .getAnnotation(CloseMethod.class);
050: if (annotation != null) {
051: found = checkAnnotation(actionBeanClass, found, method);
052: if (found) {
053: methodToReturn = method;
054: }
055: }
056: }
057: closeMethod = methodToReturn;
058: }
059:
060: private boolean checkAnnotation(Class actionBeanClass,
061: boolean found, Method method) {
062: if (found) {
063: throw new ApplicationConfigurationException(
064: InitMethod.class.getSimpleName()
065: + " annotation used more than once in the same class "
066: + actionBeanClass);
067: }
068:
069: final Class<?>[] parameterTypes = method.getParameterTypes();
070:
071: if (parameterTypes != null && parameterTypes.length > 0) {
072: throw new ApplicationConfigurationException(
073: InitMethod.class.getSimpleName()
074: + " annotation used with method " + method
075: + " cannot have parameters");
076: }
077:
078: final Class<?> returnType = method.getReturnType();
079: if (returnType != null && !returnType.equals(Void.TYPE)) {
080: throw new ApplicationConfigurationException(
081: InitMethod.class.getSimpleName()
082: + " annotation used with method " + method
083: + " cannot have a return type");
084: }
085:
086: found = true;
087: return found;
088: }
089:
090: public void populateController(LifecycleMethodAware controller) {
091: if (initMethod != null)
092: controller.setInitMethod(initMethod);
093: if (closeMethod != null)
094: controller.setCloseMethod(closeMethod);
095: }
096:
097: public Method getInitMethod() {
098: return initMethod;
099: }
100:
101: public Method getCloseMethod() {
102: return closeMethod;
103: }
104:
105: }
|