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

The following examples show how to use org.apache.commons.configuration2.builder.ConfigurationBuilderEvent. 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: CombinedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a configuration builder based on a source declaration in the
 * definition configuration.
 *
 * @param decl the current {@code ConfigurationDeclaration}
 * @return the newly created builder
 * @throws ConfigurationException if an error occurs
 */
private ConfigurationBuilder<? extends Configuration> createConfigurationBuilder(
        final ConfigurationDeclaration decl) throws ConfigurationException
{
    final ConfigurationBuilderProvider provider =
            providerForTag(decl.getConfiguration().getRootElementName());
    if (provider == null)
    {
        throw new ConfigurationException(
                "Unsupported configuration source: "
                        + decl.getConfiguration().getRootElementName());
    }

    final ConfigurationBuilder<? extends Configuration> builder =
            provider.getConfigurationBuilder(decl);
    if (decl.getName() != null)
    {
        namedBuilders.put(decl.getName(), builder);
    }
    allBuilders.add(builder);
    builder.addEventListener(ConfigurationBuilderEvent.RESET,
            changeListener);
    return builder;
}
 
Example #3
Source File: MultiFileConfigurationBuilder.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new {@code ConfigurationBuilderEvent} based on the passed in
 * event, but with the source changed to this builder. This method is called
 * when an event was received from a managed builder. In this case, the
 * event has to be passed to the builder listeners registered at this
 * object, but with the correct source property.
 *
 * @param event the event received from a managed builder
 * @return the event to be propagated
 */
private ConfigurationBuilderEvent createEventWithChangedSource(
        final ConfigurationBuilderEvent event)
{
    if (ConfigurationBuilderResultCreatedEvent.RESULT_CREATED.equals(event
            .getEventType()))
    {
        return new ConfigurationBuilderResultCreatedEvent(this,
                ConfigurationBuilderResultCreatedEvent.RESULT_CREATED,
                ((ConfigurationBuilderResultCreatedEvent) event)
                        .getConfiguration());
    }
    @SuppressWarnings("unchecked")
    final
    // This is safe due to the constructor of ConfigurationBuilderEvent
    EventType<? extends ConfigurationBuilderEvent> type =
            (EventType<? extends ConfigurationBuilderEvent>) event
                    .getEventType();
    return new ConfigurationBuilderEvent(this, type);
}
 
Example #4
Source File: TestMultiFileConfigurationBuilder.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether builder reset events are handled correctly.
 */
@Test
public void testBuilderListenerReset() throws ConfigurationException
{
    final BuilderEventListenerImpl listener = new BuilderEventListenerImpl();
    final Collection<FileBasedConfigurationBuilder<XMLConfiguration>> managedBuilders =
            new ArrayList<>();
    final MultiFileConfigurationBuilder<XMLConfiguration> builder =
            createBuilderWithAccessToManagedBuilders(managedBuilders);
    switchToConfig(1);
    builder.addEventListener(ConfigurationBuilderEvent.RESET, listener);
    final XMLConfiguration configuration = builder.getConfiguration();
    managedBuilders.iterator().next().resetResult();
    final ConfigurationBuilderEvent event =
            listener.nextEvent(ConfigurationBuilderEvent.RESET);
    assertSame("Wrong event source", builder, event.getSource());
    assertNotSame("Configuration not reset", configuration,
            builder.getConfiguration());
}
 
Example #5
Source File: TestMultiFileConfigurationBuilder.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether listeners at managed builders are removed when the cache is
 * cleared.
 */
@Test
public void testRemoveBuilderListenerOnReset()
        throws ConfigurationException
{
    final BuilderEventListenerImpl listener = new BuilderEventListenerImpl();
    final Collection<FileBasedConfigurationBuilder<XMLConfiguration>> managedBuilders =
            new ArrayList<>();
    final MultiFileConfigurationBuilder<XMLConfiguration> builder =
            createBuilderWithAccessToManagedBuilders(managedBuilders);
    switchToConfig(1);
    builder.addEventListener(ConfigurationBuilderEvent.RESET, listener);
    builder.getConfiguration();
    builder.resetParameters();
    managedBuilders.iterator().next().resetResult();
    listener.assertNoMoreEvents();
}
 
