View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.orchestra.viewController;
20  
21  import org.apache.myfaces.orchestra.annotation.AnnotationInfo;
22  import org.apache.myfaces.orchestra.annotation.AnnotationInfoManager;
23  import org.apache.myfaces.orchestra.lib.OrchestraException;
24  
25  import java.lang.reflect.InvocationTargetException;
26  import java.lang.reflect.Method;
27  
28  /**
29   * Execute the various viewController events on the viewController by calling
30   * the corresponding annotated method.
31   */
32  public class AnnotationsViewControllerExecutor extends AbstractViewControllerExecutor
33  {
34      private final AnnotationInfoManager annotationInfoManager;
35  
36      public AnnotationsViewControllerExecutor(AnnotationInfoManager annotationInfoManager)
37      {
38          this.annotationInfoManager = annotationInfoManager;
39      }
40  
41      protected void invokeMethod(Object bean, Method method)
42      {
43          try
44          {
45              method.invoke(bean, (Object[]) null);
46          }
47          catch (IllegalAccessException e)
48          {
49              throw new OrchestraException(e);
50          }
51          catch (InvocationTargetException e)
52          {
53              throw new OrchestraException(e);
54          }
55      }
56  
57      public boolean invokeInitView(String beanName, Object bean)
58      {
59          AnnotationInfo annotationsInfo = annotationInfoManager.getAnnotationInfoByBeanName(beanName);
60          if (annotationsInfo != null)
61          {
62              Method method = annotationsInfo.getInitViewMethod();
63              if (method != null)
64              {
65                  invokeMethod(bean, method);
66                  return true;
67              }
68          }
69          return false;
70      }
71  
72      public boolean invokePreRenderView(String beanName, Object bean)
73      {
74          AnnotationInfo annotationsInfo = annotationInfoManager.getAnnotationInfoByBeanName(beanName);
75          if (annotationsInfo != null)
76          {
77              Method method = annotationsInfo.getPreRenderViewMethod();
78              if (method != null)
79              {
80                  invokeMethod(bean, method);
81                  return true;
82              }
83          }
84          return false;
85      }
86  
87      public boolean invokePreProcess(String beanName, Object bean)
88      {
89          AnnotationInfo annotationsInfo = annotationInfoManager.getAnnotationInfoByBeanName(beanName);
90          if (annotationsInfo != null)
91          {
92              Method method = annotationsInfo.getPreProcessMethod();
93              if (method != null)
94              {
95                  invokeMethod(bean, method);
96                  return true;
97              }
98          }
99          return false;
100     }
101 }