1/*2 * Copyright 2000-2001,2004 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */16/*17 * PortletActionEvent.java18 *19 * Created on January 29, 2003, 4:20 PM20 */21packageorg.apache.jetspeed.modules.actions.portlets;
2223import java.lang.reflect.Method;
2425import java.util.Enumeration;
26import java.util.HashMap;
2728import org.apache.jetspeed.portal.Portlet;
29import org.apache.jetspeed.portal.portlets.GenericMVCPortlet;
30import org.apache.jetspeed.util.PortletSessionState;
31import org.apache.turbine.modules.ActionEvent;
32import org.apache.turbine.services.velocity.TurbineVelocity;
33import org.apache.turbine.util.ParameterParser;
34import org.apache.turbine.util.RunData;
3536import org.apache.velocity.context.Context;
373839/***40 * Provides form based action handling via the eventSubmit_do[action] pattern.41 * Works just like the mechanism described for the velocity portlet. Extends42 * this convienent functionality to all GenericMVCPortlets43 * 44 * @author tkuebler45 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>46 * @version $Id: PortletActionEvent.java,v 1.3.2.1 2003/02/24 18:45:42 tkuebler Exp $47 * @stereotype moment-interval48 */49publicabstractclassPortletActionEvent50extends ActionEvent
51 {
5253/***54 * Cache ActionEvent methods to avoid repeated replection55 * method lookups.56 */57privatestaticfinal HashMap eventMethods = new HashMap();
5859/***60 * You need to implement this in your classes that extend this61 * class.62 *63 * @param data A Turbine RunData object.64 * @exception Exception, a generic exception.65 */66publicabstractvoid doPerform(RunData data)
67 throws Exception;
6869/***70 * This overrides the default Action.perform() to execute the71 * doEvent() method. If that fails, then it will execute the72 * doPerform() method instead.73 *74 * @param data A Turbine RunData object.75 * @exception Exception, a generic exception.76 */77protectedvoid perform(RunData data)
78 throws Exception
79 {
8081try82 {
83 executeEvents(data, TurbineVelocity.getContext(data));
84 }
85catch (NoSuchMethodException e)
86 {
87 doPerform(data);
88 }
89 }
9091/***92 * This method should be called to execute the event based system.93 *94 * @param data A Turbine RunData object.95 * @param context context information.96 * @exception Exception, a generic exception.97 */98publicvoid executeEvents(RunData data, Context context)
99 throws Exception
100 {
101102// Name of the button.103 String theButton = null;
104105// Portlet whom this action is a target of106Portlet portlet = (Portlet) context.get(GenericMVCPortlet.PORTLET);
107108// ParameterParser.109 ParameterParser pp = data.getParameters();
110 String button = pp.convert(BUTTON);
111112// Loop through and find the button.113for (Enumeration e = pp.keys(); e.hasMoreElements();)
114 {
115116 String key = (String) e.nextElement();
117118if (key.startsWith(button))
119 {
120 theButton = formatString(key);
121122break;
123 }
124 }
125126if (theButton == null )
127 {
128thrownew NoSuchMethodException("ActionEvent: The button was null");
129 }
130131132133if (!fireEvent(data, Context.class, context, theButton) && PortletSessionState.isMyRequest(data, portlet))
134 {
135// Old JSP actions use Portlet instead of Context136// as their event method's 2nd parameter137if (!fireEvent(data, Portlet.class,
138 portlet,
139 theButton))
140 {
141// Attempt to execut things the old way..142super.executeEvents(data);
143 }
144 }
145146 }
147148/***149 * Convenience method for firing portlet events.150 * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>151 */152protectedboolean fireEvent(RunData data, Class deltaClass, Object deltaValue, String theButton)
153 {
154try155 {
156// The arguments to the method to find.157 Class[] classes = new Class[2];
158classes[0] = RunData.class;
159 classes[1] = deltaClass;
160161// The arguments to pass to the method to execute.162 Object[] args = new Object[2];
163164 String methodKey = getClass().getName()+":"165 +theButton+":"+classes[0].getName()
166 +":"+classes[1].getName();
167168 Method method = (Method)eventMethods.get(methodKey);
169if(method == null)
170 {
171 method = getClass().getMethod(theButton, classes);
172 eventMethods.put(methodKey, method);
173 }
174 args[0] = data;
175 args[1] = deltaValue;
176 method.invoke(this, args);
177returntrue;
178 }
179catch (Exception e)
180 {
181return false;
182 }
183184 }
185186187 }