選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

SystemReader.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * Copyright (C) 2009, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2009, Yann Simon <yann.simon.fr@gmail.com>
  5. * Copyright (C) 2012, Daniel Megert <daniel_megert@ch.ibm.com>
  6. * and other copyright owners as documented in the project's IP log.
  7. *
  8. * This program and the accompanying materials are made available
  9. * under the terms of the Eclipse Distribution License v1.0 which
  10. * accompanies this distribution, is reproduced below, and is
  11. * available at http://www.eclipse.org/org/documents/edl-v10.php
  12. *
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or
  16. * without modification, are permitted provided that the following
  17. * conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright
  20. * notice, this list of conditions and the following disclaimer.
  21. *
  22. * - Redistributions in binary form must reproduce the above
  23. * copyright notice, this list of conditions and the following
  24. * disclaimer in the documentation and/or other materials provided
  25. * with the distribution.
  26. *
  27. * - Neither the name of the Eclipse Foundation, Inc. nor the
  28. * names of its contributors may be used to endorse or promote
  29. * products derived from this software without specific prior
  30. * written permission.
  31. *
  32. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  33. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  34. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  35. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  36. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  37. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  38. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  39. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  40. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  41. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  42. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  43. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  44. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  45. */
  46. package org.eclipse.jgit.util;
  47. import java.io.File;
  48. import java.net.InetAddress;
  49. import java.net.UnknownHostException;
  50. import java.security.AccessController;
  51. import java.security.PrivilegedAction;
  52. import java.text.DateFormat;
  53. import java.text.SimpleDateFormat;
  54. import java.util.Locale;
  55. import java.util.TimeZone;
  56. import org.eclipse.jgit.errors.CorruptObjectException;
  57. import org.eclipse.jgit.lib.Config;
  58. import org.eclipse.jgit.lib.ObjectChecker;
  59. import org.eclipse.jgit.storage.file.FileBasedConfig;
  60. import org.eclipse.jgit.util.time.MonotonicClock;
  61. import org.eclipse.jgit.util.time.MonotonicSystemClock;
  62. /**
  63. * Interface to read values from the system.
  64. * <p>
  65. * When writing unit tests, extending this interface with a custom class
  66. * permits to simulate an access to a system variable or property and
  67. * permits to control the user's global configuration.
  68. * </p>
  69. */
  70. public abstract class SystemReader {
  71. private static final SystemReader DEFAULT;
  72. private static Boolean isMacOS;
  73. private static Boolean isWindows;
  74. static {
  75. SystemReader r = new Default();
  76. r.init();
  77. DEFAULT = r;
  78. }
  79. private static class Default extends SystemReader {
  80. private volatile String hostname;
  81. @Override
  82. public String getenv(String variable) {
  83. return System.getenv(variable);
  84. }
  85. @Override
  86. public String getProperty(String key) {
  87. return System.getProperty(key);
  88. }
  89. @Override
  90. public FileBasedConfig openSystemConfig(Config parent, FS fs) {
  91. File configFile = fs.getGitSystemConfig();
  92. if (configFile == null) {
  93. return new FileBasedConfig(null, fs) {
  94. @Override
  95. public void load() {
  96. // empty, do not load
  97. }
  98. @Override
  99. public boolean isOutdated() {
  100. // regular class would bomb here
  101. return false;
  102. }
  103. };
  104. }
  105. return new FileBasedConfig(parent, configFile, fs);
  106. }
  107. @Override
  108. public FileBasedConfig openUserConfig(Config parent, FS fs) {
  109. final File home = fs.userHome();
  110. return new FileBasedConfig(parent, new File(home, ".gitconfig"), fs); //$NON-NLS-1$
  111. }
  112. @Override
  113. public String getHostname() {
  114. if (hostname == null) {
  115. try {
  116. InetAddress localMachine = InetAddress.getLocalHost();
  117. hostname = localMachine.getCanonicalHostName();
  118. } catch (UnknownHostException e) {
  119. // we do nothing
  120. hostname = "localhost"; //$NON-NLS-1$
  121. }
  122. assert hostname != null;
  123. }
  124. return hostname;
  125. }
  126. @Override
  127. public long getCurrentTime() {
  128. return System.currentTimeMillis();
  129. }
  130. @Override
  131. public int getTimezone(long when) {
  132. return getTimeZone().getOffset(when) / (60 * 1000);
  133. }
  134. }
  135. private static SystemReader INSTANCE = DEFAULT;
  136. /**
  137. * Get time since epoch, with up to millisecond resolution.
  138. *
  139. * @return time since epoch, with up to millisecond resolution.
  140. */
  141. public static SystemReader getInstance() {
  142. return INSTANCE;
  143. }
  144. /**
  145. * Set the new instance to use when accessing properties.
  146. *
  147. * @param newReader
  148. * the new instance to use when accessing properties, or null for
  149. * the default instance.
  150. */
  151. public static void setInstance(SystemReader newReader) {
  152. isMacOS = null;
  153. isWindows = null;
  154. if (newReader == null)
  155. INSTANCE = DEFAULT;
  156. else {
  157. newReader.init();
  158. INSTANCE = newReader;
  159. }
  160. }
  161. private ObjectChecker platformChecker;
  162. private void init() {
  163. // Creating ObjectChecker must be deferred. Unit tests change
  164. // behavior of is{Windows,MacOS} in constructor of subclass.
  165. if (platformChecker == null)
  166. setPlatformChecker();
  167. }
  168. /**
  169. * Should be used in tests when the platform is explicitly changed.
  170. *
  171. * @since 3.6
  172. */
  173. protected final void setPlatformChecker() {
  174. platformChecker = new ObjectChecker()
  175. .setSafeForWindows(isWindows())
  176. .setSafeForMacOS(isMacOS());
  177. }
  178. /**
  179. * Gets the hostname of the local host. If no hostname can be found, the
  180. * hostname is set to the default value "localhost".
  181. *
  182. * @return the canonical hostname
  183. */
  184. public abstract String getHostname();
  185. /**
  186. * Get value of the system variable
  187. *
  188. * @param variable
  189. * system variable to read
  190. * @return value of the system variable
  191. */
  192. public abstract String getenv(String variable);
  193. /**
  194. * Get value of the system property
  195. *
  196. * @param key
  197. * of the system property to read
  198. * @return value of the system property
  199. */
  200. public abstract String getProperty(String key);
  201. /**
  202. * Open the git configuration found in the user home
  203. *
  204. * @param parent
  205. * a config with values not found directly in the returned config
  206. * @param fs
  207. * the file system abstraction which will be necessary to perform
  208. * certain file system operations.
  209. * @return the git configuration found in the user home
  210. */
  211. public abstract FileBasedConfig openUserConfig(Config parent, FS fs);
  212. /**
  213. * Open the gitonfig configuration found in the system-wide "etc" directory
  214. *
  215. * @param parent
  216. * a config with values not found directly in the returned
  217. * config. Null is a reasonable value here.
  218. * @param fs
  219. * the file system abstraction which will be necessary to perform
  220. * certain file system operations.
  221. * @return the gitonfig configuration found in the system-wide "etc"
  222. * directory
  223. */
  224. public abstract FileBasedConfig openSystemConfig(Config parent, FS fs);
  225. /**
  226. * Get the current system time
  227. *
  228. * @return the current system time
  229. */
  230. public abstract long getCurrentTime();
  231. /**
  232. * Get clock instance preferred by this system.
  233. *
  234. * @return clock instance preferred by this system.
  235. * @since 4.6
  236. */
  237. public MonotonicClock getClock() {
  238. return new MonotonicSystemClock();
  239. }
  240. /**
  241. * Get the local time zone
  242. *
  243. * @param when
  244. * a system timestamp
  245. * @return the local time zone
  246. */
  247. public abstract int getTimezone(long when);
  248. /**
  249. * Get system time zone, possibly mocked for testing
  250. *
  251. * @return system time zone, possibly mocked for testing
  252. * @since 1.2
  253. */
  254. public TimeZone getTimeZone() {
  255. return TimeZone.getDefault();
  256. }
  257. /**
  258. * Get the locale to use
  259. *
  260. * @return the locale to use
  261. * @since 1.2
  262. */
  263. public Locale getLocale() {
  264. return Locale.getDefault();
  265. }
  266. /**
  267. * Returns a simple date format instance as specified by the given pattern.
  268. *
  269. * @param pattern
  270. * the pattern as defined in
  271. * {@link java.text.SimpleDateFormat#SimpleDateFormat(String)}
  272. * @return the simple date format
  273. * @since 2.0
  274. */
  275. public SimpleDateFormat getSimpleDateFormat(String pattern) {
  276. return new SimpleDateFormat(pattern);
  277. }
  278. /**
  279. * Returns a simple date format instance as specified by the given pattern.
  280. *
  281. * @param pattern
  282. * the pattern as defined in
  283. * {@link java.text.SimpleDateFormat#SimpleDateFormat(String)}
  284. * @param locale
  285. * locale to be used for the {@code SimpleDateFormat}
  286. * @return the simple date format
  287. * @since 3.2
  288. */
  289. public SimpleDateFormat getSimpleDateFormat(String pattern, Locale locale) {
  290. return new SimpleDateFormat(pattern, locale);
  291. }
  292. /**
  293. * Returns a date/time format instance for the given styles.
  294. *
  295. * @param dateStyle
  296. * the date style as specified in
  297. * {@link java.text.DateFormat#getDateTimeInstance(int, int)}
  298. * @param timeStyle
  299. * the time style as specified in
  300. * {@link java.text.DateFormat#getDateTimeInstance(int, int)}
  301. * @return the date format
  302. * @since 2.0
  303. */
  304. public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
  305. return DateFormat.getDateTimeInstance(dateStyle, timeStyle);
  306. }
  307. /**
  308. * Whether we are running on Windows.
  309. *
  310. * @return true if we are running on Windows.
  311. */
  312. public boolean isWindows() {
  313. if (isWindows == null) {
  314. String osDotName = getOsName();
  315. isWindows = Boolean.valueOf(osDotName.startsWith("Windows")); //$NON-NLS-1$
  316. }
  317. return isWindows.booleanValue();
  318. }
  319. /**
  320. * Whether we are running on Mac OS X
  321. *
  322. * @return true if we are running on Mac OS X
  323. */
  324. public boolean isMacOS() {
  325. if (isMacOS == null) {
  326. String osDotName = getOsName();
  327. isMacOS = Boolean.valueOf(
  328. "Mac OS X".equals(osDotName) || "Darwin".equals(osDotName)); //$NON-NLS-1$ //$NON-NLS-2$
  329. }
  330. return isMacOS.booleanValue();
  331. }
  332. private String getOsName() {
  333. return AccessController.doPrivileged(new PrivilegedAction<String>() {
  334. @Override
  335. public String run() {
  336. return getProperty("os.name"); //$NON-NLS-1$
  337. }
  338. });
  339. }
  340. /**
  341. * Check tree path entry for validity.
  342. * <p>
  343. * Scans a multi-directory path string such as {@code "src/main.c"}.
  344. *
  345. * @param path path string to scan.
  346. * @throws org.eclipse.jgit.errors.CorruptObjectException path is invalid.
  347. * @since 3.6
  348. */
  349. public void checkPath(String path) throws CorruptObjectException {
  350. platformChecker.checkPath(path);
  351. }
  352. /**
  353. * Check tree path entry for validity.
  354. * <p>
  355. * Scans a multi-directory path string such as {@code "src/main.c"}.
  356. *
  357. * @param path
  358. * path string to scan.
  359. * @throws org.eclipse.jgit.errors.CorruptObjectException
  360. * path is invalid.
  361. * @since 4.2
  362. */
  363. public void checkPath(byte[] path) throws CorruptObjectException {
  364. platformChecker.checkPath(path, 0, path.length);
  365. }
  366. }