diff --git a/dist/tprofiler.jar b/dist/tprofiler.jar index 4d5c06f..0142e05 100644 Binary files a/dist/tprofiler.jar and b/dist/tprofiler.jar differ diff --git a/src/main/java/com/taobao/profile/config/ConfigureProperties.java b/src/main/java/com/taobao/profile/config/ConfigureProperties.java new file mode 100644 index 0000000..6e081c0 --- /dev/null +++ b/src/main/java/com/taobao/profile/config/ConfigureProperties.java @@ -0,0 +1,194 @@ +package com.taobao.profile.config; + +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.util.Collection; +import java.util.Enumeration; +import java.util.InvalidPropertiesFormatException; +import java.util.Map; +import java.util.Properties; +import java.util.Set; + +import com.taobao.profile.utils.Utilities; +import com.taobao.profile.utils.VariableNotFoundException; + +/** + * 用于加载配置文件的properties类,与java默认的不同,在调用get方法返回value时,会对value + * 会检查是否存在变量(如:${user.home}),如果存在,会将变量替换成具体的值。 + * 该类使用dectorator设计模式 + * @author manlge + * @since 2013-08-18 + */ +public class ConfigureProperties extends Properties { + + /** + * + */ + private static final long serialVersionUID = -3868173073422671544L; + + private Properties delegate; + + private Properties context; + + + public ConfigureProperties(Properties delegate, Properties context) { + super(); + this.delegate = delegate; + this.context = context; + } + + public Object setProperty(String key, String value) { + return delegate.setProperty(key, value); + } + + public void load(Reader reader) throws IOException { + delegate.load(reader); + } + + public int size() { + return delegate.size(); + } + + public boolean isEmpty() { + return delegate.isEmpty(); + } + + public Enumeration keys() { + return delegate.keys(); + } + + public Enumeration elements() { + return delegate.elements(); + } + + public void load(InputStream inStream) throws IOException { + delegate.load(inStream); + } + + public boolean contains(Object value) { + return delegate.contains(value); + } + + public boolean containsValue(Object value) { + return delegate.containsValue(value); + } + + public boolean containsKey(Object key) { + return delegate.containsKey(key); + } + + public Object get(Object key) { + return delegate.get(key); + } + + public Object put(Object key, Object value) { + return delegate.put(key, value); + } + + public Object remove(Object key) { + return delegate.remove(key); + } + + public void putAll(Map t) { + delegate.putAll(t); + } + + public void clear() { + delegate.clear(); + } + + public Object clone() { + return delegate.clone(); + } + + public String toString() { + return delegate.toString(); + } + + public Set keySet() { + return delegate.keySet(); + } + + public Set> entrySet() { + return delegate.entrySet(); + } + + public void save(OutputStream out, String comments) { + delegate.save(out, comments); + } + + public void store(Writer writer, String comments) throws IOException { + delegate.store(writer, comments); + } + + public Collection values() { + return delegate.values(); + } + + public void store(OutputStream out, String comments) throws IOException { + delegate.store(out, comments); + } + + public boolean equals(Object o) { + return delegate.equals(o); + } + + public int hashCode() { + return delegate.hashCode(); + } + + public void loadFromXML(InputStream in) throws IOException, + InvalidPropertiesFormatException { + delegate.loadFromXML(in); + } + + public void storeToXML(OutputStream os, String comment) throws IOException { + delegate.storeToXML(os, comment); + } + + public void storeToXML(OutputStream os, String comment, String encoding) + throws IOException { + delegate.storeToXML(os, comment, encoding); + } + + public String getProperty(String key) { + String value = delegate.getProperty(key); + try { + return Utilities.repleseVariables(value, context); + } catch (VariableNotFoundException e) { + throw new RuntimeException(e); + } + } + + public String getProperty(String key, String defaultValue) { + String value = delegate.getProperty(key, defaultValue); + try { + return Utilities.repleseVariables(value, context); + } catch (VariableNotFoundException e) { + throw new RuntimeException(e); + } + } + + public Enumeration propertyNames() { + return delegate.propertyNames(); + } + + public Set stringPropertyNames() { + return delegate.stringPropertyNames(); + } + + public void list(PrintStream out) { + delegate.list(out); + } + + public void list(PrintWriter out) { + delegate.list(out); + } + + +} diff --git a/src/main/java/com/taobao/profile/config/ProfConfig.java b/src/main/java/com/taobao/profile/config/ProfConfig.java index 60a661f..9e6f376 100644 --- a/src/main/java/com/taobao/profile/config/ProfConfig.java +++ b/src/main/java/com/taobao/profile/config/ProfConfig.java @@ -13,7 +13,6 @@ import java.io.InputStream; import java.util.Properties; -import com.taobao.profile.utils.Utilities; import com.taobao.profile.utils.VariableNotFoundException; /** @@ -125,7 +124,7 @@ private void parseProperty(File path) { Properties resource = new Properties(); try { resource.load(new FileReader(path)); - loadConfig(resource); + loadConfig(new ConfigureProperties(resource, System.getProperties())); } catch (Exception e) { e.printStackTrace(); } @@ -134,15 +133,13 @@ private void parseProperty(File path) { /** * 加载配置 * @param properties - * @throws ParseException */ private void loadConfig(Properties properties) throws VariableNotFoundException { - Properties context = System.getProperties(); String startProfTime = properties.getProperty("startProfTime"); String endProfTime = properties.getProperty("endProfTime"); - String logFilePath = Utilities.repleseVariables(properties.getProperty("logFilePath"), context) ; - String methodFilePath = Utilities.repleseVariables(properties.getProperty("methodFilePath"), context) ; - String samplerFilePath = Utilities.repleseVariables(properties.getProperty("samplerFilePath"), context) ; + String logFilePath = properties.getProperty("logFilePath"); + String methodFilePath = properties.getProperty("methodFilePath"); + String samplerFilePath = properties.getProperty("samplerFilePath"); String includePackageStartsWith = properties.getProperty("includePackageStartsWith"); String eachProfUseTime = properties.getProperty("eachProfUseTime"); String eachProfIntervalTime = properties.getProperty("eachProfIntervalTime"); @@ -193,7 +190,7 @@ private void parse(String configName) { try{ Properties properties = new Properties(); properties.load(in); - loadConfig(properties); + loadConfig(new ConfigureProperties(properties, System.getProperties())); } finally{ in.close(); } diff --git a/src/test/java/com/taobao/profile/test/ConfigurePropertiesTest.java b/src/test/java/com/taobao/profile/test/ConfigurePropertiesTest.java new file mode 100644 index 0000000..9b9bf56 --- /dev/null +++ b/src/test/java/com/taobao/profile/test/ConfigurePropertiesTest.java @@ -0,0 +1,19 @@ +package com.taobao.profile.test; + +import java.util.Properties; + +import org.junit.Assert; +import org.junit.Test; + +import com.taobao.profile.config.ConfigureProperties; + +public class ConfigurePropertiesTest { + + @Test + public void testConfigureProperties(){ + Properties prop = new Properties(); + prop.put("log.file.path", "${user.home}/tprofiler.log"); + Properties properties = new ConfigureProperties(prop, System.getProperties()); + Assert.assertEquals(properties.getProperty("log.file.path"), System.getProperty("user.home") + "/tprofiler.log" ); + } +}