Java Code Examples for org.apache.commons.configuration2.HierarchicalConfiguration#addProperty()

The following examples show how to use org.apache.commons.configuration2.HierarchicalConfiguration#addProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractStateCompositeProcessor.java    From james-project with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() throws Exception {
    List<HierarchicalConfiguration<ImmutableNode>> processorConfs = config.configurationsAt("processor");
    for (HierarchicalConfiguration<ImmutableNode> processorConf : processorConfs) {
        String processorName = processorConf.getString("[@state]");

        // if the "child" processor has no jmx config we just use the one of
        // the composite
        if (!processorConf.containsKey("[@enableJmx]")) {
            processorConf.addProperty("[@enableJmx]", enableJmx);
        }
        processors.put(processorName, createMailProcessor(processorName, processorConf));
    }

    if (enableJmx) {
        this.jmxListener = new JMXStateCompositeProcessorListener(this);
        addListener(jmxListener);
    }

    // check if all needed processors are configured
    checkProcessors();
}
 
Example 2
Source File: TestCombinedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether the entity resolver is initialized with other XML-related
 * properties.
 */
@Test
public void testConfigureEntityResolverWithProperties()
        throws ConfigurationException
{
    final HierarchicalConfiguration<ImmutableNode> config = new BaseHierarchicalConfiguration();
    config.addProperty("header.entity-resolver[@config-class]",
            EntityResolverWithPropertiesTestImpl.class.getName());
    final XMLBuilderParametersImpl xmlParams = new XMLBuilderParametersImpl();
    final FileSystem fs = EasyMock.createMock(FileSystem.class);
    final String baseDir = ConfigurationAssert.OUT_DIR_NAME;
    xmlParams.setBasePath(baseDir);
    xmlParams.setFileSystem(fs);
    builder.configureEntityResolver(config, xmlParams);
    final EntityResolverWithPropertiesTestImpl resolver =
            (EntityResolverWithPropertiesTestImpl) xmlParams
                    .getEntityResolver();
    assertSame("File system not set", fs, resolver.getFileSystem());
    assertSame("Base directory not set", baseDir, resolver.getBaseDir());
}
 
Example 3
Source File: IMAPServerTest.java    From james-project with Apache License 2.0 6 votes vote down vote up
@Test
public void getImapConfigurationShouldReturnSetValue() throws Exception {
    HierarchicalConfiguration<ImmutableNode> configurationBuilder = new BaseHierarchicalConfiguration();
    configurationBuilder.addProperty("enableIdle", "false");
    configurationBuilder.addProperty("idleTimeInterval", "1");
    configurationBuilder.addProperty("idleTimeIntervalUnit", "MINUTES");
    configurationBuilder.addProperty("disabledCaps", "ACL | MOVE");
    ImapConfiguration imapConfiguration = IMAPServer.getImapConfiguration(configurationBuilder);

    ImapConfiguration expectImapConfiguration = ImapConfiguration.builder()
            .enableIdle(false)
            .idleTimeInterval(1)
            .idleTimeIntervalUnit(TimeUnit.MINUTES)
            .disabledCaps(ImmutableSet.of("ACL", "MOVE"))
            .build();

    assertThat(imapConfiguration).isEqualTo(expectImapConfiguration);
}
 
Example 4
Source File: TestBaseConfigurationBuilderProvider.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a configuration object describing a configuration source.
 *
 * @param reload a flag whether reload operations are supported
 * @return the configuration object
 */
private HierarchicalConfiguration<?> setUpConfig(final boolean reload)
{
    final HierarchicalConfiguration<?> config = new BaseHierarchicalConfiguration();
    config.addProperty(CombinedConfigurationBuilder.ATTR_RELOAD,
            Boolean.valueOf(reload));
    config.addProperty("[@throwExceptionOnMissing]", Boolean.TRUE);
    config.addProperty("[@path]",
            ConfigurationAssert.getTestFile("test.properties")
                    .getAbsolutePath());
    config.addProperty("listDelimiterHandler[@config-class]",
            DefaultListDelimiterHandler.class.getName());
    config.addProperty(
            "listDelimiterHandler.config-constrarg[@config-value]", ";");
    return config;
}
 
