@SuppressWarnings("unchecked")
@Override
protected PackageConfig.Builder buildPackageContext(Element packageElement) {
// Struts2的API设计得没法扩展,只有用反射来扩展了……
Configuration configuration;
Map<String, Element> declaredPackages;
try {
Class<?> clazz = XmlConfigurationProvider.class;
Field configurationField = clazz.getDeclaredField("configuration");
configurationField.setAccessible(true);
configuration = (Configuration) configurationField.get(this);
Field declaredPackagesField = clazz
.getDeclaredField("declaredPackages");
declaredPackagesField.setAccessible(true);
declaredPackages = (Map<String, Element>) declaredPackagesField
.get(this);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
String parent = packageElement.getAttribute("extends");
String abstractVal = packageElement.getAttribute("abstract");
boolean isAbstract = Boolean.parseBoolean(abstractVal);
String name = StringUtils.defaultString(packageElement
.getAttribute("name"));
String namespace = handleNamespace(StringUtils
.defaultString(packageElement.getAttribute("namespace")));
String strictDMIVal = StringUtils.defaultString(packageElement
.getAttribute("strict-method-invocation"));
boolean strictDMI = Boolean.parseBoolean(strictDMIVal);
if (StringUtils.isNotEmpty(packageElement
.getAttribute("externalReferenceResolver"))) {
throw new ConfigurationException(
"The 'externalReferenceResolver' attribute has been removed. Please use "
+ "a custom ObjectFactory or Interceptor.",
packageElement);
}
PackageConfig.Builder cfg = new PackageConfig.Builder(name)
.namespace(namespace).isAbstract(isAbstract)
.strictMethodInvocation(strictDMI)
.location(DomHelper.getLocationObject(packageElement));
if (StringUtils.isNotEmpty(StringUtils.defaultString(parent))) { // has
// parents,
// let's
// look
// it
// up
List<PackageConfig> parents = new ArrayList<PackageConfig>();
for (String parentPackageName : ConfigurationUtil
.buildParentListFromString(parent)) {
if (configuration.getPackageConfigNames().contains(
parentPackageName)) {
parents.add(configuration
.getPackageConfig(parentPackageName));
} else if (declaredPackages.containsKey(parentPackageName)) {
if (configuration.getPackageConfig(parentPackageName) == null) {
addPackage(declaredPackages.get(parentPackageName));
}
parents.add(configuration
.getPackageConfig(parentPackageName));
} else {
throw new ConfigurationException(
"Parent package is not defined: "
+ parentPackageName);