You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by oh...@apache.org on 2013/03/28 17:21:06 UTC
svn commit: r1462198 - in /commons/proper/configuration/trunk/src:
main/java/org/apache/commons/configuration/builder/AutoSaveListener.java
test/java/org/apache/commons/configuration/builder/TestAutoSaveListener.java
Author: oheger
Date: Thu Mar 28 16:21:06 2013
New Revision: 1462198
URL: http://svn.apache.org/r1462198
Log:
Added a new event listener implementation responsible for auto-saving
file-based configurations.
Added:
commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/AutoSaveListener.java
commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestAutoSaveListener.java
Added: commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/AutoSaveListener.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/AutoSaveListener.java?rev=1462198&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/AutoSaveListener.java (added)
+++ commons/proper/configuration/trunk/src/main/java/org/apache/commons/configuration/builder/AutoSaveListener.java Thu Mar 28 16:21:06 2013
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration.builder;
+
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.event.ConfigurationEvent;
+import org.apache.commons.configuration.event.ConfigurationListener;
+import org.apache.commons.configuration.io.FileHandler;
+import org.apache.commons.configuration.io.FileHandlerListenerAdapter;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * <p>
+ * A listener class implementing an auto save mechanism for file-based
+ * configurations.
+ * </p>
+ * <p>
+ * Instances of this class are used by {@link FileBasedConfigurationBuilder} to
+ * save their managed configuration instances when they are changed. Objects are
+ * registered at {@code Configuration} objects as event listeners and thus can
+ * trigger save operations whenever a change event is received.
+ * </p>
+ * <p>
+ * There is one complication however: Some configuration implementations fire
+ * change events during a load operation. Such events must be ignored to prevent
+ * corruption of the source file. This is achieved by monitoring the associated
+ * {@code FileHandler}: during load operations no auto-save is performed.
+ * </p>
+ *
+ * @version $Id: $
+ * @since 2.0
+ */
+class AutoSaveListener extends FileHandlerListenerAdapter implements
+ ConfigurationListener
+{
+ /** The logger. */
+ private final Log log = LogFactory.getLog(getClass());
+
+ /** The associated builder. */
+ private final FileBasedConfigurationBuilder<?> builder;
+
+ /** Stores the file handler monitored by this listener. */
+ private FileHandler handler;
+
+ /**
+ * A counter to keep track whether a load operation is currently in
+ * progress.
+ */
+ private int loading;
+
+ /**
+ * Creates a new instance of {@code AutoSaveListener} and initializes it
+ * with the associated builder.
+ *
+ * @param bldr the associated builder
+ */
+ public AutoSaveListener(FileBasedConfigurationBuilder<?> bldr)
+ {
+ builder = bldr;
+ }
+
+ /**
+ * {@inheritDoc} This implementation checks whether an auto-safe operation
+ * should be performed. This is the case if the event indicates that an
+ * update of the configuration has been performed and currently no load
+ * operation is in progress.
+ */
+ public void configurationChanged(ConfigurationEvent event)
+ {
+ if (autoSaveRequired(event))
+ {
+ try
+ {
+ builder.save();
+ }
+ catch (ConfigurationException ce)
+ {
+ log.warn("Auto save failed!", ce);
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc} This implementation increments the counter for load
+ * operations in progress.
+ */
+ @Override
+ public synchronized void loading(FileHandler handler)
+ {
+ loading++;
+ }
+
+ /**
+ * {@inheritDoc} This implementation decrements the counter for load
+ * operations in progress.
+ */
+ @Override
+ public synchronized void loaded(FileHandler handler)
+ {
+ loading--;
+ }
+
+ /**
+ * Updates the {@code FileHandler}. This method is called by the builder
+ * when a new configuration instance was created which is associated with a
+ * new file handler. It updates the internal file handler reference and
+ * performs necessary listener registrations.
+ *
+ * @param fh the new {@code FileHandler} (can be <b>null</b>)
+ */
+ public synchronized void updateFileHandler(FileHandler fh)
+ {
+ if (handler != null)
+ {
+ handler.removeFileHandlerListener(this);
+ }
+
+ if (fh != null)
+ {
+ fh.addFileHandlerListener(this);
+ }
+ handler = fh;
+ }
+
+ /**
+ * Returns a flag whether a load operation is currently in progress.
+ *
+ * @return a flag whether a load operation is in progress
+ */
+ private synchronized boolean inLoadOperation()
+ {
+ return loading > 0;
+ }
+
+ /**
+ * Checks whether an auto save operation has to be performed based on the
+ * passed in event and the current state of this object.
+ *
+ * @param event the configuration change event
+ * @return <b>true</b> if a save operation should be performed, <b>false</b>
+ * otherwise
+ */
+ private boolean autoSaveRequired(ConfigurationEvent event)
+ {
+ return !event.isBeforeUpdate() && !inLoadOperation();
+ }
+}
Added: commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestAutoSaveListener.java
URL: http://svn.apache.org/viewvc/commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestAutoSaveListener.java?rev=1462198&view=auto
==============================================================================
--- commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestAutoSaveListener.java (added)
+++ commons/proper/configuration/trunk/src/test/java/org/apache/commons/configuration/builder/TestAutoSaveListener.java Thu Mar 28 16:21:06 2013
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.configuration.builder;
+
+import org.apache.commons.configuration.AbstractConfiguration;
+import org.apache.commons.configuration.ConfigurationException;
+import org.apache.commons.configuration.event.ConfigurationEvent;
+import org.apache.commons.configuration.io.FileHandler;
+import org.easymock.EasyMock;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test class for {@code AutoSaveListener}.
+ *
+ * @version $Id: $
+ */
+public class TestAutoSaveListener
+{
+ /** A mock for the associated builder. */
+ private FileBasedConfigurationBuilder<?> builder;
+
+ /** The listener to be tested. */
+ private AutoSaveListener listener;
+
+ @Before
+ public void setUp() throws Exception
+ {
+ builder = EasyMock.createMock(FileBasedConfigurationBuilder.class);
+ listener = new AutoSaveListener(builder);
+ }
+
+ /**
+ * Sends a configuration changed notification to the test listener.
+ *
+ * @param before flag whether the event is before the update
+ */
+ private void fireChangeEvent(boolean before)
+ {
+ listener.configurationChanged(new ConfigurationEvent(this,
+ AbstractConfiguration.EVENT_ADD_PROPERTY, "someProperty",
+ "someValue", before));
+ }
+
+ /**
+ * Tests whether the file handler can be updated and is correctly
+ * initialized.
+ */
+ @Test
+ public void testUpdateFileHandler()
+ {
+ FileHandler handler = EasyMock.createMock(FileHandler.class);
+ FileHandler handler2 = EasyMock.createMock(FileHandler.class);
+ handler.addFileHandlerListener(listener);
+ handler.removeFileHandlerListener(listener);
+ handler2.addFileHandlerListener(listener);
+ EasyMock.replay(handler, handler2);
+ listener.updateFileHandler(handler);
+ listener.updateFileHandler(handler2);
+ EasyMock.verify(handler, handler2);
+ }
+
+ /**
+ * Tests whether updateFileHandler() can deal with null input. This is used
+ * for removing the listener when it is no longer needed.
+ */
+ @Test
+ public void testUpdateFileHandlerNull()
+ {
+ FileHandler handler = EasyMock.createMock(FileHandler.class);
+ handler.addFileHandlerListener(listener);
+ handler.removeFileHandlerListener(listener);
+ EasyMock.replay(handler);
+ listener.updateFileHandler(handler);
+ listener.updateFileHandler(null);
+ EasyMock.verify(handler);
+ }
+
+ /**
+ * Tests whether a change of the monitored configuration causes a save
+ * operation.
+ */
+ @Test
+ public void testConfigurationChangedAutoSave()
+ throws ConfigurationException
+ {
+ builder.save();
+ EasyMock.replay(builder);
+ fireChangeEvent(false);
+ EasyMock.verify(builder);
+ }
+
+ /**
+ * Tests whether an exception thrown by the builder's save() method is
+ * handled.
+ */
+ @Test
+ public void testConfigurationChangedAutoSaveException()
+ throws ConfigurationException
+ {
+ builder.save();
+ EasyMock.expectLastCall().andThrow(new ConfigurationException());
+ EasyMock.replay(builder);
+ fireChangeEvent(false);
+ EasyMock.verify(builder);
+ }
+
+ /**
+ * Tests whether no auto save is triggered before the change to the
+ * monitored configuration actually happens.
+ */
+ @Test
+ public void testConfigurationChangedBeforeUpdateNoSave()
+ {
+ EasyMock.replay(builder);
+ fireChangeEvent(true);
+ }
+
+ /**
+ * Tests that updated during load operations do not create an auto save.
+ */
+ @Test
+ public void testConfigurationChangedWhileLoading()
+ {
+ EasyMock.replay(builder);
+ listener.loading(new FileHandler());
+ fireChangeEvent(false);
+ }
+
+ /**
+ * Tests that after a load operation changes on the monitored configuration
+ * are detected again.
+ */
+ @Test
+ public void testConfigurationChangedAfterLoading()
+ throws ConfigurationException
+ {
+ builder.save();
+ EasyMock.replay(builder);
+ FileHandler handler = new FileHandler();
+ listener.loading(handler);
+ fireChangeEvent(false);
+ listener.loaded(handler);
+ fireChangeEvent(false);
+ EasyMock.verify(builder);
+ }
+}