Author: mrdon Date: Fri Jun 13 21:20:22 2008 New Revision: 667742 URL: http://svn.apache.org/viewvc?rev=667742&view=rev Log: Adding support for multiple parent packages to be defined at the package level, adding more restrictions on the namespace, parentpackage, and results annotations
Added: struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/config/parenttest/ struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/config/parenttest/SomeAction.java struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/config/parenttest/package-info.java Modified: struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/config/ClasspathPackageProvider.java struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/config/Namespace.java struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/config/ParentPackage.java struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/config/Results.java struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/config/ClasspathPackageProviderTest.java Modified: struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/config/ClasspathPackageProvider.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/config/ClasspathPackageProvider.java?rev=667742&r1=667741&r2=667742&view=diff ============================================================================== --- struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/config/ClasspathPackageProvider.java (original) +++ struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/config/ClasspathPackageProvider.java Fri Jun 13 21:20:22 2008 @@ -387,17 +387,14 @@ actionPackage = pkgConfig.getName(); } - Annotation annotation = cls.getAnnotation(ParentPackage.class); - if (annotation != null) { - String parent = ((ParentPackage)annotation).value(); - PackageConfig parentPkg = configuration.getPackageConfig(parent); - if (parentPkg == null) { - throw new ConfigurationException("ClasspathPackageProvider: Unable to locate parent package "+parent, annotation); - } - pkgConfig.addParent(parentPkg); - - if (!TextUtils.stringSet(pkgConfig.getNamespace()) && TextUtils.stringSet(parentPkg.getNamespace())) { - pkgConfig.namespace(parentPkg.getNamespace()); + List<PackageConfig> parents = findAllParentPackages(cls); + if (parents.size() > 0) { + pkgConfig.addParents(parents); + + // Try to guess the namespace from the first package + PackageConfig firstParent = parents.get(0); + if (!TextUtils.stringSet(pkgConfig.getNamespace()) && TextUtils.stringSet(firstParent.getNamespace())) { + pkgConfig.namespace(firstParent.getNamespace()); } } @@ -410,6 +407,36 @@ } /** + * Finds all parent packages by first looking at the ParentPackage annotation on the package, then the class + * @param cls The action class + * @return A list of unique packages to add + */ + private List<PackageConfig> findAllParentPackages(Class<?> cls) { + + List<PackageConfig> parents = new ArrayList<PackageConfig>(); + // Favor parent package annotations from the package + Set<String> parentNames = new LinkedHashSet<String>(); + ParentPackage annotation = cls.getPackage().getAnnotation(ParentPackage.class); + if (annotation != null) { + parentNames.addAll(Arrays.asList(annotation.value())); + } + annotation = cls.getAnnotation(ParentPackage.class); + if (annotation != null) { + parentNames.addAll(Arrays.asList(annotation.value())); + } + if (parentNames.size() > 0) { + for (String parent : parentNames) { + PackageConfig parentPkg = configuration.getPackageConfig(parent); + if (parentPkg == null) { + throw new ConfigurationException("ClasspathPackageProvider: Unable to locate parent package "+parent, annotation); + } + parents.add(parentPkg); + } + } + return parents; + } + + /** * Finds or creates the package configuration for an Action class. * * The namespace annotation is honored, if found, Modified: struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/config/Namespace.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/config/Namespace.java?rev=667742&r1=667741&r2=667742&view=diff ============================================================================== --- struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/config/Namespace.java (original) +++ struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/config/Namespace.java Fri Jun 13 21:20:22 2008 @@ -23,11 +23,14 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.annotation.ElementType; /** * Allows an action class to specify its namespace */ @Retention(RetentionPolicy.RUNTIME) [EMAIL PROTECTED](ElementType.TYPE) public @interface Namespace { String value(); } Modified: struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/config/ParentPackage.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/config/ParentPackage.java?rev=667742&r1=667741&r2=667742&view=diff ============================================================================== --- struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/config/ParentPackage.java (original) +++ struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/config/ParentPackage.java Fri Jun 13 21:20:22 2008 @@ -23,11 +23,14 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.annotation.ElementType; /** - * Allows an action class to specify an xwork package to inherit + * Allows an action class or package to specify an xwork package to inherit */ @Retention(RetentionPolicy.RUNTIME) [EMAIL PROTECTED]({ElementType.TYPE, ElementType.PACKAGE}) public @interface ParentPackage { - String value(); + String[] value(); } Modified: struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/config/Results.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/config/Results.java?rev=667742&r1=667741&r2=667742&view=diff ============================================================================== --- struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/config/Results.java (original) +++ struts/struts2/trunk/plugins/codebehind/src/main/java/org/apache/struts2/config/Results.java Fri Jun 13 21:20:22 2008 @@ -23,11 +23,14 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; +import java.lang.annotation.ElementType; /** * Defines multiple XWork Results */ @Retention(RetentionPolicy.RUNTIME) [EMAIL PROTECTED](ElementType.TYPE) public @interface Results { Result[] value(); } Modified: struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/config/ClasspathPackageProviderTest.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/config/ClasspathPackageProviderTest.java?rev=667742&r1=667741&r2=667742&view=diff ============================================================================== --- struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/config/ClasspathPackageProviderTest.java (original) +++ struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/config/ClasspathPackageProviderTest.java Fri Jun 13 21:20:22 2008 @@ -39,7 +39,13 @@ public void setUp() throws Exception { provider = new ClasspathPackageProvider(); provider.setActionPackages("org.apache.struts2.config"); - config = new DefaultConfiguration(); + config = createNewConfiguration(); + provider.init(config); + provider.loadPackages(); + } + + private Configuration createNewConfiguration() { + Configuration config = new DefaultConfiguration(); PackageConfig strutsDefault = new PackageConfig.Builder("struts-default") .addResultTypeConfig(new ResultTypeConfig.Builder("dispatcher", ServletDispatcherResult.class.getName()) .defaultResultParam("location") @@ -51,17 +57,16 @@ .namespace("/custom") .build(); config.addPackageConfig("custom-package", customPackage); - provider.init(config); - provider.loadPackages(); + return config; } - + public void tearDown() throws Exception { provider = null; config = null; } public void testFoundRootPackages() { - assertEquals(6, config.getPackageConfigs().size()); + assertEquals(7, config.getPackageConfigs().size()); PackageConfig pkg = config.getPackageConfig("org.apache.struts2.config"); assertNotNull(pkg); Map configs = pkg.getActionConfigs(); @@ -91,6 +96,23 @@ assertEquals("/custom", pkg.getNamespace()); } + public void testParentPackageOnPackage() { + provider = new ClasspathPackageProvider(); + provider.setActionPackages("org.apache.struts2.config.parenttest"); + provider.init(createNewConfiguration()); + provider.loadPackages(); + + + PackageConfig pkg = config.getPackageConfig("org.apache.struts2.config.parenttest"); + // assertEquals(2, pkg.getParents().size()); + assertNotNull(pkg); + + assertEquals("custom-package", pkg.getParents().get(0).getName()); + Map configs = pkg.getActionConfigs(); + ActionConfig config = (ActionConfig) configs.get("some"); + assertNotNull(config); + } + public void testCustomNamespace() { PackageConfig pkg = config.getPackageConfig("org.apache.struts2.config.CustomNamespaceAction"); Map configs = pkg.getAllActionConfigs(); Added: struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/config/parenttest/SomeAction.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/config/parenttest/SomeAction.java?rev=667742&view=auto ============================================================================== --- struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/config/parenttest/SomeAction.java (added) +++ struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/config/parenttest/SomeAction.java Fri Jun 13 21:20:22 2008 @@ -0,0 +1,30 @@ +/* + * $Id: ParentPackage.java 651946 2008-04-27 13:41:38Z apetrelli $ + * + * 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.config.parenttest; + +import com.opensymphony.xwork2.Action; + +public class SomeAction implements Action { + + public String execute() throws Exception { + return null; //To change body of implemented methods use File | Settings | File Templates. + } +} Added: struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/config/parenttest/package-info.java URL: http://svn.apache.org/viewvc/struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/config/parenttest/package-info.java?rev=667742&view=auto ============================================================================== --- struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/config/parenttest/package-info.java (added) +++ struts/struts2/trunk/plugins/codebehind/src/test/java/org/apache/struts2/config/parenttest/package-info.java Fri Jun 13 21:20:22 2008 @@ -0,0 +1,24 @@ +/* + * $Id: ParentPackage.java 651946 2008-04-27 13:41:38Z apetrelli $ + * + * 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. + */ [EMAIL PROTECTED]("custom-package") +package org.apache.struts2.config.parenttest; + +import org.apache.struts2.config.ParentPackage; \ No newline at end of file