Search in sources :

Example 1 with Builder

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());
    }
}
Also used : Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException)

Example 2 with Builder

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;
}
Also used : ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) ArrayList(java.util.ArrayList) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 3 with Builder

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;
}
Also used : XmlConfigurationProvider(com.opensymphony.xwork2.config.providers.XmlConfigurationProvider) StrutsDefaultConfigurationProvider(com.opensymphony.xwork2.config.providers.StrutsDefaultConfigurationProvider) XmlConfigurationProvider(com.opensymphony.xwork2.config.providers.XmlConfigurationProvider) Container(com.opensymphony.xwork2.inject.Container) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties) StrutsDefaultConfigurationProvider(com.opensymphony.xwork2.config.providers.StrutsDefaultConfigurationProvider)

Example 4 with Builder

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();
}
Also used : StrutsException(org.apache.struts2.StrutsException) ObjectFactory(com.opensymphony.xwork2.ObjectFactory) SAXParser(javax.xml.parsers.SAXParser) StrutsException(org.apache.struts2.StrutsException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 5 with Builder

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());
    }
}
Also used : ObjectFactory(com.opensymphony.xwork2.ObjectFactory) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException)

Aggregations

PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)23 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)21 ServletContext (javax.servlet.ServletContext)21 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)19 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)18 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)17 HashSet (java.util.HashSet)14 StubConfigurationProvider (com.opensymphony.xwork2.test.StubConfigurationProvider)10 NoAnnotationAction (org.apache.struts2.convention.actions.NoAnnotationAction)7 ClassLevelResultPathAction (org.apache.struts2.convention.actions.resultpath.ClassLevelResultPathAction)6 Action (org.apache.struts2.convention.annotation.Action)6 ObjectFactory (com.opensymphony.xwork2.ObjectFactory)4 Configuration (com.opensymphony.xwork2.config.Configuration)4 Container (com.opensymphony.xwork2.inject.Container)4 Context (com.opensymphony.xwork2.inject.Context)4 ArrayList (java.util.ArrayList)4 ActionContext (com.opensymphony.xwork2.ActionContext)3 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)3 ResultTypeConfig (com.opensymphony.xwork2.config.entities.ResultTypeConfig)3 StrutsDefaultConfigurationProvider (com.opensymphony.xwork2.config.providers.StrutsDefaultConfigurationProvider)3