설정파일을 저장하고 가져오는 라이브러리입니다.
sjava-config는 http://www.jconfig.org/ 의 jconfig를 보고 필요한 형태(xml만 지원)로만 개발했습니다.
여러형태의 설정파일을 읽어올 수 있도록 common한 기능은 추상 클래스로 빼고 설정을 읽는 코드는 하위 클래스에서 구현을 합니다.
설정파일의 형태는 아래와 같습니다.
sjava-config.xml
그리고 위 파일(sjava-config.xml)은 classpath에 위치를 해야 읽을 수 있습니다.
ConfigTest.java
바이너리
소스
sjava-config는 http://www.jconfig.org/ 의 jconfig를 보고 필요한 형태(xml만 지원)로만 개발했습니다.
여러형태의 설정파일을 읽어올 수 있도록 common한 기능은 추상 클래스로 빼고 설정을 읽는 코드는 하위 클래스에서 구현을 합니다.
설정파일의 형태는 아래와 같습니다.
sjava-config.xml
<?xml version="1.0" encoding="utf-8"?>
<sjava-config>
<!-- array variable delimeter is "," -->
<!-- sjava-config 설정, 아래 설정은 지우지 마세요 -->
<sjava-service name="config">
<key name="watch" value="true" /> <!-- true, false -->
<key name="interval" value="60" /> <!-- 60 seconds -->
</sjava-service>
<!-- 로그서버 설정 -->
<sjava-service name="log">
<key name="host" value="111.111.111.111,222.222.222.222" />
<key name="port" value="20003" />
</sjava-service>
</sjava-config>
<sjava-config>
<!-- array variable delimeter is "," -->
<!-- sjava-config 설정, 아래 설정은 지우지 마세요 -->
<sjava-service name="config">
<key name="watch" value="true" /> <!-- true, false -->
<key name="interval" value="60" /> <!-- 60 seconds -->
</sjava-service>
<!-- 로그서버 설정 -->
<sjava-service name="log">
<key name="host" value="111.111.111.111,222.222.222.222" />
<key name="port" value="20003" />
</sjava-service>
</sjava-config>
그리고 위 파일(sjava-config.xml)은 classpath에 위치를 해야 읽을 수 있습니다.
ConfigTest.java
package net.sjava.config.demo;
import net.sjava.config.ConfigHandler;
/**
* Code Coverage Test Class using EclEmma
* @author mcsong@gmail.com
* @since 2009. 6. 29.
*/
public class ConfigTest {
/**
* @param args
*/
public static void main(String[] args) {
ConfigHandler configHandler = ConfigHandler.getInstance();
System.out.println(configHandler.isLoading() ? "loading true" : "loading fail");
System.out.println("- get vlaue test ---");
System.out.println(configHandler.getValue("config", "watch"));
System.out.println(configHandler.getValue("config", "interval"));
System.out.println(configHandler.getValue("log", "host"));
System.out.println(configHandler.getValue("log", "port"));
System.out.println(configHandler.getValue("auth", "host"));
System.out.println(configHandler.getValue("auth", "port"));
System.out.println("- default value test ---");
System.out.println(configHandler.getValue("config", "watch", "false"));
System.out.println(configHandler.getValue("config", "interval", "11"));
System.out.println(configHandler.getValue("log", "host", "222.222.222.222"));
System.out.println(configHandler.getValue("log", "port", "20003"));
System.out.println(configHandler.getValue("auth", "host", "222.222.222.222"));
System.out.println("- array value test ---");
for(int i = 0; i < configHandler.getValues("log", "host").length; i++) {
System.out.println(configHandler.getValues("log", "host")[i].toString());
}
for(int i = 0; i < configHandler.getValues("auth", "host").length; i++) {
System.out.println(configHandler.getValues("auth", "host")[i].toString());
}
System.out.println("- add value ---");
configHandler.addValue("test", "host", "1.1.1.1");
System.out.println(configHandler.getValue("test", "host"));
System.out.println("- modify value ---");
configHandler.setValue("test", "host", "2.2.2.2");
System.out.println(configHandler.getValue("test", "host"));
System.out.println("- modify values ---");
String[] values = {"2.2.2.2", "3.3.3.3"};
configHandler.setValues("test", "host", values);
for(int i = 0; i < configHandler.getValues("test", "host").length; i++) {
System.out.println(configHandler.getValues("test", "host")[i].toString());
}
}
}
import net.sjava.config.ConfigHandler;
/**
* Code Coverage Test Class using EclEmma
* @author mcsong@gmail.com
* @since 2009. 6. 29.
*/
public class ConfigTest {
/**
* @param args
*/
public static void main(String[] args) {
ConfigHandler configHandler = ConfigHandler.getInstance();
System.out.println(configHandler.isLoading() ? "loading true" : "loading fail");
System.out.println("- get vlaue test ---");
System.out.println(configHandler.getValue("config", "watch"));
System.out.println(configHandler.getValue("config", "interval"));
System.out.println(configHandler.getValue("log", "host"));
System.out.println(configHandler.getValue("log", "port"));
System.out.println(configHandler.getValue("auth", "host"));
System.out.println(configHandler.getValue("auth", "port"));
System.out.println("- default value test ---");
System.out.println(configHandler.getValue("config", "watch", "false"));
System.out.println(configHandler.getValue("config", "interval", "11"));
System.out.println(configHandler.getValue("log", "host", "222.222.222.222"));
System.out.println(configHandler.getValue("log", "port", "20003"));
System.out.println(configHandler.getValue("auth", "host", "222.222.222.222"));
System.out.println("- array value test ---");
for(int i = 0; i < configHandler.getValues("log", "host").length; i++) {
System.out.println(configHandler.getValues("log", "host")[i].toString());
}
for(int i = 0; i < configHandler.getValues("auth", "host").length; i++) {
System.out.println(configHandler.getValues("auth", "host")[i].toString());
}
System.out.println("- add value ---");
configHandler.addValue("test", "host", "1.1.1.1");
System.out.println(configHandler.getValue("test", "host"));
System.out.println("- modify value ---");
configHandler.setValue("test", "host", "2.2.2.2");
System.out.println(configHandler.getValue("test", "host"));
System.out.println("- modify values ---");
String[] values = {"2.2.2.2", "3.3.3.3"};
configHandler.setValues("test", "host", values);
for(int i = 0; i < configHandler.getValues("test", "host").length; i++) {
System.out.println(configHandler.getValues("test", "host")[i].toString());
}
}
}
바이너리
소스
'My Project' 카테고리의 다른 글
| sjava-logging 1.0 Level (0) | 2009/07/13 |
|---|---|
| sjava-logging 1.0 (0) | 2009/07/09 |
| sjava-config 1.2 (0) | 2009/07/08 |
| sjava-config 1.1 (0) | 2009/07/03 |
| sjava-config 1.0 (0) | 2009/06/30 |
sjava-config-1.0.0.zip