diff --git a/spring-integration-core/src/main/java/org/springframework/integration/context/metadata/FileBasedPropertiesStore.java b/spring-integration-core/src/main/java/org/springframework/integration/context/metadata/FileBasedPropertiesStore.java index 434c2e9af1..2f2036a846 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/context/metadata/FileBasedPropertiesStore.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/context/metadata/FileBasedPropertiesStore.java @@ -23,33 +23,37 @@ import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.util.Assert; import org.springframework.util.DefaultPropertiesPersister; /** + * Properties file-based implementation of {@link MetadataStore}. To avoid conflicts + * each instance should be constructed with the unique key from which unique file name + * will be generated. The file name will be 'persistentKey' + ".last.entry". + * Files will be written to the 'java.io.tmpdir' + "/spring-integration/". + * * @author Oleg Zhurakousky * @since 2.0 */ -public class FileBasedPropertiesStore implements MetadataStore { - protected final Log logger = LogFactory.getLog(getClass()); +public class FileBasedPropertiesStore implements MetadataStore, InitializingBean{ + private final Log logger = LogFactory.getLog(getClass()); private final DefaultPropertiesPersister persister = new DefaultPropertiesPersister(); - private final String key; - private File persistentFile; + private final String persistentKey; + private volatile File persistentFile; + private volatile String baseDirectory = System.getProperty("java.io.tmpdir") + "/spring-integration/"; + + public FileBasedPropertiesStore(String persistentKey){ + Assert.notNull(persistentKey, "'persistentKey' must not be null"); + this.persistentKey = persistentKey; + } - public FileBasedPropertiesStore(String key){ - this.key = key; - String dirPath = System.getProperty("java.io.tmpdir") + "/spring-integration/"; - String fileName = this.key + ".last.entry"; - File baseDir = new File(dirPath); - baseDir.mkdirs(); - persistentFile = new File(baseDir, fileName); - try { - if (!persistentFile.exists()){ - persistentFile.createNewFile(); - } - } catch (Exception e) { - e.printStackTrace(); - } - + public void setBaseDirectory(String baseDirectory) { + this.baseDirectory = baseDirectory; + } + + public String getBaseDirectory() { + return baseDirectory; } public void write(Properties metadata) { @@ -98,4 +102,19 @@ public class FileBasedPropertiesStore implements MetadataStore { } return properties; } + + public void afterPropertiesSet() throws Exception { + String fileName = this.persistentKey + ".last.entry"; + File baseDir = new File(baseDirectory); + baseDir.mkdirs(); + persistentFile = new File(baseDir, fileName); + try { + if (!persistentFile.exists()){ + persistentFile.createNewFile(); + } + } catch (Exception e) { + throw new IllegalArgumentException("Failed to create metadata-store file '" + + persistentFile.getAbsolutePath() + "'", e); + } + } } diff --git a/spring-integration-core/src/test/java/org/springframework/integration/context/metadata/FileBasedPropertiesStoreTests.java b/spring-integration-core/src/test/java/org/springframework/integration/context/metadata/FileBasedPropertiesStoreTests.java new file mode 100644 index 0000000000..d557a47ece --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/context/metadata/FileBasedPropertiesStoreTests.java @@ -0,0 +1,70 @@ +/* + * Copyright 2002-2010 the original author or authors. + * + * Licensed 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.springframework.integration.context.metadata; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNotNull; +import static junit.framework.Assert.assertTrue; + +import java.io.File; +import java.util.Properties; + +import org.junit.Test; + +/** + * @author Oleg Zhurakousky + * + */ +public class FileBasedPropertiesStoreTests { + + @Test(expected=IllegalArgumentException.class) + public void validateFailureWithNoPersistentKey(){ + new FileBasedPropertiesStore(null); + } + + @Test + public void validateWithDefaultBaseDir() throws Exception{ + File file = new File(System.getProperty("java.io.tmpdir") + "/spring-integration/" + "foo.last.entry"); + file.delete(); + FileBasedPropertiesStore metaStore = new FileBasedPropertiesStore("foo"); + metaStore.afterPropertiesSet(); + assertTrue(file.exists()); + Properties prop = new Properties(); + prop.setProperty("foo", "bar"); + metaStore.write(prop); + Properties persistentProperties = metaStore.load(); + assertNotNull(persistentProperties); + assertEquals(1, persistentProperties.size()); + assertEquals("bar", persistentProperties.get("foo")); + } + @Test + public void validateWithCustomBaseDir() throws Exception{ + File file = new File("foo/" + "foo.last.entry"); + file.delete(); + file.deleteOnExit(); + FileBasedPropertiesStore metaStore = new FileBasedPropertiesStore("foo"); + metaStore.setBaseDirectory("foo"); + metaStore.afterPropertiesSet(); + assertTrue(file.exists()); + Properties prop = new Properties(); + prop.setProperty("foo", "bar"); + metaStore.write(prop); + Properties persistentProperties = metaStore.load(); + assertNotNull(persistentProperties); + assertEquals(1, persistentProperties.size()); + assertEquals("bar", persistentProperties.get("foo")); + } +} diff --git a/spring-integration-feed/src/main/java/org/springframework/integration/feed/FeedEntryReaderMessageSource.java b/spring-integration-feed/src/main/java/org/springframework/integration/feed/FeedEntryReaderMessageSource.java index bdcec40c7d..80e578bbec 100644 --- a/spring-integration-feed/src/main/java/org/springframework/integration/feed/FeedEntryReaderMessageSource.java +++ b/spring-integration-feed/src/main/java/org/springframework/integration/feed/FeedEntryReaderMessageSource.java @@ -128,6 +128,7 @@ public class FeedEntryReaderMessageSource extends IntegrationObjectSupport imple if (this.metadataStore == null){ logger.info("Creating FileBasedPropertiesStore"); metadataStore = new FileBasedPropertiesStore(this.persistentIdentifier); + ((FileBasedPropertiesStore)metadataStore).afterPropertiesSet(); } lastPersistentEntry = metadataStore.load(); }