
com.soulgalore.velocity.sitespeed.RuleManager Maven / Gradle / Ivy
package com.soulgalore.velocity.sitespeed;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* Rule helper for sitespeed.io.
*
*/
public class RuleManager {
private static final String PROPERTY_FILE = "rules.properties";
private final Map rules = new HashMap();
public static final String UNDEFINED = "UNDEFINED";
public RuleManager() {
populateRules();
}
public String getRuleColor(String ruleName, Double value) {
Rule rule = rules.get(ruleName);
if (rule != null && value!=null)
return rule.getColor(value);
return UNDEFINED;
}
public Rule getRule(String ruleName) {
return rules.get(ruleName);
}
private void populateRules() {
Properties properties = new Properties();
InputStream is = null;
try {
is = getClass().getResourceAsStream("/" + PROPERTY_FILE);
if (is!=null)
properties.load(is);
File localFile = new File(
System.getProperty(
"com.soulgalore.velocity.sitespeed.rules.file"));
if (localFile.exists()) {
InputStream in = new FileInputStream(localFile);
properties.load(in);
}
Enumeration enuKeys = properties.keys();
while (enuKeys.hasMoreElements()) {
String key = (String) enuKeys.nextElement();
String value = properties.getProperty(key);
String[] values = value.split(";");
Rule rule = new Rule(key, Rule.Type.valueOf(values[0]),
Double.parseDouble(values[1]),
Double.parseDouble(values[2]));
rules.put(rule.getName(), rule);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null)
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy