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 as Double.
      default java.util.Optional<java.lang.Float> getFloat​(java.lang.String key)
      Effective value as Float.
      default java.util.Optional<java.lang.Integer> getInt​(java.lang.String key)
      Effective value as int.
      default java.util.Optional<java.lang.Long> getLong​(java.lang.String key)
      Effective value as long.
      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 return Optional#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, else false.
      • 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 is empty if get(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, then empty is returned.
      • getInt

        default java.util.Optional<java.lang.Integer> getInt​(java.lang.String key)
        Effective value as int.
        Returns:
        the value as int. If the property does not have value nor default value, then empty 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 as long.
        Returns:
        the value as long. If the property does not have value nor default value, then empty is returned.
        Throws:
        java.lang.NumberFormatException - if value is not empty and is not a parsable long
      • getFloat

        default java.util.Optional<java.lang.Float> getFloat​(java.lang.String key)
        Effective value as Float.
        Returns:
        the value as Float. If the property does not have value nor default value, then empty 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 as Double.
        Returns:
        the value as Double. If the property does not have value nor default value, then empty is returned.
        Throws:
        java.lang.NumberFormatException - if value is not empty and is not a parsable number