Example 5
Source File: TestHierarchicalConfigurationEvents.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether manipulations of a connected sub configuration trigger correct
 * events.
 */
@Test
public void testSubConfigurationChangedEventConnected()
{
    final HierarchicalConfiguration<ImmutableNode> sub =
            ((BaseHierarchicalConfiguration) config)
                    .configurationAt(EXIST_PROPERTY, true);
    sub.addProperty("newProp", "newValue");
    checkSubnodeEvent(
            listener.nextEvent(ConfigurationEvent.SUBNODE_CHANGED),
            true);
    checkSubnodeEvent(
            listener.nextEvent(ConfigurationEvent.SUBNODE_CHANGED),
            false);
    listener.done();
}
 
Example 6
Source File: ReadOnlyUsersLDAPRepositoryTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void supportVirtualHostingShouldReturnTrueWhenReportedInConfig() throws Exception {
    HierarchicalConfiguration<ImmutableNode> configuration = ldapRepositoryConfiguration(ldapContainer);
    configuration.addProperty(SUPPORTS_VIRTUAL_HOSTING, "true");

    ReadOnlyUsersLDAPRepository usersLDAPRepository = new ReadOnlyUsersLDAPRepository(new SimpleDomainList());
    usersLDAPRepository.configure(configuration);

    assertThat(usersLDAPRepository.supportVirtualHosting()).isTrue();
}
 
Example 7
Source File: TestConfigurationDeclaration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether an invalid value of an optional attribute is detected.
 */
@Test(expected = ConfigurationRuntimeException.class)
public void testConfigurationDeclarationOptionalAttributeInvalid()
{
    final HierarchicalConfiguration<?> factory = new BaseHierarchicalConfiguration();
    factory.addProperty("xml.fileName", "test.xml");
    factory.setProperty("xml[@optional]", "invalid value");
    final ConfigurationDeclaration decl =
            createDeclaration(factory.configurationAt("xml"));
    decl.isOptional();
}
 
Example 8
Source File: TestConfigurationDeclaration.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests access to certain reserved attributes of a
 * ConfigurationDeclaration.
 */
@Test
public void testConfigurationDeclarationGetAttributes()
{
    final HierarchicalConfiguration<?> config = new BaseHierarchicalConfiguration();
    config.addProperty("xml.fileName", "test.xml");
    ConfigurationDeclaration decl =
            createDeclaration(config.configurationAt("xml"));
    assertNull("Found an at attribute", decl.getAt());
    assertFalse("Found an optional attribute", decl.isOptional());
    config.addProperty("xml[@config-at]", "test1");
    decl = createDeclaration(config.configurationAt("xml"));
    assertEquals("Wrong value of at attribute", "test1", decl.getAt());
    config.addProperty("xml[@at]", "test2");
    decl = createDeclaration(config.configurationAt("xml"));
    assertEquals("Wrong value of config-at attribute", "test1",
            decl.getAt());
    config.clearProperty("xml[@config-at]");
    decl = createDeclaration(config.configurationAt("xml"));
    assertEquals("Old at attribute not detected", "test2", decl.getAt());
    config.addProperty("xml[@config-optional]", "true");
    decl = createDeclaration(config.configurationAt("xml"));
    assertTrue("Wrong value of optional attribute", decl.isOptional());
    config.addProperty("xml[@optional]", "false");
    decl = createDeclaration(config.configurationAt("xml"));
    assertTrue("Wrong value of config-optional attribute",
            decl.isOptional());
    config.clearProperty("xml[@config-optional]");
    config.setProperty("xml[@optional]", Boolean.TRUE);
    decl = createDeclaration(config.configurationAt("xml"));
    assertTrue("Old optional attribute not detected", decl.isOptional());
}
 
