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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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.lib.Config;
  58. /**
  59. * Interface to read values from the system.
  60. * <p>
  61. * When writing unit tests, extending this interface with a custom class
  62. * permits to simulate an access to a system variable or property and
  63. * permits to control the user's global configuration.
  64. * </p>
  65. */
  66. public abstract class SystemReader {
  67. private static SystemReader DEFAULT = new SystemReader() {
  68. private volatile String hostname;
  69. public String getenv(String variable) {
  70. return System.getenv(variable);
  71. }
  72. public String getProperty(String key) {
  73. return System.getProperty(key);
  74. }
  75. public FileBasedConfig openSystemConfig(Config parent, FS fs) {
  76. File prefix = fs.gitPrefix();
  77. if (prefix == null) {
  78. return new FileBasedConfig(null, fs) {
  79. public void load() {
  80. // empty, do not load
  81. }
  82. public boolean isOutdated() {
  83. // regular class would bomb here
  84. return false;
  85. }
  86. };
  87. }
  88. File etc = fs.resolve(prefix, "etc"); //$NON-NLS-1$
  89. File config = fs.resolve(etc, "gitconfig"); //$NON-NLS-1$
  90. return new FileBasedConfig(parent, config, fs);
  91. }
  92. public FileBasedConfig openUserConfig(Config parent, FS fs) {
  93. final File home = fs.userHome();
  94. return new FileBasedConfig(parent, new File(home, ".gitconfig"), fs); //$NON-NLS-1$
  95. }
  96. public String getHostname() {
  97. if (hostname == null) {
  98. try {
  99. InetAddress localMachine = InetAddress.getLocalHost();
  100. hostname = localMachine.getCanonicalHostName();
  101. } catch (UnknownHostException e) {
  102. // we do nothing
  103. hostname = "localhost"; //$NON-NLS-1$
  104. }
  105. assert hostname != null;
  106. }
  107. return hostname;
  108. }
  109. @Override
  110. public long getCurrentTime() {
  111. return System.currentTimeMillis();
  112. }
  113. @Override
  114. public int getTimezone(long when) {
  115. return getTimeZone().getOffset(when) / (60 * 1000);
  116. }
  117. };
  118. private static SystemReader INSTANCE = DEFAULT;
  119. /** @return the live instance to read system properties. */
  120. public static SystemReader getInstance() {
  121. return INSTANCE;
  122. }
  123. /**
  124. * @param newReader
  125. * the new instance to use when accessing properties, or null for
  126. * the default instance.
  127. */
  128. public static void setInstance(SystemReader newReader) {
  129. if (newReader == null)
  130. INSTANCE = DEFAULT;
  131. else
  132. INSTANCE = newReader;
  133. }
  134. /**
  135. * Gets the hostname of the local host. If no hostname can be found, the
  136. * hostname is set to the default value "localhost".
  137. *
  138. * @return the canonical hostname
  139. */
  140. public abstract String getHostname();
  141. /**
  142. * @param variable system variable to read
  143. * @return value of the system variable
  144. */
  145. public abstract String getenv(String variable);
  146. /**
  147. * @param key of the system property to read
  148. * @return value of the system property
  149. */
  150. public abstract String getProperty(String key);
  151. /**
  152. * @param parent
  153. * a config with values not found directly in the returned config
  154. * @param fs
  155. * the file system abstraction which will be necessary to perform
  156. * certain file system operations.
  157. * @return the git configuration found in the user home
  158. */
  159. public abstract FileBasedConfig openUserConfig(Config parent, FS fs);
  160. /**
  161. * @param parent
  162. * a config with values not found directly in the returned
  163. * config. Null is a reasonable value here.
  164. * @param fs
  165. * the file system abstraction which will be necessary to perform
  166. * certain file system operations.
  167. * @return the gitonfig configuration found in the system-wide "etc"
  168. * directory
  169. */
  170. public abstract FileBasedConfig openSystemConfig(Config parent, FS fs);
  171. /**
  172. * @return the current system time
  173. */
  174. public abstract long getCurrentTime();
  175. /**
  176. * @param when TODO
  177. * @return the local time zone
  178. */
  179. public abstract int getTimezone(long when);
  180. /**
  181. * @return system time zone, possibly mocked for testing
  182. * @since 1.2
  183. */
  184. public TimeZone getTimeZone() {
  185. return TimeZone.getDefault();
  186. }
  187. /**
  188. * @return the locale to use
  189. * @since 1.2
  190. */
  191. public Locale getLocale() {
  192. return Locale.getDefault();
  193. }
  194. /**
  195. * Returns a simple date format instance as specified by the given pattern.
  196. *
  197. * @param pattern
  198. * the pattern as defined in
  199. * {@link SimpleDateFormat#SimpleDateFormat(String)}
  200. * @return the simple date format
  201. * @since 2.0
  202. */
  203. public SimpleDateFormat getSimpleDateFormat(String pattern) {
  204. return new SimpleDateFormat(pattern);
  205. }
  206. /**
  207. * Returns a simple date format instance as specified by the given pattern.
  208. *
  209. * @param pattern
  210. * the pattern as defined in
  211. * {@link SimpleDateFormat#SimpleDateFormat(String)}
  212. * @param locale
  213. * locale to be used for the {@code SimpleDateFormat}
  214. * @return the simple date format
  215. * @since 3.2
  216. */
  217. public SimpleDateFormat getSimpleDateFormat(String pattern, Locale locale) {
  218. return new SimpleDateFormat(pattern, locale);
  219. }
  220. /**
  221. * Returns a date/time format instance for the given styles.
  222. *
  223. * @param dateStyle
  224. * the date style as specified in
  225. * {@link DateFormat#getDateTimeInstance(int, int)}
  226. * @param timeStyle
  227. * the time style as specified in
  228. * {@link DateFormat#getDateTimeInstance(int, int)}
  229. * @return the date format
  230. * @since 2.0
  231. */
  232. public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
  233. return DateFormat.getDateTimeInstance(dateStyle, timeStyle);
  234. }
  235. /**
  236. * @return true if we are running on a Windows.
  237. */
  238. public boolean isWindows() {
  239. String osDotName = AccessController
  240. .doPrivileged(new PrivilegedAction<String>() {
  241. public String run() {
  242. return getProperty("os.name"); //$NON-NLS-1$
  243. }
  244. });
  245. return osDotName.startsWith("Windows"); //$NON-NLS-1$
  246. }
  247. /**
  248. * @return true if we are running on Mac OS X
  249. */
  250. public boolean isMacOS() {
  251. String osDotName = AccessController
  252. .doPrivileged(new PrivilegedAction<String>() {
  253. public String run() {
  254. return getProperty("os.name"); //$NON-NLS-1$
  255. }
  256. });
  257. return "Mac OS X".equals(osDotName) || "Darwin".equals(osDotName); //$NON-NLS-1$ //$NON-NLS-2$
  258. }
  259. }