Class SystemProperties
System.getProperty(String) and related calls.
This allows us to get values not just from environment variables but also from
the ServletContext, so properties like jenkins.whatever.Clazz.disabled
can be set in context.xml and the app server's boot script does not
have to be changed.
This should be used to obtain hudson/jenkins "app"-level parameters
(e.g. jenkins.whatever.Clazz.disabled), but not for system parameters
(e.g. os.name).
If you run multiple instances of Jenkins in the same virtual machine and wish
to obtain properties from context.xml, make sure these Jenkins instances use
different ClassLoaders. Tomcat, for example, does this automatically. If you do
not use different ClassLoaders, the values of properties specified in
context.xml is undefined.
Property access is logged on Level.CONFIG. Note that some properties
may be accessed by Jenkins before logging is configured properly, so early access to
some properties may not be logged.
While it looks like it on first glance, this cannot be mapped to EnvVars,
because EnvVars is only for build variables, not Jenkins itself variables.
- Since:
- 2.236
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classstatic final class -
Method Summary
Modifier and TypeMethodDescriptionstatic voidallowOnAgent(String key) Mark a key whose value should be made accessible in agent JVMs.static booleangetBoolean(String name) Returnstrueif the system property named by the argument exists and is equal to the string"true".static booleangetBoolean(String name, boolean def) Returnstrueif the system property named by the argument exists and is equal to the string"true", or a default value.static DurationgetDuration(String name) Determines the duration value of the system property with the specified name.static DurationgetDuration(String name, Duration defaultValue) Determines the duration value of the system property with the specified name, or a default value.static DurationgetDuration(String name, ChronoUnit unit) Determines the duration value of the system property with the specified name.static DurationgetDuration(String name, ChronoUnit unit, Duration defaultValue) Determines the duration value of the system property with the specified name, or a default value.static IntegergetInteger(String name) Determines the integer value of the system property with the specified name.static IntegergetInteger(String name, Integer def) Determines the integer value of the system property with the specified name, or a default value.static IntegergetInteger(String name, Integer def, Level logLevel) Determines the integer value of the system property with the specified name, or a default value.static LongDetermines the long value of the system property with the specified name.static LongDetermines the integer value of the system property with the specified name, or a default value.static LongDetermines the integer value of the system property with the specified name, or a default value.static StringGets the system property indicated by the specified key.static StringGets the system property indicated by the specified key, or a default value.static StringGets the system property indicated by the specified key, or a default value.static BooleanoptBoolean(String name) ReturnsBoolean.TRUEif the named system property exists and is equal to the string"true(ignoring case), returnsBoolean.FALSEif the system property exists and doesn't equal"trueotherwise returnsnullif the named system property does not exist.
-
Method Details
-
allowOnAgent
Mark a key whose value should be made accessible in agent JVMs.- Parameters:
key- Property key to be explicitly allowed
-
getString
Gets the system property indicated by the specified key. This behaves just likeSystem.getProperty(java.lang.String), except that it also consults theServletContext's "init" parameters.- Parameters:
key- the name of the system property.- Returns:
- the string value of the system property,
or
nullif there is no property with that key. - Throws:
NullPointerException- ifkeyisnull.IllegalArgumentException- ifkeyis empty.
-
getString
Gets the system property indicated by the specified key, or a default value. This behaves just likeSystem.getProperty(java.lang.String, java.lang.String), except that it also consults theServletContext's "init" parameters.- Parameters:
key- the name of the system property.def- a default value.- Returns:
- the string value of the system property,
or
nullif the property is missing and the default value isnull. - Throws:
NullPointerException- ifkeyisnull.IllegalArgumentException- ifkeyis empty.
-
getString
Gets the system property indicated by the specified key, or a default value. This behaves just likeSystem.getProperty(java.lang.String, java.lang.String), except that it also consults theServletContext's "init" parameters.- Parameters:
key- the name of the system property.def- a default value.logLevel- the level of the log if the provided key is not found.- Returns:
- the string value of the system property,
or
nullif the property is missing and the default value isnull. - Throws:
NullPointerException- ifkeyisnull.IllegalArgumentException- ifkeyis empty.
-
getBoolean
Returnstrueif the system property named by the argument exists and is equal to the string"true". If the system property does not exist, return"false". if a property by this name exists in theServletContextand is equal to the string"true". This behaves just likeBoolean.getBoolean(java.lang.String), except that it also consults theServletContext's "init" parameters.- Parameters:
name- the system property name.- Returns:
- the
booleanvalue of the system property.
-
getBoolean
Returnstrueif the system property named by the argument exists and is equal to the string"true", or a default value. If the system property does not exist, return"true"if a property by this name exists in theServletContextand is equal to the string"true". If that property does not exist either, return the default value. This behaves just likeBoolean.getBoolean(java.lang.String)with a default value, except that it also consults theServletContext's "init" parameters.- Parameters:
name- the system property name.def- a default value.- Returns:
- the
booleanvalue of the system property.
-
optBoolean
ReturnsBoolean.TRUEif the named system property exists and is equal to the string"true(ignoring case), returnsBoolean.FALSEif the system property exists and doesn't equal"trueotherwise returnsnullif the named system property does not exist.- Parameters:
name- the system property name.- Returns:
Boolean.TRUE,Boolean.FALSEornull- Since:
- 2.16
-
getDuration
Determines the duration value of the system property with the specified name.- Parameters:
name- property name.- Returns:
- the property value as a duration.
- Since:
- 2.516
-
getDuration
@CheckForNull public static Duration getDuration(@NonNull String name, @CheckForNull ChronoUnit unit) Determines the duration value of the system property with the specified name.- Parameters:
name- property name.unit- the duration unit to use if the value doesn't specify one (defaults to `ms`)- Returns:
- the property value as a duration.
- Since:
- 2.516
-
getDuration
@Nullable public static Duration getDuration(@NonNull String name, @CheckForNull Duration defaultValue) Determines the duration value of the system property with the specified name, or a default value.- Parameters:
name- property name.defaultValue- a default value- Returns:
- the property value as a duration.
- Since:
- 2.516
-
getDuration
@Nullable public static Duration getDuration(@NonNull String name, @CheckForNull ChronoUnit unit, @CheckForNull Duration defaultValue) Determines the duration value of the system property with the specified name, or a default value.- Parameters:
name- property name.unit- the duration unit to use if the value doesn't specify one (defaults to `ms`)defaultValue- a default value- Returns:
- the property value as a duration.
- Since:
- 2.516
-
getInteger
Determines the integer value of the system property with the specified name. This behaves just likeInteger.getInteger(java.lang.String), except that it also consults theServletContext's "init" parameters.- Parameters:
name- property name.- Returns:
- the
Integervalue of the property.
-
getInteger
Determines the integer value of the system property with the specified name, or a default value. This behaves just likeInteger.getInteger(String,Integer), except that it also consults theServletContext's "init" parameters. If neither exist, return the default value.- Parameters:
name- property name.def- a default value.- Returns:
- the
Integervalue of the property. If the property is missing, return the default value. Result may benullonly if the default value isnull.
-
getInteger
Determines the integer value of the system property with the specified name, or a default value. This behaves just likeInteger.getInteger(String,Integer), except that it also consults theServletContext's "init" parameters. If neither exist, return the default value.- Parameters:
name- property name.def- a default value.logLevel- the level of the log if the provided system property name cannot be decoded into Integer.- Returns:
- the
Integervalue of the property. If the property is missing, return the default value. Result may benullonly if the default value isnull.
-
getLong
Determines the long value of the system property with the specified name. This behaves just likeLong.getLong(java.lang.String), except that it also consults theServletContext's "init" parameters.- Parameters:
name- property name.- Returns:
- the
Longvalue of the property.
-
getLong
Determines the integer value of the system property with the specified name, or a default value. This behaves just likeLong.getLong(String,Long), except that it also consults theServletContext's "init" parameters. If neither exist, return the default value.- Parameters:
name- property name.def- a default value.- Returns:
- the
Longvalue of the property. If the property is missing, return the default value. Result may benullonly if the default value isnull.
-
getLong
Determines the integer value of the system property with the specified name, or a default value. This behaves just likeLong.getLong(String, Long), except that it also consults theServletContext's "init" parameters. If neither exist, return the default value.- Parameters:
name- property name.def- a default value.logLevel- the level of the log if the provided system property name cannot be decoded into Long.- Returns:
- the
Longvalue of the property. If the property is missing, return the default value. Result may benullonly if the default value isnull.
-