Package org.sonar.api.config
Interface Configuration
-
@ScannerSide @ServerSide @ComputeEngineSide @SonarLintSide public interface Configuration
Component to get effective configuration. Values of properties depend on the runtime environment:- immutable project configuration in scanner.
- global configuration in Web Server and Compute Engine. It does not allow to get the settings overridden on projects.
Usage
public class MyExtension { private final Configuration config; public MyExtension(Configuration config) { this.config = config; } public void doSomething() { String fooValue = config.get("sonar.foo").orElse(null); // .. } }
Scanner example
Scanner sensor can get the reference on Configuration directly through SensorContext, without injecting the component into constructor.public class MySensor implements Sensor { @Override public void execute(SensorContext context) { String fooValue = context.config().get("sonar.foo").orElse(null); // .. } }
For testing, and only for testing, the in-memory implementation MapSettings can be used.
@Test public void my_test() { MapSettings settings = new MapSettings(); settings.setProperty("foo", "bar"); MyExtension underTest = new MyExtension(settings.asConfig()); // ... }
- Since:
- 6.5
- See Also:
PropertyDefinition
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description java.util.Optional<java.lang.String>
get(java.lang.String key)
The effective value of the specified property.default java.util.Optional<java.lang.Boolean>
getBoolean(java.lang.String key)
Effective value as boolean.default java.util.Optional<java.lang.Double>
getDouble(java.lang.String key)
Effective value asDouble
.default java.util.Optional<java.lang.Float>
getFloat(java.lang.String key)
Effective value asFloat
.default java.util.Optional<java.lang.Integer>
getInt(java.lang.String key)
Effective value asint
.default java.util.Optional<java.lang.Long>
getLong(java.lang.String key)
Effective value aslong
.java.lang.String[]
getStringArray(java.lang.String key)
Used to read multi-valued properties.boolean
hasKey(java.lang.String key)
-
-
-
Method Detail
-
get
java.util.Optional<java.lang.String> get(java.lang.String key)
The effective value of the specified property. Can returnOptional#empty()
if the property is not set and has no defined default value.If the property is encrypted with a secret key, then the returned value is decrypted.
- Throws:
java.lang.IllegalStateException
- if value is encrypted but fails to be decrypted.
-
hasKey
boolean hasKey(java.lang.String key)
- Returns:
true
if the property has a non-default value, elsefalse
.
-
getStringArray
java.lang.String[] getStringArray(java.lang.String key)
Used to read multi-valued properties.See
PropertyDefinition.Builder.multiValues(boolean)
Multi-valued properties coming from scanner are parsed as CSV lines (ie comma separator and optional double quotes to escape values). Non quoted values are trimmed and empty fields are ignored.
Examples :- "one,two,three " -> ["one", "two", "three"]
- " one, two, three " -> ["one", "two", "three"]
- "one, three" -> ["one", "three"]
- "one,"", three" -> ["one", "", "three"]
- "one, " " , three" -> ["one", " ", "three"]
- "one,\"two,three\",\" four \"" -> ["one", "two,three", " four "]
-
getBoolean
default java.util.Optional<java.lang.Boolean> getBoolean(java.lang.String key)
Effective value as boolean. It isempty
ifget(String)
is empty or if it does not return"true"
, even if it's not a boolean representation.- Returns:
true
if the effective value is"true"
,false
for any other non empty value. If the property does not have value nor default value, thenempty
is returned.
-
getInt
default java.util.Optional<java.lang.Integer> getInt(java.lang.String key)
Effective value asint
.- Returns:
- the value as
int
. If the property does not have value nor default value, thenempty
is returned. - Throws:
java.lang.NumberFormatException
- if value is not empty and is not a parsable integer
-
getLong
default java.util.Optional<java.lang.Long> getLong(java.lang.String key)
Effective value aslong
.- Returns:
- the value as
long
. If the property does not have value nor default value, thenempty
is returned. - Throws:
java.lang.NumberFormatException
- if value is not empty and is not a parsablelong
-
getFloat
default java.util.Optional<java.lang.Float> getFloat(java.lang.String key)
Effective value asFloat
.- Returns:
- the value as
Float
. If the property does not have value nor default value, thenempty
is returned. - Throws:
java.lang.NumberFormatException
- if value is not empty and is not a parsable number
-
getDouble
default java.util.Optional<java.lang.Double> getDouble(java.lang.String key)
Effective value asDouble
.- Returns:
- the value as
Double
. If the property does not have value nor default value, thenempty
is returned. - Throws:
java.lang.NumberFormatException
- if value is not empty and is not a parsable number
-
-