@Restricted(value=org.kohsuke.accmod.restrictions.NoExternalUse.class) public class SystemProperties extends Object implements javax.servlet.ServletContextListener, OnMaster
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 hudson.DNSMultiCast.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. hudson.DNSMultiCast.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.
Constructor and Description |
---|
SystemProperties()
Public for the servlet container.
|
Modifier and Type | Method and Description |
---|---|
void |
contextDestroyed(javax.servlet.ServletContextEvent event) |
void |
contextInitialized(javax.servlet.ServletContextEvent event)
Called by the servlet container to initialize the
ServletContext . |
static boolean |
getBoolean(String name)
Returns
true if the system property
named by the argument exists and is equal to the string
"true" . |
static boolean |
getBoolean(String name,
boolean def)
Returns
true if the system property
named by the argument exists and is equal to the string
"true" , or a default value. |
static Integer |
getInteger(String name)
Determines the integer value of the system property with the
specified name.
|
static Integer |
getInteger(String name,
Integer def)
Determines the integer value of the system property with the
specified name, or a default value.
|
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.
|
static Long |
getLong(String name)
Determines the long value of the system property with the
specified name.
|
static Long |
getLong(String name,
Long def)
Determines the integer value of the system property with the
specified name, or a default value.
|
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.
|
static String |
getString(String key)
Gets the system property indicated by the specified key.
|
static String |
getString(String key,
String def)
Gets the system property indicated by the specified key, or a default value.
|
static String |
getString(String key,
String def,
Level logLevel)
Gets the system property indicated by the specified key, or a default value.
|
static Boolean |
optBoolean(String name)
Returns
Boolean.TRUE if the named system property exists and is equal to the string "true
(ignoring case), returns Boolean.FALSE if the system property exists and doesn't equal "true
otherwise returns null if the named system property does not exist. |
public void contextInitialized(javax.servlet.ServletContextEvent event)
ServletContext
.contextInitialized
in interface javax.servlet.ServletContextListener
@CheckForNull public static String getString(String key)
System.getProperty(java.lang.String)
, except that it
also consults the ServletContext
's "init" parameters.key
- the name of the system property.null
if there is no property with that key.NullPointerException
- if key
is null
.IllegalArgumentException
- if key
is empty.public static String getString(String key, @CheckForNull String def)
System.getProperty(java.lang.String, java.lang.String)
, except
that it also consults the ServletContext
's "init" parameters.key
- the name of the system property.def
- a default value.null
if the property is missing and the default value is null
.NullPointerException
- if key
is null
.IllegalArgumentException
- if key
is empty.public static String getString(String key, @CheckForNull String def, Level logLevel)
System.getProperty(java.lang.String, java.lang.String)
, except
that it also consults the ServletContext
's "init" 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.null
if the property is missing and the default value is null
.NullPointerException
- if key
is null
.IllegalArgumentException
- if key
is empty.public static boolean getBoolean(String name)
true
if 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 the ServletContext
and is equal to the string "true"
.
This behaves just like Boolean.getBoolean(java.lang.String)
, except that it
also consults the ServletContext
's "init" parameters.name
- the system property name.boolean
value of the system property.public static boolean getBoolean(String name, boolean def)
true
if 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 the ServletContext
and is equal to the string "true"
. If that property does not
exist either, return the default value.
This behaves just like Boolean.getBoolean(java.lang.String)
with a default
value, except that it also consults the ServletContext
's "init" parameters.name
- the system property name.def
- a default value.boolean
value of the system property.@CheckForNull public static Boolean optBoolean(String name)
Boolean.TRUE
if the named system property exists and is equal to the string "true
(ignoring case), returns Boolean.FALSE
if the system property exists and doesn't equal "true
otherwise returns null
if the named system property does not exist.name
- the system property name.Boolean.TRUE
, Boolean.FALSE
or null
@CheckForNull public static Integer getInteger(String name)
Integer.getInteger(java.lang.String)
, except that it
also consults the ServletContext
's "init" parameters.name
- property name.Integer
value of the property.public static Integer getInteger(String name, Integer def)
Integer.getInteger(String,Integer)
, except that it
also consults the ServletContext
's "init" parameters. If neither exist,
return the default value.name
- property name.def
- a default value.Integer
value of the property.
If the property is missing, return the default value.
Result may be null
only if the default value is null
.public static Integer getInteger(String name, Integer def, Level logLevel)
Integer.getInteger(String,Integer)
, except that it
also consults the ServletContext
's "init" parameters. If neither exist,
return the default value.name
- property name.def
- a default value.logLevel
- the level of the log if the provided system property name cannot be decoded into Integer.Integer
value of the property.
If the property is missing, return the default value.
Result may be null
only if the default value is null
.@CheckForNull public static Long getLong(String name)
Long.getLong(java.lang.String)
, except that it
also consults the ServletContext
's "init" parameters.name
- property name.Long
value of the property.public static Long getLong(String name, Long def)
Long.getLong(String,Long)
, except that it
also consults the ServletContext
's "init" parameters. If neither exist,
return the default value.name
- property name.def
- a default value.Long
value of the property.
If the property is missing, return the default value.
Result may be null
only if the default value is null
.public static Long getLong(String name, Long def, Level logLevel)
Long.getLong(String,Long)
, except that it
also consults the ServletContext
's "init" parameters. If neither exist,
return the default value.name
- property name.def
- a default value.logLevel
- the level of the log if the provided system property name cannot be decoded into Long.Long
value of the property.
If the property is missing, return the default value.
Result may be null
only if the default value is null
.public void contextDestroyed(javax.servlet.ServletContextEvent event)
contextDestroyed
in interface javax.servlet.ServletContextListener
Copyright © 2004–2017. All rights reserved.