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 package org.apache.myfaces.orchestra.viewController; 21 22 import org.apache.myfaces.orchestra.lib.OrchestraException; 23 24 import java.lang.reflect.InvocationTargetException; 25 import java.lang.reflect.Method; 26 27 /** 28 * Invokes ViewController events using reflection. 29 * <p> 30 * If the target bean has a method with any of the following signatures then it 31 * receives the appropriate callback: 32 * <ul> 33 * <li>public void initView()</li> 34 * <li>public void preProcess()</li> 35 * <li>public void preRenderView()</li> 36 * </ul> 37 * <p> 38 * Note that each method here returns false if the target bean does not 39 * have a method with an appropriate signature; this allows this executor to 40 * be "chained" with others. 41 */ 42 public class ReflectiveViewControllerExecutor extends AbstractViewControllerExecutor 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 }