Class SystemProperties
- java.lang.Object
-
- jenkins.util.SystemProperties
-
public class SystemProperties extends Object
Centralizes calls toSystem.getProperty(String)and related calls. This allows us to get values not just from environment variables but also from theServletContext, so properties likejenkins.whatever.Clazz.disabledcan be set incontext.xmland 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 incontext.xmlis 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, becauseEnvVarsis only for build variables, not Jenkins itself variables.- Since:
- 2.236
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classSystemProperties.AgentCopierstatic classSystemProperties.Listener
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static 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 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 LonggetLong(String name)Determines the long value of the system property with the specified name.static LonggetLong(String name, Long def)Determines the integer value of the system property with the specified name, or a default value.static LonggetLong(String name, Long def, Level logLevel)Determines the integer value of the system property with the specified name, or a default value.static StringgetString(String key)Gets the system property indicated by the specified key.static StringgetString(String key, String def)Gets the system property indicated by the specified key, or a default value.static StringgetString(String key, String def, Level logLevel)Gets 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 Detail
-
allowOnAgent
public static void allowOnAgent(String key)
Mark a key whose value should be made accessible in agent JVMs.- Parameters:
key- Property key to be explicitly allowed
-
getString
@CheckForNull public static String getString(String key)
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
public static String getString(String key, @CheckForNull String def)
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
public static String getString(String key, @CheckForNull String def, Level logLevel)
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
public static boolean getBoolean(String name)
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
public static boolean getBoolean(String name, boolean def)
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
@CheckForNull public static Boolean optBoolean(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.- Parameters:
name- the system property name.- Returns:
Boolean.TRUE,Boolean.FALSEornull- Since:
- 2.16
-
getInteger
@CheckForNull public static Integer getInteger(String name)
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
public static Integer getInteger(String name, Integer def)
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
public static Integer getInteger(String name, Integer def, Level logLevel)
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
@CheckForNull public static Long getLong(String name)
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
public static Long getLong(String name, Long def)
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
public static Long getLong(String name, Long def, Level logLevel)
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.
-
-