Search in sources :

Example 1 with ConstantConfig

use of org.apache.struts2.config.entities.ConstantConfig in project struts by apache.

the class LocationUtilsTest method testGetLocationStrutsJavaConfiguration.

public void testGetLocationStrutsJavaConfiguration() throws Exception {
    StrutsJavaConfiguration conf = new StrutsJavaConfiguration() {

        @Override
        public List<String> unknownHandlerStack() {
            return null;
        }

        @Override
        public List<ConstantConfig> constants() {
            return null;
        }

        @Override
        public List<BeanConfig> beans() {
            return null;
        }

        @Override
        public Optional<BeanSelectionConfig> beanSelection() {
            return Optional.empty();
        }
    };
    Location loc = LocationUtils.getLocation(conf, null);
    assertNotNull(loc);
    assertEquals(conf.toString(), loc.getURI());
}
Also used : BeanConfig(org.apache.struts2.config.entities.BeanConfig) BeanSelectionConfig(org.apache.struts2.config.entities.BeanSelectionConfig) ConstantConfig(org.apache.struts2.config.entities.ConstantConfig) StrutsJavaConfiguration(org.apache.struts2.config.StrutsJavaConfiguration)

Example 2 with ConstantConfig

use of org.apache.struts2.config.entities.ConstantConfig 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)

Example 3 with ConstantConfig

use of org.apache.struts2.config.entities.ConstantConfig in project struts by apache.

the class StrutsJavaConfigurationProviderTest method testRegister.

@Test
public void testRegister() throws Exception {
    final ConstantConfig constantConfig = new ConstantConfig();
    constantConfig.setDevMode(true);
    final String expectedUnknownHandler = "expectedUnknownHandler";
    StrutsJavaConfiguration javaConfig = new StrutsJavaConfiguration() {

        @Override
        public List<String> unknownHandlerStack() {
            return Collections.singletonList(expectedUnknownHandler);
        }

        @Override
        public List<ConstantConfig> constants() {
            return Collections.singletonList(constantConfig);
        }

        @Override
        public List<BeanConfig> beans() {
            return Arrays.asList(new BeanConfig(TestBean.class, "struts"), new BeanConfig(TestBean.class, "struts.static", TestBean.class, Scope.PROTOTYPE, true, true), new BeanConfig(TestBean.class, "struts.test.bean", TestBean.class));
        }

        @Override
        public Optional<BeanSelectionConfig> beanSelection() {
            return Optional.of(new BeanSelectionConfig(TestBeanSelectionProvider.class, "testBeans"));
        }
    };
    StrutsJavaConfigurationProvider provider = new StrutsJavaConfigurationProvider(javaConfig);
    Configuration configuration = new MockConfiguration();
    provider.init(configuration);
    ContainerBuilder builder = new ContainerBuilder();
    LocatableProperties props = new LocatableProperties();
    provider.register(builder, props);
    // constant
    Assert.assertEquals(String.valueOf(constantConfig.getDevMode()), props.get(StrutsConstants.STRUTS_DEVMODE));
    // unknown-handler-stack
    Assert.assertNotNull(configuration.getUnknownHandlerStack());
    Assert.assertEquals(1, configuration.getUnknownHandlerStack().size());
    Assert.assertEquals(expectedUnknownHandler, configuration.getUnknownHandlerStack().get(0).getName());
    // bean
    Container container = builder.create(true);
    TestBean testBean = container.getInstance(TestBean.class);
    Assert.assertNotNull(testBean);
    testBean = container.getInstance(TestBean.class, "struts");
    Assert.assertNotNull(testBean);
    // bean selection
    Set<String> names = container.getInstanceNames(TestBean.class);
    Assert.assertTrue(names.contains("struts"));
    Assert.assertTrue(names.contains("struts.test.bean"));
}
Also used : BeanConfig(org.apache.struts2.config.entities.BeanConfig) MockConfiguration(com.opensymphony.xwork2.config.impl.MockConfiguration) Configuration(com.opensymphony.xwork2.config.Configuration) Container(com.opensymphony.xwork2.inject.Container) ContainerBuilder(com.opensymphony.xwork2.inject.ContainerBuilder) TestBean(com.opensymphony.xwork2.TestBean) LocatableProperties(com.opensymphony.xwork2.util.location.LocatableProperties) MockConfiguration(com.opensymphony.xwork2.config.impl.MockConfiguration) BeanSelectionConfig(org.apache.struts2.config.entities.BeanSelectionConfig) ConstantConfig(org.apache.struts2.config.entities.ConstantConfig) Test(org.junit.Test)

Aggregations

BeanConfig (org.apache.struts2.config.entities.BeanConfig)3 ConstantConfig (org.apache.struts2.config.entities.ConstantConfig)3 BeanSelectionConfig (org.apache.struts2.config.entities.BeanSelectionConfig)2 TestBean (com.opensymphony.xwork2.TestBean)1 BeanSelectionProvider (com.opensymphony.xwork2.config.BeanSelectionProvider)1 Configuration (com.opensymphony.xwork2.config.Configuration)1 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)1 UnknownHandlerConfig (com.opensymphony.xwork2.config.entities.UnknownHandlerConfig)1 MockConfiguration (com.opensymphony.xwork2.config.impl.MockConfiguration)1 Container (com.opensymphony.xwork2.inject.Container)1 ContainerBuilder (com.opensymphony.xwork2.inject.ContainerBuilder)1 LocatableProperties (com.opensymphony.xwork2.util.location.LocatableProperties)1 Location (com.opensymphony.xwork2.util.location.Location)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 StrutsJavaConfiguration (org.apache.struts2.config.StrutsJavaConfiguration)1 Test (org.junit.Test)1