INT-3798: Customizable File in Props MetadataStore
JIRA: https://jira.spring.io/browse/INT-3798 Add `fileName` property.
This commit is contained in:
@@ -42,8 +42,10 @@ 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/".
|
||||
* will be generated.
|
||||
* By default, the properties file will be
|
||||
* {@code 'java.io.tmpdir' + "/spring-integration/metadata-store.properties"},
|
||||
* but the directory and filename are settable.
|
||||
*
|
||||
* @author Oleg Zhurakousky
|
||||
* @author Mark Fisher
|
||||
@@ -63,21 +65,36 @@ public class PropertiesPersistingMetadataStore implements ConcurrentMetadataStor
|
||||
|
||||
private String baseDirectory = System.getProperty("java.io.tmpdir") + "/spring-integration/";
|
||||
|
||||
private String fileName = "metadata-store.properties";
|
||||
|
||||
private File file;
|
||||
|
||||
private volatile boolean dirty;
|
||||
|
||||
|
||||
/**
|
||||
* Set the location for the properties file. Defaults to
|
||||
* {@code 'java.io.tmpdir' + "/spring-integration/"}.
|
||||
* @param baseDirectory the directory.
|
||||
*/
|
||||
public void setBaseDirectory(String baseDirectory) {
|
||||
Assert.hasText(baseDirectory, "'baseDirectory' must be non-empty");
|
||||
this.baseDirectory = baseDirectory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the name of the properties file in {@link #setBaseDirectory(String)}.
|
||||
* Defaults to {@code metadata-store.properties},
|
||||
* @param fileName the properties file name.
|
||||
*/
|
||||
public void setFileName(String fileName) {
|
||||
this.fileName = fileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void afterPropertiesSet() throws Exception {
|
||||
File baseDir = new File(baseDirectory);
|
||||
File baseDir = new File(this.baseDirectory);
|
||||
baseDir.mkdirs();
|
||||
this.file = new File(baseDir, "metadata-store.properties");
|
||||
this.file = new File(baseDir, this.fileName);
|
||||
try {
|
||||
if (!this.file.exists()) {
|
||||
this.file.createNewFile();
|
||||
|
||||
@@ -25,7 +25,9 @@ import static org.junit.Assert.assertTrue;
|
||||
import java.io.File;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
import org.springframework.core.io.support.PropertiesLoaderUtils;
|
||||
@@ -39,6 +41,9 @@ import org.springframework.core.io.support.PropertiesLoaderUtils;
|
||||
*/
|
||||
public class PropertiesPersistingMetadataStoreTests {
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder folder = new TemporaryFolder();
|
||||
|
||||
@Test
|
||||
public void validateWithDefaultBaseDir() throws Exception {
|
||||
File file = new File(System.getProperty("java.io.tmpdir") + "/spring-integration/metadata-store.properties");
|
||||
@@ -60,10 +65,25 @@ public class PropertiesPersistingMetadataStoreTests {
|
||||
|
||||
@Test
|
||||
public void validateWithCustomBaseDir() throws Exception {
|
||||
File file = new File("target/foo" + "/metadata-store.properties");
|
||||
file.deleteOnExit();
|
||||
File file = new File(this.folder.getRoot(), "metadata-store.properties");
|
||||
PropertiesPersistingMetadataStore metadataStore = new PropertiesPersistingMetadataStore();
|
||||
metadataStore.setBaseDirectory("target/foo");
|
||||
metadataStore.setBaseDirectory(folder.getRoot().getAbsolutePath());
|
||||
metadataStore.afterPropertiesSet();
|
||||
metadataStore.put("foo", "bar");
|
||||
metadataStore.close();
|
||||
assertTrue(file.exists());
|
||||
Properties persistentProperties = PropertiesLoaderUtils.loadProperties(new FileSystemResource(file));
|
||||
assertNotNull(persistentProperties);
|
||||
assertEquals(1, persistentProperties.size());
|
||||
assertEquals("bar", persistentProperties.get("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void validateWithCustomFileName() throws Exception {
|
||||
File file = new File(this.folder.getRoot(), "foo.properties");
|
||||
PropertiesPersistingMetadataStore metadataStore = new PropertiesPersistingMetadataStore();
|
||||
metadataStore.setBaseDirectory(folder.getRoot().getAbsolutePath());
|
||||
metadataStore.setFileName("foo.properties");
|
||||
metadataStore.afterPropertiesSet();
|
||||
metadataStore.put("foo", "bar");
|
||||
metadataStore.close();
|
||||
|
||||
Reference in New Issue
Block a user