org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder Java Examples

The following examples show how to use org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder. 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: CommonsConfigurationLookupService.java    From nifi with Apache License 2.0 6 votes vote down vote up
@OnEnabled
public void onEnabled(final ConfigurationContext context) throws InitializationException {
    final String config = context.getProperty(CONFIGURATION_FILE).evaluateAttributeExpressions().getValue();
    final FileBasedBuilderParameters params = new Parameters().fileBased().setFile(new File(config));
    this.builder = new ReloadingFileBasedConfigurationBuilder<>(resultClass).configure(params);
    builder.addEventListener(ConfigurationBuilderEvent.CONFIGURATION_REQUEST,
        new EventListener<ConfigurationBuilderEvent>() {
            @Override
            public void onEvent(ConfigurationBuilderEvent event) {
                if (builder.getReloadingController().checkForReloading(null)) {
                    getLogger().debug("Reloading " + config);
                }
            }
        });

    try {
        // Try getting configuration to see if there is any issue, for example wrong file format.
        // Then throw InitializationException to keep this service in 'Enabling' state.
        builder.getConfiguration();
    } catch (ConfigurationException e) {
        throw new InitializationException(e);
    }
}
 
Example #2
Source File: TestReloadingMultiFileConfigurationBuilder.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc} This implementation creates a specialized reloading
 * builder which is associated with a mock reloading controller.
 */
@Override
protected FileBasedConfigurationBuilder<XMLConfiguration> createManagedBuilder(
        final String fileName, final Map<String, Object> params)
        throws ConfigurationException
{
    final ReloadingController ctrl =
            EasyMock.createMock(ReloadingController.class);
    reloadingControllers.add(ctrl);
    return new ReloadingFileBasedConfigurationBuilder<XMLConfiguration>(
            getResultClass(), params)
    {
        @Override
        public ReloadingController getReloadingController()
        {
            return ctrl;
        }
    };
}
 
Example #3
Source File: TestFileExtensionConfigurationBuilderProvider.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether the super class is correctly initialized.
 */
@Test
public void testInitSuper()
{
    final FileExtensionConfigurationBuilderProvider provider =
            new FileExtensionConfigurationBuilderProvider(
                    BasicConfigurationBuilder.class.getName(),
                    ReloadingFileBasedConfigurationBuilder.class.getName(),
                    MATCH_CLASS, DEF_CLASS, EXT, null);
    assertEquals("Wrong builder class",
            BasicConfigurationBuilder.class.getName(),
            provider.getBuilderClass());
    assertEquals("Wrong reloading builder class",
            ReloadingFileBasedConfigurationBuilder.class.getName(),
            provider.getReloadingBuilderClass());
    assertEquals("Wrong configuration class", DEF_CLASS,
            provider.getConfigurationClass());
}
 
Example #4
Source File: FileTokenRepository.java    From TNT4J with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize property configuration based on a configured configuration file name. The method attempts to load it
 * from URL if given config is URL, then load it from class path and then from file system.
 *
 * @throws MalformedURLException
 *             if malformed configuration file name
 */
protected void initConfig() throws MalformedURLException {
	int urlIndex = configName.indexOf("://");

	PropertiesBuilderParameters params = new Parameters().properties();

	if (urlIndex > 0) {
		params.setURL(new URL(configName));
	} else {
		URL configResource = getClass().getResource("/" + configName);
		if (configResource != null) {
			params.setURL(configResource);
		} else {
			params.setFileName(configName);
		}
	}

	if (refDelay > 0) {
		params.setReloadingRefreshDelay(refDelay);
		ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> builder = new ReloadingFileBasedConfigurationBuilder<>(PropertiesConfiguration.class);
		builder.configure(params);
		cfgReloadTrigger = new PeriodicReloadingTrigger(builder.getReloadingController(), null, refDelay, TimeUnit.MILLISECONDS);
		config = builder;
	} else {
		config = new FileBasedConfigurationBuilder<>(PropertiesConfiguration.class);
		config.configure(params);
	}

}
 