Example 9
Source File: TestBaseConfigurationBuilderProvider.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Helper method for testing whether the builder's allowFailOnInit flag is
 * set correctly.
 *
 * @param expFlag the expected flag value
 * @param props the properties to set in the configuration for the
 *        declaration
 * @throws ConfigurationException if an error occurs
 */
private void checkAllowFailOnInit(final boolean expFlag, final String... props)
        throws ConfigurationException
{
    final HierarchicalConfiguration<?> declConfig = setUpConfig(false);
    for (final String key : props)
    {
        declConfig.addProperty(key, Boolean.TRUE);
    }
    final ConfigurationDeclaration decl = createDeclaration(declConfig);
    final BasicConfigurationBuilder<? extends Configuration> builder =
            (BasicConfigurationBuilder<? extends Configuration>) createProvider()
                    .getConfigurationBuilder(decl);
    assertEquals("Wrong flag value", expFlag, builder.isAllowFailOnInit());
}
 
Example 10
Source File: TestHierarchicalConfigurationEvents.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that no events are generated for a disconnected sub configuration.
 */
@Test
public void testSubConfigurationChangedEventNotConnected()
{
    final HierarchicalConfiguration<ImmutableNode> sub =
            ((BaseHierarchicalConfiguration) config)
                    .configurationAt(EXIST_PROPERTY);
    sub.addProperty("newProp", "newValue");
    listener.done();
}
 
Example 11
Source File: PreDeletionHookConfigurationTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void fromShouldReturnValueWithCorrectClassName() throws ConfigurationException {
    HierarchicalConfiguration<ImmutableNode> configuration = new BaseHierarchicalConfiguration();
    String className = "a.class";
    configuration.addProperty("class", className);

    assertThat(PreDeletionHookConfiguration.from(configuration))
        .isEqualTo(PreDeletionHookConfiguration.forClass(className));
}
 
Example 12
Source File: PreDeletionHookConfigurationTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void fromShouldThrowWhenClassNameIsEmpty() {
    HierarchicalConfiguration<ImmutableNode> configuration = new BaseHierarchicalConfiguration();
    configuration.addProperty("class", "");

    assertThatThrownBy(() -> PreDeletionHookConfiguration.from(configuration))
        .isInstanceOf(ConfigurationException.class);
}
 
Example 13
Source File: ReadOnlyUsersLDAPRepositoryTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void configureShouldThrowOnNonBooleanValueForSupportsVirtualHosting() {
    HierarchicalConfiguration<ImmutableNode> configuration = ldapRepositoryConfiguration(ldapContainer);
    configuration.addProperty(SUPPORTS_VIRTUAL_HOSTING, "bad");

    ReadOnlyUsersLDAPRepository usersLDAPRepository = new ReadOnlyUsersLDAPRepository(new SimpleDomainList());

    assertThatThrownBy(() -> usersLDAPRepository.configure(configuration))
        .isInstanceOf(ConversionException.class);
}
 
Example 14
Source File: ReadOnlyUsersLDAPRepositoryTest.java    From james-project with Apache License 2.0 5 votes vote down vote up
@Test
void supportVirtualHostingShouldReturnFalseWhenReportedInConfig() throws Exception {
    HierarchicalConfiguration<ImmutableNode> configuration = ldapRepositoryConfiguration(ldapContainer);
    configuration.addProperty(SUPPORTS_VIRTUAL_HOSTING, "false");

    ReadOnlyUsersLDAPRepository usersLDAPRepository = new ReadOnlyUsersLDAPRepository(new SimpleDomainList());
    usersLDAPRepository.configure(configuration);

    assertThat(usersLDAPRepository.supportVirtualHosting()).isFalse();
}
 
