#815 - ENH: When loading ebean-autotune.xml also look in resources (if not found as file)

This commit is contained in:
Robin Bygrave
2016-08-24 22:19:46 +12:00
parent 2b7c204072
commit 157dfcfeb6
5 changed files with 87 additions and 13 deletions
@@ -19,8 +19,7 @@ public class AutoTuneXmlReader {
/**
* Read and return a Profiling from an xml file.
*/
public Autotune read(File file) {
public static Autotune read(File file) {
try {
return readFile(file);
} catch (IOException e) {
@@ -28,7 +27,7 @@ public class AutoTuneXmlReader {
}
}
protected Autotune readFile(File file) throws IOException {
protected static Autotune readFile(File file) throws IOException {
if (!file.exists()) {
return new Autotune();
}
@@ -43,8 +42,7 @@ public class AutoTuneXmlReader {
/**
* Read and return a Profiling from an xml document.
*/
public Autotune read(InputStream is) {
public static Autotune read(InputStream is) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Autotune.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
@@ -12,6 +12,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
/**
@@ -101,19 +102,26 @@ public class DefaultAutoTuneService implements AutoTuneService {
*/
private void loadTuningFile() {
File file = new File(tuningFile);
if (!file.exists()) {
logger.warn("AutoTune file {} not found - no initial automatic query tuning", file.getAbsolutePath());
if (file.exists()) {
loadAutoTuneProfiling(AutoTuneXmlReader.read(file));
} else {
AutoTuneXmlReader reader = new AutoTuneXmlReader();
Autotune profiling = reader.read(file);
logger.info("AutoTune loading {} tuning entries", profiling.getOrigin().size());
for (Origin origin : profiling.getOrigin()) {
queryTuner.put(origin);
// look for autotune as a resource
InputStream stream = getClass().getResourceAsStream("/" + tuningFile);
if (stream != null) {
loadAutoTuneProfiling(AutoTuneXmlReader.read(stream));
} else {
logger.warn("AutoTune file {} not found - no initial automatic query tuning", tuningFile);
}
}
}
private void loadAutoTuneProfiling(Autotune profiling) {
logger.info("AutoTune loading {} tuning entries", profiling.getOrigin().size());
for (Origin origin : profiling.getOrigin()) {
queryTuner.put(origin);
}
}
/**
* Collect profiling, check for new/diff to existing tuning and apply changes.
*/