diff --git a/plugins/codebehind/pom.xml b/plugins/codebehind/pom.xml
new file mode 100644
index 000000000..374957a00
--- /dev/null
+++ b/plugins/codebehind/pom.xml
@@ -0,0 +1,65 @@
+
+
+ 4.0.0
+
+ org.apache.struts
+ struts2-plugins
+ 2.0.2-SNAPSHOT
+
+ org.apache.struts
+ struts2-codebehind-plugin
+ jar
+ Struts 2 Codebehind Plugin
+
+
+ scm:svn:http://svn.apache.org/repos/asf/struts/struts2/trunk/plugins/codebehind/
+ scm:svn:https://svn.apache.org/repos/asf/struts/struts2/trunk/plugins/codebehind/
+ http://svn.apache.org/viewcvs.cgi/struts/struts2/trunk/plugins/codebehind/
+
+
+
+
+ junit
+ junit
+ test
+ 3.8.1
+
+
+ mockobjects
+ mockobjects-core
+ 0.09
+ test
+
+
+ org.springframework
+ spring-mock
+ 1.2.8
+ test
+
+
+ org.springframework
+ spring-core
+ 1.2.8
+ test
+
+
+ javax.servlet
+ servlet-api
+ 2.4
+ provided
+
+
+
+ javax.servlet
+ jsp-api
+ 2.0
+ provided
+
+
+
+
+
+
+
+
diff --git a/plugins/codebehind/src/main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java b/plugins/codebehind/src/main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java
new file mode 100644
index 000000000..b69cccd11
--- /dev/null
+++ b/plugins/codebehind/src/main/java/org/apache/struts2/codebehind/CodebehindUnknownHandler.java
@@ -0,0 +1,212 @@
+/*
+ * $Id: DWRValidator.java 476642 2006-11-18 22:40:18Z mrdon $
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.codebehind;
+
+import java.net.MalformedURLException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.ServletContext;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import com.opensymphony.xwork2.Action;
+import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.ActionSupport;
+import com.opensymphony.xwork2.ObjectFactory;
+import com.opensymphony.xwork2.Result;
+import com.opensymphony.xwork2.UnknownHandler;
+import com.opensymphony.xwork2.XWorkException;
+import com.opensymphony.xwork2.config.Configuration;
+import com.opensymphony.xwork2.config.entities.ActionConfig;
+import com.opensymphony.xwork2.config.entities.PackageConfig;
+import com.opensymphony.xwork2.config.entities.ResultConfig;
+import com.opensymphony.xwork2.config.entities.ResultTypeConfig;
+import com.opensymphony.xwork2.config.providers.InterceptorBuilder;
+import com.opensymphony.xwork2.inject.Inject;
+
+public class CodebehindUnknownHandler implements UnknownHandler {
+
+ protected String defaultPackageName = "codebehind-default";
+ protected ServletContext servletContext;
+ protected Map resultsByExtension = new HashMap();
+ protected String templatePathPrefix = "/";
+ protected Configuration configuration;
+ protected ObjectFactory objectFactory;
+
+ protected static final Log LOG = LogFactory.getLog(CodebehindUnknownHandler.class);
+
+ @Inject("struts.codebehind.defaultPackage")
+ public void setDefaultPackage(String pkg) {
+ this.defaultPackageName = pkg;
+ }
+
+ @Inject
+ public void setConfiguration(Configuration config) {
+ this.configuration = config;
+ }
+
+ @Inject
+ public void setServletContext(ServletContext servletContext) {
+ this.servletContext = servletContext;
+ }
+
+ @Inject
+ public void setObjectFactory(ObjectFactory objectFactory) {
+ this.objectFactory = objectFactory;
+ }
+
+ protected Map loadResultTypes(Configuration config) {
+ Map resultTypes = new LinkedHashMap();
+ PackageConfig parentPackage = config.getPackageConfig(defaultPackageName);
+ Map results = parentPackage.getAllResultTypeConfigs();
+
+ resultTypes.put("jsp", results.get("dispatcher"));
+ resultTypes.put("vm", results.get("velocity"));
+ resultTypes.put("ftl", results.get("freemarker"));
+ return resultTypes;
+ }
+
+ public ActionConfig handleUnknownAction(String namespace, String actionName)
+ throws XWorkException {
+ if (resultsByExtension == null) {
+ resultsByExtension = loadResultTypes(configuration);
+ }
+ String pathPrefix = determinePath(templatePathPrefix, namespace);
+ ActionConfig actionConfig = null;
+ for (String ext : resultsByExtension.keySet()) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Trying to locate unknown action template with extension ."+ext+" in directory "+pathPrefix);
+ }
+ String path = string(pathPrefix, actionName, "." , ext);
+ try {
+ if (servletContext.getResource(path) != null) {
+ actionConfig = buildActionConfig(path, namespace, actionName, resultsByExtension.get(ext));
+ break;
+ }
+ } catch (MalformedURLException e) {
+ LOG.warn("Unable to parse template path: "+path+", skipping...");
+ }
+ }
+ return actionConfig;
+ }
+
+ protected ActionConfig buildActionConfig(String path, String namespace, String actionName, ResultTypeConfig resultTypeConfig) {
+ if (resultsByExtension == null) {
+ resultsByExtension = loadResultTypes(configuration);
+ }
+ Map results = new HashMap();
+ HashMap params = new HashMap();
+ if (resultTypeConfig.getParams() != null) {
+ params.putAll(resultTypeConfig.getParams());
+ }
+ params.put(resultTypeConfig.getDefaultResultParam(), path);
+
+ PackageConfig pkg = configuration.getPackageConfig(defaultPackageName);
+ List interceptors = InterceptorBuilder.constructInterceptorReference(pkg, pkg.getFullDefaultInterceptorRef(),
+ Collections.EMPTY_MAP, null, objectFactory);
+ ResultConfig config = new ResultConfig(Action.SUCCESS, resultTypeConfig.getClazz(), params);
+ results.put(Action.SUCCESS, config);
+ return new ActionConfig("execute", ActionSupport.class.getName(), defaultPackageName, new HashMap(), results, interceptors);
+ }
+
+ public Result handleUnknownResult(ActionContext actionContext, String actionName,
+ ActionConfig actionConfig, String resultCode) throws XWorkException {
+
+ Result result = null;
+ PackageConfig pkg = configuration.getPackageConfig(actionConfig.getPackageName());
+ String ns = pkg.getNamespace();
+ String pathPrefix = determinePath(templatePathPrefix, ns);
+
+ for (String ext : resultsByExtension.keySet()) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Trying to locate result with extension ."+ext+" in directory "+pathPrefix);
+ }
+ String path = string(pathPrefix, actionName, "-", resultCode, "." , ext);
+ try {
+ if (servletContext.getResource(path) != null) {
+ result = buildResult(path, resultCode, resultsByExtension.get(ext), actionContext);
+ break;
+ }
+ } catch (MalformedURLException e) {
+ LOG.warn("Unable to parse template path: "+path+", skipping...");
+ }
+
+ path = string(pathPrefix, actionName, "." , ext);
+ try {
+ if (servletContext.getResource(path) != null) {
+ result = buildResult(path, resultCode, resultsByExtension.get(ext), actionContext);
+ break;
+ }
+ } catch (MalformedURLException e) {
+ LOG.warn("Unable to parse template path: "+path+", skipping...");
+ }
+ }
+
+ return result;
+ }
+
+ protected Result buildResult(String path, String resultCode, ResultTypeConfig config, ActionContext invocationContext) {
+ String resultClass = config.getClazz();
+
+ Map params = new LinkedHashMap();
+ if (config.getParams() != null) {
+ params.putAll(config.getParams());
+ }
+ params.put(config.getDefaultResultParam(), path);
+
+ ResultConfig resultConfig = new ResultConfig(resultCode, resultClass, params);
+ try {
+ return objectFactory.buildResult(resultConfig, invocationContext.getContextMap());
+ } catch (Exception e) {
+ throw new XWorkException("Unable to build codebehind result", e, resultConfig);
+ }
+ }
+
+ protected String string(String... parts) {
+ StringBuilder sb = new StringBuilder();
+ for (String part : parts) {
+ sb.append(part);
+ }
+ return sb.toString();
+ }
+
+ protected String determinePath(String prefix, String ns) {
+ if (ns == null || "/".equals(ns)) {
+ ns = "";
+ }
+ if (ns.length() > 0) {
+ if (ns.charAt(0) == '/') {
+ ns = ns.substring(1);
+ }
+ if (ns.charAt(ns.length() - 1) != '/') {
+ ns += "/";
+ }
+ }
+ return prefix + ns;
+ }
+
+}
diff --git a/plugins/codebehind/src/main/resources/struts-plugin.xml b/plugins/codebehind/src/main/resources/struts-plugin.xml
new file mode 100644
index 000000000..3d1f2cac5
--- /dev/null
+++ b/plugins/codebehind/src/main/resources/struts-plugin.xml
@@ -0,0 +1,12 @@
+
+
+
+
+
+
+
+
+
+
diff --git a/plugins/codebehind/src/test/java/org/apache/struts2/codebehind/CodebehindUnknownHandlerTest.java b/plugins/codebehind/src/test/java/org/apache/struts2/codebehind/CodebehindUnknownHandlerTest.java
new file mode 100644
index 000000000..099218443
--- /dev/null
+++ b/plugins/codebehind/src/test/java/org/apache/struts2/codebehind/CodebehindUnknownHandlerTest.java
@@ -0,0 +1,90 @@
+/*
+ * $Id: DWRValidator.java 476642 2006-11-18 22:40:18Z mrdon $
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.struts2.codebehind;
+
+import java.util.HashMap;
+
+import javax.servlet.ServletContext;
+
+import org.apache.struts2.StrutsTestCase;
+import org.apache.struts2.config.NullResult;
+import org.apache.struts2.dispatcher.ServletDispatcherResult;
+
+import com.mockobjects.dynamic.Mock;
+import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.ActionInvocation;
+import com.opensymphony.xwork2.ObjectFactory;
+import com.opensymphony.xwork2.Result;
+import com.opensymphony.xwork2.VoidResult;
+import com.opensymphony.xwork2.config.entities.ResultTypeConfig;
+
+public class CodebehindUnknownHandlerTest extends StrutsTestCase {
+
+ CodebehindUnknownHandler handler;
+ Mock mockServletContext;
+
+ public void setUp() throws Exception {
+ super.setUp();
+ mockServletContext = new Mock(ServletContext.class);
+ handler = new CodebehindUnknownHandler();
+ handler.setConfiguration(configuration);
+ handler.setObjectFactory(container.getInstance(ObjectFactory.class));
+ handler.setServletContext((ServletContext)mockServletContext.proxy());
+
+ }
+
+ public void testBuildResult() {
+ ActionContext ctx = new ActionContext(new HashMap());
+ ResultTypeConfig config = new ResultTypeConfig("null", SomeResult.class.getName(), "location");
+
+ Result result = handler.buildResult("/foo.jsp", "success", config, ctx);
+ assertNotNull(result);
+ assertTrue(result instanceof SomeResult);
+ assertEquals("/foo.jsp", ((SomeResult) result).location);
+
+ }
+
+ public void testString() {
+ assertEquals("foo.bar.jim", handler.string("foo", ".", "bar", ".", "jim"));
+ }
+
+ public void testDeterminePath() {
+ assertEquals("/", handler.determinePath("/", ""));
+ assertEquals("/", handler.determinePath("/", null));
+ assertEquals("/", handler.determinePath("/", "/"));
+ assertEquals("/foo/", handler.determinePath("/", "/foo"));
+ assertEquals("/foo/", handler.determinePath("/", "/foo/"));
+ assertEquals("/foo/", handler.determinePath("/", "foo"));
+ }
+
+ public static class SomeResult implements Result {
+
+ public String location;
+ public void setLocation(String loc) {
+ this.location = loc;
+ }
+
+ public void execute(ActionInvocation invocation) throws Exception {
+ }
+
+ }
+
+}
diff --git a/plugins/pom.xml b/plugins/pom.xml
index 3a0532b0f..f10ba846f 100644
--- a/plugins/pom.xml
+++ b/plugins/pom.xml
@@ -19,6 +19,7 @@
+ codebehind
config-browser
jasperreports
jfreechart