Example 15
Source File: MailRepositoryStoreBeanFactory.java    From james-project with Apache License 2.0 4 votes vote down vote up
/**
 * This method accept a Configuration object as hint and return the
 * corresponding MailRepository. The Configuration must be in the form of:
 * <p/>
 * <pre>
 * &lt;repository destinationURL="[URL of this mail repository]"
 *             type="[repository type ex. OBJECT or STREAM or MAIL etc.]"
 *             model="[repository model ex. PERSISTENT or CACHE etc.]"&gt;
 *   [addition configuration]
 * &lt;/repository&gt;
 * </pre>
 *
 * @param destination the destinationURL used to look up the repository
 * @return the selected repository
 * @throws MailRepositoryStoreException if any error occurs while parsing the Configuration or
 *                                      retrieving the MailRepository
 */
@Override
@SuppressWarnings("deprecation")
public synchronized MailRepository select(MailRepositoryUrl destination) throws MailRepositoryStoreException {
    MailRepository reply = repositories.get(destination);
    if (reply != null) {
        LOGGER.debug("obtained repository: {},{}", destination, reply.getClass());
        return reply;
    } else {
        String repClass = classes.get(destination.getProtocol());
        LOGGER.debug("obtained repository: {} to handle: {}", repClass, destination.getProtocol().getValue());

        // If default values have been set, create a new repository
        // configuration element using the default values
        // and the values in the selector.
        // If no default values, just use the selector.
        final CombinedConfiguration config = new CombinedConfiguration();
        HierarchicalConfiguration<ImmutableNode> defConf = defaultConfigs.get(destination.getProtocol());
        if (defConf != null) {
            config.addConfiguration(defConf);
        }
        HierarchicalConfiguration<ImmutableNode> builder = new BaseHierarchicalConfiguration();
        builder.addProperty("[@destinationURL]", destination.asString());
        config.addConfiguration(builder);

        try {
            // Use the classloader which is used for bean instance stuff
            @SuppressWarnings("unchecked")
            Class<MailRepository> clazz = (Class<MailRepository>) getBeanFactory().getBeanClassLoader().loadClass(repClass);
            reply = (MailRepository) getBeanFactory().autowire(clazz, ConfigurableListableBeanFactory.AUTOWIRE_AUTODETECT, false);

            if (reply instanceof Configurable) {
                ((Configurable) reply).configure(config);
            }

            reply = (MailRepository) getBeanFactory().initializeBean(reply, destination.getProtocol().getValue());

            repositories.put(destination, reply);
            LOGGER.info("added repository: {}->{}", defConf, repClass);
            return reply;
        } catch (Exception e) {
            LOGGER.warn("Exception while creating repository: {}", e.getMessage(), e);
            throw new UnsupportedRepositoryStoreException("Cannot find or init repository", e);
        }
    }

}
 
Example 16
Source File: TestXMLBeanDeclaration.java    From commons-configuration with Apache License 2.0 3 votes vote down vote up
/**
 * Initializes a configuration object with a bean declaration. Under the
 * specified key the given properties will be added.
 *
 * @param config the configuration to initialize
 * @param key the key of the bean declaration
 * @param names an array with the names of the properties
 * @param values an array with the corresponding values
 */
private static void setupBeanDeclaration(final HierarchicalConfiguration<?> config,
        final String key, final String[] names, final String[] values)
{
    for (int i = 0; i < names.length; i++)
    {
        config.addProperty(key + "[@" + names[i] + "]", values[i]);
    }
}
 
Example 17
Source File: ProtocolHandlerChainImpl.java    From james-project with Apache License 2.0 2 votes vote down vote up
/**
 * Return a DefaultConfiguration build on the given command name and
 * classname.
 *
 * @param className The class name
 * @return DefaultConfiguration
 */
private HierarchicalConfiguration<ImmutableNode> addHandler(String className) {
    HierarchicalConfiguration<ImmutableNode> hConf = new BaseHierarchicalConfiguration();
    hConf.addProperty("[@class]", className);
    return hConf;
}