KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > faces > application > ActionListenerImpl


1 /*
2  * Copyright 2002,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 at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */

16
17 package org.apache.struts.faces.application;
18
19
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 JavaDoc;
28 import javax.servlet.http.HttpServletRequest JavaDoc;
29 import javax.servlet.http.HttpServletResponse JavaDoc;
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;
39
40
41 /**
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.
45  * </p>
46  *
47  * @version $Rev: 54934 $ $Date: 2004-10-16 18:07:50 +0100 (Sat, 16 Oct 2004) $
48  */

49
50 public final class ActionListenerImpl implements ActionListener {
51
52
53     // ------------------------------------------------------------ Constructors
54

55
56     /**
57      * <p>Construct a new default <code>ActionListener</code> instance,
58      * passing it the previously configured one.</p>
59      *
60      * @param original Original default <code>ActionListener</code>
61      *
62      * @exception NullPointerException if <code>original</code>
63      * is <code>null</code>
64      */

65     public ActionListenerImpl(ActionListener original) {
66
67         if (original == null) {
68             throw new NullPointerException JavaDoc();
69         }
70         this.original = original;
71         if (log.isInfoEnabled()) {
72             log.info("Create ActionListener wrapping instance of type '" +
73                      original.getClass().getName() + "'");
74         }
75
76     }
77
78
79
80     // ------------------------------------------------------ Instance Variables
81

82
83     /**
84      * <p>The logger for this instance.</p>
85      */

86     private static final Log log = LogFactory.getLog(ActionListenerImpl.class);
87
88
89     /**
90      * <p>The previously configured <code>ActionListener</code> instance.</p>
91      */

92     private ActionListener original;
93
94
95     // ---------------------------------------------------------- Public Methods
96

97
98     /**
99      * <p>Process the specified <code>ActionEvent</code>.</p>
100      *
101      * @param event The <code>ActionEvent</code> to be processed
102      *
103      * @exception AbortProcessingException to signal that no further
104      * event processing should be performed
105      */

106     public void processAction(ActionEvent event)
107         throws AbortProcessingException {
108
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();
114         if (!standard) {
115             UIComponent parent = component.getParent();
116             while (parent != null) {
117                 if (parent instanceof UIForm) {
118                     if (!(parent instanceof FormComponent)) {
119                         standard = true;
120                     }
121                     break;
122                 }
123                 parent = parent.getParent();
124             }
125         }
126         if (standard) {
127             if (log.isDebugEnabled()) {
128                 log.debug("Performing standard handling for event " +
129                           "from source component '" + component.getId() + "'");
130             }
131             original.processAction(event);
132             return;
133         }
134
135
136         // Acquire Servlet API Object References
137
FacesContext context = FacesContext.getCurrentInstance();
138         ServletContext JavaDoc servletContext = (ServletContext JavaDoc)
139             context.getExternalContext().getContext();
140         HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc)
141             context.getExternalContext().getRequest();
142         HttpServletResponse JavaDoc response = (HttpServletResponse JavaDoc)
143             context.getExternalContext().getResponse();
144
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() + "'");
150         }
151
152         // Invoke the appropriate request processor for this request
153
try {
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() + "'");
161             }
162             RequestProcessor processor =
163                 getRequestProcessor(moduleConfig, servletContext);
164             if (log.isTraceEnabled()) {
165                 log.trace("Invoking request processor instance " + processor);
166             }
167             processor.process(request, response);
168             context.responseComplete();
169         } catch (Exception JavaDoc e) {
170             log.error("Exception processing action event " + event, e);
171         } finally {
172             request.removeAttribute(Constants.ACTION_EVENT_KEY);
173         }
174
175     }
176
177
178     // ------------------------------------------------------ Protected Methods
179

180
181     /**
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>
186      *
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
191      *
192      * @exception IllegalStateException if we cannot instantiate a
193      * RequestProcessor instance
194      */

195     protected RequestProcessor getRequestProcessor(ModuleConfig config,
196                                                    ServletContext JavaDoc context) {
197             
198         String JavaDoc key = Globals.REQUEST_PROCESSOR_KEY + config.getPrefix();
199         RequestProcessor processor =
200             (RequestProcessor) context.getAttribute(key);
201             
202         if (processor == null) {
203             try {
204                 if (log.isDebugEnabled()) {
205                     log.debug("Instantiating RequestProcessor of class " +
206                               config.getControllerConfig().getProcessorClass());
207                 }
208                 ActionServlet servlet = (ActionServlet)
209                 context.getAttribute(Globals.ACTION_SERVLET_KEY);
210                 processor =
211                     (RequestProcessor) RequestUtils.applicationInstance(
212                         config.getControllerConfig().getProcessorClass());
213                 processor.init(servlet, config);
214                 context.setAttribute(key, processor);
215             } catch (Exception JavaDoc e) {
216                 log.error("Cannot instantiate RequestProcessor of class "
217                           + config.getControllerConfig().getProcessorClass(),
218                           e);
219                 throw new IllegalStateException JavaDoc(
220                     "Cannot initialize RequestProcessor of class "
221                         + config.getControllerConfig().getProcessorClass()
222                         + ": "
223                         + e);
224             }
225
226         }
227         return (processor);
228
229     }
230     
231
232 }
233
Popular Tags