net.n2oapp.properties.ReloadableProperties Maven / Gradle / Ivy
package net.n2oapp.properties;
import net.n2oapp.properties.io.PropertiesRewriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.Enumeration;
import java.util.InvalidPropertiesFormatException;
import java.util.Set;
import java.util.concurrent.Semaphore;
/**
* ReloadableProperties reads values from .properties file every {@link #cacheTime} seconds
* User: operhod
* Date: 10.10.13
* Time: 17:05
*/
public class ReloadableProperties extends OverrideProperties {
private volatile long timeStamp = Long.MIN_VALUE;
private volatile int cacheTime = 60;
private volatile boolean exists;
private volatile Resource resource;
private final Semaphore available = new Semaphore(1);
public ReloadableProperties(URI uri) throws MalformedURLException {
this.resource = new UrlResource(uri);
}
public ReloadableProperties(Resource resource) {
this.resource = resource;
}
public ReloadableProperties() {
}
private Logger logger = LoggerFactory.getLogger(getClass());
@Override
public Set stringPropertyNames() {
refresh();
return super.stringPropertyNames();
}
@Override
public Enumeration> propertyNames() {
refresh();
return super.propertyNames();
}
@Override
public synchronized Enumeration