Search in sources :

Example 1 with LocatableProperties

use of com.opensymphony.xwork2.util.location.LocatableProperties 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 LocatableProperties

use of com.opensymphony.xwork2.util.location.LocatableProperties 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 3 with LocatableProperties

use of com.opensymphony.xwork2.util.location.LocatableProperties in project struts by apache.

the class PreResultListenerTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    loadConfigurationProviders(new ConfigurationProvider() {

        Configuration configuration;

        public void destroy() {
        }

        public void init(Configuration config) {
            this.configuration = config;
        }

        public void loadPackages() {
            PackageConfig packageConfig = new PackageConfig.Builder("package").addActionConfig("action", new ActionConfig.Builder("package", "action", SimpleFooAction.class.getName()).build()).build();
            configuration.addPackageConfig("package", packageConfig);
        }

        /**
         * Tells whether the ConfigurationProvider should reload its configuration
         */
        public boolean needsReload() {
            return false;
        }

        public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
            builder.factory(ActionProxyFactory.class, DefaultActionProxyFactory.class);
            builder.factory(ObjectFactory.class);
        }
    });
}
Also used : ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) Configuration(com.opensymphony.xwork2.config.Configuration) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) ConfigurationProvider(com.opensymphony.xwork2.config.ConfigurationProvider) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) PackageConfig(com.opensymphony.xwork2.config.entities.PackageConfig)

Example 4 with LocatableProperties

use of com.opensymphony.xwork2.util.location.LocatableProperties in project struts by apache.

the class OgnlUtilTest method reloadTestContainerConfiguration.

private void reloadTestContainerConfiguration(boolean allowStaticField) {
    loadConfigurationProviders(new StubConfigurationProvider() {

        @Override
        public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
            props.setProperty(StrutsConstants.STRUTS_ALLOW_STATIC_FIELD_ACCESS, "" + allowStaticField);
        }
    });
    ognlUtil = container.getInstance(OgnlUtil.class);
}
Also used : ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) StubConfigurationProvider(com.opensymphony.xwork2.test.StubConfigurationProvider) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException)

Example 5 with LocatableProperties

use of com.opensymphony.xwork2.util.location.LocatableProperties in project struts by apache.

the class StrutsJavaConfigurationProvider method register.

@Override
public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
    Map<String, Object> loadedBeans = new HashMap<>();
    // bean
    List<BeanConfig> beanConfigs = javaConfig.beans();
    if (beanConfigs != null) {
        for (BeanConfig bc : beanConfigs) {
            if (bc != null) {
                registerBean(loadedBeans, builder, bc);
            }
        }
    }
    // constant
    List<ConstantConfig> constantConfigList = javaConfig.constants();
    if (constantConfigList != null) {
        for (ConstantConfig constantConf : constantConfigList) {
            if (constantConf != null) {
                Map<String, String> constantMap = constantConf.getAllAsStringsMap();
                for (Entry<String, String> entr : constantMap.entrySet()) {
                    if (entr.getKey() != null && entr.getValue() != null) {
                        registerConstant(props, entr.getKey(), entr.getValue());
                    }
                }
            }
        }
    }
    // bean-selection
    javaConfig.beanSelection().ifPresent(beanSelectionConfig -> {
        try {
            LOG.debug("Registering bean selection provider {} of type {}", beanSelectionConfig.getName(), beanSelectionConfig.getClazz().getName());
            BeanSelectionProvider provider = beanSelectionConfig.getClazz().newInstance();
            provider.register(builder, props);
        } catch (IllegalAccessException | InstantiationException e) {
            throw new ConfigurationException("Unable to load : name:" + beanSelectionConfig.getName() + " class:" + beanSelectionConfig.getClazz().getName());
        }
    });
    // unknown-handler-stack
    List<String> unknownHandlers = javaConfig.unknownHandlerStack();
    if (unknownHandlers != null) {
        List<UnknownHandlerConfig> unknownHandlerStack = new ArrayList<>();
        for (String unknownHandler : unknownHandlers) {
            Location location = LocationUtils.getLocation(unknownHandler);
            unknownHandlerStack.add(new UnknownHandlerConfig(unknownHandler, location));
        }
        if (!unknownHandlerStack.isEmpty()) {
            configuration.setUnknownHandlerStack(unknownHandlerStack);
        }
    }
}
Also used : BeanConfig(org.apache.struts2.config.entities.BeanConfig) BeanSelectionProvider(com.opensymphony.xwork2.config.BeanSelectionProvider) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UnknownHandlerConfig(com.opensymphony.xwork2.config.entities.UnknownHandlerConfig) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) ConstantConfig(org.apache.struts2.config.entities.ConstantConfig) Location(com.opensymphony.xwork2.util.location.Location)

Aggregations

LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)19 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)18 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)15 StubConfigurationProvider (com.opensymphony.xwork2.test.StubConfigurationProvider)10 Container (com.opensymphony.xwork2.inject.Container)4 Context (com.opensymphony.xwork2.inject.Context)4 Configuration (com.opensymphony.xwork2.config.Configuration)3 ConfigurationProvider (com.opensymphony.xwork2.config.ConfigurationProvider)3 StrutsDefaultConfigurationProvider (com.opensymphony.xwork2.config.providers.StrutsDefaultConfigurationProvider)3 Factory (com.opensymphony.xwork2.inject.Factory)3 ActionContext (com.opensymphony.xwork2.ActionContext)2 ObjectFactory (com.opensymphony.xwork2.ObjectFactory)2 BeanSelectionProvider (com.opensymphony.xwork2.config.BeanSelectionProvider)2 UnknownHandlerConfig (com.opensymphony.xwork2.config.entities.UnknownHandlerConfig)2 ObjectTypeDeterminer (com.opensymphony.xwork2.conversion.ObjectTypeDeterminer)2 MockObjectTypeDeterminer (com.opensymphony.xwork2.mock.MockObjectTypeDeterminer)2 Foo (com.opensymphony.xwork2.util.Foo)2 ValueStack (com.opensymphony.xwork2.util.ValueStack)2 Location (com.opensymphony.xwork2.util.location.Location)2 ArrayList (java.util.ArrayList)2