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 package org.eclipse.jgit.util;
48
49 import java.io.File;
50 import java.net.InetAddress;
51 import java.net.UnknownHostException;
52 import java.security.AccessController;
53 import java.security.PrivilegedAction;
54 import java.text.DateFormat;
55 import java.text.SimpleDateFormat;
56 import java.util.Locale;
57 import java.util.TimeZone;
58
59 import org.eclipse.jgit.storage.file.FileBasedConfig;
60 import org.eclipse.jgit.errors.CorruptObjectException;
61 import org.eclipse.jgit.lib.Config;
62 import org.eclipse.jgit.lib.ObjectChecker;
63
64
65
66
67
68
69
70
71
72 public abstract class SystemReader {
73 private static final SystemReader DEFAULT;
74
75 private static Boolean isMacOS;
76
77 private static Boolean isWindows;
78
79 static {
80 SystemReader r = new Default();
81 r.init();
82 DEFAULT = r;
83 }
84
85 private static class Default extends SystemReader {
86 private volatile String hostname;
87
88 public String getenv(String variable) {
89 return System.getenv(variable);
90 }
91
92 public String getProperty(String key) {
93 return System.getProperty(key);
94 }
95
96 public FileBasedConfig openSystemConfig(Config parent, FS fs) {
97 File configFile = fs.getGitSystemConfig();
98 if (configFile == null) {
99 return new FileBasedConfig(null, fs) {
100 public void load() {
101
102 }
103
104 public boolean isOutdated() {
105
106 return false;
107 }
108 };
109 }
110 return new FileBasedConfig(parent, configFile, fs);
111 }
112
113 public FileBasedConfig openUserConfig(Config parent, FS fs) {
114 final File home = fs.userHome();
115 return new FileBasedConfig(parent, new File(home, ".gitconfig"), fs);
116 }
117
118 public String getHostname() {
119 if (hostname == null) {
120 try {
121 InetAddress localMachine = InetAddress.getLocalHost();
122 hostname = localMachine.getCanonicalHostName();
123 } catch (UnknownHostException e) {
124
125 hostname = "localhost";
126 }
127 assert hostname != null;
128 }
129 return hostname;
130 }
131
132 @Override
133 public long getCurrentTime() {
134 return System.currentTimeMillis();
135 }
136
137 @Override
138 public int getTimezone(long when) {
139 return getTimeZone().getOffset(when) / (60 * 1000);
140 }
141 }
142
143 private static SystemReader INSTANCE = DEFAULT;
144
145
146 public static SystemReader getInstance() {
147 return INSTANCE;
148 }
149
150
151
152
153
154
155 public static void setInstance(SystemReader newReader) {
156 isMacOS = null;
157 isWindows = null;
158 if (newReader == null)
159 INSTANCE = DEFAULT;
160 else {
161 newReader.init();
162 INSTANCE = newReader;
163 }
164 }
165
166 private ObjectChecker platformChecker;
167
168 private void init() {
169
170
171 if (platformChecker == null)
172 setPlatformChecker();
173 }
174
175
176
177
178
179
180 protected final void setPlatformChecker() {
181 platformChecker = new ObjectChecker()
182 .setSafeForWindows(isWindows())
183 .setSafeForMacOS(isMacOS());
184 }
185
186
187
188
189
190
191
192 public abstract String getHostname();
193
194
195
196
197
198 public abstract String getenv(String variable);
199
200
201
202
203
204 public abstract String getProperty(String key);
205
206
207
208
209
210
211
212
213
214 public abstract FileBasedConfig openUserConfig(Config parent, FS fs);
215
216
217
218
219
220
221
222
223
224
225
226 public abstract FileBasedConfig openSystemConfig(Config parent, FS fs);
227
228
229
230
231 public abstract long getCurrentTime();
232
233
234
235
236
237 public abstract int getTimezone(long when);
238
239
240
241
242
243 public TimeZone getTimeZone() {
244 return TimeZone.getDefault();
245 }
246
247
248
249
250
251 public Locale getLocale() {
252 return Locale.getDefault();
253 }
254
255
256
257
258
259
260
261
262
263
264 public SimpleDateFormat getSimpleDateFormat(String pattern) {
265 return new SimpleDateFormat(pattern);
266 }
267
268
269
270
271
272
273
274
275
276
277
278
279 public SimpleDateFormat getSimpleDateFormat(String pattern, Locale locale) {
280 return new SimpleDateFormat(pattern, locale);
281 }
282
283
284
285
286
287
288
289
290
291
292
293
294
295 public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
296 return DateFormat.getDateTimeInstance(dateStyle, timeStyle);
297 }
298
299
300
301
302 public boolean isWindows() {
303 if (isWindows == null) {
304 String osDotName = getOsName();
305 isWindows = Boolean.valueOf(osDotName.startsWith("Windows"));
306 }
307 return isWindows.booleanValue();
308 }
309
310
311
312
313 public boolean isMacOS() {
314 if (isMacOS == null) {
315 String osDotName = getOsName();
316 isMacOS = Boolean.valueOf(
317 "Mac OS X".equals(osDotName) || "Darwin".equals(osDotName));
318 }
319 return isMacOS.booleanValue();
320 }
321
322 private String getOsName() {
323 return AccessController.doPrivileged(new PrivilegedAction<String>() {
324 public String run() {
325 return getProperty("os.name");
326 }
327 });
328 }
329
330
331
332
333
334
335
336
337
338
339 public void checkPath(String path) throws CorruptObjectException {
340 platformChecker.checkPath(path);
341 }
342 }