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   */
20  
21  package org.apache.myfaces.orchestra.viewController;
22  
23  import org.apache.myfaces.orchestra.lib.OrchestraException;
24  
25  import java.lang.reflect.InvocationTargetException;
26  import java.lang.reflect.Method;
27  
28  /***
29   * <p>Invokes ViewController events using reflection.</p>
30   * <p/>
31   * Your beans can optionally provide one of the following methods
32   * <ul>
33   * <li>public void initView()</li>
34   * <li>public void preRenderView()</li>
35   * </ul>
36   * </p>
37   */
38  public class ReflectiveViewControllerExecutor extends AbstractViewControllerExecutor
39  {
40  	/*
41  	 * Do not copy this code if you implement new ones, instead have a look
42  	 * a few lines below.
43  	 */
44  	/***
45  	 * Helper method to find the method which should get invoked.
46  	 */
47  	protected boolean invokeOnViewController(Object bean, String methodName)
48  	{
49  		try
50  		{
51  			Method method = bean.getClass().getMethod(methodName, (Class[]) null); // NON-NLS
52  			method.invoke(bean, (Object[]) null);
53  		}
54  		catch (NoSuchMethodException e)
55  		{
56  			return false;
57  		}
58  		catch (IllegalAccessException e)
59  		{
60  			throw new OrchestraException(e);
61  		}
62  		catch (InvocationTargetException e)
63  		{
64  			throw new OrchestraException(e);
65  		}
66  
67  		return true;
68  	}
69  
70  	/* the ViewControllerExecutor interface */ 
71  	public boolean invokeInitView(String beanName, Object bean)
72  	{
73  		return invokeOnViewController(bean, "initView");
74  	}
75  
76  	public boolean invokePreRenderView(String beanName, Object bean)
77  	{
78  		return invokeOnViewController(bean, "preRenderView");
79  	}
80  
81  	public boolean invokePreProcess(String beanName, Object bean)
82  	{
83  		return invokeOnViewController(bean, "preProcess");
84  	}
85  }