📄 xmlconfigurationprovider.java
字号:
/* * Copyright (c) 2002-2006 by OpenSymphony * All rights reserved. */package com.opensymphony.xwork2.config.providers;import com.opensymphony.xwork2.util.ClassLoaderUtil;import com.opensymphony.xwork2.util.FileManager;import com.opensymphony.xwork2.util.TextUtils;import com.opensymphony.xwork2.Action;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ObjectFactory;import com.opensymphony.xwork2.XWorkException;import com.opensymphony.xwork2.config.*;import com.opensymphony.xwork2.config.impl.LocatableFactory;import com.opensymphony.xwork2.config.entities.*;import com.opensymphony.xwork2.inject.*;import com.opensymphony.xwork2.util.DomHelper;import com.opensymphony.xwork2.util.location.LocatableProperties;import com.opensymphony.xwork2.util.location.Location;import com.opensymphony.xwork2.util.location.LocationUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.InputSource;import java.io.IOException;import java.io.InputStream;import java.lang.reflect.Modifier;import java.net.URL;import java.util.*;/** * Looks in the classpath for an XML file, "xwork.xml" by default, * and uses it for the XWork configuration. * * @author tmjee * @author Rainer Hermanns * @author Neo * @version $Revision: 1483 $ */public class XmlConfigurationProvider implements ConfigurationProvider { private static final Log LOG = LogFactory.getLog(XmlConfigurationProvider.class); private List<Document> documents; private Set<String> includedFileNames; private String configFileName; private ObjectFactory objectFactory; private Set<String> loadedFileUrls = new HashSet<String>(); private boolean errorIfMissing; private Map<String, String> dtdMappings; private Configuration configuration; public XmlConfigurationProvider() { this("xwork.xml", true); } public XmlConfigurationProvider(String filename) { this(filename, true); } public XmlConfigurationProvider(String filename, boolean errorIfMissing) { this.configFileName = filename; this.errorIfMissing = errorIfMissing; Map<String, String> mappings = new HashMap<String, String>(); mappings.put("-//OpenSymphony Group//XWork 2.0//EN", "xwork-2.0.dtd"); mappings.put("-//OpenSymphony Group//XWork 1.1.1//EN", "xwork-1.1.1.dtd"); mappings.put("-//OpenSymphony Group//XWork 1.1//EN", "xwork-1.1.dtd"); mappings.put("-//OpenSymphony Group//XWork 1.0//EN", "xwork-1.0.dtd"); setDtdMappings(mappings); } public void setDtdMappings(Map<String, String> mappings) { this.dtdMappings = Collections.unmodifiableMap(mappings); } @Inject public void setObjectFactory(ObjectFactory objectFactory) { this.objectFactory = objectFactory; } /** * Returns an unmodifiable map of DTD mappings */ public Map<String, String> getDtdMappings() { return dtdMappings; } public void init(Configuration configuration) { this.configuration = configuration; this.includedFileNames = configuration.getLoadedFileNames(); loadDocuments(configFileName); } public void destroy() { } public boolean equals(Object o) { if (this == o) { return true; } if (!(o instanceof XmlConfigurationProvider)) { return false; } final XmlConfigurationProvider xmlConfigurationProvider = (XmlConfigurationProvider) o; if ((configFileName != null) ? (!configFileName.equals(xmlConfigurationProvider.configFileName)) : (xmlConfigurationProvider.configFileName != null)) { return false; } return true; } public int hashCode() { return ((configFileName != null) ? configFileName.hashCode() : 0); } private void loadDocuments(String configFileName) { try { loadedFileUrls.clear(); documents = loadConfigurationFiles(configFileName, null); } catch (ConfigurationException e) { throw e; } catch (Exception e) { throw new ConfigurationException("Error loading configuration file " + configFileName, e); } } public void register(ContainerBuilder containerBuilder, LocatableProperties props) throws ConfigurationException { LOG.info("Parsing configuration file [" + configFileName + "]"); Map<String, Node> loadedBeans = new HashMap<String, Node>(); for (Document doc : documents) { Element rootElement = doc.getDocumentElement(); NodeList children = rootElement.getChildNodes(); int childSize = children.getLength(); for (int i = 0; i < childSize; i++) { Node childNode = children.item(i); if (childNode instanceof Element) { Element child = (Element) childNode; final String nodeName = child.getNodeName(); if (nodeName.equals("bean")) { String type = child.getAttribute("type"); String name = child.getAttribute("name"); String impl = child.getAttribute("class"); String onlyStatic = child.getAttribute("static"); String scopeStr = child.getAttribute("scope"); boolean optional = "true".equals(child.getAttribute("optional")); Scope scope = Scope.SINGLETON; if ("default".equals(scopeStr)) { scope = Scope.DEFAULT; } else if ("request".equals(scopeStr)) { scope = Scope.REQUEST; } else if ("session".equals(scopeStr)) { scope = Scope.SESSION; } else if ("singleton".equals(scopeStr)) { scope = Scope.SINGLETON; } else if ("thread".equals(scopeStr)) { scope = Scope.THREAD; } if (!TextUtils.stringSet(name)) { name = Container.DEFAULT_NAME; } try { Class cimpl = ClassLoaderUtil.loadClass(impl, getClass()); Class ctype = cimpl; if (TextUtils.stringSet(type)) { ctype = ClassLoaderUtil.loadClass(type, getClass()); } if ("true".equals(onlyStatic)) { // Force loading of class to detect no class def found exceptions cimpl.getDeclaredClasses(); containerBuilder.injectStatics(cimpl); } else { if (containerBuilder.contains(ctype, name)) { Location loc = LocationUtils.getLocation(loadedBeans.get(ctype.getName() + name)); throw new ConfigurationException("Bean type " + ctype + " with the name " + name + " has already been loaded by " + loc, child); } // Force loading of class to detect no class def found exceptions cimpl.getDeclaredConstructors(); if (LOG.isDebugEnabled()) { LOG.debug("Loaded type:" + type + " name:" + name + " impl:" + impl); } containerBuilder.factory(ctype, name, new LocatableFactory(name, ctype, cimpl, scope, childNode), scope); } loadedBeans.put(ctype.getName() + name, child); } catch (Throwable ex) { if (!optional) { throw new ConfigurationException("Unable to load bean: type:" + type + " class:" + impl, ex, childNode); } else { LOG.debug("Unable to load optional class: " + ex); } } } else if (nodeName.equals("constant")) { String name = child.getAttribute("name"); String value = child.getAttribute("value"); props.setProperty(name, value, childNode); } } } } } public void loadPackages() throws ConfigurationException { List<Element> reloads = new ArrayList<Element>(); for (Document doc : documents) { Element rootElement = doc.getDocumentElement(); NodeList children = rootElement.getChildNodes(); int childSize = children.getLength(); for (int i = 0; i < childSize; i++) { Node childNode = children.item(i); if (childNode instanceof Element) { Element child = (Element) childNode; final String nodeName = child.getNodeName(); if (nodeName.equals("package")) { PackageConfig cfg = addPackage(child); if (cfg.isNeedsRefresh()) { reloads.add(child); } } } } loadExtraConfiguration(doc); } if (reloads.size() > 0) { reloadRequiredPackages(reloads); } for (Document doc : documents) { loadExtraConfiguration(doc); } documents.clear(); configuration = null; } private void reloadRequiredPackages(List<Element> reloads) { if (reloads.size() > 0) { List<Element> result = new ArrayList<Element>(); for (Element pkg : reloads) { PackageConfig cfg = addPackage(pkg); if (cfg.isNeedsRefresh()) { result.add(pkg); } } if ((result.size() > 0) && (result.size() != reloads.size())) { reloadRequiredPackages(result); return; } // Print out error messages for all misconfigured inheritence packages if (result.size() > 0 ) { for (Element rp : result) { String parent = rp.getAttribute("extends"); if ( parent != null) { List parents = ConfigurationUtil.buildParentsFromString(configuration, parent); if (parents != null && parents.size() <= 0) { LOG.error("Unable to find parent packages " + parent); } } } } } } /** * Tells whether the ConfigurationProvider should reload its configuration. This method should only be called * if ConfigurationManager.isReloadingConfigs() is true. * * @return true if the file has been changed since the last time we read it */ public boolean needsReload() { for (String url : loadedFileUrls) { if (FileManager.fileNeedsReloading(url)) { return true; } } return false;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -