2
* Copyright 2002,2004 The Apache Software Foundation.
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 at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* 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 and
14
* limitations under the License.
17
package org.apache.struts.faces.application;
20
import javax.faces.component.ActionSource;
21
import javax.faces.component.UIComponent;
22
import javax.faces.component.UIForm;
23
import javax.faces.context.FacesContext;
24
import javax.faces.event.AbortProcessingException;
25
import javax.faces.event.ActionEvent;
26
import javax.faces.event.ActionListener;
27
import javax.servlet.ServletContext;
28
import javax.servlet.http.HttpServletRequest;
29
import javax.servlet.http.HttpServletResponse;
30
import org.apache.commons.logging.Log;
31
import org.apache.commons.logging.LogFactory;
32
import org.apache.struts.Globals;
33
import org.apache.struts.action.ActionServlet;
34
import org.apache.struts.action.RequestProcessor;
35
import org.apache.struts.config.ModuleConfig;
36
import org.apache.struts.faces.Constants;
37
import org.apache.struts.faces.component.FormComponent;
38
import org.apache.struts.util.RequestUtils;
42
* <p>Concrete implementation of <code>ActionListener</code> that replaces
43
* the default provided implementation. It converts application-level events
44
* into execution of the corresponding Struts request processing lifecycle.
47
* @version $Revision: 1.9 $ $Date: 2004/06/24 01:02:31 $
50
public final class ActionListenerImpl implements ActionListener {
53
// ------------------------------------------------------------ Constructors
57
* <p>Construct a new default <code>ActionListener</code> instance,
58
* passing it the previously configured one.</p>
60
* @param original Original default <code>ActionListener</code>
62
* @exception NullPointerException if <code>original</code>
63
* is <code>null</code>
65
public ActionListenerImpl(ActionListener original) {
67
if (original == null) {
68
throw new NullPointerException();
70
this.original = original;
71
if (log.isInfoEnabled()) {
72
log.info("Create ActionListener wrapping instance of type '" +
73
original.getClass().getName() + "'");
80
// ------------------------------------------------------ Instance Variables
84
* <p>The logger for this instance.</p>
86
private static final Log log = LogFactory.getLog(ActionListenerImpl.class);
90
* <p>The previously configured <code>ActionListener</code> instance.</p>
92
private ActionListener original;
95
// ---------------------------------------------------------- Public Methods
99
* <p>Process the specified <code>ActionEvent</code>.</p>
101
* @param event The <code>ActionEvent</code> to be processed
103
* @exception AbortProcessingException to signal that no further
104
* event processing should be performed
106
public void processAction(ActionEvent event)
107
throws AbortProcessingException {
109
// If this is an immediate action, or we are NOT nested in a
110
// Struts form, perform the standard processing
111
UIComponent component = event.getComponent();
112
ActionSource source = (ActionSource) component;
113
boolean standard = source.isImmediate();
115
UIComponent parent = component.getParent();
116
while (parent != null) {
117
if (parent instanceof UIForm) {
118
if (!(parent instanceof FormComponent)) {
123
parent = parent.getParent();
127
if (log.isDebugEnabled()) {
128
log.debug("Performing standard handling for event " +
129
"from source component '" + component.getId() + "'");
131
original.processAction(event);
136
// Acquire Servlet API Object References
137
FacesContext context = FacesContext.getCurrentInstance();
138
ServletContext servletContext = (ServletContext)
139
context.getExternalContext().getContext();
140
HttpServletRequest request = (HttpServletRequest)
141
context.getExternalContext().getRequest();
142
HttpServletResponse response = (HttpServletResponse)
143
context.getExternalContext().getResponse();
145
// Log this event if requested
146
if (log.isDebugEnabled()) {
147
log.debug("Performing Struts form submit for event " +
148
" from source component '" +
149
component.getId() + "'");
152
// Invoke the appropriate request processor for this request
154
request.setAttribute(Constants.ACTION_EVENT_KEY, event);
155
RequestUtils.selectModule(request, servletContext);
156
ModuleConfig moduleConfig = (ModuleConfig)
157
request.getAttribute(Globals.MODULE_KEY);
158
if (log.isTraceEnabled()) {
159
log.trace("Assigned to module with prefix '" +
160
moduleConfig.getPrefix() + "'");
162
RequestProcessor processor =
163
getRequestProcessor(moduleConfig, servletContext);
164
if (log.isTraceEnabled()) {
165
log.trace("Invoking request processor instance " + processor);
167
processor.process(request, response);
168
context.responseComplete();
169
} catch (Exception e) {
170
log.error("Exception processing action event " + event, e);
172
request.removeAttribute(Constants.ACTION_EVENT_KEY);
178
// ------------------------------------------------------ Protected Methods
182
* <p>Look up and return the <code>RequestProcessor</code> responsible for
183
* the specified module, creating a new one if necessary. This method is
184
* based on the corresponding code in <code>ActionServlet</code>, which
185
* cannot be used directly because it is a protected method.</p>
187
* @param config The module configuration for which to
188
* acquire and return a RequestProcessor
189
* @param context The <code>ServletContext</code> instance
190
* for this web application
192
* @exception IllegalStateException if we cannot instantiate a
193
* RequestProcessor instance
195
protected RequestProcessor getRequestProcessor(ModuleConfig config,
196
ServletContext context) {
198
String key = Globals.REQUEST_PROCESSOR_KEY + config.getPrefix();
199
RequestProcessor processor =
200
(RequestProcessor) context.getAttribute(key);
202
if (processor == null) {
204
if (log.isDebugEnabled()) {
205
log.debug("Instantiating RequestProcessor of class " +
206
config.getControllerConfig().getProcessorClass());
208
ActionServlet servlet = (ActionServlet)
209
context.getAttribute(Globals.ACTION_SERVLET_KEY);
211
(RequestProcessor) RequestUtils.applicationInstance(
212
config.getControllerConfig().getProcessorClass());
213
processor.init(servlet, config);
214
context.setAttribute(key, processor);
215
} catch (Exception e) {
216
log.error("Cannot instantiate RequestProcessor of class "
217
+ config.getControllerConfig().getProcessorClass(),
219
throw new IllegalStateException(
220
"Cannot initialize RequestProcessor of class "
221
+ config.getControllerConfig().getProcessorClass()