Skip to content

Commit

Permalink
配置加载相关优化及单无测试
Browse files Browse the repository at this point in the history
  • Loading branch information
manlge committed Aug 19, 2013
1 parent 97e97ff commit 75da7b9
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 54 deletions.
16 changes: 13 additions & 3 deletions dist/profile.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#log file name
logFileName = tprofiler.log
methodFileName = tmethod.log
samplerFileName = tsampler.log

#basic configuration items
startProfTime = 9:00:00
endProfTime = 11:00:00
eachProfUseTime = 5
Expand All @@ -7,9 +13,13 @@ port = 50000
debugMode = false
needNanoTime = false
ignoreGetSetMethod = true
logFilePath = ${user.home}/logs/tprofiler.log
methodFilePath = ${user.home}/logs/tmethod.log
samplerFilePath = ${user.home}/logs/tsampler.log

#file paths
logFilePath = ${user.home}/logs/${logFileName}
methodFilePath = ${user.home}/logs/${methodFileName}
samplerFilePath = ${user.home}/logs/${samplerFileName}

#exclude & excludes items
excludeClassLoader = org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader
includePackageStartsWith = com.taobao;com.taobao.common
excludePackageStartsWith = com.taobao.sketch;org.apache.velocity;com.alibaba;com.taobao.forest.domain.dataobject
Binary file modified dist/tprofiler.jar
Binary file not shown.
6 changes: 1 addition & 5 deletions src/main/java/com/taobao/profile/Manager.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,6 @@
* @since 2012-1-9
*/
public class Manager {
/**
* 默认配置文件
*/
private static final String DEFAULT_CONFIG = "profile.properties";

/**
* 远程连接端口
Expand Down Expand Up @@ -121,7 +117,7 @@ private Manager() {}
* 初始化配置
*/
public void initialization() {
profConfig = new ProfConfig(DEFAULT_CONFIG);
profConfig = new ProfConfig();
NEED_NANO_TIME = profConfig.isNeedNanoTime();
IGNORE_GETSET_METHOD = profConfig.isIgnoreGetSetMethod();
METHOD_LOG_PATH = profConfig.getMethodFilePath();
Expand Down
46 changes: 44 additions & 2 deletions src/main/java/com/taobao/profile/config/ConfigureProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,51 @@
import com.taobao.profile.utils.VariableNotFoundException;

/**
* 用于加载配置文件的properties类,与java默认的不同,在调用get方法返回value时,会对value
* <p>用于加载配置文件的properties类,与java默认的不同,在调用get方法返回value时,会对value
* 会检查是否存在变量(如:${user.home}),如果存在,会将变量替换成具体的值。
* 该类使用dectorator设计模式
* <p>该类使用dectorator设计模式
*
* <p>示例文件内容(profile.properties):
* <pre>
* logFileName = tprofiler.log
* methodFileName = tmethod.log
* samplerFileName = tsampler.log
*
* startProfTime = 9:00:00
* endProfTime = 11:00:00
* eachProfUseTime = 5
* eachProfIntervalTime = 50
* samplerIntervalTime = 20
* port = 50000
* debugMode = false
* needNanoTime = false
* ignoreGetSetMethod = true
*
* logFilePath = ${user.home}/logs/${logFileName}
* methodFilePath = ${user.home}/logs/${methodFileName}
* samplerFilePath = ${user.home}/logs/${samplerFileName}
*
* excludeClassLoader = org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader
* includePackageStartsWith = com.taobao;com.taobao.common
* excludePackageStartsWith = com.taobao.sketch;org.apache.velocity;com.alibaba;com.taobao.forest.domain.dataobject
* </pre>
* <p>未例代码:
* <pre>
* Properties properties = new Properties();
* InputStream in = getClass().getClassLoader().getResourceAsStream("profile.properties");
* properties.load(in);
*
* Properties context = new Properties(System.getProperties());
* context.putAll(System.getProperties());
* context.putAll(properties);
* try{
* ConfigureProperties configureProperties = new ConfigureProperties(properties, context);
* String logFilePath = configureProperties.getProperty("logFilePath");
* Assert.assertEquals(logFilePath, System.getProperty("user.home") + "/logs/tprofiler.log");
* }finally{
* in.close();
* }
* </pre>
* @author manlge
* @since 2013-08-18
*/
Expand Down
105 changes: 66 additions & 39 deletions src/main/java/com/taobao/profile/config/ProfConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
*/
package com.taobao.profile.config;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;

import com.taobao.profile.utils.VariableNotFoundException;
Expand All @@ -22,6 +26,17 @@
* @since 2010-6-22
*/
public class ProfConfig {

/**
* 配置文件名
*/
private static final String CONFIG_FILE_NAME = "profile.properties";

/**
* 默认的配置文件路径
*/
private File DEFAULT_PROFILE_PATH = new File(System.getProperty("user.home"), "/.tprofiler/" + CONFIG_FILE_NAME);


/**
* 开始profile时间
Expand Down Expand Up @@ -101,7 +116,7 @@ public class ProfConfig {
/**
* 构造方法
*/
public ProfConfig(String defaultPath) {
public ProfConfig() {

boolean debug = "true".equalsIgnoreCase(System.getProperty("tprofiler.debug"));
/*
Expand All @@ -111,39 +126,70 @@ public ProfConfig(String defaultPath) {
* 3. 有户文件夹~/.tprofiler/profile.properties,如:/home/manlge/.tprofiler/profile.properties
* 4. 默认jar包中的profile.properties
*/
String configPaths[] = {System.getProperty("profile.properties"),
"profile.properties",
new File(System.getProperty("user.home"), "/.tprofiler/profile.properties").getAbsolutePath()};
File configFiles[] = {
new File(System.getProperty(CONFIG_FILE_NAME)),
new File(CONFIG_FILE_NAME),
DEFAULT_PROFILE_PATH
};

for (String configPath : configPaths){
if (configPath != null && !configPath.isEmpty()) {
File file = new File(configPath);
if (file.exists()) {
if (debug){
System.out.println(String.format("load configuration from \"%s\".", file.getAbsolutePath()));
}
parseProperty(file);
return;
}
for (File file : configFiles){
if (file.exists() && file.isFile()) {
if (debug){
System.out.println(String.format("load configuration from \"%s\".", file.getAbsolutePath()));
}
parseProperty(file);
return;
}
}
//加载默认配置
if (debug){
System.out.println(String.format("load configuration from \"%s\".", Thread.currentThread().getContextClassLoader().getResource(defaultPath)));
System.out.println(String.format("load configuration from \"%s\".", DEFAULT_PROFILE_PATH.getAbsolutePath()));
}
try {
extractDefaultProFile();
parseProperty(DEFAULT_PROFILE_PATH);
} catch (IOException e) {
throw new RuntimeException("error load config file", e);
}
parse(defaultPath);

}

/**
* 解压默认的配置文件到~/.tprofiler/profile.properties,可以作为模板,以便用户编辑
* @throws IOException
*/
private void extractDefaultProFile() throws IOException {
InputStream in = getClass().getClassLoader().getResourceAsStream(CONFIG_FILE_NAME);
OutputStream out = null;
try{
File profileDirectory = DEFAULT_PROFILE_PATH.getParentFile();
if (!profileDirectory.exists()){
profileDirectory.mkdirs();
}
out = new BufferedOutputStream(new FileOutputStream(DEFAULT_PROFILE_PATH));
byte[] buffer = new byte[1024];
for (int len = -1; (len = in.read(buffer)) != -1;){
out.write(buffer, 0, len);
}

}finally{
in.close();
out.close();
}
}

/**
* 解析用户自定义配置文件
*
* @param path
*/
private void parseProperty(File path) {
Properties resource = new Properties();
Properties properties = new Properties();
try {
resource.load(new FileReader(path));
loadConfig(new ConfigureProperties(resource, System.getProperties()));
properties.load(new FileReader(path));
Properties context = new Properties();
context.putAll(System.getProperties());
context.putAll(properties);
loadConfig(new ConfigureProperties(properties, context));
} catch (Exception e) {
e.printStackTrace();
}
Expand Down Expand Up @@ -198,25 +244,6 @@ private void loadConfig(Properties properties) throws VariableNotFoundException
}
}

/**
* 解析默认配置文件
*
* @param configName
*/
private void parse(String configName) {
try {
InputStream in = getClass().getResourceAsStream("/" + configName);
try{
Properties properties = new Properties();
properties.load(in);
loadConfig(new ConfigureProperties(properties, System.getProperties()));
} finally{
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* @return
Expand Down
16 changes: 13 additions & 3 deletions src/main/resources/profile.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#log file name
logFileName = tprofiler.log
methodFileName = tmethod.log
samplerFileName = tsampler.log

#basic configuration items
startProfTime = 9:00:00
endProfTime = 11:00:00
eachProfUseTime = 5
Expand All @@ -7,9 +13,13 @@ port = 50000
debugMode = false
needNanoTime = false
ignoreGetSetMethod = true
logFilePath = ${user.home}/logs/tprofiler.log
methodFilePath = ${user.home}/logs/tmethod.log
samplerFilePath = ${user.home}/logs/tsampler.log

#file paths
logFilePath = ${user.home}/logs/${logFileName}
methodFilePath = ${user.home}/logs/${methodFileName}
samplerFilePath = ${user.home}/logs/${samplerFileName}

#exclude & excludes items
excludeClassLoader = org.eclipse.osgi.internal.baseadaptor.DefaultClassLoader
includePackageStartsWith = com.taobao;com.taobao.common
excludePackageStartsWith = com.taobao.sketch;org.apache.velocity;com.alibaba;com.taobao.forest.domain.dataobject
28 changes: 26 additions & 2 deletions src/test/java/com/taobao/profile/test/ConfigurePropertiesTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.taobao.profile.test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.junit.Assert;
Expand All @@ -12,8 +14,30 @@ 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());
prop.put("file.name", "tprofiler.log");
prop.put("log.file.path", "${user.home}/${file.name}");
Properties context = System.getProperties();
context.putAll(prop);

Properties properties = new ConfigureProperties(prop, context);
Assert.assertEquals(properties.getProperty("log.file.path"), System.getProperty("user.home") + "/tprofiler.log" );
}

@Test
public void testConfigure() throws IOException{
Properties properties = new Properties();
InputStream in = getClass().getClassLoader().getResourceAsStream("profile.properties");
properties.load(in);

Properties context = new Properties(System.getProperties());
context.putAll(System.getProperties());
context.putAll(properties);
try{
ConfigureProperties configureProperties = new ConfigureProperties(properties, context);
String logFilePath = configureProperties.getProperty("logFilePath");
Assert.assertEquals(logFilePath, System.getProperty("user.home") + "/logs/tprofiler.log");
}finally{
in.close();
}
}
}

0 comments on commit 75da7b9

Please sign in to comment.