1 16 17 package org.springframework.web.struts; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.struts.action.ActionMapping; 22 import org.apache.struts.action.ActionServlet; 23 import org.apache.struts.config.ModuleConfig; 24 25 import org.springframework.beans.factory.config.AutowireCapableBeanFactory; 26 import org.springframework.web.context.WebApplicationContext; 27 import org.springframework.web.context.support.WebApplicationContextUtils; 28 29 45 public abstract class DelegatingActionUtils { 46 47 51 public static final String PARAM_AUTOWIRE = "spring.autowire"; 52 53 57 public static final String PARAM_DEPENDENCY_CHECK = "spring.dependencyCheck"; 58 59 63 public static final String AUTOWIRE_BY_NAME = "byName"; 64 65 69 public static final String AUTOWIRE_BY_TYPE = "byType"; 70 71 72 private static final Log logger = LogFactory.getLog(DelegatingActionUtils.class); 73 74 75 84 public static WebApplicationContext getWebApplicationContext( 85 ActionServlet actionServlet, ModuleConfig moduleConfig) { 86 87 WebApplicationContext wac = null; 88 String modulePrefix = null; 89 90 if (moduleConfig != null) { 92 modulePrefix = moduleConfig.getPrefix(); 93 wac = (WebApplicationContext) actionServlet.getServletContext().getAttribute( 94 ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX + modulePrefix); 95 } 96 97 if (wac == null && !"".equals(modulePrefix)) { 99 wac = (WebApplicationContext) actionServlet.getServletContext().getAttribute( 100 ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX); 101 } 102 103 return wac; 104 } 105 106 116 public static WebApplicationContext getRequiredWebApplicationContext( 117 ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException { 118 119 WebApplicationContext wac = getWebApplicationContext(actionServlet, moduleConfig); 120 if (wac == null) { 122 throw new IllegalStateException ( 123 "Could not find ContextLoaderPlugIn's WebApplicationContext as ServletContext attribute [" + 124 ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX + "]: Did you register [" + 125 ContextLoaderPlugIn.class.getName() + "]?"); 126 } 127 return wac; 128 } 129 130 142 public static WebApplicationContext findRequiredWebApplicationContext( 143 ActionServlet actionServlet, ModuleConfig moduleConfig) throws IllegalStateException { 144 145 WebApplicationContext wac = getWebApplicationContext(actionServlet, moduleConfig); 146 if (wac == null) { 148 wac = WebApplicationContextUtils.getRequiredWebApplicationContext(actionServlet.getServletContext()); 149 } 150 return wac; 151 } 152 153 161 public static String determineActionBeanName(ActionMapping mapping) { 162 String prefix = mapping.getModuleConfig().getPrefix(); 163 String path = mapping.getPath(); 164 String beanName = prefix + path; 165 if (logger.isDebugEnabled()) { 166 logger.debug("DelegatingActionProxy with mapping path '" + path + "' and module prefix '" + 167 prefix + "' delegating to Spring bean with name [" + beanName + "]"); 168 } 169 return beanName; 170 } 171 172 184 public static int getAutowireMode(ActionServlet actionServlet) { 185 String autowire = actionServlet.getInitParameter(PARAM_AUTOWIRE); 186 if (autowire != null) { 187 if (AUTOWIRE_BY_NAME.equals(autowire)) { 188 return AutowireCapableBeanFactory.AUTOWIRE_BY_NAME; 189 } 190 else if (!AUTOWIRE_BY_TYPE.equals(autowire)) { 191 throw new IllegalArgumentException ("ActionServlet 'autowire' parameter must be 'byName' or 'byType'"); 192 } 193 } 194 return AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE; 195 } 196 197 205 public static boolean getDependencyCheck(ActionServlet actionServlet) { 206 String dependencyCheck = actionServlet.getInitParameter(PARAM_DEPENDENCY_CHECK); 207 return Boolean.valueOf(dependencyCheck).booleanValue(); 208 } 209 210 } 211 | Popular Tags |