Adding support for multiple parent packages to be defined at the package level, adding more

restrictions on the namespace, parentpackage, and results annotations


git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@667742 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Donald J. Brown
2008-06-14 04:20:22 +00:00
parent 5030b89b33
commit 873d150890
7 changed files with 129 additions and 17 deletions
@@ -387,17 +387,14 @@ public class ClasspathPackageProvider implements PackageProvider {
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);
List<PackageConfig> parents = findAllParentPackages(cls);
if (parents.size() > 0) {
pkgConfig.addParents(parents);
if (!TextUtils.stringSet(pkgConfig.getNamespace()) && TextUtils.stringSet(parentPkg.getNamespace())) {
pkgConfig.namespace(parentPkg.getNamespace());
// 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());
}
}
@@ -409,6 +406,36 @@ public class ClasspathPackageProvider implements PackageProvider {
pkgConfig.addActionConfig(actionName, actionConfig);
}
/**
* 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.
*
@@ -23,11 +23,14 @@ package org.apache.struts2.config;
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)
@Target(ElementType.TYPE)
public @interface Namespace {
String value();
}
@@ -23,11 +23,14 @@ package org.apache.struts2.config;
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)
@Target({ElementType.TYPE, ElementType.PACKAGE})
public @interface ParentPackage {
String value();
String[] value();
}
@@ -23,11 +23,14 @@ package org.apache.struts2.config;
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)
@Target(ElementType.TYPE)
public @interface Results {
Result[] value();
}