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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.
  126. */
  127. public static void setInstance(SystemReader newReader) {
  128. if (newReader == null)
  129. INSTANCE = DEFAULT;
  130. else
  131. INSTANCE = newReader;
  132. }
  133. /**
  134. * Gets the hostname of the local host. If no hostname can be found, the
  135. * hostname is set to the default value "localhost".
  136. *
  137. * @return the canonical hostname
  138. */
  139. public abstract String getHostname();
  140. /**
  141. * @param variable system variable to read
  142. * @return value of the system variable
  143. */
  144. public abstract String getenv(String variable);
  145. /**
  146. * @param key of the system property to read
  147. * @return value of the system property
  148. */
  149. public abstract String getProperty(String key);
  150. /**
  151. * @param parent
  152. * a config with values not found directly in the returned config
  153. * @param fs
  154. * the file system abstraction which will be necessary to perform
  155. * certain file system operations.
  156. * @return the git configuration found in the user home
  157. */
  158. public abstract FileBasedConfig openUserConfig(Config parent, FS fs);
  159. /**
  160. * @param parent
  161. * a config with values not found directly in the returned
  162. * config. Null is a reasonable value here.
  163. * @param fs
  164. * the file system abstraction which will be necessary to perform
  165. * certain file system operations.
  166. * @return the gitonfig configuration found in the system-wide "etc"
  167. * directory
  168. */
  169. public abstract FileBasedConfig openSystemConfig(Config parent, FS fs);
  170. /**
  171. * @return the current system time
  172. */
  173. public abstract long getCurrentTime();
  174. /**
  175. * @param when TODO
  176. * @return the local time zone
  177. */
  178. public abstract int getTimezone(long when);
  179. /**
  180. * @return system time zone, possibly mocked for testing
  181. * @since 1.2
  182. */
  183. public TimeZone getTimeZone() {
  184. return TimeZone.getDefault();
  185. }
  186. /**
  187. * @return the locale to use
  188. * @since 1.2
  189. */
  190. public Locale getLocale() {
  191. return Locale.getDefault();
  192. }
  193. /**
  194. * Returns a simple date format instance as specified by the given pattern.
  195. *
  196. * @param pattern
  197. * the pattern as defined in
  198. * {@link SimpleDateFormat#SimpleDateFormat(String)}
  199. * @return the simple date format
  200. * @since 2.0
  201. */
  202. public SimpleDateFormat getSimpleDateFormat(String pattern) {
  203. return new SimpleDateFormat(pattern);
  204. }
  205. /**
  206. * Returns a date/time format instance for the given styles.
  207. *
  208. * @param dateStyle
  209. * the date style as specified in
  210. * {@link DateFormat#getDateTimeInstance(int, int)}
  211. * @param timeStyle
  212. * the time style as specified in
  213. * {@link DateFormat#getDateTimeInstance(int, int)}
  214. * @return the date format
  215. * @since 2.0
  216. */
  217. public DateFormat getDateTimeInstance(int dateStyle, int timeStyle) {
  218. return DateFormat.getDateTimeInstance(dateStyle, timeStyle);
  219. }
  220. /**
  221. * @return true if we are running on a Windows.
  222. */
  223. public boolean isWindows() {
  224. String osDotName = AccessController
  225. .doPrivileged(new PrivilegedAction<String>() {
  226. public String run() {
  227. return getProperty("os.name"); //$NON-NLS-1$
  228. }
  229. });
  230. return osDotName.startsWith("Windows"); //$NON-NLS-1$
  231. }
  232. /**
  233. * @return true if we are running on Mac OS X
  234. */
  235. public boolean isMacOS() {
  236. String osDotName = AccessController
  237. .doPrivileged(new PrivilegedAction<String>() {
  238. public String run() {
  239. return getProperty("os.name"); //$NON-NLS-1$
  240. }
  241. });
  242. return "Mac OS X".equals(osDotName) || "Darwin".equals(osDotName); //$NON-NLS-1$ //$NON-NLS-2$
  243. }
  244. }