KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > util > velocity > VelocityActionEvent


1 package org.apache.turbine.util.velocity;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License")
7  * you may not use this file except in compliance with the License.
8  * 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, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.lang.reflect.InvocationTargetException JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21
22 import java.util.Iterator JavaDoc;
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 /**
32  * If you are using VelocitySite stuff, then your Action's should
33  * extend this class instead of extending the ActionEvent class. The
34  * difference between this class and the ActionEvent class is that
35  * this class will first attempt to execute one of your doMethod's
36  * with a constructor like this:
37  *
38  * <p><code>doEvent(RunData data, Context context)</code></p>
39  *
40  * <p>It gets the context from the TemplateInfo.getTemplateContext()
41  * method. If it can't find a method like that, then it will try to
42  * execute the method without the Context in it.</p>
43  *
44  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
45  * @author <a HREF="mailto:jvanzyl@periapt.com">Jason van Zyl</a>
46  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
47  * @version $Id: VelocityActionEvent.java,v 1.18.2.2 2004/05/20 03:28:01 seade Exp $
48  */

49 public abstract class VelocityActionEvent extends ActionEvent
50 {
51     /** Constant needed for Reflection */
52     private static final Class JavaDoc [] methodParams
53             = new Class JavaDoc [] { RunData.class, Context.class };
54
55     /**
56      * You need to implement this in your classes that extend this
57      * class.
58      *
59      * @param data A Turbine RunData object.
60      * @exception Exception a generic exception.
61      */

62     public abstract void doPerform(RunData data)
63             throws Exception JavaDoc;
64
65     /**
66      * This overrides the default Action.perform() to execute the
67      * doEvent() method. If that fails, then it will execute the
68      * doPerform() method instead.
69      *
70      * @param data A Turbine RunData object.
71      * @exception Exception a generic exception.
72      */

73     protected void perform(RunData data)
74             throws Exception JavaDoc
75     {
76         try
77         {
78             executeEvents(data, TurbineVelocity.getContext(data));
79         }
80         catch (NoSuchMethodException JavaDoc e)
81         {
82             doPerform(data);
83         }
84     }
85
86     /**
87      * This method should be called to execute the event based system.
88      *
89      * @param data A Turbine RunData object.
90      * @param context Velocity context information.
91      * @exception Exception a generic exception.
92      */

93     public void executeEvents(RunData data, Context context)
94             throws Exception JavaDoc
95     {
96         // Name of the button.
97
String JavaDoc theButton = null;
98
99         // ParameterParser.
100
ParameterParser pp = data.getParameters();
101
102         String JavaDoc button = pp.convert(BUTTON);
103         String JavaDoc key = null;
104
105         // Loop through and find the button.
106
for (Iterator JavaDoc it = pp.keySet().iterator(); it.hasNext();)
107         {
108             key = (String JavaDoc) 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 JavaDoc(
122                     "ActionEvent: The button was null");
123         }
124
125         Method JavaDoc method = null;
126         try
127         {
128             method = getClass().getMethod(theButton, methodParams);
129             Object JavaDoc[] methodArgs = new Object JavaDoc[] { data, context };
130
131             if (log.isDebugEnabled())
132             {
133                 log.debug("Invoking " + method);
134             }
135
136             method.invoke(this, methodArgs);
137         }
138         catch (NoSuchMethodException JavaDoc nsme)
139         {
140             // Attempt to execute things the old way..
141
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 JavaDoc ite)
151         {
152             Throwable JavaDoc t = ite.getTargetException();
153             log.error("Invokation of " + method , t);
154         }
155         finally
156         {
157             pp.remove(key);
158         }
159     }
160 }
161
Popular Tags