📄 xmlconfigurationprovider.java
字号:
} protected void addAction(Element actionElement, PackageConfig packageContext) throws ConfigurationException { String name = actionElement.getAttribute("name"); String className = actionElement.getAttribute("class"); String methodName = actionElement.getAttribute("method"); Location location = DomHelper.getLocationObject(actionElement); if (location == null) { LOG.warn("location null for " + className); } //methodName should be null if it's not set methodName = (methodName.trim().length() > 0) ? methodName.trim() : null; // if there isnt a class name specified for an <action/> then try to // use the default-class-ref from the <package/> if (!TextUtils.stringSet(className)) { // if there is a package default-class-ref use that, otherwise use action support if (TextUtils.stringSet(packageContext.getDefaultClassRef())) { className = packageContext.getDefaultClassRef(); } else { className = ActionSupport.class.getName(); } } if (!verifyAction(className, name, location)) { return; } Map actionParams = XmlHelper.getParams(actionElement); Map results; try { results = buildResults(actionElement, packageContext); } catch (ConfigurationException e) { throw new ConfigurationException("Error building results for action " + name + " in namespace " + packageContext.getNamespace(), e, actionElement); } List interceptorList = buildInterceptorList(actionElement, packageContext); List exceptionMappings = buildExceptionMappings(actionElement, packageContext); ActionConfig actionConfig = new ActionConfig(methodName, className, packageContext.getName(), actionParams, results, interceptorList, exceptionMappings); actionConfig.setLocation(location); packageContext.addActionConfig(name, actionConfig); if (LOG.isDebugEnabled()) { LOG.debug("Loaded " + (TextUtils.stringSet(packageContext.getNamespace()) ? (packageContext.getNamespace() + "/") : "") + name + " in '" + packageContext.getName() + "' package:" + actionConfig); } } protected boolean verifyAction(String className, String name, Location loc) { if (className.indexOf('{') > -1) { if (LOG.isDebugEnabled()) { LOG.debug("Action class [" + className + "] contains a wildcard " + "replacement value, so it can't be verified"); } return true; } try { Class clazz = objectFactory.getClassInstance(className); if (objectFactory.isNoArgConstructorRequired()) { if (!Modifier.isPublic(clazz.getModifiers())) { throw new ConfigurationException("Action class [" + className + "] is not public", loc); } clazz.getConstructor(new Class[]{}); } } catch (ClassNotFoundException e) { throw new ConfigurationException("Action class [" + className + "] not found", loc); } catch (NoSuchMethodException e) { throw new ConfigurationException("Action class [" + className + "] does not have a public no-arg constructor", e, loc); // Probably not a big deal, like request or session-scoped Spring 2 beans that need a real request } catch (RuntimeException ex) { LOG.info("Unable to verify action class [" + className + "] exists at initialization"); if (LOG.isDebugEnabled()) { LOG.debug("Action verification cause", ex); } // Default to failing fast } catch (Exception ex) { throw new ConfigurationException(ex, loc); } return true; } /** * Create a PackageConfig from an XML element representing it. */ protected PackageConfig addPackage(Element packageElement) throws ConfigurationException { PackageConfig newPackage = buildPackageContext(packageElement); if (newPackage.isNeedsRefresh()) { return newPackage; } if (LOG.isDebugEnabled()) { LOG.debug("Loaded " + newPackage); } // add result types (and default result) to this package addResultTypes(newPackage, packageElement); // load the interceptors and interceptor stacks for this package loadInterceptors(newPackage, packageElement); // load the default interceptor reference for this package loadDefaultInterceptorRef(newPackage, packageElement); // load the default class ref for this package loadDefaultClassRef(newPackage, packageElement); // load the global result list for this package loadGlobalResults(newPackage, packageElement); // load the global exception handler list for this package loadGobalExceptionMappings(newPackage, packageElement); // get actions NodeList actionList = packageElement.getElementsByTagName("action"); for (int i = 0; i < actionList.getLength(); i++) { Element actionElement = (Element) actionList.item(i); addAction(actionElement, newPackage); } // load the default action reference for this package loadDefaultActionRef(newPackage, packageElement); configuration.addPackageConfig(newPackage.getName(), newPackage); return newPackage; } protected void addResultTypes(PackageConfig packageContext, Element element) { NodeList resultTypeList = element.getElementsByTagName("result-type"); for (int i = 0; i < resultTypeList.getLength(); i++) { Element resultTypeElement = (Element) resultTypeList.item(i); String name = resultTypeElement.getAttribute("name"); String className = resultTypeElement.getAttribute("class"); String def = resultTypeElement.getAttribute("default"); Location loc = DomHelper.getLocationObject(resultTypeElement); Class clazz = verifyResultType(className, loc); if (clazz != null) { String paramName = null; try { paramName = (String) clazz.getField("DEFAULT_PARAM").get(null); } catch (Throwable t) { // if we get here, the result type doesn't have a default param defined. } ResultTypeConfig resultType = new ResultTypeConfig(name, className, paramName); resultType.setLocation(DomHelper.getLocationObject(resultTypeElement)); Map params = XmlHelper.getParams(resultTypeElement); if (!params.isEmpty()) { resultType.setParams(params); } packageContext.addResultTypeConfig(resultType); // set the default result type if ("true".equals(def)) { packageContext.setDefaultResultType(name); } } } } protected Class verifyResultType(String className, Location loc) { try { return objectFactory.getClassInstance(className); } catch (ClassNotFoundException e) { LOG.warn("Result class [" + className + "] doesn't exist (ClassNotFoundException) at " + loc.toString() + ", ignoring", e); } catch (NoClassDefFoundError e) { LOG.warn("Result class [" + className + "] doesn't exist (NoClassDefFoundError) at " + loc.toString() + ", ignoring", e); } return null; } protected List buildInterceptorList(Element element, PackageConfig context) throws ConfigurationException { List interceptorList = new ArrayList(); NodeList interceptorRefList = element.getElementsByTagName("interceptor-ref"); for (int i = 0; i < interceptorRefList.getLength(); i++) { Element interceptorRefElement = (Element) interceptorRefList.item(i); if (interceptorRefElement.getParentNode().equals(element) || interceptorRefElement.getParentNode().getNodeName().equals(element.getNodeName())) { List interceptors = lookupInterceptorReference(context, interceptorRefElement); interceptorList.addAll(interceptors); } } return interceptorList; } /** * This method builds a package context by looking for the parents of this new package. * <p/> * If no parents are found, it will return a root package. */ protected PackageConfig buildPackageContext(Element packageElement) { String parent = packageElement.getAttribute("extends"); String abstractVal = packageElement.getAttribute("abstract"); boolean isAbstract = Boolean.valueOf(abstractVal).booleanValue(); String name = TextUtils.noNull(packageElement.getAttribute("name")); String namespace = TextUtils.noNull(packageElement.getAttribute("namespace")); if (TextUtils.stringSet(packageElement.getAttribute("externalReferenceResolver"))) { throw new ConfigurationException("The 'externalReferenceResolver' attribute has been removed. Please use " + "a custom ObjectFactory or Interceptor.", packageElement); } PackageConfig cfg = null; if (!TextUtils.stringSet(TextUtils.noNull(parent))) { // no parents cfg = new PackageConfig(name, namespace, isAbstract); } else { // has parents, let's look it up List parents = ConfigurationUtil.buildParentsFromString(configuration, parent); if (parents.size() <= 0) { cfg = new PackageConfig(name, namespace, isAbstract); cfg.setNeedsRefresh(true); } else { cfg = new PackageConfig(name, namespace, isAbstract, parents); } } if (cfg != null) { cfg.setLocation(DomHelper.getLocationObject(packageElement)); } return cfg; } /** * Build a map of ResultConfig objects from below a given XML element. */ protected Map buildResults(Element element, PackageConfig packageContext) { NodeList resultEls = element.getElementsByTagName("result"); Map results = new LinkedHashMap(); for (int i = 0; i < resultEls.getLength(); i++) { Element resultElement = (Element) resultEls.item(i); if (resultElement.getParentNode().equals(element) || resultElement.getParentNode().getNodeName().equals(element.getNodeName())) { String resultName = resultElement.getAttribute("name"); String resultType = resultElement.getAttribute("type"); // if you don't specify a name on <result/>, it defaults to "success" if (!TextUtils.stringSet(resultName)) { resultName = Action.SUCCESS; } // there is no result type, so let's inherit from the parent package if (!TextUtils.stringSet(resultType)) { resultType = packageContext.getFullDefaultResultType(); // now check if there is a result type now if (!TextUtils.stringSet(resultType)) { // uh-oh, we have a problem throw new ConfigurationException("No result type specified for result named '" + resultName + "', perhaps the parent package does not specify the result type?", resultElement); } } ResultTypeConfig config = packageContext.getAllResultTypeConfigs().get(resultType); if (config == null) { throw new ConfigurationException("There is no result type defined for type '" + resultType + "' mapped with name '" + resultName + "'", resultElement); } String resultClass = config.getClazz(); // invalid result type specified in result definition if (resultClass == null) { throw new ConfigurationException("Result type '" + resultType + "' is invalid"); } Map<String, String> resultParams = XmlHelper.getParams(resultElement); if (resultParams.size() == 0) // maybe we just have a body - therefore a default parameter { // if <result ...>something</result> then we add a parameter of 'something' as this is the most used result param if (resultElement.getChildNodes().getLength() >= 1) { resultParams = new LinkedHashMap(); String paramName = config.getDefaultResultParam(); if (paramName != null) { StringBuffer paramValue = new StringBuffer(); for (int j = 0; j < resultElement.getChildNodes().getLength(); j++) { if (resultElement.getChildNodes().item(j).getNodeType() == Node.TEXT_NODE) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -