Moving zero config code into codebehind plugin, minor fix to showcase

WW-2247


git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@584166 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Donald J. Brown
2007-10-12 14:07:52 +00:00
parent b07f659693
commit 5494663fd4
15 changed files with 76 additions and 67 deletions
@@ -67,6 +67,8 @@
</interceptor-stack>
</interceptors>
<default-action-ref name="showcase" />
<action name="showcase">
<result>showcase.jsp</result>
</action>
@@ -39,12 +39,9 @@ import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsConstants;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.config.BeanSelectionProvider;
import org.apache.struts2.config.ClasspathConfigurationProvider;
import org.apache.struts2.config.DefaultPropertiesProvider;
import org.apache.struts2.config.LegacyPropertiesConfigurationProvider;
import org.apache.struts2.config.StrutsXmlConfigurationProvider;
import org.apache.struts2.config.ClasspathConfigurationProvider.ClasspathPageLocator;
import org.apache.struts2.config.ClasspathConfigurationProvider.PageLocator;
import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.dispatcher.multipart.MultiPartRequest;
import org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper;
@@ -302,19 +299,6 @@ public class Dispatcher {
}
}
private void init_ZeroConfiguration() {
String packages = initParams.get("actionPackages");
if (packages != null) {
String[] names = packages.split("\\s*[,]\\s*");
// Initialize the classloader scanner with the configured packages
if (names.length > 0) {
ClasspathConfigurationProvider provider = new ClasspathConfigurationProvider(names);
provider.setPageLocator(new ServletContextPageLocator(servletContext));
configurationManager.addConfigurationProvider(provider);
}
}
}
private void init_CustomConfigurationProviders() {
String configProvs = initParams.get("configProviders");
if (configProvs != null) {
@@ -434,7 +418,6 @@ Caused by: com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyExcepti
init_DefaultProperties(); // [1]
init_TraditionalXmlConfigurations(); // [2]
init_LegacyStrutsProperties(); // [3]
init_ZeroConfiguration(); // [4]
init_CustomConfigurationProviders(); // [5]
init_MethodConfigurationProvider();
init_FilterInitParameters() ; // [6]
@@ -757,32 +740,7 @@ Caused by: com.opensymphony.xwork2.inject.ContainerImpl$MissingDependencyExcepti
}
}
/**
* Search classpath for a page.
*/
private final class ServletContextPageLocator implements PageLocator {
private final ServletContext context;
private ClasspathPageLocator classpathPageLocator = new ClasspathPageLocator();
private ServletContextPageLocator(ServletContext context) {
this.context = context;
}
public URL locate(String path) {
URL url = null;
try {
url = context.getResource(path);
if (url == null) {
url = classpathPageLocator.locate(path);
}
} catch (MalformedURLException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Unable to resolve path "+path+" against the servlet context");
}
}
return url;
}
}
/**
* Provide an accessor class for static XWork utility.
@@ -1,5 +1,5 @@
/*
* $Id$
* $Id: ClasspathPackageProvider.java 582626 2007-10-07 13:26:12Z mrdon $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -22,15 +22,19 @@ package org.apache.struts2.config;
import java.lang.annotation.Annotation;
import java.lang.reflect.Modifier;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletContext;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.config.Configuration;
import com.opensymphony.xwork2.config.ConfigurationException;
import com.opensymphony.xwork2.config.ConfigurationProvider;
import com.opensymphony.xwork2.config.PackageProvider;
import com.opensymphony.xwork2.config.entities.ActionConfig;
import com.opensymphony.xwork2.config.entities.PackageConfig;
import com.opensymphony.xwork2.config.entities.ResultConfig;
@@ -46,14 +50,14 @@ import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;
/**
* ClasspathConfigurationProvider loads the configuration
* ClasspathPackageProvider loads the configuration
* by scanning the classpath or selected packages for Action classes.
* <p>
* This provider is only invoked if one or more action packages are passed to the dispatcher,
* usually from the web.xml.
* Configurations are created for objects that either implement Action or have classnames that end with "Action".
*/
public class ClasspathConfigurationProvider implements ConfigurationProvider {
public class ClasspathPackageProvider implements PackageProvider {
/**
* The default page prefix (or "path").
@@ -119,11 +123,6 @@ public class ClasspathConfigurationProvider implements ConfigurationProvider {
*/
private boolean initialized = false;
/**
* The list of packages to scan for Action classes.
*/
private String[] packages;
/**
* The package configurations for scanned Actions.
*
@@ -134,7 +133,7 @@ public class ClasspathConfigurationProvider implements ConfigurationProvider {
/**
* Logging instance for this class.
*/
private static final Logger LOG = LoggerFactory.getLogger(ClasspathConfigurationProvider.class);
private static final Logger LOG = LoggerFactory.getLogger(ClasspathPackageProvider.class);
/**
* The XWork Configuration for this application.
@@ -143,13 +142,16 @@ public class ClasspathConfigurationProvider implements ConfigurationProvider {
*/
private Configuration configuration;
private String actionPackages;
private ServletContext servletContext;
/**
* Create instance utilizing a list of packages to scan for Action classes.
*
* @param pkgs List of pacaktges to scan for Action Classes.
*/
public ClasspathConfigurationProvider(String[] pkgs) {
this.packages = pkgs;
public ClasspathPackageProvider() {
}
/**
@@ -167,6 +169,15 @@ public class ClasspathConfigurationProvider implements ConfigurationProvider {
return ClassLoaderUtil.getResource(path, getClass());
}
}
@Inject("actionPackages")
public void setActionPackages(String packages) {
this.actionPackages = packages;
}
public void setServletContext(ServletContext ctx) {
this.servletContext = ctx;
}
/**
* Register a default parent package for the actions.
@@ -271,7 +282,7 @@ public class ClasspathConfigurationProvider implements ConfigurationProvider {
for (String pkg : pkgs) {
if (name.startsWith(pkg)) {
if (LOG.isDebugEnabled()) {
LOG.debug("ClasspathConfigurationProvider: Processing class "+name);
LOG.debug("ClasspathPackageProvider: Processing class "+name);
}
name = name.substring(pkg.length() + 1);
@@ -298,7 +309,7 @@ public class ClasspathConfigurationProvider implements ConfigurationProvider {
String parent = ((ParentPackage)annotation).value();
PackageConfig parentPkg = configuration.getPackageConfig(parent);
if (parentPkg == null) {
throw new ConfigurationException("ClasspathConfigurationProvider: Unable to locate parent package "+parent, annotation);
throw new ConfigurationException("ClasspathPackageProvider: Unable to locate parent package "+parent, annotation);
}
pkgConfig.addParent(parentPkg);
@@ -363,7 +374,7 @@ public class ClasspathConfigurationProvider implements ConfigurationProvider {
}
if (parent == null) {
throw new ConfigurationException("ClasspathConfigurationProvider: Unable to locate default parent package: " +
throw new ConfigurationException("ClasspathPackageProvider: Unable to locate default parent package: " +
defaultParentPackage);
}
pkgConfig.addParent(parent);
@@ -398,7 +409,14 @@ public class ClasspathConfigurationProvider implements ConfigurationProvider {
*/
public void loadPackages() throws ConfigurationException {
loadedPackageConfigs.clear();
loadPackages(packages);
if (actionPackages != null) {
String[] names = actionPackages.split("\\s*[,]\\s*");
// Initialize the classloader scanner with the configured packages
if (names.length > 0) {
setPageLocator(new ServletContextPageLocator(servletContext));
}
loadPackages(names);
}
initialized = true;
}
@@ -501,7 +519,7 @@ public class ClasspathConfigurationProvider implements ConfigurationProvider {
try {
resultClass = ClassLoaderUtil.loadClass(className, getClass());
} catch (ClassNotFoundException ex) {
throw new ConfigurationException("ClasspathConfigurationProvider: Unable to locate result class "+className, actionClass);
throw new ConfigurationException("ClasspathPackageProvider: Unable to locate result class "+className, actionClass);
}
}
@@ -523,8 +541,30 @@ public class ClasspathConfigurationProvider implements ConfigurationProvider {
}
}
// See superclass for Javadoc
public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
// Override to provide functionality
/**
* Search classpath for a page.
*/
private final class ServletContextPageLocator implements PageLocator {
private final ServletContext context;
private ClasspathPageLocator classpathPageLocator = new ClasspathPageLocator();
private ServletContextPageLocator(ServletContext context) {
this.context = context;
}
public URL locate(String path) {
URL url = null;
try {
url = context.getResource(path);
if (url == null) {
url = classpathPageLocator.locate(path);
}
} catch (MalformedURLException e) {
if (LOG.isDebugEnabled()) {
LOG.debug("Unable to resolve path "+path+" against the servlet context");
}
}
return url;
}
}
}
@@ -28,6 +28,7 @@
<struts>
<bean type="com.opensymphony.xwork2.UnknownHandler" class="org.apache.struts2.codebehind.CodebehindUnknownHandler" />
<bean type="com.opensymphony.xwork2.config.PackageProvider" name="codebehind" class="org.apache.struts2.config.ClasspathPackageProvider" />
<constant name="struts.codebehind.pathPrefix" value="/"/>
<constant name="struts.codebehind.defaultPackage" value="codebehind-default"/>
@@ -22,6 +22,7 @@ package org.apache.struts2.codebehind;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Collections;
import java.util.HashMap;
import javax.servlet.ServletContext;
@@ -34,9 +35,11 @@ import com.mockobjects.dynamic.C;
import com.mockobjects.dynamic.Mock;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionProxyFactory;
import com.opensymphony.xwork2.ObjectFactory;
import com.opensymphony.xwork2.Result;
import com.opensymphony.xwork2.config.entities.ResultTypeConfig;
import com.opensymphony.xwork2.util.XWorkTestCaseHelper;
public class CodebehindUnknownHandlerTest extends StrutsTestCase {
@@ -44,7 +47,11 @@ public class CodebehindUnknownHandlerTest extends StrutsTestCase {
Mock mockServletContext;
public void setUp() throws Exception {
super.setUp();
configurationManager = XWorkTestCaseHelper.setUp();
configuration = configurationManager.getConfiguration();
container = configuration.getContainer();
actionProxyFactory = container.getInstance(ActionProxyFactory.class);
initDispatcher(Collections.singletonMap("actionPackages", "foo.bar"));
mockServletContext = new Mock(ServletContext.class);
handler = new CodebehindUnknownHandler("codebehind-default", configuration);
handler.setPathPrefix("/");
@@ -1,5 +1,5 @@
/*
* $Id$
* $Id: ClasspathPackageProviderTest.java 501717 2007-01-31 03:51:11Z mrdon $
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
@@ -33,13 +33,14 @@ import com.opensymphony.xwork2.config.impl.DefaultConfiguration;
import junit.framework.TestCase;
public class ClasspathConfigurationProviderTest extends TestCase {
public class ClasspathPackageProviderTest extends TestCase {
ClasspathConfigurationProvider provider;
ClasspathPackageProvider provider;
Configuration config;
public void setUp() {
provider = new ClasspathConfigurationProvider(new String[]{"org.apache.struts2.config"});
provider = new ClasspathPackageProvider();
provider.setActionPackages("org.apache.struts2.config");
config = new DefaultConfiguration();
PackageConfig strutsDefault = new PackageConfig("struts-default");
strutsDefault.addResultTypeConfig(new ResultTypeConfig("dispatcher", ServletDispatcherResult.class.getName(), "location"));