You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SystemReader.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. public String getenv(String variable) {
  82. return System.getenv(variable);
  83. }
  84. public String getProperty(String key) {
  85. return System.getProperty(key);
  86. }
  87. public FileBasedConfig openSystemConfig(Config parent, FS fs) {
  88. File configFile = fs.getGitSystemConfig();
  89. if (configFile == null) {
  90. return new FileBasedConfig(null, fs) {
  91. public void load() {
  92. // empty, do not load
  93. }
  94. public boolean isOutdated() {
  95. // regular class would bomb here
  96. return false;
  97. }
  98. };
  99. }
  100. return new FileBasedConfig(parent, configFile, fs);
  101. }
  102. public FileBasedConfig openUserConfig(Config parent, FS fs) {
  103. final File home = fs.userHome();
  104. return new FileBasedConfig(parent, new File(home, ".gitconfig"), fs); //$NON-NLS-1$
  105. }
  106. public String getHostname() {
  107. if (hostname == null) {
  108. try {
  109. InetAddress localMachine = InetAddress.getLocalHost();
  110. hostname = localMachine.getCanonicalHostName();
  111. } catch (UnknownHostException e) {
  112. // we do nothing
  113. hostname = "localhost"; //$NON-NLS-1$
  114. }
  115. assert hostname != null;
  116. }
  117. return hostname;
  118. }
  119. @Override
  120. public long getCurrentTime() {
  121. return System.currentTimeMillis();
  122. }
  123. @Override
  124. public int getTimezone(long when) {
  125. return getTimeZone().getOffset(when) / (60 * 1000);
  126. }
  127. }
  128. private static SystemReader INSTANCE = DEFAULT;
  129. /** @return the live instance to read system properties. */
  130. public static SystemReader getInstance() {
  131. return INSTANCE;
  132. }
  133. /**
  134. * @param newReader
  135. * the new instance to use when accessing properties, or null for
  136. * the default instance.
  137. */
  138. public static void setInstance(SystemReader newReader) {
  139. isMacOS = null;
  140. isWindows = null;
  141. if (newReader == null)
  142. INSTANCE = DEFAULT;
  143. else {
  144. newReader.init();
  145. INSTANCE = newReader;
  146. }
  147. }
  148. private ObjectChecker platformChecker;
  149. private void init() {
  150. // Creating ObjectChecker must be deferred. Unit tests change
  151. // behavior of is{Windows,MacOS} in constructor of subclass.
  152. if (platformChecker == null)
  153. setPlatformChecker();
  154. }
  155. /**
  156. * Should be used in tests when the platform is explicitly changed.
  157. *
  158. * @since 3.6
  159. */
  160. protected final void setPlatformChecker() {
  161. platformChecker = new ObjectChecker()
  162. .setSafeForWindows(isWindows())
  163. .setSafeForMacOS(isMacOS());
  164. }
  165. /**
  166. * Gets the hostname of the local host. If no hostname can be found, the
  167. * hostname is set to the default value "localhost".
  168. *
  169. * @return the canonical hostname
  170. */
  171. public abstract String getHostname();
  172. /**
  173. * @param variable system variable to read
  174. * @return value of the system variable
  175. */
  176. public abstract String getenv(String variable);
  177. /**
  178. * @param key of the system property to read
  179. * @return value of the system property
  180. */
  181. public abstract String getProperty(String key);
  182. /**
  183. * @param parent
  184. * a config with values not found directly in the returned config
  185. * @param fs
  186. * the file system abstraction which will be necessary to perform
  187. * certain file system operations.
  188. * @return the git configuration found in the user home
  189. */
  190. public abstract FileBasedConfig openUserConfig(Config parent, FS fs);
  191. /**
  192. * @param parent
  193. * a config with values not found directly in the returned
  194. * config. Null is a reasonable value here.
  195. * @param fs
  196. * the file system abstraction which will be necessary to perform
  197. * certain file system operations.
  198. * @return the gitonfig configuration found in the system-wide "etc"
  199. * directory
  200. */
  201. public abstract FileBasedConfig openSystemConfig(Config parent, FS fs);
  202. /**
  203. * @return the current system time
  204. */
  205. public abstract long getCurrentTime();
  206. /**
  207. * @return clock instance preferred by this system.
  208. * @since 4.6
  209. */
  210. public MonotonicClock getClock() {
  211. return new MonotonicSystemClock();
  212. }
  213. /**
  214. * @param when TODO
  215. * @return the local time zone
  216. */
  217. public abstract int getTimezone(long when);
  218. /**
  219. * @return system time zone, possibly mocked for testing
  220. * @since 1.2
  221. */
  222. public TimeZone getTimeZone() {
  223. return TimeZone.getDefault();
  224. }
  225. /**
  226. * @return the locale to use
  227. * @since 1.2
  228. */
  229. public Locale getLocale() {
  230. return Locale.getDefault();
  231. }
  232. /**
  233. * Returns a simple date format instance as specified by the given pattern.
  234. *
  235. * @param pattern
  236. * the pattern as defined in
  237. * {@link SimpleDateFormat#SimpleDateFormat(String)}
  238. * @return the simple date format
  239. * @since 2.0
  240. */
  241. public SimpleDateFormat getSimpleDateFormat(String pattern) {
  242. return new SimpleDateFormat(pattern);
  243. }
  244. /**
  245. * Returns a simple date format instance as specified by the given pattern.
  246. *
  247. * @param pattern
  248. * the pattern as defined in
  249. * {@link SimpleDateFormat#SimpleDateFormat(String)}
  250. * @param locale
  251. * locale to be used for the {@code SimpleDateFormat}
  252. * @return the simple date format
  253. * @since 3.2
  254. */
  255. public SimpleDateFormat getSimpleDateFormat(String pattern, Locale locale) {
  256. return new SimpleDateFormat(pattern, locale);
  257. }
  258. /**
  259. * Returns a date/time format instance for the given styles.
  260. *
  261. * @param dateStyle
  262. * the date style as specified in
  263. * {@link DateFormat#getDateTimeInstance(int, int)}
  264. * @param timeStyle
  265. * the time style as specified in
  266. * {@link DateFormat#getDateTimeInstance(int, int)}
  267. * @return the date format
  268. * @since 2.0
  269. */
  270. public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
  271. return DateFormat.getDateTimeInstance(dateStyle, timeStyle);
  272. }
  273. /**
  274. * @return true if we are running on a Windows.
  275. */
  276. public boolean isWindows() {
  277. if (isWindows == null) {
  278. String osDotName = getOsName();
  279. isWindows = Boolean.valueOf(osDotName.startsWith("Windows")); //$NON-NLS-1$
  280. }
  281. return isWindows.booleanValue();
  282. }
  283. /**
  284. * @return true if we are running on Mac OS X
  285. */
  286. public boolean isMacOS() {
  287. if (isMacOS == null) {
  288. String osDotName = getOsName();
  289. isMacOS = Boolean.valueOf(
  290. "Mac OS X".equals(osDotName) || "Darwin".equals(osDotName)); //$NON-NLS-1$ //$NON-NLS-2$
  291. }
  292. return isMacOS.booleanValue();
  293. }
  294. private String getOsName() {
  295. return AccessController.doPrivileged(new PrivilegedAction<String>() {
  296. public String run() {
  297. return getProperty("os.name"); //$NON-NLS-1$
  298. }
  299. });
  300. }
  301. /**
  302. * Check tree path entry for validity.
  303. * <p>
  304. * Scans a multi-directory path string such as {@code "src/main.c"}.
  305. *
  306. * @param path path string to scan.
  307. * @throws CorruptObjectException path is invalid.
  308. * @since 3.6
  309. */
  310. public void checkPath(String path) throws CorruptObjectException {
  311. platformChecker.checkPath(path);
  312. }
  313. /**
  314. * Check tree path entry for validity.
  315. * <p>
  316. * Scans a multi-directory path string such as {@code "src/main.c"}.
  317. *
  318. * @param path
  319. * path string to scan.
  320. * @throws CorruptObjectException
  321. * path is invalid.
  322. * @since 4.2
  323. */
  324. public void checkPath(byte[] path) throws CorruptObjectException {
  325. platformChecker.checkPath(path, 0, path.length);
  326. }
  327. }