KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > core > actions > AbstractMultiFormDispatchAction


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20
21 package com.sslexplorer.core.actions;
22
23 import java.io.IOException JavaDoc;
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.servlet.ServletException JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 import org.apache.commons.beanutils.BeanUtils;
36 import org.apache.struts.action.Action;
37 import org.apache.struts.action.ActionForm;
38 import org.apache.struts.action.ActionForward;
39 import org.apache.struts.action.ActionMapping;
40 import org.apache.struts.config.ActionConfig;
41 import org.apache.struts.config.FormBeanConfig;
42 import org.apache.struts.util.RequestUtils;
43
44 import com.sslexplorer.boot.Util;
45 import com.sslexplorer.core.DefaultPanel;
46 import com.sslexplorer.core.Panel;
47 import com.sslexplorer.core.PanelManager;
48 import com.sslexplorer.core.forms.AbstractMultiFormDispatchForm;
49 import com.sslexplorer.policyframework.Permission;
50 import com.sslexplorer.policyframework.ResourceType;
51 import com.sslexplorer.security.SessionInfo;
52
53 /**
54  */

55 public abstract class AbstractMultiFormDispatchAction extends AuthenticatedDispatchAction {
56     private final Map JavaDoc<String JavaDoc, AuthenticatedDispatchAction> actions = new HashMap JavaDoc<String JavaDoc, AuthenticatedDispatchAction>();
57     private final int placement_;
58
59     /**
60      * Constructor
61      *
62      * @param resourceType
63      * @param permissions
64      * @param placement
65      */

66     public AbstractMultiFormDispatchAction(ResourceType resourceType, Permission permissions[], int placement) {
67         super(resourceType, permissions);
68         placement_ = placement;
69     }
70
71     @SuppressWarnings JavaDoc("unchecked")
72     protected ActionForward dispatchMethod(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
73                                            HttpServletResponse JavaDoc response, String JavaDoc name) throws Exception JavaDoc {
74         Collection JavaDoc<Panel> panels = PanelManager.getInstance().getPanels(placement_, request, response, DefaultPanel.MAIN_LAYOUT);
75         Collection JavaDoc<SubActionWrapper> subActions = getSubActions(mapping, form, request, panels);
76
77         AbstractMultiFormDispatchForm dispatchForm = (AbstractMultiFormDispatchForm) form;
78         dispatchForm.init(subActions, mapping, request);
79         
80         ActionForward fwd = provideActionTargets(subActions, dispatchForm, request, response, name);
81         if(fwd != null) {
82             return fwd;
83         }
84
85         // Now do the normal dispatch handling. This is what determines the
86
// forward
87
if (null == name)
88             return unspecified(mapping, form, request, response);
89
90         return getActionForward(mapping, form, request, response, name);
91     }
92
93     private Collection JavaDoc<SubActionWrapper> getSubActions(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
94                                                        Collection JavaDoc<Panel> panels) throws ClassNotFoundException JavaDoc,
95                     IllegalAccessException JavaDoc, InstantiationException JavaDoc, InvocationTargetException JavaDoc {
96         Collection JavaDoc<SubActionWrapper> subActions = new ArrayList JavaDoc<SubActionWrapper>();
97         int index = 0;
98         for (Panel panel : panels) {
99             /*
100              * First get all of the availble action mappings and create all of
101              * the forms First try to find the panels action config. Look for
102              * the same name as the panel id prefixed by a /
103              */

104             ActionConfig config = mapping.getModuleConfig().findActionConfig("/" + panel.getId());
105             if (null != config && config instanceof ActionMapping) {
106                 ActionMapping subMapping = (ActionMapping) config;
107                 SubActionWrapper subActionWrapper = getSubActionWrapper(mapping, form, subMapping, request, index);
108                 if (null != subActionWrapper)
109                     subActions.add(subActionWrapper);
110             }
111             index++;
112         }
113         return subActions;
114     }
115
116     private static SubActionWrapper getSubActionWrapper(ActionMapping mapping, ActionForm form, ActionMapping subMapping,
117                                                         HttpServletRequest JavaDoc request, int index) throws ClassNotFoundException JavaDoc,
118                     IllegalAccessException JavaDoc, InstantiationException JavaDoc, InvocationTargetException JavaDoc {
119         String JavaDoc formName = subMapping.getName();
120         ActionForm subForm = getActionForm(subMapping, request);
121
122         FormBeanConfig formBean = mapping.getModuleConfig().findFormBeanConfig(formName);
123         String JavaDoc className = formBean == null ? null : formBean.getType();
124         if (className == null)
125             return null;
126
127         if (subForm == null || !className.equals(subForm.getClass().getName()))
128             subForm = (ActionForm) Class.forName(className).newInstance();
129
130         if ("request".equals(mapping.getScope()))
131             request.setAttribute(formName, subForm);
132         else
133             request.getSession().setAttribute(formName, subForm);
134
135         subForm.reset(mapping, request);
136
137         /*
138          * We dont want to try and populate all forms on a post, only the one
139          * that has requested it. For this the form must have a hidden parameter
140          * with the name of 'subForm' and the value being the form name to
141          * populate
142          */

143         AbstractMultiFormDispatchForm dispatchForm = (AbstractMultiFormDispatchForm) form;
144         if (formName.equals(dispatchForm.getSubForm())) {
145             dispatchForm.setSelectedTab(dispatchForm.getTabName(index));
146             BeanUtils.populate(subForm, request.getParameterMap());
147         }
148
149         return new SubActionWrapper(subForm, subMapping);
150     }
151
152     private static ActionForm getActionForm(ActionMapping subMapping, HttpServletRequest JavaDoc request) {
153         String JavaDoc formName = subMapping.getName();
154         if ("request".equals(subMapping.getScope()))
155             return (ActionForm) request.getAttribute(formName);
156         else
157             return (ActionForm) request.getSession().getAttribute(formName);
158     }
159
160     private ActionForward provideActionTargets(Collection JavaDoc<SubActionWrapper> subActions, AbstractMultiFormDispatchForm dispatchForm,
161                                       HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, String JavaDoc name)
162                     throws ClassNotFoundException JavaDoc, IOException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
163         if (null == name || name.equals(""))
164             name = "unspecified";
165         // JDR added the ability to refresh all sub forms.
166
boolean refreshAll = new Boolean JavaDoc(request.getParameter("refreshAll"));
167         
168         String JavaDoc subForm = dispatchForm.getSubForm();
169         ActionForm subform = (ActionForm) request.getSession().getAttribute(subForm);
170         ActionForward fwd = null;
171
172         // Now try to create actions and run the provided action target on each.
173
// Any forwards returned are ignored
174
for (SubActionWrapper wrapper : subActions) {
175             try {
176                     // We have an action config, so get the action instance
177
Action action = processActionCreate(request, response, wrapper.getMapping());
178                     // Identify the method object to be dispatched to
179
if (dispatchForm.isSubFormEmpty() || wrapper.getForm().equals(subform) || refreshAll) {
180                         Method JavaDoc method = getMethod(action, name);
181                         Object JavaDoc args[] = { wrapper.getMapping(), wrapper.getForm(), request, response };
182                         // Invoke the method. We don't care about the forward
183
// returned,
184
// it will not be used.
185
if(wrapper.getForm().equals(subform)) {
186                             fwd = (ActionForward)method.invoke(action, args);
187                         }
188                         else {
189                             method.invoke(action, args);
190                         }
191                 }
192             } catch (Exception JavaDoc e) {
193                 log.error(e);
194             }
195         }
196         
197         return fwd;
198     }
199
200     private Method JavaDoc getMethod(Action action, String JavaDoc name) throws NoSuchMethodException JavaDoc {
201         synchronized (methods) {
202             String JavaDoc methodName = action.getClass().getName() + "#" + name;
203             if (!methods.containsKey(methods)) {
204                 Method JavaDoc method = action.getClass().getMethod(name, types);
205                 methods.put(methodName, method);
206             }
207             return (Method JavaDoc) methods.get(methodName);
208         }
209     }
210
211     private Action processActionCreate(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, ActionConfig mapping)
212                     throws IOException JavaDoc, ClassNotFoundException JavaDoc, IllegalAccessException JavaDoc, InstantiationException JavaDoc {
213         synchronized (actions) {
214             String JavaDoc className = mapping.getType();
215             if (!actions.containsKey(className)) {
216                 if (log.isTraceEnabled())
217                     log.trace(" Creating new Action instance");
218                 AuthenticatedDispatchAction instance = (AuthenticatedDispatchAction) RequestUtils.applicationInstance(className);
219                 instance.setServlet(servlet);
220                 actions.put(className, instance);
221             }
222             return actions.get(className);
223         }
224     }
225
226     private ActionForward getActionForward(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
227                                            HttpServletResponse JavaDoc response, String JavaDoc name) throws Exception JavaDoc {
228         try {
229             Method JavaDoc method = getMethod(name);
230             Object JavaDoc args[] = { mapping, form, request, response };
231             return (ActionForward) method.invoke(this, args);
232         } catch (ClassCastException JavaDoc e) {
233             log.error(getMessage(mapping, name, "dispatch.return"), e);
234             throw e;
235         } catch (IllegalAccessException JavaDoc e) {
236             log.error(getMessage(mapping, name, "dispatch.error"), e);
237             throw e;
238         } catch (NoSuchMethodException JavaDoc e) {
239             return unspecified(mapping, form, request, response);
240         } catch (InvocationTargetException JavaDoc e) {
241             Throwable JavaDoc t = e.getTargetException();
242             if (t instanceof Exception JavaDoc) {
243                 throw (Exception JavaDoc) t;
244             } else {
245                 log.error(getMessage(mapping, name, "dispatch.error"), e);
246                 throw new ServletException JavaDoc(t);
247             }
248         }
249     }
250
251     private static String JavaDoc getMessage(ActionMapping mapping, String JavaDoc name, String JavaDoc message) {
252         return messages.getMessage(message, mapping.getPath(), name);
253     }
254
255     /*
256      * (non-Javadoc)
257      *
258      * @see org.apache.struts.actions.DispatchAction#unspecified(org.apache.struts.action.ActionMapping,
259      * org.apache.struts.action.ActionForm,
260      * javax.servlet.http.HttpServletRequest,
261      * javax.servlet.http.HttpServletResponse)
262      */

263     public ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request,
264                                      HttpServletResponse JavaDoc response) {
265         return mapping.findForward("display");
266     }
267
268     /*
269      * (non-Javadoc)
270      *
271      * @see com.sslexplorer.core.actions.CoreAction#getNavigationContext(org.apache.struts.action.ActionMapping,
272      * org.apache.struts.action.ActionForm,
273      * javax.servlet.http.HttpServletRequest,
274      * javax.servlet.http.HttpServletResponse)
275      */

276     public int getNavigationContext(ActionMapping mapping, ActionForm form, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) {
277         return SessionInfo.MANAGEMENT_CONSOLE_CONTEXT;
278     }
279
280     public static final class SubActionWrapper {
281         private final ActionForm form_;
282         private final ActionMapping mapping_;
283
284         SubActionWrapper(ActionForm form, ActionMapping mapping) {
285             form_ = form;
286             mapping_ = mapping;
287         }
288
289         public ActionForm getForm() {
290             return form_;
291         }
292
293         public ActionMapping getMapping() {
294             return mapping_;
295         }
296     }
297 }
Popular Tags