Search in sources :

Example 1 with FileBasedBuilderParameters

use of org.apache.commons.configuration2.builder.fluent.FileBasedBuilderParameters in project nifi by apache.

the class CommonsConfigurationLookupService method onEnabled.

@OnEnabled
public void onEnabled(final ConfigurationContext context) throws InitializationException {
    final String config = context.getProperty(CONFIGURATION_FILE).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);
    }
}
Also used : Parameters(org.apache.commons.configuration2.builder.fluent.Parameters) FileBasedBuilderParameters(org.apache.commons.configuration2.builder.fluent.FileBasedBuilderParameters) ConfigurationException(org.apache.commons.configuration2.ex.ConfigurationException) FileBasedBuilderParameters(org.apache.commons.configuration2.builder.fluent.FileBasedBuilderParameters) ReloadingFileBasedConfigurationBuilder(org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder) InitializationException(org.apache.nifi.reporting.InitializationException) File(java.io.File) ConfigurationBuilderEvent(org.apache.commons.configuration2.builder.ConfigurationBuilderEvent) OnEnabled(org.apache.nifi.annotation.lifecycle.OnEnabled)

Example 2 with FileBasedBuilderParameters

use of org.apache.commons.configuration2.builder.fluent.FileBasedBuilderParameters in project vcell by virtualcell.

the class VCellConfiguration method getConfiguration.

private static synchronized Configuration getConfiguration() throws ConfigurationException {
    if (configurationBuilder == null) {
        Parameters params = new Parameters();
        File propertiesFile = new File(ResourceUtil.getVcellHome(), "vcellconfig.properties");
        if (!propertiesFile.exists()) {
            try {
                propertiesFile.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        FileBasedBuilderParameters thing = params.fileBased().setFile(propertiesFile);
        configurationBuilder = new FileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class).configure(thing);
        configurationBuilder.setAutoSave(true);
        try {
            propertiesConfiguration = configurationBuilder.getConfiguration();
            propertiesConfiguration.setSynchronizer(new ReadWriteSynchronizer());
        } catch (org.apache.commons.configuration2.ex.ConfigurationException e) {
            e.printStackTrace();
            throw new ConfigurationException("failed to create configuration from file " + propertiesFile + ": " + e.getMessage());
        }
    }
    return propertiesConfiguration;
}
Also used : Parameters(org.apache.commons.configuration2.builder.fluent.Parameters) FileBasedBuilderParameters(org.apache.commons.configuration2.builder.fluent.FileBasedBuilderParameters) FileBasedConfigurationBuilder(org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder) ConfigurationException(org.vcell.util.ConfigurationException) FileBasedBuilderParameters(org.apache.commons.configuration2.builder.fluent.FileBasedBuilderParameters) ReadWriteSynchronizer(org.apache.commons.configuration2.sync.ReadWriteSynchronizer) IOException(java.io.IOException) File(java.io.File)

Aggregations

File (java.io.File)2 FileBasedBuilderParameters (org.apache.commons.configuration2.builder.fluent.FileBasedBuilderParameters)2 Parameters (org.apache.commons.configuration2.builder.fluent.Parameters)2 IOException (java.io.IOException)1 ConfigurationBuilderEvent (org.apache.commons.configuration2.builder.ConfigurationBuilderEvent)1 FileBasedConfigurationBuilder (org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder)1 ReloadingFileBasedConfigurationBuilder (org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder)1 ConfigurationException (org.apache.commons.configuration2.ex.ConfigurationException)1 ReadWriteSynchronizer (org.apache.commons.configuration2.sync.ReadWriteSynchronizer)1 OnEnabled (org.apache.nifi.annotation.lifecycle.OnEnabled)1 InitializationException (org.apache.nifi.reporting.InitializationException)1 ConfigurationException (org.vcell.util.ConfigurationException)1