1
1
package pro .belbix .dbchecker ;
2
2
3
+ import org .slf4j .Logger ;
4
+ import org .slf4j .LoggerFactory ;
5
+
6
+ import java .io .BufferedReader ;
7
+ import java .io .FileReader ;
8
+ import java .io .IOException ;
9
+ import java .lang .reflect .Field ;
10
+
3
11
public class Config {
12
+ private final static Logger log = LoggerFactory .getLogger (Config .class );
4
13
private static String path ;
5
14
private static Config instance ;
6
15
@@ -18,20 +27,54 @@ public static Config getInstance() {
18
27
}
19
28
20
29
private void init () {
21
- if (path == null ) return ;
30
+ if (path == null ) return ;
31
+ try (BufferedReader br = new BufferedReader (new FileReader (path ))) {
32
+ String s ;
33
+ while ((s = br .readLine ()) != null ) {
34
+ readLine (s );
35
+ }
36
+ } catch (IOException e ) {
37
+ log .error (e .getMessage (), e );
38
+ }
39
+
40
+ }
41
+
42
+ private void readLine (String s ) {
43
+ String [] splt = s .split (":" , 2 );
44
+ if (splt .length < 2 ) throw new IllegalStateException ("Wrong config: " + s );
45
+ String name = splt [0 ].trim ();
46
+ String value = splt [1 ].trim ();
47
+
48
+ try {
49
+ Field field = this .getClass ().getDeclaredField (name );
50
+ field .setAccessible (true );
51
+ switch (field .getType ().getSimpleName ()) {
52
+ case "String" :
53
+ field .set (this , value );
54
+ break ;
55
+ case "Integer" :
56
+ field .set (this , Integer .valueOf (value ));
57
+ break ;
58
+ default :
59
+ throw new UnsupportedOperationException ("Type not supported: " + field .getType ().getSimpleName ());
60
+ }
61
+
62
+ } catch (NoSuchFieldException | IllegalAccessException e ) {
63
+ log .error (e .getMessage (), e );
64
+ }
22
65
}
23
66
24
- public static void setPath (String path ){
67
+ public static void setPath (String path ) {
25
68
Config .path = path ;
26
69
}
27
70
28
71
private String dbClassName = "com.mysql.cj.jdbc.Driver" ;
29
72
private String dbUrl = "jdbc:mysql://localhost:3306/tim?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true" ;
30
73
private String dbUser = "belbix" ;
31
74
private String dbPassword = "111111" ;
32
- private int loopTime = 10 ;
75
+ private Integer loopTime = 10 ;
33
76
private String dateSelect = "select t.date from ticks as t order by t.date desc limit 1" ;
34
- private int maxDiff = 100 ;
77
+ private Integer maxDiff = 100 ;
35
78
36
79
public String getDbClassName () {
37
80
return dbClassName ;
0 commit comments