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

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