public abstract class SystemReader
extends java.lang.Object
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.
Modifier and Type | Class and Description |
---|---|
private static class |
SystemReader.Default |
Modifier and Type | Field and 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 and Description |
---|
SystemReader() |
Modifier and Type | Method and 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 time
|
java.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 variable
|
abstract java.lang.String |
getHostname()
Gets the hostname of the local host.
|
static SystemReader |
getInstance()
Get the current SystemReader instance
|
StoredConfig |
getJGitConfig()
Get the jgit configuration located at $XDG_CONFIG_HOME/jgit/config.
|
java.util.Locale |
getLocale()
Get the locale to use
|
private java.lang.String |
getOsName() |
abstract java.lang.String |
getProperty(java.lang.String key)
Get value of the system property
|
java.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.
|
java.util.TimeZone |
getTimeZone()
Get system time zone, possibly mocked for testing
|
abstract int |
getTimezone(long when)
Get the local time zone
|
StoredConfig |
getUserConfig()
Get the git configuration found in the user home.
|
private void |
init() |
boolean |
isMacOS()
Whether we are running on Mac OS X
|
boolean |
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
|
private static final org.slf4j.Logger LOG
private static final SystemReader DEFAULT
private static java.lang.Boolean isMacOS
private static java.lang.Boolean isWindows
private static volatile SystemReader INSTANCE
private ObjectChecker platformChecker
private java.util.concurrent.atomic.AtomicReference<FileBasedConfig> systemConfig
private java.util.concurrent.atomic.AtomicReference<FileBasedConfig> userConfig
private java.util.concurrent.atomic.AtomicReference<FileBasedConfig> jgitConfig
public static SystemReader getInstance()
public static void setInstance(SystemReader newReader)
newReader
- the new instance to use when accessing properties, or null for
the default instance.private void init()
protected final void setPlatformChecker()
public abstract java.lang.String getHostname()
public abstract java.lang.String getenv(java.lang.String variable)
variable
- system variable to readpublic abstract java.lang.String getProperty(java.lang.String key)
key
- of the system property to readpublic abstract FileBasedConfig openUserConfig(Config parent, FS fs)
getUserConfig()
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.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.public abstract FileBasedConfig openSystemConfig(Config parent, FS fs)
getSystemConfig()
to get the current system-wide
git configuration since it manages automatic reloading when the gitconfig
file was modified and avoids unnecessary reloads.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.public abstract FileBasedConfig openJGitConfig(Config parent, FS fs)
getJGitConfig()
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.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.public StoredConfig getUserConfig() throws ConfigInvalidException, java.io.IOException
ConfigInvalidException
- if configuration is invalidjava.io.IOException
- if something went wrong when reading filespublic StoredConfig getJGitConfig() throws ConfigInvalidException, java.io.IOException
ConfigInvalidException
- if configuration is invalidjava.io.IOException
- if something went wrong when reading filespublic StoredConfig getSystemConfig() throws ConfigInvalidException, java.io.IOException
ConfigInvalidException
- if configuration is invalidjava.io.IOException
- if something went wrong when reading filesprivate void updateAll(Config config) throws ConfigInvalidException, java.io.IOException
config
- configuration to reload if outdatedConfigInvalidException
- if configuration is invalidjava.io.IOException
- if something went wrong when reading filespublic abstract long getCurrentTime()
public MonotonicClock getClock()
public abstract int getTimezone(long when)
when
- a system timestamppublic java.util.TimeZone getTimeZone()
public java.util.Locale getLocale()
public java.text.SimpleDateFormat getSimpleDateFormat(java.lang.String pattern)
pattern
- the pattern as defined in
SimpleDateFormat.SimpleDateFormat(String)
public java.text.SimpleDateFormat getSimpleDateFormat(java.lang.String pattern, java.util.Locale locale)
pattern
- the pattern as defined in
SimpleDateFormat.SimpleDateFormat(String)
locale
- locale to be used for the SimpleDateFormat
public java.text.DateFormat getDateTimeInstance(int dateStyle, int timeStyle)
dateStyle
- the date style as specified in
DateFormat.getDateTimeInstance(int, int)
timeStyle
- the time style as specified in
DateFormat.getDateTimeInstance(int, int)
public boolean isWindows()
public boolean isMacOS()
private java.lang.String getOsName()
public void checkPath(java.lang.String path) throws CorruptObjectException
Scans a multi-directory path string such as "src/main.c"
.
path
- path string to scan.CorruptObjectException
- path is invalid.public void checkPath(byte[] path) throws CorruptObjectException
Scans a multi-directory path string such as "src/main.c"
.
path
- path string to scan.CorruptObjectException
- path is invalid.