1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package patterntesting.sample;
21
22 import java.io.*;
23 import java.util.Properties;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.impl.LogFactoryImpl;
27
28 import patterntesting.annotation.check.runtime.*;
29 import patterntesting.runtime.NullConstants;
30
31
32
33
34
35
36
37
38 public class Config {
39
40 private static final Log log = LogFactoryImpl.getLog(Config.class);
41 private static String propertyResource = "default.properties";
42 private static Properties properties = null;
43
44
45 private Config() {}
46
47
48
49
50
51
52 public static void setResource(final String name) {
53 propertyResource = name;
54 properties = null;
55 }
56
57
58
59
60
61
62 public static String getResource() {
63 return propertyResource;
64 }
65
66
67
68
69
70
71 public static String getVersion() {
72 return getProperty("patterntesting.sample.version");
73 }
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90 public static void setVersion(final Object prefix, String version,
91 final Object suffix) {
92 if (prefix != NullConstants.NULL_OBJECT) {
93 version = prefix.toString() + version;
94 }
95 if (suffix != NullConstants.NULL_OBJECT) {
96 version = version + suffix.toString();
97 }
98 setProperty("patterntesting.sample.version", version);
99 }
100
101
102
103
104
105
106
107
108 @MayReturnNull
109 public static String getProperty(final String name) {
110 Properties props = getProperties();
111 return props.getProperty(name);
112 }
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128 @NullArgsAllowed
129 public static void setProperty(@NotNull final String key, final String value) {
130 Properties properties = getProperties();
131 if (value == null) {
132 properties.remove(key);
133 } else {
134 properties.setProperty(key, value);
135 }
136 }
137
138
139
140
141
142
143 public static synchronized Properties getProperties() {
144 if (properties != null) {
145 return properties;
146 }
147 properties = new Properties();
148 try {
149 properties.load(getPropertyStream());
150 log.info(propertyResource + " loaded");
151 return properties;
152 } catch (IOException ioe) {
153 log.warn("can't load properties", ioe);
154 return null;
155 }
156 }
157
158
159
160
161
162
163 public static InputStream getPropertyStream() {
164 return Config.class.getResourceAsStream(propertyResource);
165 }
166
167
168
169
170
171
172 public static String getOS() {
173 return System.getProperty("os.name");
174 }
175
176
177
178
179
180
181 public static String getJdkHome() {
182 return System.getenv("JDK_HOME");
183 }
184
185
186
187
188
189
190 public static File getJdkHomeDir() {
191 String jdkHome = getJdkHome();
192 assert jdkHome != null;
193 return new File(jdkHome);
194 }
195
196 }