From 92cea867a3533372251046a3f1f5f868034ddeed Mon Sep 17 00:00:00 2001 From: Musachy Barroso Date: Thu, 5 Jun 2008 13:29:25 +0000 Subject: [PATCH] WW-2667 REST should not extend Codebehind git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@663603 13f79535-47bb-0310-9956-ffa450edef68 --- .../config/ClasspathPackageProvider.java | 86 ++++++++++++++----- .../src/main/resources/struts-plugin.xml | 2 +- plugins/rest/pom.xml | 5 -- .../ControllerClasspathPackageProvider.java | 75 ---------------- .../rest/src/main/resources/struts-plugin.xml | 17 ++-- 5 files changed, 75 insertions(+), 110 deletions(-) delete mode 100644 plugins/rest/src/main/java/org/apache/struts2/rest/ControllerClasspathPackageProvider.java diff --git a/plugins/codebehind/src/main/java/org/apache/struts2/config/ClasspathPackageProvider.java b/plugins/codebehind/src/main/java/org/apache/struts2/config/ClasspathPackageProvider.java index 88ee3dd64..d12f29ed9 100644 --- a/plugins/codebehind/src/main/java/org/apache/struts2/config/ClasspathPackageProvider.java +++ b/plugins/codebehind/src/main/java/org/apache/struts2/config/ClasspathPackageProvider.java @@ -84,7 +84,7 @@ public class ClasspathPackageProvider implements PackageProvider { * to use in place of "struts-default". */ protected static final String DEFAULT_PARENT_PACKAGE = "struts.configuration.classpath.defaultParentPackage"; - + /** * A setting to disable action scanning. */ @@ -109,10 +109,25 @@ public class ClasspathPackageProvider implements PackageProvider { */ private boolean forceLowerCase = true; + protected static final String CLASS_SUFFIX = "struts.codebehind.classSuffix"; /** * Default suffix that can be used to indicate POJO "Action" classes. */ - private static final String ACTION = "Action"; + protected String classSuffix = "Action"; + + protected static final String CHECK_IMPLEMENTS_ACTION = "struts.codebehind.checkImplementsAction"; + + /** + * When testing a class, check that it implements Action + */ + protected boolean checkImplementsAction = true; + + protected static final String CHECK_ANNOTATION = "struts.codebehind.checkAnnotation"; + + /** + * When testing a class, check that it has an @Action annotation + */ + protected boolean checkAnnotation = true; /** * Helper class to scan class path for server pages. @@ -126,7 +141,7 @@ public class ClasspathPackageProvider implements PackageProvider { * @see #needsReload */ private boolean initialized = false; - + private boolean disableActionScanning = false; private PackageLoader packageLoader; @@ -165,12 +180,12 @@ public class ClasspathPackageProvider implements PackageProvider { return ClassLoaderUtil.getResource(path, getClass()); } } - + @Inject("actionPackages") public void setActionPackages(String packages) { this.actionPackages = packages; } - + public void setServletContext(ServletContext ctx) { this.servletContext = ctx; } @@ -184,7 +199,27 @@ public class ClasspathPackageProvider implements PackageProvider { public void setDisableActionScanning(String disableActionScanning) { this.disableActionScanning = "true".equals(disableActionScanning); } - + + /** + * Check that the class implements Action + * + * @param checkImplementsAction True to check + */ + @Inject(value=CHECK_IMPLEMENTS_ACTION, required=false) + public void setCheckImplementsAction(String checkImplementsAction) { + this.checkImplementsAction = "true".equals(checkImplementsAction); + } + + /** + * Check that the class has an @Action annotation + * + * @param checkImplementsAction True to check + */ + @Inject(value=CHECK_ANNOTATION, required=false) + public void setCheckAnnotation(String checkAnnotation) { + this.checkAnnotation = "true".equals(checkAnnotation); + } + /** * Register a default parent package for the actions. * @@ -214,10 +249,20 @@ public class ClasspathPackageProvider implements PackageProvider { public void setDefaultPagePrefix(String defaultPagePrefix) { this.defaultPagePrefix = defaultPagePrefix; } - + + /** + * Default suffix that can be used to indicate POJO "Action" classes. + * + * @param defaultPagePrefix the defaultPagePrefix to set + */ + @Inject(value=CLASS_SUFFIX, required=false) + public void setClassSuffix(String classSuffix) { + this.classSuffix = classSuffix; + } + /** * Whether to use a lowercase letter as the initial letter of an action. - * + * * @param force If false, actions will retain the initial uppercase letter from the Action class. * (view.action (true) versus View.action (false)). */ @@ -268,16 +313,16 @@ public class ClasspathPackageProvider implements PackageProvider { // Match Action implementations and classes ending with "Action" public boolean matches(Class type) { // TODO: should also find annotated classes - return (Action.class.isAssignableFrom(type) || + return ((checkImplementsAction && Action.class.isAssignableFrom(type)) || type.getSimpleName().endsWith(getClassSuffix()) || - type.getAnnotation(org.apache.struts2.config.Action.class) != null); + (checkAnnotation && type.getAnnotation(org.apache.struts2.config.Action.class) != null)); } }; } - + protected String getClassSuffix() { - return ACTION; + return classSuffix; } /** @@ -295,8 +340,8 @@ public class ClasspathPackageProvider implements PackageProvider { String actionPackage = cls.getPackage().getName(); String actionNamespace = null; String actionName = null; - - org.apache.struts2.config.Action actionAnn = + + org.apache.struts2.config.Action actionAnn = (org.apache.struts2.config.Action) cls.getAnnotation(org.apache.struts2.config.Action.class); if (actionAnn != null) { actionName = actionAnn.name(); @@ -312,7 +357,7 @@ public class ClasspathPackageProvider implements PackageProvider { LOG.debug("ClasspathPackageProvider: Processing class "+name); } name = name.substring(pkg.length() + 1); - + actionNamespace = ""; actionName = name; int pos = name.lastIndexOf('.'); @@ -360,6 +405,7 @@ public class ClasspathPackageProvider implements PackageProvider { } } + ResultTypeConfig defaultResultType = packageLoader.getDefaultResultType(pkgConfig); ActionConfig actionConfig = new ActionConfig.Builder(actionPackage, actionName, cls.getName()) .addResultConfigs(new ResultMap(cls, actionName, defaultResultType)) @@ -388,10 +434,10 @@ public class ClasspathPackageProvider implements PackageProvider { parent = loadPackageConfig(actionNamespace, actionPackage, null); actionNamespace = ns.value(); actionPackage = actionClass.getName(); - - // See if the namespace has been overridden by the @Action annotation + + // See if the namespace has been overridden by the @Action annotation } else { - org.apache.struts2.config.Action actionAnn = + org.apache.struts2.config.Action actionAnn = (org.apache.struts2.config.Action) actionClass.getAnnotation(org.apache.struts2.config.Action.class); if (actionAnn != null && !actionAnn.DEFAULT_NAMESPACE.equals(actionAnn.namespace())) { // we pass null as the namespace in case the parent package hasn't been loaded yet @@ -401,7 +447,7 @@ public class ClasspathPackageProvider implements PackageProvider { } } - + PackageConfig.Builder pkgConfig = packageLoader.getPackage(actionPackage); if (pkgConfig == null) { pkgConfig = new PackageConfig.Builder(actionPackage); @@ -429,7 +475,7 @@ public class ClasspathPackageProvider implements PackageProvider { } System.out.println("class:"+actionClass+" parent:"+parent+" current:"+(pkgConfig != null ? pkgConfig.getName() : "")); - + return pkgConfig; } diff --git a/plugins/codebehind/src/main/resources/struts-plugin.xml b/plugins/codebehind/src/main/resources/struts-plugin.xml index 43b66c71b..13b79b40f 100644 --- a/plugins/codebehind/src/main/resources/struts-plugin.xml +++ b/plugins/codebehind/src/main/resources/struts-plugin.xml @@ -34,5 +34,5 @@ - + diff --git a/plugins/rest/pom.xml b/plugins/rest/pom.xml index a341fa984..358d052e4 100644 --- a/plugins/rest/pom.xml +++ b/plugins/rest/pom.xml @@ -34,11 +34,6 @@ Struts 2 REST Plugin - - org.apache.struts - struts2-codebehind-plugin - ${pom.version} - com.thoughtworks.xstream xstream diff --git a/plugins/rest/src/main/java/org/apache/struts2/rest/ControllerClasspathPackageProvider.java b/plugins/rest/src/main/java/org/apache/struts2/rest/ControllerClasspathPackageProvider.java deleted file mode 100644 index db00fe475..000000000 --- a/plugins/rest/src/main/java/org/apache/struts2/rest/ControllerClasspathPackageProvider.java +++ /dev/null @@ -1,75 +0,0 @@ -/* - * $Id$ - * - * 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.rest; - -import org.apache.struts2.config.ClasspathPackageProvider; - -import com.opensymphony.xwork2.inject.Inject; -import com.opensymphony.xwork2.util.ResolverUtil.ClassTest; - -/** - * Checks for actions ending in Controller indicating a Rest controller - */ -public class ControllerClasspathPackageProvider extends ClasspathPackageProvider { - - /** - * A setting to disable action scanning. - */ - protected static final String DISABLE_REST_CONTROLLER_SCANNING = "struts.configuration.rest.disableControllerScanning"; - - @Override - protected ClassTest createActionClassTest() { - return new ClassTest() { - // Match Action implementations and classes ending with "Controller" - public boolean matches(Class type) { - return (type.getSimpleName().endsWith("Controller")); - } - }; - } - - @Override - protected String getClassSuffix() { - return "Controller"; - } - - /** - * Ignore setting to disable action scanning from the codebehind plugin. - * - * @param disableActionScanning True to disable - */ - @Override - @Inject(value=DISABLE_ACTION_SCANNING, required=false) - public void setDisableActionScanning(String disableActionScanning) { - // do nothing - } - - /** - * Disables controller scanning. - * - * @param disableActionScanning True to disable - */ - @Inject(value=DISABLE_REST_CONTROLLER_SCANNING, required=false) - public void setDisableRestControllerScanning(String disableActionScanning) { - super.setDisableActionScanning(disableActionScanning); - } - -} diff --git a/plugins/rest/src/main/resources/struts-plugin.xml b/plugins/rest/src/main/resources/struts-plugin.xml index cf639569a..b102cc4e2 100644 --- a/plugins/rest/src/main/resources/struts-plugin.xml +++ b/plugins/rest/src/main/resources/struts-plugin.xml @@ -24,13 +24,11 @@ - + - - @@ -43,13 +41,14 @@ - + - - - + + + + - + @@ -108,7 +107,7 @@ input,back,cancel,browse,index,show,edit,editNew - +