1 package org.apache.turbine.util.velocity; 2 3 18 19 import java.lang.reflect.InvocationTargetException ; 20 import java.lang.reflect.Method ; 21 22 import java.util.Iterator ; 23 24 import org.apache.turbine.modules.ActionEvent; 25 import org.apache.turbine.services.velocity.TurbineVelocity; 26 import org.apache.turbine.util.RunData; 27 import org.apache.turbine.util.parser.ParameterParser; 28 29 import org.apache.velocity.context.Context; 30 31 49 public abstract class VelocityActionEvent extends ActionEvent 50 { 51 52 private static final Class [] methodParams 53 = new Class [] { RunData.class, Context.class }; 54 55 62 public abstract void doPerform(RunData data) 63 throws Exception ; 64 65 73 protected void perform(RunData data) 74 throws Exception 75 { 76 try 77 { 78 executeEvents(data, TurbineVelocity.getContext(data)); 79 } 80 catch (NoSuchMethodException e) 81 { 82 doPerform(data); 83 } 84 } 85 86 93 public void executeEvents(RunData data, Context context) 94 throws Exception 95 { 96 String theButton = null; 98 99 ParameterParser pp = data.getParameters(); 101 102 String button = pp.convert(BUTTON); 103 String key = null; 104 105 for (Iterator it = pp.keySet().iterator(); it.hasNext();) 107 { 108 key = (String ) it.next(); 109 if (key.startsWith(button)) 110 { 111 if (considerKey(key, pp)) 112 { 113 theButton = formatString(key); 114 break; 115 } 116 } 117 } 118 119 if (theButton == null) 120 { 121 throw new NoSuchMethodException ( 122 "ActionEvent: The button was null"); 123 } 124 125 Method method = null; 126 try 127 { 128 method = getClass().getMethod(theButton, methodParams); 129 Object [] methodArgs = new Object [] { data, context }; 130 131 if (log.isDebugEnabled()) 132 { 133 log.debug("Invoking " + method); 134 } 135 136 method.invoke(this, methodArgs); 137 } 138 catch (NoSuchMethodException nsme) 139 { 140 if (log.isDebugEnabled()) 142 { 143 log.debug("Couldn't locate the Event ( " + theButton 144 + "), running executeEvents() in " 145 + super.getClass().getName()); 146 } 147 148 super.executeEvents(data); 149 } 150 catch (InvocationTargetException ite) 151 { 152 Throwable t = ite.getTargetException(); 153 log.error("Invokation of " + method , t); 154 } 155 finally 156 { 157 pp.remove(key); 158 } 159 } 160 } 161 | Popular Tags |