Search in sources :

Example 1 with LocatableFactory

use of com.opensymphony.xwork2.config.impl.LocatableFactory in project struts by apache.

the class XmlConfigurationProvider method register.

public void register(ContainerBuilder containerBuilder, LocatableProperties props) throws ConfigurationException {
    LOG.trace("Parsing configuration file [{}]", configFileName);
    Map<String, Node> loadedBeans = new HashMap<>();
    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 ("bean-selection".equals(nodeName)) {
                    String name = child.getAttribute("name");
                    String impl = child.getAttribute("class");
                    try {
                        Class classImpl = ClassLoaderUtil.loadClass(impl, getClass());
                        if (BeanSelectionProvider.class.isAssignableFrom(classImpl)) {
                            BeanSelectionProvider provider = (BeanSelectionProvider) classImpl.newInstance();
                            provider.register(containerBuilder, props);
                        } else {
                            throw new ConfigurationException("The bean-provider: name:" + name + " class:" + impl + " does not implement " + BeanSelectionProvider.class.getName(), childNode);
                        }
                    } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
                        throw new ConfigurationException("Unable to load bean-provider: name:" + name + " class:" + impl, e, childNode);
                    }
                } else if ("bean".equals(nodeName)) {
                    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;
                    if ("prototype".equals(scopeStr)) {
                        scope = Scope.PROTOTYPE;
                    } 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;
                    } else {
                        scope = Scope.SINGLETON;
                    }
                    if (StringUtils.isEmpty(name)) {
                        name = Container.DEFAULT_NAME;
                    }
                    try {
                        Class classImpl = ClassLoaderUtil.loadClass(impl, getClass());
                        Class classType = classImpl;
                        if (StringUtils.isNotEmpty(type)) {
                            classType = ClassLoaderUtil.loadClass(type, getClass());
                        }
                        if ("true".equals(onlyStatic)) {
                            // Force loading of class to detect no class def found exceptions
                            classImpl.getDeclaredClasses();
                            containerBuilder.injectStatics(classImpl);
                        } else {
                            if (containerBuilder.contains(classType, name)) {
                                Location loc = LocationUtils.getLocation(loadedBeans.get(classType.getName() + name));
                                if (throwExceptionOnDuplicateBeans) {
                                    throw new ConfigurationException("Bean type " + classType + " with the name " + name + " has already been loaded by " + loc, child);
                                }
                            }
                            // Force loading of class to detect no class def found exceptions
                            classImpl.getDeclaredConstructors();
                            LOG.debug("Loaded type: {} name: {} impl: {}", type, name, impl);
                            containerBuilder.factory(classType, name, new LocatableFactory(name, classType, classImpl, scope, childNode), scope);
                        }
                        loadedBeans.put(classType.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: {}", impl);
                        }
                    }
                } else if ("constant".equals(nodeName)) {
                    String name = child.getAttribute("name");
                    String value = child.getAttribute("value");
                    if (valueSubstitutor != null) {
                        LOG.debug("Substituting value [{}] using [{}]", value, valueSubstitutor.getClass().getName());
                        value = valueSubstitutor.substitute(value);
                    }
                    props.setProperty(name, value, childNode);
                } else if (nodeName.equals("unknown-handler-stack")) {
                    List<UnknownHandlerConfig> unknownHandlerStack = new ArrayList<UnknownHandlerConfig>();
                    NodeList unknownHandlers = child.getElementsByTagName("unknown-handler-ref");
                    int unknownHandlersSize = unknownHandlers.getLength();
                    for (int k = 0; k < unknownHandlersSize; k++) {
                        Element unknownHandler = (Element) unknownHandlers.item(k);
                        Location location = LocationUtils.getLocation(unknownHandler);
                        unknownHandlerStack.add(new UnknownHandlerConfig(unknownHandler.getAttribute("name"), location));
                    }
                    if (!unknownHandlerStack.isEmpty())
                        configuration.setUnknownHandlerStack(unknownHandlerStack);
                }
            }
        }
    }
}
Also used : BeanSelectionProvider(com.opensymphony.xwork2.config.BeanSelectionProvider) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LocatableFactory(com.opensymphony.xwork2.config.impl.LocatableFactory) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) UnknownHandlerConfig(com.opensymphony.xwork2.config.entities.UnknownHandlerConfig) Scope(com.opensymphony.xwork2.inject.Scope) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) Location(com.opensymphony.xwork2.util.location.Location)

Example 2 with LocatableFactory

use of com.opensymphony.xwork2.config.impl.LocatableFactory in project struts by apache.

the class StrutsJavaConfigurationProvider method registerBean.

private void registerBean(Map<String, Object> loadedBeans, ContainerBuilder containerBuilder, BeanConfig beanConf) {
    try {
        if (beanConf.isOnlyStatic()) {
            // Force loading of class to detect no class def found
            // exceptions
            beanConf.getClazz().getDeclaredClasses();
            containerBuilder.injectStatics(beanConf.getClazz());
        } else {
            if (containerBuilder.contains(beanConf.getType(), beanConf.getName())) {
                Location loc = LocationUtils.getLocation(loadedBeans.get(beanConf.getType().getName() + beanConf.getName()));
                if (throwExceptionOnDuplicateBeans) {
                    throw new ConfigurationException("Bean type " + beanConf.getType() + " with the name " + beanConf.getName() + " has already been loaded by " + loc, javaConfig);
                }
            }
            // Force loading of class to detect no class def found
            // exceptions
            beanConf.getClazz().getDeclaredConstructors();
            LOG.debug("Loaded type: {} name: {} clazz: {}", beanConf.getType(), beanConf.getName(), beanConf.getClazz());
            containerBuilder.factory(beanConf.getType(), beanConf.getName(), new LocatableFactory(beanConf.getName(), beanConf.getType(), beanConf.getClazz(), beanConf.getScope(), javaConfig), beanConf.getScope());
        }
        loadedBeans.put(beanConf.getType().getName() + beanConf.getName(), javaConfig);
    } catch (Throwable ex) {
        if (!beanConf.isOptional()) {
            throw new ConfigurationException("Unable to load bean: type:" + beanConf.getType() + " class:" + beanConf.getClazz(), ex);
        } else {
            LOG.debug("Unable to load optional class: {}", beanConf.getClazz());
        }
    }
}
Also used : ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) LocatableFactory(com.opensymphony.xwork2.config.impl.LocatableFactory) Location(com.opensymphony.xwork2.util.location.Location)

Aggregations

ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)2 LocatableFactory (com.opensymphony.xwork2.config.impl.LocatableFactory)2 Location (com.opensymphony.xwork2.util.location.Location)2 BeanSelectionProvider (com.opensymphony.xwork2.config.BeanSelectionProvider)1 UnknownHandlerConfig (com.opensymphony.xwork2.config.entities.UnknownHandlerConfig)1 Scope (com.opensymphony.xwork2.inject.Scope)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1