org.apache.commons.configuration2.event.EventListener Java Examples
The following examples show how to use
org.apache.commons.configuration2.event.EventListener.
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: MultiFileConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} This implementation ensures that the listener is also added * to managed configuration builders if necessary. Listeners for the builder-related * event types are excluded because otherwise they would be triggered by the * internally used configuration builders. */ @Override public synchronized <E extends Event> void addEventListener( final EventType<E> eventType, final EventListener<? super E> l) { super.addEventListener(eventType, l); if (isEventTypeForManagedBuilders(eventType)) { for (final FileBasedConfigurationBuilder<T> b : getManagedBuilders() .values()) { b.addEventListener(eventType, l); } configurationListeners.addEventListener(eventType, l); } }
Example #2
Source File: TestBaseConfiguration.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Tests whether a cloned configuration is decoupled from its original. */ @Test public void testCloneModify() { final EventListener<ConfigurationEvent> l = new EventListenerTestImpl(config); config.addEventListener(ConfigurationEvent.ANY, l); config.addProperty("original", Boolean.TRUE); final BaseConfiguration config2 = (BaseConfiguration) config.clone(); config2.addProperty("clone", Boolean.TRUE); assertFalse("New key appears in original", config.containsKey("clone")); config2.setProperty("original", Boolean.FALSE); assertTrue("Wrong value of original property", config .getBoolean("original")); assertTrue("Event listener was copied", config2 .getEventListeners(ConfigurationEvent.ANY).isEmpty()); }
Example #3
Source File: TestBasicConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Tests whether configuration listeners can be added. */ @Test public void testAddConfigurationListener() throws ConfigurationException { final EventListener<ConfigurationEvent> l1 = createEventListener(); final EventListener<ConfigurationEvent> l2 = createEventListener(); EasyMock.replay(l1, l2); final BasicConfigurationBuilder<PropertiesConfiguration> builder = new BasicConfigurationBuilder<>( PropertiesConfiguration.class); builder.addEventListener(ConfigurationEvent.ANY, l1); final PropertiesConfiguration config = builder.getConfiguration(); builder.addEventListener(ConfigurationEvent.ANY, l2); final Collection<EventListener<? super ConfigurationEvent>> listeners = config.getEventListeners(ConfigurationEvent.ANY); assertTrue("Listener 1 not registered", listeners.contains(l1)); assertTrue("Listener 2 not registered", listeners.contains(l2)); }
Example #4
Source File: TestReloadingBuilderSupportListener.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Tests that the controller's reloading state is reset when a new result * object is created. */ @SuppressWarnings("unchecked") @Test public void testResetReloadingStateOnResultCreation() throws ConfigurationException { final ReloadingController controller = EasyMock.createMock(ReloadingController.class); controller.addEventListener(EasyMock.eq(ReloadingEvent.ANY), EasyMock.anyObject(EventListener.class)); controller.resetReloadingState(); EasyMock.replay(controller); final BasicConfigurationBuilder<Configuration> builder = new BasicConfigurationBuilder<>( PropertiesConfiguration.class); final ReloadingBuilderSupportListener listener = ReloadingBuilderSupportListener.connect(builder, controller); assertNotNull("No listener returned", listener); builder.getConfiguration(); EasyMock.verify(controller); }
Example #5
Source File: TestReloadingController.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * Tests a reloading check with a positive result. */ @Test public void testCheckForReloadingTrue() { final EventListener<ReloadingEvent> l = createListenerMock(); final EventListener<ReloadingEvent> lRemoved = createListenerMock(); final MutableObject<ReloadingEvent> evRef = new MutableObject<>(); expectEvent(l, evRef); EasyMock.expect(detector.isReloadingRequired()).andReturn(Boolean.TRUE); EasyMock.replay(detector, l, lRemoved); final ReloadingController ctrl = createController(); ctrl.addEventListener(ReloadingEvent.ANY, lRemoved); ctrl.addEventListener(ReloadingEvent.ANY, l); assertTrue("Wrong result", ctrl.removeEventListener(ReloadingEvent.ANY, lRemoved)); final Object testData = "Some test data"; assertTrue("Wrong result", ctrl.checkForReloading(testData)); assertTrue("Not in reloading state", ctrl.isInReloadingState()); assertSame("Wrong event source", ctrl, evRef.getValue().getSource()); assertSame("Wrong controller", ctrl, evRef.getValue().getController()); assertEquals("Wrong event data", testData, evRef.getValue().getData()); EasyMock.verify(l, lRemoved, detector); }
Example #6
Source File: MultiFileConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 6 votes |
/** * {@inheritDoc} This implementation ensures that the listener is also * removed from managed configuration builders if necessary. */ @Override public synchronized <E extends Event> boolean removeEventListener( final EventType<E> eventType, final EventListener<? super E> l) { final boolean result = super.removeEventListener(eventType, l); if (isEventTypeForManagedBuilders(eventType)) { for (final FileBasedConfigurationBuilder<T> b : getManagedBuilders() .values()) { b.removeEventListener(eventType, l); } configurationListeners.removeEventListener(eventType, l); } return result; }
Example #7
Source File: CommonsConfigurationLookupService.java From nifi with Apache License 2.0 | 6 votes |
@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 #8
Source File: FileTokenRepository.java From TNT4J with Apache License 2.0 | 5 votes |
private void addListenersToOpened() { if (isOpen()) { for (Map.Entry<TokenRepositoryListener, EventListener<?>[]> le : LISTEN_MAP.entrySet()) { EventListener<?>[] pListeners = le.getValue(); if (pListeners != null) { config.addEventListener(ConfigurationEvent.ANY, (TokenConfigurationListener) pListeners[0]); config.addEventListener(ConfigurationErrorEvent.ANY, (TokenConfigurationErrorListener) pListeners[1]); } } } }
Example #9
Source File: TestAbstractHierarchicalConfiguration.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tests whether registered event handlers are handled correctly when a * configuration is cloned. They should not be registered at the clone. */ @Test public void testCloneWithEventListeners() { final EventListener<ConfigurationEvent> l = new EventListenerTestImpl(null); config.addEventListener(ConfigurationEvent.ANY, l); final AbstractHierarchicalConfiguration<?> copy = (AbstractHierarchicalConfiguration<?>) config.clone(); assertFalse("Event listener registered at clone", copy .getEventListeners(ConfigurationEvent.ANY).contains(l)); }
Example #10
Source File: TestPropertiesConfigurationLayout.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tests a newly created instance. */ @Test public void testInit() { assertTrue("Object contains keys", layout.getKeys().isEmpty()); assertNull("Header comment not null", layout.getHeaderComment()); final Iterator<EventListener<? super ConfigurationEvent>> it = config.getEventListeners(ConfigurationEvent.ANY).iterator(); assertTrue("No event listener registered", it.hasNext()); assertSame("Layout not registered as event listener", layout, it.next()); assertFalse("Multiple event listeners registered", it.hasNext()); assertFalse("Force single line flag set", layout.isForceSingleLine()); assertNull("Got a global separator", layout.getGlobalSeparator()); }
Example #11
Source File: TestReloadingController.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Creates a mock event listener. * * @return the mock listener */ private static EventListener<ReloadingEvent> createListenerMock() { @SuppressWarnings("unchecked") final EventListener<ReloadingEvent> listener = EasyMock.createMock(EventListener.class); return listener; }
Example #12
Source File: TestReloadingController.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Prepares the given event listener mock to expect an event notification. * The event received is stored in the given mutable object. * * @param l the listener mock * @param evRef the reference where to store the event */ private void expectEvent(final EventListener<ReloadingEvent> l, final MutableObject<ReloadingEvent> evRef) { l.onEvent(EasyMock.anyObject(ReloadingEvent.class)); EasyMock.expectLastCall().andAnswer(() -> { evRef.setValue((ReloadingEvent) EasyMock.getCurrentArguments()[0]); return null; }); }
Example #13
Source File: TestReloadingController.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tests a reloading check with a negative result. */ @Test public void testCheckForReloadingFalse() { final EventListener<ReloadingEvent> l = createListenerMock(); EasyMock.expect(detector.isReloadingRequired()) .andReturn(Boolean.FALSE); EasyMock.replay(detector, l); final ReloadingController ctrl = createController(); ctrl.addEventListener(ReloadingEvent.ANY, l); assertFalse("Wrong result", ctrl.checkForReloading(null)); assertFalse("In reloading state", ctrl.isInReloadingState()); EasyMock.verify(detector, l); }
Example #14
Source File: TestReloadingController.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tests that no further checks are performed when already in reloading * state. */ @Test public void testCheckForReloadingInReloadingState() { final EventListener<ReloadingEvent> l = createListenerMock(); EasyMock.expect(detector.isReloadingRequired()).andReturn(Boolean.TRUE); expectEvent(l, new MutableObject<ReloadingEvent>()); EasyMock.replay(detector, l); final ReloadingController ctrl = createController(); ctrl.addEventListener(ReloadingEvent.ANY, l); assertTrue("Wrong result (1)", ctrl.checkForReloading(1)); assertTrue("Wrong result (2)", ctrl.checkForReloading(2)); EasyMock.verify(detector, l); }
Example #15
Source File: TestConfigurationUtils.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tests asEventSource() if a mock object has to be returned. */ @Test public void testAsEventSourceUnsupportedMock() { @SuppressWarnings("unchecked") final EventListener<ConfigurationEvent> cl = EasyMock.createMock(EventListener.class); EasyMock.replay(cl); final EventSource source = ConfigurationUtils.asEventSource(this, true); source.addEventListener(ConfigurationEvent.ANY, cl); assertFalse("Wrong result (1)", source.removeEventListener(ConfigurationEvent.ANY, cl)); source.addEventListener(ConfigurationEvent.ANY, null); }
Example #16
Source File: TestBuilderConfigurationWrapperFactory.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tests whether EventSource methods can be delegated to the builder. */ @Test public void testEventSourceSupportBuilder() throws ConfigurationException { final BasicConfigurationBuilder<PropertiesConfiguration> builder = new BasicConfigurationBuilder<>( PropertiesConfiguration.class); final EventListener<ConfigurationEvent> l1 = new EventListenerTestImpl(null); final EventListener<ConfigurationEvent> l2 = new EventListenerTestImpl(null); final BuilderConfigurationWrapperFactory factory = new BuilderConfigurationWrapperFactory( EventSourceSupport.BUILDER); final EventSource src = (EventSource) factory.createBuilderConfigurationWrapper( Configuration.class, builder); src.addEventListener(ConfigurationEvent.ANY, l1); src.addEventListener(ConfigurationEvent.ANY_HIERARCHICAL, l2); assertTrue( "Wrong result for existing listener", src.removeEventListener(ConfigurationEvent.ANY_HIERARCHICAL, l2)); assertFalse( "Wrong result for non-existing listener", src.removeEventListener(ConfigurationEvent.ANY_HIERARCHICAL, l2)); final PropertiesConfiguration config = builder.getConfiguration(); final Collection<EventListener<? super ConfigurationEvent>> listeners = config.getEventListeners(ConfigurationEvent.ANY_HIERARCHICAL); assertTrue("Registered listener not found", listeners.contains(l1)); assertFalse("Removed listener still found", listeners.contains(l2)); }
Example #17
Source File: TestCombinedConfiguration.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tests whether the combined configuration removes itself as change * listener from the child configurations on a clear operation. This test is * related to CONFIGURATION-572. */ @Test public void testClearRemoveChildListener() { final AbstractConfiguration child = setUpTestConfiguration(); config.addConfiguration(child); config.clear(); for (final EventListener<?> listener : child .getEventListeners(ConfigurationEvent.ANY)) { assertNotEquals("Still registered", config, listener); } }
Example #18
Source File: TestBasicConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Creates a mock for an event listener. * * @return the event listener mock */ private static EventListener<ConfigurationEvent> createEventListener() { @SuppressWarnings("unchecked") final EventListener<ConfigurationEvent> listener = EasyMock.createMock(EventListener.class); return listener; }
Example #19
Source File: FileTokenRepository.java From TNT4J with Apache License 2.0 | 5 votes |
private void removeListenersFromClosed() { if (isOpen()) { for (Map.Entry<TokenRepositoryListener, EventListener<?>[]> le : LISTEN_MAP.entrySet()) { EventListener<?>[] pListeners = le.getValue(); if (pListeners != null) { config.removeEventListener(ConfigurationEvent.ANY, (TokenConfigurationListener) pListeners[0]); config.removeEventListener(ConfigurationErrorEvent.ANY, (TokenConfigurationErrorListener) pListeners[1]); } } } }
Example #20
Source File: TestBasicConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tests whether configuration listeners can be removed. */ @Test public void testRemoveConfigurationListener() throws ConfigurationException { final EventListener<ConfigurationEvent> l1 = createEventListener(); final EventListener<ConfigurationEvent> l2 = createEventListener(); EasyMock.replay(l1, l2); final BasicConfigurationBuilder<PropertiesConfiguration> builder = new BasicConfigurationBuilder<>( PropertiesConfiguration.class); builder.addEventListener(ConfigurationEvent.ANY_HIERARCHICAL, l1); builder.addEventListener(ConfigurationEvent.ANY, l2); assertTrue("Wrong result", builder.removeEventListener(ConfigurationEvent.ANY, l2)); final PropertiesConfiguration config = builder.getConfiguration(); assertFalse("Removed listener was registered", config .getEventListeners(ConfigurationEvent.ANY).contains(l2)); assertTrue("Listener not registered", config.getEventListeners(ConfigurationEvent.ANY_HIERARCHICAL) .contains(l1)); builder.removeEventListener( ConfigurationEvent.ANY_HIERARCHICAL, l1); assertFalse("Listener still registered", config.getEventListeners(ConfigurationEvent.ANY_HIERARCHICAL) .contains(l1)); }
Example #21
Source File: TestBasicConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tests whether event listeners can be copied to another builder. */ @Test public void testCopyEventListeners() throws ConfigurationException { final EventListener<ConfigurationEvent> l1 = createEventListener(); final EventListener<ConfigurationEvent> l2 = createEventListener(); final EventListener<ConfigurationErrorEvent> l3 = new ErrorListenerTestImpl(null); final BasicConfigurationBuilder<PropertiesConfiguration> builder = new BasicConfigurationBuilder<>( PropertiesConfiguration.class); builder.addEventListener(ConfigurationEvent.ANY, l1); builder.addEventListener(ConfigurationEvent.ANY_HIERARCHICAL, l2); builder.addEventListener(ConfigurationErrorEvent.ANY, l3); final BasicConfigurationBuilder<XMLConfiguration> builder2 = new BasicConfigurationBuilder<>( XMLConfiguration.class); builder.copyEventListeners(builder2); final XMLConfiguration config = builder2.getConfiguration(); Collection<EventListener<? super ConfigurationEvent>> listeners = config.getEventListeners(ConfigurationEvent.ANY); assertEquals("Wrong number of listeners", 1, listeners.size()); assertTrue("Wrong listener", listeners.contains(l1)); listeners = config.getEventListeners(ConfigurationEvent.ANY_HIERARCHICAL); assertEquals("Wrong number of listeners for hierarchical", 2, listeners.size()); assertTrue("Listener 1 not found", listeners.contains(l1)); assertTrue("Listener 2 not found", listeners.contains(l2)); final Collection<EventListener<? super ConfigurationErrorEvent>> errListeners = config.getEventListeners(ConfigurationErrorEvent.ANY); assertEquals("Wrong number of error listeners", 1, errListeners.size()); assertTrue("Wrong error listener", errListeners.contains(l3)); }
Example #22
Source File: TestMultiFileConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tests whether configuration listeners are handled correctly. */ @Test public void testAddConfigurationListener() throws ConfigurationException { final EventListener<ConfigurationEvent> l1 = new EventListenerTestImpl(null); @SuppressWarnings("unchecked") final EventListener<Event> l2 = EasyMock.createMock(EventListener.class); EasyMock.replay(l2); final MultiFileConfigurationBuilder<XMLConfiguration> builder = createTestBuilder(null); builder.addEventListener(ConfigurationEvent.ANY, l1); switchToConfig(1); final XMLConfiguration config = builder.getConfiguration(); assertTrue("Listener not added", config.getEventListeners(ConfigurationEvent.ANY) .contains(l1)); builder.addEventListener(Event.ANY, l2); assertTrue("Listener 2 not added", config.getEventListeners(Event.ANY) .contains(l2)); assertTrue("Wrong result", builder.removeEventListener(Event.ANY, l2)); assertFalse("Wrong result after removal", builder.removeEventListener(Event.ANY, l2)); assertFalse("Listener not removed", config.getEventListeners(Event.ANY) .contains(l2)); switchToConfig(2); final XMLConfiguration config2 = builder.getConfiguration(); assertFalse("Listener not globally removed", config2 .getEventListeners(Event.ANY).contains(l2)); }
Example #23
Source File: TestCombinedConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Tests whether a child configuration builder inherits the event listeners * from its parent. */ @Test public void testConfigurationBuilderProviderInheritEventListeners() throws ConfigurationException { @SuppressWarnings("unchecked") final EventListener<Event> l1 = EasyMock.createNiceMock(EventListener.class); @SuppressWarnings("unchecked") final EventListener<ConfigurationEvent> l2 = EasyMock.createNiceMock(EventListener.class); EasyMock.replay(l1, l2); final File testFile = ConfigurationAssert .getTestFile("testCCCombinedChildBuilder.xml"); builder.configure(new XMLBuilderParametersImpl().setFile(testFile)); builder.addEventListener(Event.ANY, l1); builder.addEventListener(ConfigurationEvent.ANY, l2); final CombinedConfiguration cc = builder.getConfiguration(); final CombinedConfiguration cc2 = (CombinedConfiguration) cc.getConfiguration("subcc"); final Collection<EventListener<? super ConfigurationEvent>> listeners = cc2.getEventListeners(ConfigurationEvent.ANY); assertTrue("Listener 1 not found", listeners.contains(l1)); assertTrue("Listener 2 not found", listeners.contains(l2)); final Collection<EventListener<? super Event>> eventListeners = cc2.getEventListeners(Event.ANY); assertEquals("Wrong number of event listeners", 1, eventListeners.size()); assertTrue("Wrong listener", eventListeners.contains(l1)); }
Example #24
Source File: TestJNDIConfiguration.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Configures the test config to throw an exception. */ private PotentialErrorJNDIConfiguration setUpErrorConfig() { conf.installException(); // remove log error listener to avoid output in tests final Iterator<EventListener<? super ConfigurationErrorEvent>> iterator = conf.getEventListeners(ConfigurationErrorEvent.ANY).iterator(); conf.removeEventListener(ConfigurationErrorEvent.ANY, iterator.next()); return conf; }
Example #25
Source File: FileTokenRepository.java From TNT4J with Apache License 2.0 | 5 votes |
@Override public void removeRepositoryListener(TokenRepositoryListener listener) { if (configName == null || !isOpen()) { return; } EventListener<?>[] pListeners = LISTEN_MAP.get(listener); if (pListeners != null) { LISTEN_MAP.remove(listener); config.removeEventListener(ConfigurationEvent.ANY, (TokenConfigurationListener) pListeners[0]); config.removeEventListener(ConfigurationErrorEvent.ANY, (TokenConfigurationErrorListener) pListeners[1]); } }
Example #26
Source File: TestCombinedConfiguration.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * Checks if a configuration was correctly added to the combined config. * * @param c the config to check */ private void checkAddConfig(final AbstractConfiguration c) { final Collection<EventListener<? super ConfigurationEvent>> listeners = c.getEventListeners(ConfigurationEvent.ANY); assertEquals("Wrong number of configuration listeners", 1, listeners .size()); assertTrue("Combined config is no listener", listeners.contains(config)); }
Example #27
Source File: DynamicCombinedConfiguration.java From commons-configuration with Apache License 2.0 | 5 votes |
@Override public <T extends Event> void addEventListener(final EventType<T> eventType, final EventListener<? super T> listener) { for (final CombinedConfiguration cc : configs.values()) { cc.addEventListener(eventType, listener); } super.addEventListener(eventType, listener); }
Example #28
Source File: FileTokenRepository.java From TNT4J with Apache License 2.0 | 5 votes |
@Override public void addRepositoryListener(TokenRepositoryListener listener) { if (configName == null || !isOpen()) { return; } TokenConfigurationListener cfListener = new TokenConfigurationListener(listener, logger); TokenConfigurationErrorListener cfErrListener = new TokenConfigurationErrorListener(listener, logger); EventListener<?>[] pListeners = new EventListener[2]; pListeners[0] = cfListener; pListeners[1] = cfErrListener; LISTEN_MAP.put(listener, pListeners); config.addEventListener(ConfigurationEvent.ANY, cfListener); config.addEventListener(ConfigurationErrorEvent.ANY, cfErrListener); }
Example #29
Source File: ReloadingController.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} This class generates events of type {@code ReloadingEvent}. */ @Override public <T extends Event> void addEventListener( final EventType<T> eventType, final EventListener<? super T> listener) { listeners.addEventListener(eventType, listener); }
Example #30
Source File: BasicConfigurationBuilder.java From commons-configuration with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} This implementation also takes care that the event listener * is removed from the managed configuration object. */ @Override public <E extends Event> boolean removeEventListener( final EventType<E> eventType, final EventListener<? super E> listener) { fetchEventSource().removeEventListener(eventType, listener); return eventListeners.removeEventListener(eventType, listener); }