001: package org.apache.turbine.util.jsp;
002:
003: /*
004: * Copyright 2001-2004 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.lang.reflect.Method;
020: import java.util.Enumeration;
021: import org.apache.turbine.modules.ActionEvent;
022: import org.apache.turbine.services.jsp.TurbineJsp;
023: import org.apache.turbine.util.parser.ParameterParser;
024: import org.apache.turbine.util.RunData;
025: import org.apache.velocity.context.Context;
026:
027: /**
028: * In order to use Context in Jsp templates your Action's should
029: * extend this class instead of extending the ActionEvent class. The
030: * difference between this class and the ActionEvent class is that
031: * this class will first attempt to execute one of your doMethod's
032: * with a constructor like this:
033: *
034: * <p>doEvent(RunData data, Context context)
035: *
036: * <p>It gets the context from the TemplateInfo.getTemplateContext()
037: * method. If it can't find a method like that, then it will try to
038: * execute the method without the Context in it.
039: *
040: * @author <a href="mailto:jon@latchkey.com">Jon S. Stevens</a>
041: * @author <a href="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
042: * @author <a href="mailto:gabrielm@itcsoluciones.com">Gabriel A. Moreno</a>
043: * @version $Id: JspActionEvent.java 534872 2007-05-03 14:07:18Z tv $
044: */
045: public abstract class JspActionEvent extends ActionEvent {
046: /**
047: * You need to implement this in your classes that extend this
048: * class.
049: *
050: * @param data A Turbine RunData object.
051: * @exception Exception, a generic exception.
052: */
053: public abstract void doPerform(RunData data) throws Exception;
054:
055: /**
056: * This overrides the default Action.perform() to execute the
057: * doEvent() method. If that fails, then it will execute the
058: * doPerform() method instead.
059: *
060: * @param data A Turbine RunData object.
061: * @exception Exception, a generic exception.
062: */
063: protected void perform(RunData data) throws Exception {
064: try {
065: executeEvents(data, TurbineJsp.getContext(data));
066: } catch (NoSuchMethodException e) {
067: doPerform(data);
068: }
069: }
070:
071: /**
072: * This method should be called to execute the event based system.
073: *
074: * @param data A Turbine RunData object.
075: * @param context Jsp context information.
076: * @exception Exception, a generic exception.
077: */
078: public void executeEvents(RunData data, Context context)
079: throws Exception {
080: // Name of the button.
081: String theButton = null;
082:
083: // ParameterParser.
084: ParameterParser pp = data.getParameters();
085:
086: String button = pp.convert(BUTTON);
087:
088: // Loop through and find the button.
089: for (Enumeration e = pp.keys(); e.hasMoreElements();) {
090: String key = (String) e.nextElement();
091: if (key.startsWith(button)) {
092: theButton = formatString(key);
093: break;
094: }
095: }
096:
097: if (theButton == null)
098: throw new NoSuchMethodException(
099: "ActionEvent: The button was null");
100:
101: try {
102: // The arguments to the method to find.
103: Class[] classes = new Class[2];
104: classes[0] = RunData.class;
105: classes[1] = Context.class;
106:
107: // The arguments to pass to the method to execute.
108: Object[] args = new Object[2];
109:
110: Method method = getClass().getMethod(theButton, classes);
111: args[0] = data;
112: args[1] = context;
113: method.invoke(this , args);
114: } catch (NoSuchMethodException nsme) {
115: // Attempt to execut things the old way..
116: super.executeEvents(data);
117: }
118: }
119: }
|