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 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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.storage.file.FileBasedConfig;
  57. import org.eclipse.jgit.errors.CorruptObjectException;
  58. import org.eclipse.jgit.lib.Config;
  59. import org.eclipse.jgit.lib.ObjectChecker;
  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. static {
  71. SystemReader r = new Default();
  72. r.init();
  73. DEFAULT = r;
  74. }
  75. private static class Default extends SystemReader {
  76. private volatile String hostname;
  77. public String getenv(String variable) {
  78. return System.getenv(variable);
  79. }
  80. public String getProperty(String key) {
  81. return System.getProperty(key);
  82. }
  83. public FileBasedConfig openSystemConfig(Config parent, FS fs) {
  84. File prefix = fs.gitPrefix();
  85. if (prefix == null) {
  86. return new FileBasedConfig(null, fs) {
  87. public void load() {
  88. // empty, do not load
  89. }
  90. public boolean isOutdated() {
  91. // regular class would bomb here
  92. return false;
  93. }
  94. };
  95. }
  96. File etc = fs.resolve(prefix, "etc"); //$NON-NLS-1$
  97. File config = fs.resolve(etc, "gitconfig"); //$NON-NLS-1$
  98. return new FileBasedConfig(parent, config, 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. if (newReader == null)
  138. INSTANCE = DEFAULT;
  139. else {
  140. newReader.init();
  141. INSTANCE = newReader;
  142. }
  143. }
  144. private ObjectChecker platformChecker;
  145. private void init() {
  146. // Creating ObjectChecker must be deferred. Unit tests change
  147. // behavior of is{Windows,MacOS} in constructor of subclass.
  148. if (platformChecker == null) {
  149. platformChecker = new ObjectChecker()
  150. .setSafeForWindows(isWindows())
  151. .setSafeForMacOS(isMacOS());
  152. }
  153. }
  154. /**
  155. * Gets the hostname of the local host. If no hostname can be found, the
  156. * hostname is set to the default value "localhost".
  157. *
  158. * @return the canonical hostname
  159. */
  160. public abstract String getHostname();
  161. /**
  162. * @param variable system variable to read
  163. * @return value of the system variable
  164. */
  165. public abstract String getenv(String variable);
  166. /**
  167. * @param key of the system property to read
  168. * @return value of the system property
  169. */
  170. public abstract String getProperty(String key);
  171. /**
  172. * @param parent
  173. * a config with values not found directly in the returned config
  174. * @param fs
  175. * the file system abstraction which will be necessary to perform
  176. * certain file system operations.
  177. * @return the git configuration found in the user home
  178. */
  179. public abstract FileBasedConfig openUserConfig(Config parent, FS fs);
  180. /**
  181. * @param parent
  182. * a config with values not found directly in the returned
  183. * config. Null is a reasonable value here.
  184. * @param fs
  185. * the file system abstraction which will be necessary to perform
  186. * certain file system operations.
  187. * @return the gitonfig configuration found in the system-wide "etc"
  188. * directory
  189. */
  190. public abstract FileBasedConfig openSystemConfig(Config parent, FS fs);
  191. /**
  192. * @return the current system time
  193. */
  194. public abstract long getCurrentTime();
  195. /**
  196. * @param when TODO
  197. * @return the local time zone
  198. */
  199. public abstract int getTimezone(long when);
  200. /**
  201. * @return system time zone, possibly mocked for testing
  202. * @since 1.2
  203. */
  204. public TimeZone getTimeZone() {
  205. return TimeZone.getDefault();
  206. }
  207. /**
  208. * @return the locale to use
  209. * @since 1.2
  210. */
  211. public Locale getLocale() {
  212. return Locale.getDefault();
  213. }
  214. /**
  215. * Returns a simple date format instance as specified by the given pattern.
  216. *
  217. * @param pattern
  218. * the pattern as defined in
  219. * {@link SimpleDateFormat#SimpleDateFormat(String)}
  220. * @return the simple date format
  221. * @since 2.0
  222. */
  223. public SimpleDateFormat getSimpleDateFormat(String pattern) {
  224. return new SimpleDateFormat(pattern);
  225. }
  226. /**
  227. * Returns a simple date format instance as specified by the given pattern.
  228. *
  229. * @param pattern
  230. * the pattern as defined in
  231. * {@link SimpleDateFormat#SimpleDateFormat(String)}
  232. * @param locale
  233. * locale to be used for the {@code SimpleDateFormat}
  234. * @return the simple date format
  235. * @since 3.2
  236. */
  237. public SimpleDateFormat getSimpleDateFormat(String pattern, Locale locale) {
  238. return new SimpleDateFormat(pattern, locale);
  239. }
  240. /**
  241. * Returns a date/time format instance for the given styles.
  242. *
  243. * @param dateStyle
  244. * the date style as specified in
  245. * {@link DateFormat#getDateTimeInstance(int, int)}
  246. * @param timeStyle
  247. * the time style as specified in
  248. * {@link DateFormat#getDateTimeInstance(int, int)}
  249. * @return the date format
  250. * @since 2.0
  251. */
  252. public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
  253. return DateFormat.getDateTimeInstance(dateStyle, timeStyle);
  254. }
  255. /**
  256. * @return true if we are running on a Windows.
  257. */
  258. public boolean isWindows() {
  259. String osDotName = AccessController
  260. .doPrivileged(new PrivilegedAction<String>() {
  261. public String run() {
  262. return getProperty("os.name"); //$NON-NLS-1$
  263. }
  264. });
  265. return osDotName.startsWith("Windows"); //$NON-NLS-1$
  266. }
  267. /**
  268. * @return true if we are running on Mac OS X
  269. */
  270. public boolean isMacOS() {
  271. String osDotName = AccessController
  272. .doPrivileged(new PrivilegedAction<String>() {
  273. public String run() {
  274. return getProperty("os.name"); //$NON-NLS-1$
  275. }
  276. });
  277. return "Mac OS X".equals(osDotName) || "Darwin".equals(osDotName); //$NON-NLS-1$ //$NON-NLS-2$
  278. }
  279. /**
  280. * Check tree path entry for validity.
  281. * <p>
  282. * Scans a multi-directory path string such as {@code "src/main.c"}.
  283. *
  284. * @param path path string to scan.
  285. * @throws CorruptObjectException path is invalid.
  286. * @since 3.6
  287. */
  288. public void checkPath(String path) throws CorruptObjectException {
  289. platformChecker.checkPath(path);
  290. }
  291. }