728x90
반응형

간단한 배치모듈을 구성중 외부 설정파일(properties) 에 대해 원하는 키를 인자로 넣어 값을 리턴하는 클래스가 필요하여 

간단하게 구성한 클래스이다. 

이 클래스의 예제 에서는 getString(),getBoolean(),getLong(),getInt() 와 getObject()를 이용하여 설정값을 가져오고있다.

이를 응용하여 다른 자료형으로 반환하는 메서드를 만들어 사용해도 될것이다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
 *      create by sanghoon lee
 *
 * */
package util;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
 
public class ConfigInjector {
 
    private static ConfigInjector config = new ConfigInjector();
    final static String CRLF = "\n\r";
    Properties props;
 
    private ConfigInjector() {
        init();
    }
 
    private void init() {
        loadConfig();
    }
 
    /**
     * properties load..
     * */
    public void loadConfig() {
        props = new Properties();
        try {
            if(isWindow()) {    //    윈도우인 경우..
                 props.load(
                         new BufferedReader(
                         new FileReader(
                         new File( "properties/batch.properties" )
                         )));
            }
            else {    //    윈도우가 아닌경우..
                props.load(new BufferedReader(new FileReader(System
                        .getProperty("batch.properties"))));    
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public boolean isWindow() {
        String os = System.getProperty("os.name");
        if(os.indexOf("Window") != -1) 
            return true;    //    윈도우 일경우 
        return false;    //    윈도우가 아닐경우
    }
    
    /**
     * load 한 프로퍼티 파싱
     * */
    public String parseConfig(String key) {
        String value = "";
        try {
            value = props.getProperty(key);
        } catch (Exception e) {
            // ignore
        }
        return value;
    }
 
    /**
     * propertis 를 읽어와 Object 으로 리턴 getObject 에서는
     * */
    public Object getObject(String key) {
        String value = "";
        try {
            value = parseConfig(key);
        } catch (Exception e) {
            // ignore
        }
        return value.trim();
    }
 
    /**
     * properties 를 읽어와 String 으로 리턴
     * */
    public String getString(String key) {
        String value = "";
        try {
            value = (String) getObject(key);
        } catch (ClassCastException e) {
            // ignore
        }
        return value;
    }
 
    /**
     * properties 를 읽어와 boolean 으로 리턴
     * */
    public boolean getBoolean(String key) {
        boolean flag = false;
        try {
            flag = Boolean.parseBoolean((String) getObject(key));
        } catch (ClassCastException e) {
            // ignore
        }
        return flag;
    }
 
    /**
     * properties 를 읽어와 int 으로 리턴
     * */
    public int getInt(String key) {
        int result = 0;
        try {
            result = Integer.parseInt((String) getObject(key));
        } catch (ClassCastException e) {
            // ignore
        }
        return result;
    }
 
    /**
     * properties 를 읽어와 Long 으로 리턴
     * */
    public long getLong(String key) {
        long result = 0;
        try {
            result = Long.parseLong((String) getObject(key));
        } catch (ClassCastException e) {
            // ignore
        }
        return result;
    }
 
    /**
     * ConfigInjector 인스턴스를 리턴
     * */
    public static ConfigInjector getInstance() {
        return config;
    }
 
    /*
     * public static void main( String[] args ) { ConfigInjector config =
     * ConfigInjector.getInstance(); String pw =
     * config.getString("copydb.userpw"); String dir =
     * config.getString("batchjob.log.dir"); System.out.println( pw );
     * System.out.println( dir ); ConfigEntity entity =
     * config.getConfigEntity(); System.out.println(entity); }
     */
 
}


728x90
반응형

+ Recent posts