use of com.opensymphony.xwork2.config.entities.PackageConfig.Builder in project struts by apache.
the class XmlConfigurationProvider method verifyPackageStructure.
private void verifyPackageStructure() {
DirectedGraph<String> graph = new DirectedGraph<>();
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 ("package".equals(nodeName)) {
String packageName = child.getAttribute("name");
declaredPackages.put(packageName, child);
graph.addNode(packageName);
String extendsAttribute = child.getAttribute("extends");
List<String> parents = ConfigurationUtil.buildParentListFromString(extendsAttribute);
for (String parent : parents) {
graph.addNode(parent);
graph.addEdge(packageName, parent);
}
}
}
}
}
CycleDetector<String> detector = new CycleDetector<>(graph);
if (detector.containsCycle()) {
StringBuilder builder = new StringBuilder("The following packages participate in cycles:");
for (String packageName : detector.getVerticesInCycles()) {
builder.append(" ");
builder.append(packageName);
}
throw new ConfigurationException(builder.toString());
}
}
use of com.opensymphony.xwork2.config.entities.PackageConfig.Builder in project struts by apache.
the class XmlConfigurationProvider method buildPackageContext.
/**
* <p>
* This method builds a package context by looking for the parents of this new package.
* </p>
*
* <p>
* If no parents are found, it will return a root package.
* </p>
*
* @param packageElement the package element
*
* @return the package config builder
*/
protected PackageConfig.Builder buildPackageContext(Element packageElement) {
String parent = packageElement.getAttribute("extends");
String abstractVal = packageElement.getAttribute("abstract");
boolean isAbstract = Boolean.parseBoolean(abstractVal);
String name = StringUtils.defaultString(packageElement.getAttribute("name"));
String namespace = StringUtils.defaultString(packageElement.getAttribute("namespace"));
// Strict DMI is enabled by default, it can disabled by user
boolean strictDMI = true;
if (packageElement.hasAttribute("strict-method-invocation")) {
strictDMI = Boolean.parseBoolean(packageElement.getAttribute("strict-method-invocation"));
}
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<>();
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);
}
}
if (parents.size() <= 0) {
cfg.needsRefresh(true);
} else {
cfg.addParents(parents);
}
}
return cfg;
}
use of com.opensymphony.xwork2.config.entities.PackageConfig.Builder in project struts by apache.
the class XWorkTestCaseHelper method loadConfigurationProviders.
public static ConfigurationManager loadConfigurationProviders(ConfigurationManager configurationManager, ConfigurationProvider... providers) {
try {
tearDown(configurationManager);
} catch (Exception e) {
throw new RuntimeException("Cannot clean old configuration", e);
}
configurationManager = new ConfigurationManager(Container.DEFAULT_NAME);
configurationManager.addContainerProvider(new ContainerProvider() {
public void destroy() {
}
public void init(Configuration configuration) throws ConfigurationException {
}
public boolean needsReload() {
return false;
}
public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
builder.setAllowDuplicates(true);
}
});
configurationManager.addContainerProvider(new StrutsDefaultConfigurationProvider());
for (ConfigurationProvider prov : providers) {
if (prov instanceof XmlConfigurationProvider) {
((XmlConfigurationProvider) prov).setThrowExceptionOnDuplicateBeans(false);
}
configurationManager.addContainerProvider(prov);
}
Container container = configurationManager.getConfiguration().getContainer();
// Reset the value stack
ValueStack stack = container.getInstance(ValueStackFactory.class).createValueStack();
stack.getActionContext().withContainer(container).withValueStack(stack).bind();
return configurationManager;
}
use of com.opensymphony.xwork2.config.entities.PackageConfig.Builder in project struts by apache.
the class DomHelper method parse.
/**
* Creates a W3C Document that remembers the location of each element in
* the source file. The location of element nodes can then be retrieved
* using the {@link #getLocationObject(Element)} method.
*
* @param inputSource the inputSource to read the document from
* @param dtdMappings a map of DTD names and public ids
*
* @return the W3C Document
*/
public static Document parse(InputSource inputSource, Map<String, String> dtdMappings) {
SAXParserFactory factory = null;
String parserProp = System.getProperty("xwork.saxParserFactory");
if (parserProp != null) {
try {
ObjectFactory objectFactory = ActionContext.getContext().getContainer().getInstance(ObjectFactory.class);
Class clazz = objectFactory.getClassInstance(parserProp);
factory = (SAXParserFactory) clazz.newInstance();
} catch (Exception e) {
LOG.error("Unable to load saxParserFactory set by system property 'xwork.saxParserFactory': {}", parserProp, e);
}
}
if (factory == null) {
factory = SAXParserFactory.newInstance();
}
factory.setValidating((dtdMappings != null));
factory.setNamespaceAware(true);
SAXParser parser;
try {
parser = factory.newSAXParser();
} catch (Exception ex) {
throw new StrutsException("Unable to create SAX parser", ex);
}
DOMBuilder builder = new DOMBuilder();
// Enhance the sax stream with location information
ContentHandler locationHandler = new LocationAttributes.Pipe(builder);
try {
parser.parse(inputSource, new StartHandler(locationHandler, dtdMappings));
} catch (Exception ex) {
throw new StrutsException(ex);
}
return builder.getDocument();
}
use of com.opensymphony.xwork2.config.entities.PackageConfig.Builder in project struts by apache.
the class AbstractBeanSelectionProvider method alias.
protected void alias(Class type, String key, ContainerBuilder builder, Properties props, Scope scope) {
if (!builder.contains(type, Container.DEFAULT_NAME)) {
String foundName = props.getProperty(key, DEFAULT_BEAN_NAME);
if (builder.contains(type, foundName)) {
LOG.trace("Choosing bean ({}) for ({})", foundName, type.getName());
builder.alias(type, foundName, Container.DEFAULT_NAME);
} else {
try {
Class cls = ClassLoaderUtil.loadClass(foundName, this.getClass());
LOG.trace("Choosing bean ({}) for ({})", cls.getName(), type.getName());
builder.factory(type, cls, scope);
} catch (ClassNotFoundException ex) {
// Perhaps a spring bean id, so we'll delegate to the object factory at runtime
LOG.trace("Choosing bean ({}) for ({}) to be loaded from the ObjectFactory", foundName, type.getName());
if (DEFAULT_BEAN_NAME.equals(foundName)) {
// Probably an optional bean, will ignore
} else {
if (ObjectFactory.class != type) {
builder.factory(type, new ObjectFactoryDelegateFactory(foundName, type), scope);
} else {
throw new ConfigurationException("Cannot locate the chosen ObjectFactory implementation: " + foundName);
}
}
}
}
} else {
LOG.warn("Unable to alias bean type ({}), default mapping already assigned.", type.getName());
}
}
Aggregations