Example #6
Source File: TestMultiFileConfigurationBuilder.java    From commons-configuration with Apache License 2.0 6 votes vote down vote up
/**
 * Tests whether builder events of other types can be received.
 */
@Test
public void testBuilderListenerOtherTypes() throws ConfigurationException
{
    final BuilderEventListenerImpl listener = new BuilderEventListenerImpl();
    final MultiFileConfigurationBuilder<XMLConfiguration> builder =
            createTestBuilder(null);
    builder.addEventListener(ConfigurationBuilderEvent.ANY, listener);
    switchToConfig(1);
    builder.getConfiguration();
    final ConfigurationBuilderEvent event =
            listener.nextEvent(ConfigurationBuilderEvent.CONFIGURATION_REQUEST);
    assertEquals("Wrong event source of request event", builder,
            event.getSource());
    final ConfigurationBuilderResultCreatedEvent createdEvent =
            listener.nextEvent(ConfigurationBuilderResultCreatedEvent.RESULT_CREATED);
    assertEquals("Wrong source of creation event", builder,
            createdEvent.getSource());
    listener.assertNoMoreEvents();
}
 
Example #7
Source File: CombinedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Adds a listener at the given definition builder which resets this builder
 * when a reset of the definition builder happens. This way it is ensured
 * that this builder produces a new combined configuration when its
 * definition configuration changes.
 *
 * @param defBuilder the definition builder
 */
private void addDefinitionBuilderChangeListener(
        final ConfigurationBuilder<? extends HierarchicalConfiguration<?>> defBuilder)
{
    defBuilder.addEventListener(ConfigurationBuilderEvent.RESET,
            event -> {
                synchronized (CombinedConfigurationBuilder.this)
                {
                    reset();
                    definitionBuilder = defBuilder;
                }
            });
}
 
Example #8
Source File: CombinedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Frees resources used by this object and performs clean up. This
 * method is called when the owning builder is reset.
 */
public void cleanUp()
{
    for (final ConfigurationBuilder<?> b : getChildBuilders())
    {
        b.removeEventListener(ConfigurationBuilderEvent.RESET,
                changeListener);
    }
    namedBuilders.clear();
}
 
Example #9
Source File: MultiFileConfigurationBuilder.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc} This implementation clears the cache with all managed
 * builders.
 */
@Override
public synchronized void resetParameters()
{
    for (final FileBasedConfigurationBuilder<T> b : getManagedBuilders().values())
    {
        b.removeEventListener(ConfigurationBuilderEvent.ANY,
                managedBuilderDelegationListener);
    }
    getManagedBuilders().clear();
    interpolator.set(null);
    super.resetParameters();
}
 
Example #10
Source File: MultiFileConfigurationBuilder.java    From commons-configuration with Apache License 2.0 5 votes vote down vote up
/**
 * Handles events received from managed configuration builders. This method
 * creates a new event with a source pointing to this builder and propagates
 * it to all registered listeners.
 *
 * @param event the event received from a managed builder
 */
private void handleManagedBuilderEvent(final ConfigurationBuilderEvent event)
{
    if (ConfigurationBuilderEvent.RESET.equals(event.getEventType()))
    {
        resetResult();
    }
    else
    {
        fireBuilderEvent(createEventWithChangedSource(event));
    }
}
 
Example #11
Source File: CombinedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a listener for builder change events. This listener is
 * registered at all builders for child configurations.
 */
private EventListener<ConfigurationBuilderEvent> createBuilderChangeListener()
{
    return event -> resetResult();
}
 
