Package org.eclipse.jgit.util
Class SystemReader
- java.lang.Object
-
- org.eclipse.jgit.util.SystemReader
-
- Direct Known Subclasses:
MockSystemReader
,SystemReader.Default
public abstract class SystemReader extends java.lang.Object
Interface to read values from the system.When writing unit tests, extending this interface with a custom class permits to simulate an access to a system variable or property and permits to control the user's global configuration.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
SystemReader.Default
-
Field Summary
Fields Modifier and Type Field Description private static SystemReader
DEFAULT
private static SystemReader
INSTANCE
private static java.lang.Boolean
isMacOS
private static java.lang.Boolean
isWindows
private java.util.concurrent.atomic.AtomicReference<FileBasedConfig>
jgitConfig
private static org.slf4j.Logger
LOG
private ObjectChecker
platformChecker
private java.util.concurrent.atomic.AtomicReference<FileBasedConfig>
systemConfig
private java.util.concurrent.atomic.AtomicReference<FileBasedConfig>
userConfig
-
Constructor Summary
Constructors Constructor Description SystemReader()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description void
checkPath(byte[] path)
Check tree path entry for validity.void
checkPath(java.lang.String path)
Check tree path entry for validity.MonotonicClock
getClock()
Get clock instance preferred by this system.abstract long
getCurrentTime()
Get the current system timejava.text.DateFormat
getDateTimeInstance(int dateStyle, int timeStyle)
Returns a date/time format instance for the given styles.abstract java.lang.String
getenv(java.lang.String variable)
Get value of the system variableabstract java.lang.String
getHostname()
Gets the hostname of the local host.static SystemReader
getInstance()
Get the current SystemReader instanceStoredConfig
getJGitConfig()
Get the jgit configuration located at $XDG_CONFIG_HOME/jgit/config.java.util.Locale
getLocale()
Get the locale to useprivate java.lang.String
getOsName()
abstract java.lang.String
getProperty(java.lang.String key)
Get value of the system propertyjava.text.SimpleDateFormat
getSimpleDateFormat(java.lang.String pattern)
Returns a simple date format instance as specified by the given pattern.java.text.SimpleDateFormat
getSimpleDateFormat(java.lang.String pattern, java.util.Locale locale)
Returns a simple date format instance as specified by the given pattern.StoredConfig
getSystemConfig()
Get the gitconfig configuration found in the system-wide "etc" directory.abstract int
getTimezone(long when)
Get the local time zonejava.util.TimeZone
getTimeZone()
Get system time zone, possibly mocked for testingStoredConfig
getUserConfig()
Get the git configuration found in the user home.private void
init()
boolean
isMacOS()
Whether we are running on Mac OS Xboolean
isWindows()
Whether we are running on Windows.abstract FileBasedConfig
openJGitConfig(Config parent, FS fs)
Open the jgit configuration located at $XDG_CONFIG_HOME/jgit/config.abstract FileBasedConfig
openSystemConfig(Config parent, FS fs)
Open the gitconfig configuration found in the system-wide "etc" directory.abstract FileBasedConfig
openUserConfig(Config parent, FS fs)
Open the git configuration found in the user home.static void
setInstance(SystemReader newReader)
Set a new SystemReader instance to use when accessing properties.protected void
setPlatformChecker()
Should be used in tests when the platform is explicitly changed.private void
updateAll(Config config)
Update config and its parents if they seem modified
-
-
-
Field Detail
-
LOG
private static final org.slf4j.Logger LOG
-
DEFAULT
private static final SystemReader DEFAULT
-
isMacOS
private static java.lang.Boolean isMacOS
-
isWindows
private static java.lang.Boolean isWindows
-
INSTANCE
private static volatile SystemReader INSTANCE
-
platformChecker
private ObjectChecker platformChecker
-
systemConfig
private java.util.concurrent.atomic.AtomicReference<FileBasedConfig> systemConfig
-
userConfig
private java.util.concurrent.atomic.AtomicReference<FileBasedConfig> userConfig
-
jgitConfig
private java.util.concurrent.atomic.AtomicReference<FileBasedConfig> jgitConfig
-
-
Method Detail
-
getInstance
public static SystemReader getInstance()
Get the current SystemReader instance- Returns:
- the current SystemReader instance.
-
setInstance
public static void setInstance(SystemReader newReader)
Set a new SystemReader instance to use when accessing properties.- Parameters:
newReader
- the new instance to use when accessing properties, or null for the default instance.
-
init
private void init()
-
setPlatformChecker
protected final void setPlatformChecker()
Should be used in tests when the platform is explicitly changed.- Since:
- 3.6
-
getHostname
public abstract java.lang.String getHostname()
Gets the hostname of the local host. If no hostname can be found, the hostname is set to the default value "localhost".- Returns:
- the canonical hostname
-
getenv
public abstract java.lang.String getenv(java.lang.String variable)
Get value of the system variable- Parameters:
variable
- system variable to read- Returns:
- value of the system variable
-
getProperty
public abstract java.lang.String getProperty(java.lang.String key)
Get value of the system property- Parameters:
key
- of the system property to read- Returns:
- value of the system property
-
openUserConfig
public abstract FileBasedConfig openUserConfig(Config parent, FS fs)
Open the git configuration found in the user home. UsegetUserConfig()
to get the current git configuration in the user home since it manages automatic reloading when the gitconfig file was modified and avoids unnecessary reloads.- Parameters:
parent
- a config with values not found directly in the returned configfs
- the file system abstraction which will be necessary to perform certain file system operations.- Returns:
- the git configuration found in the user home
-
openSystemConfig
public abstract FileBasedConfig openSystemConfig(Config parent, FS fs)
Open the gitconfig configuration found in the system-wide "etc" directory. UsegetSystemConfig()
to get the current system-wide git configuration since it manages automatic reloading when the gitconfig file was modified and avoids unnecessary reloads.- Parameters:
parent
- a config with values not found directly in the returned config. Null is a reasonable value here.fs
- the file system abstraction which will be necessary to perform certain file system operations.- Returns:
- the gitconfig configuration found in the system-wide "etc" directory
-
openJGitConfig
public abstract FileBasedConfig openJGitConfig(Config parent, FS fs)
Open the jgit configuration located at $XDG_CONFIG_HOME/jgit/config. UsegetJGitConfig()
to get the current jgit configuration in the user home since it manages automatic reloading when the jgit config file was modified and avoids unnecessary reloads.- Parameters:
parent
- a config with values not found directly in the returned configfs
- the file system abstraction which will be necessary to perform certain file system operations.- Returns:
- the jgit configuration located at $XDG_CONFIG_HOME/jgit/config
- Since:
- 5.5.2
-
getUserConfig
public StoredConfig getUserConfig() throws ConfigInvalidException, java.io.IOException
Get the git configuration found in the user home. The configuration will be reloaded automatically if the configuration file was modified. Also reloads the system config if the system config file was modified. If the configuration file wasn't modified returns the cached configuration.- Returns:
- the git configuration found in the user home
- Throws:
ConfigInvalidException
- if configuration is invalidjava.io.IOException
- if something went wrong when reading files- Since:
- 5.1.9
-
getJGitConfig
public StoredConfig getJGitConfig() throws ConfigInvalidException, java.io.IOException
Get the jgit configuration located at $XDG_CONFIG_HOME/jgit/config. The configuration will be reloaded automatically if the configuration file was modified. If the configuration file wasn't modified returns the cached configuration.- Returns:
- the jgit configuration located at $XDG_CONFIG_HOME/jgit/config
- Throws:
ConfigInvalidException
- if configuration is invalidjava.io.IOException
- if something went wrong when reading files- Since:
- 5.5.2
-
getSystemConfig
public StoredConfig getSystemConfig() throws ConfigInvalidException, java.io.IOException
Get the gitconfig configuration found in the system-wide "etc" directory. The configuration will be reloaded automatically if the configuration file was modified otherwise returns the cached system level config.- Returns:
- the gitconfig configuration found in the system-wide "etc" directory
- Throws:
ConfigInvalidException
- if configuration is invalidjava.io.IOException
- if something went wrong when reading files- Since:
- 5.1.9
-
updateAll
private void updateAll(Config config) throws ConfigInvalidException, java.io.IOException
Update config and its parents if they seem modified- Parameters:
config
- configuration to reload if outdated- Throws:
ConfigInvalidException
- if configuration is invalidjava.io.IOException
- if something went wrong when reading files
-
getCurrentTime
public abstract long getCurrentTime()
Get the current system time- Returns:
- the current system time
-
getClock
public MonotonicClock getClock()
Get clock instance preferred by this system.- Returns:
- clock instance preferred by this system.
- Since:
- 4.6
-
getTimezone
public abstract int getTimezone(long when)
Get the local time zone- Parameters:
when
- a system timestamp- Returns:
- the local time zone
-
getTimeZone
public java.util.TimeZone getTimeZone()
Get system time zone, possibly mocked for testing- Returns:
- system time zone, possibly mocked for testing
- Since:
- 1.2
-
getLocale
public java.util.Locale getLocale()
Get the locale to use- Returns:
- the locale to use
- Since:
- 1.2
-
getSimpleDateFormat
public java.text.SimpleDateFormat getSimpleDateFormat(java.lang.String pattern)
Returns a simple date format instance as specified by the given pattern.- Parameters:
pattern
- the pattern as defined inSimpleDateFormat(String)
- Returns:
- the simple date format
- Since:
- 2.0
-
getSimpleDateFormat
public java.text.SimpleDateFormat getSimpleDateFormat(java.lang.String pattern, java.util.Locale locale)
Returns a simple date format instance as specified by the given pattern.- Parameters:
pattern
- the pattern as defined inSimpleDateFormat(String)
locale
- locale to be used for theSimpleDateFormat
- Returns:
- the simple date format
- Since:
- 3.2
-
getDateTimeInstance
public java.text.DateFormat getDateTimeInstance(int dateStyle, int timeStyle)
Returns a date/time format instance for the given styles.- Parameters:
dateStyle
- the date style as specified inDateFormat.getDateTimeInstance(int, int)
timeStyle
- the time style as specified inDateFormat.getDateTimeInstance(int, int)
- Returns:
- the date format
- Since:
- 2.0
-
isWindows
public boolean isWindows()
Whether we are running on Windows.- Returns:
- true if we are running on Windows.
-
isMacOS
public boolean isMacOS()
Whether we are running on Mac OS X- Returns:
- true if we are running on Mac OS X
-
getOsName
private java.lang.String getOsName()
-
checkPath
public void checkPath(java.lang.String path) throws CorruptObjectException
Check tree path entry for validity.Scans a multi-directory path string such as
"src/main.c"
.- Parameters:
path
- path string to scan.- Throws:
CorruptObjectException
- path is invalid.- Since:
- 3.6
-
checkPath
public void checkPath(byte[] path) throws CorruptObjectException
Check tree path entry for validity.Scans a multi-directory path string such as
"src/main.c"
.- Parameters:
path
- path string to scan.- Throws:
CorruptObjectException
- path is invalid.- Since:
- 4.2
-
-