/*
* 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 LocatableFac
评论2