org.codelibs.elasticsearch.velocity.util.ContextProperties Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of elasticsearch-lang-velocity Show documentation
Show all versions of elasticsearch-lang-velocity Show documentation
This plugin adds Velocity language to Elasticsearch.
package org.codelibs.elasticsearch.velocity.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.InvalidPropertiesFormatException;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.elasticsearch.ElasticsearchException;
public class ContextProperties extends Properties {
private static final long serialVersionUID = 1L;
public long checkInterval = 5000L;
protected volatile long lastChecked = 0L;
protected volatile long lastModified = 0L;
protected volatile File propertiesFile;
protected volatile Properties properties;
public ContextProperties(final File file) {
// check path
if (file == null) {
throw new ElasticsearchException("file is null.");
}
this.propertiesFile = file;
if (!this.propertiesFile.exists()) {
throw new ElasticsearchException(propertiesFile.getAbsolutePath() + " does not exist.");
} else if (!this.propertiesFile.isFile()) {
throw new ElasticsearchException(propertiesFile.getAbsolutePath() + " is not a file.");
}
load();
}
public String getName() {
return propertiesFile.getName().split("\\.")[0];
}
public boolean isUpdated() {
final long now = System.currentTimeMillis();
if (now - lastChecked < checkInterval) {
lastChecked = now;
return false;
}
lastChecked = now;
final long timestamp = propertiesFile.lastModified();
if (timestamp <= lastModified) {
return false;
}
return true;
}
public synchronized void load() {
properties = AccessController.doPrivileged((PrivilegedAction) () -> {
final Properties prop = new Properties();
FileInputStream fis = null;
try {
fis = new FileInputStream(propertiesFile);
lastModified = propertiesFile.lastModified();
prop.load(fis);
} catch (final IOException e1) {
throw new ElasticsearchException(e1);
} finally {
if (fis != null) {
try {
fis.close();
} catch (final IOException e2) {
// ignore
}
}
}
return prop;
});
}
protected Properties getProperties() {
if (isUpdated()) {
load();
}
return properties;
}
public Set