Example #5
Source File: ReloadingMultiFileConfigurationBuilder.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc} This implementation returns a file-based configuration
 * builder with reloading support.
 */
@Override
protected FileBasedConfigurationBuilder<T> createManagedBuilder(
        final String fileName, final Map<String, Object> params)
        throws ConfigurationException
{
    return new ReloadingFileBasedConfigurationBuilder<>(getResultClass(),
            params, isAllowFailOnInit());
}
 
Example #6
Source File: ReloadingCombinedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc} This implementation creates a builder for XML
 * configurations with reloading support.
 */
@Override
protected ConfigurationBuilder<? extends HierarchicalConfiguration<?>> createXMLDefinitionBuilder(
        final BuilderParameters builderParams)
{
    return new ReloadingFileBasedConfigurationBuilder<>(
            XMLConfiguration.class).configure(builderParams);
}
 
Example #7
Source File: TestBaseConfigurationBuilderProvider.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a default test instance.
 *
 * @return the test instance
 */
private BaseConfigurationBuilderProvider createProvider()
{
    return new BaseConfigurationBuilderProvider(
            FileBasedConfigurationBuilder.class.getName(),
            ReloadingFileBasedConfigurationBuilder.class.getName(),
            PropertiesConfiguration.class.getName(),
            Arrays.asList(FileBasedBuilderParametersImpl.class.getName()));
}
 
Example #8
Source File: TestBaseConfigurationBuilderProvider.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether a builder with reloading support can be created.
 */
@Test
public void testGetBuilderReloading() throws ConfigurationException
{
    final ConfigurationBuilder<? extends Configuration> builder =
            checkBuilder(true);
    assertEquals("Wrong builder class",
            ReloadingFileBasedConfigurationBuilder.class,
            builder.getClass());
}
 
Example #9
Source File: TestReloadingMultiFileConfigurationBuilder.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether correct managed builders are created.
 */
@Test
public void testCreateManagedBuilder() throws ConfigurationException
{
    final ReloadingMultiFileConfigurationBuilder<XMLConfiguration> builder =
            new ReloadingMultiFileConfigurationBuilder<>(
                    XMLConfiguration.class);
    final FileBasedConfigurationBuilder<XMLConfiguration> managedBuilder =
            builder.createManagedBuilder("test.xml",
                    createTestBuilderParameters(null).getParameters());
    assertTrue(
            "Not a reloading builder",
            managedBuilder instanceof ReloadingFileBasedConfigurationBuilder);
    assertFalse("Wrong flag value", managedBuilder.isAllowFailOnInit());
}
 
Example #10
Source File: TestReloadingCombinedConfigurationBuilderFileBased.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether a change in the definition file is detected and causes a
 * reload if a specific builder for the definition configuration is
 * provided.
 */
@Test
public void testReloadDefinitionFileExplicitBuilder()
        throws ConfigurationException, IOException, InterruptedException
{
    final File defFile = folder.newFile();
    builder.configure(parameters.combined().setDefinitionBuilder(
            new ReloadingFileBasedConfigurationBuilder<>(
                    XMLConfiguration.class).configure(parameters.xml()
                    .setReloadingRefreshDelay(0L).setFile(defFile))));
    checkReloadDefinitionFile(defFile);
}
 
Example #11
Source File: TestCombinedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether a reloading sub builder can be created.
 */
@Test
public void testReloadingBuilder() throws ConfigurationException
{
    final Map<String, Object> attrs = new HashMap<>();
    attrs.put("config-reload", Boolean.TRUE);
    prepareSubBuilderTest(attrs);
    builder.getConfiguration();
    assertTrue(
            "Not a reloading builder",
            builder.getNamedBuilder(BUILDER_NAME) instanceof ReloadingFileBasedConfigurationBuilder);
}