Example #12
Source File: TestCombinedConfigurationBuilder.java    From commons-configuration with Apache License 2.0 4 votes vote down vote up
/**
 * Tests whether reloading support works for MultiFileConfigurationBuilder.
 */
@Test
public void testMultiTenentConfigurationReloading()
        throws ConfigurationException, InterruptedException
{
    final CombinedConfiguration config =
            createMultiFileConfig("testCCMultiTenentReloading.xml");
    final File outFile =
            ConfigurationAssert.getOutFile("MultiFileReloadingTest.xml");
    switchToMultiFile(outFile.getAbsolutePath());
    final XMLConfiguration reloadConfig = new XMLConfiguration();
    final FileHandler handler = new FileHandler(reloadConfig);
    handler.setFile(outFile);
    final String key = "test.reload";
    reloadConfig.setProperty(key, "no");
    handler.save();
    try
    {
        assertEquals("Wrong property", "no", config.getString(key));
        final ConfigurationBuilder<? extends Configuration> childBuilder =
                builder.getNamedBuilder("clientConfig");
        assertTrue("Not a reloading builder",
                childBuilder instanceof ReloadingControllerSupport);
        final ReloadingController ctrl =
                ((ReloadingControllerSupport) childBuilder)
                        .getReloadingController();
        ctrl.checkForReloading(null); // initialize reloading
        final BuilderEventListenerImpl l = new BuilderEventListenerImpl();
        childBuilder.addEventListener(ConfigurationBuilderEvent.RESET, l);
        reloadConfig.setProperty(key, "yes");
        handler.save();

        int attempts = 10;
        boolean changeDetected;
        do
        {
            changeDetected = ctrl.checkForReloading(null);
            if (!changeDetected)
            {
                Thread.sleep(1000);
                handler.save(outFile);
            }
        } while (!changeDetected && --attempts > 0);
        assertTrue("No change detected", changeDetected);
        assertEquals("Wrong updated property", "yes", builder
                .getConfiguration().getString(key));
        final ConfigurationBuilderEvent event = l.nextEvent(ConfigurationBuilderEvent.RESET);
        l.assertNoMoreEvents();
        final BasicConfigurationBuilder<? extends Configuration> multiBuilder =
                (BasicConfigurationBuilder<? extends Configuration>) event.getSource();
        childBuilder.removeEventListener(ConfigurationBuilderEvent.RESET, l);
        multiBuilder.resetResult();
        l.assertNoMoreEvents();
    }
    finally
    {
        assertTrue("Output file could not be deleted", outFile.delete());
    }
}
 
Example #13
Source File: MultiFileConfigurationBuilder.java    From commons-configuration with Apache License 2.0 3 votes vote down vote up
/**
 * Registers event listeners at the passed in newly created managed builder.
 * This method registers a special {@code EventListener} which propagates
 * builder events to listeners registered at this builder. In addition,
 * {@code ConfigurationListener} and {@code ConfigurationErrorListener}
 * objects are registered at the new builder.
 *
 * @param newBuilder the builder to be initialized
 */
private void initListeners(final FileBasedConfigurationBuilder<T> newBuilder)
{
    copyEventListeners(newBuilder, configurationListeners);
    newBuilder.addEventListener(ConfigurationBuilderEvent.ANY,
            managedBuilderDelegationListener);
}
 
Example #14
Source File: MultiFileConfigurationBuilder.java    From commons-configuration with Apache License 2.0 2 votes vote down vote up
/**
 * Checks whether the given event type is of interest for the managed
 * configuration builders. This method is called by the methods for managing
 * event listeners to find out whether a listener should be passed to the
 * managed builders, too.
 *
 * @param eventType the event type object
 * @return a flag whether this event type is of interest for managed
 *         builders
 */
private static boolean isEventTypeForManagedBuilders(final EventType<?> eventType)
{
    return !EventType
            .isInstanceOf(eventType, ConfigurationBuilderEvent.ANY);
}