INT-3606: Implement Closeable/Flushable
JIRA: https://jira.spring.io/browse/INT-3606 Close the store when an (S)FTP synchronizing MessageSource is stopped. Will need another commit on 4.2.WIP to change to `Lifecycle`. Add dirty Flag to Properties MetadataStore Avoid unnecessary persists. Remove unnecessary implementation of `AbstractInboundFileSynchronizingMessageSource#getComponentType()`
This commit is contained in:
committed by
Artem Bilan
parent
8896d500ed
commit
2581388cc2
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -18,9 +18,11 @@ package org.springframework.integration.metadata;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.Closeable;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.Flushable;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
@@ -48,7 +50,8 @@ import org.springframework.util.DefaultPropertiesPersister;
|
||||
* @author Gary Russell
|
||||
* @since 2.0
|
||||
*/
|
||||
public class PropertiesPersistingMetadataStore implements ConcurrentMetadataStore, InitializingBean, DisposableBean {
|
||||
public class PropertiesPersistingMetadataStore implements ConcurrentMetadataStore, InitializingBean, DisposableBean,
|
||||
Closeable, Flushable {
|
||||
|
||||
private final Log logger = LogFactory.getLog(getClass());
|
||||
|
||||
@@ -62,6 +65,8 @@ public class PropertiesPersistingMetadataStore implements ConcurrentMetadataStor
|
||||
|
||||
private File file;
|
||||
|
||||
private volatile boolean dirty;
|
||||
|
||||
|
||||
public void setBaseDirectory(String baseDirectory) {
|
||||
Assert.hasText(baseDirectory, "'baseDirectory' must be non-empty");
|
||||
@@ -95,6 +100,7 @@ public class PropertiesPersistingMetadataStore implements ConcurrentMetadataStor
|
||||
this.metadata.setProperty(key, value);
|
||||
}
|
||||
finally {
|
||||
this.dirty = true;
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
@@ -121,6 +127,7 @@ public class PropertiesPersistingMetadataStore implements ConcurrentMetadataStor
|
||||
return (String) this.metadata.remove(key);
|
||||
}
|
||||
finally {
|
||||
this.dirty = true;
|
||||
lock.unlock();
|
||||
}
|
||||
}
|
||||
@@ -135,6 +142,7 @@ public class PropertiesPersistingMetadataStore implements ConcurrentMetadataStor
|
||||
String property = this.metadata.getProperty(key);
|
||||
if (property == null) {
|
||||
this.metadata.setProperty(key, value);
|
||||
this.dirty = true;
|
||||
return null;
|
||||
}
|
||||
else {
|
||||
@@ -157,6 +165,7 @@ public class PropertiesPersistingMetadataStore implements ConcurrentMetadataStor
|
||||
String property = this.metadata.getProperty(key);
|
||||
if (oldValue.equals(property)) {
|
||||
this.metadata.setProperty(key, newValue);
|
||||
this.dirty = true;
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
@@ -168,12 +177,26 @@ public class PropertiesPersistingMetadataStore implements ConcurrentMetadataStor
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() throws IOException {
|
||||
flush();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void flush() {
|
||||
saveMetadata();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
this.saveMetadata();
|
||||
flush();
|
||||
}
|
||||
|
||||
private void saveMetadata() {
|
||||
if (this.file == null || !this.dirty) {
|
||||
return;
|
||||
}
|
||||
this.dirty = false;
|
||||
OutputStream outputStream = null;
|
||||
try {
|
||||
outputStream = new BufferedOutputStream(new FileOutputStream(this.file));
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
* Copyright 2002-2015 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.
|
||||
@@ -50,7 +50,7 @@ public class PropertiesPersistingMetadataStoreTests {
|
||||
assertNotNull(metadataStore.putIfAbsent("foo", "baz"));
|
||||
assertFalse(metadataStore.replace("foo", "xxx", "bar"));
|
||||
assertTrue(metadataStore.replace("foo", "baz", "bar"));
|
||||
metadataStore.destroy();
|
||||
metadataStore.close();
|
||||
Properties persistentProperties = PropertiesLoaderUtils.loadProperties(new FileSystemResource(file));
|
||||
assertNotNull(persistentProperties);
|
||||
assertEquals(1, persistentProperties.size());
|
||||
@@ -66,7 +66,7 @@ public class PropertiesPersistingMetadataStoreTests {
|
||||
metadataStore.setBaseDirectory("target/foo");
|
||||
metadataStore.afterPropertiesSet();
|
||||
metadataStore.put("foo", "bar");
|
||||
metadataStore.destroy();
|
||||
metadataStore.close();
|
||||
assertTrue(file.exists());
|
||||
Properties persistentProperties = PropertiesLoaderUtils.loadProperties(new FileSystemResource(file));
|
||||
assertNotNull(persistentProperties);
|
||||
|
||||
Reference in New Issue
Block a user