1 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 ; 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; 39 40 41 49 50 public final class ActionListenerImpl implements ActionListener { 51 52 53 55 56 65 public ActionListenerImpl(ActionListener original) { 66 67 if (original == null) { 68 throw new NullPointerException (); 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 82 83 86 private static final Log log = LogFactory.getLog(ActionListenerImpl.class); 87 88 89 92 private ActionListener original; 93 94 95 97 98 106 public void processAction(ActionEvent event) 107 throws AbortProcessingException { 108 109 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 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(); 144 145 if (log.isDebugEnabled()) { 147 log.debug("Performing Struts form submit for event " + 148 " from source component '" + 149 component.getId() + "'"); 150 } 151 152 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 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 180 181 195 protected RequestProcessor getRequestProcessor(ModuleConfig config, 196 ServletContext context) { 197 198 String 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 e) { 216 log.error("Cannot instantiate RequestProcessor of class " 217 + config.getControllerConfig().getProcessorClass(), 218 e); 219 throw new IllegalStateException ( 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 |