/**
 * Copyright (C) 2006-2018 Talend Inc. - www.talend.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.talend.sdk.component.server.configuration;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.microprofile.config.spi.ConfigSource;

public class OpenTracingConfigSource implements ConfigSource {

    private Map<String, String> configuration = new HashMap<String, String>() {

        {
            final String isTracingOn = String
                    .valueOf(Boolean.getBoolean("TRACING_ON") && Integer.getInteger("TRACING_SAMPLING_RATATE", 0) != 0);
            put("geronimo.opentracing.filter.active", isTracingOn);
            put("span.converter.zipkin.active", isTracingOn);
            put("span.converter.zipkin.logger.active", isTracingOn);
        }
    };

    @Override
    public Map<String, String> getProperties() {
        return configuration;
    }

    @Override
    public int getOrdinal() {
        return 2000;
    }

    @Override
    public String getValue(final String propertyName) {
        return configuration.get(propertyName);
    }

    @Override
    public String getName() {
        return "talend-opentracing";
    }
}

@@ -772,16 +772,73 @@
 gSource;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Apublic
@@ -891,24 +891,250 @@
 igSource %7B%0A%0A
+    private final static Logger LOGGER = LoggerFactory.getLogger(OpenTracingConfigSource.class);%0A%0A    private final String TRACING_ON = %22TRACING_ON%22;%0A%0A    private final String TRACING_SAMPLING_RATE = %22TRACING_SAMPLING_RATE%22;%0A%0A
     private 
@@ -1234,18 +1234,17 @@
 nal 
-String isT
+boolean t
 raci
@@ -1249,23 +1249,16 @@
 cingOn =
- String
 %0A       
@@ -1274,25 +1274,53 @@
     
-.valueOf(
+System.getenv(TRACING_ON) != null && 
 Boolean.
 getB
@@ -1319,19 +1319,21 @@
 ean.
-get
+parse
 Boolean(
 %22TRA
@@ -1332,45 +1332,378 @@
 ean(
-%22TRACING_ON%22) && Integer.getInteger(%22
+System.getenv(TRACING_ON));%0A            int tracingRate = 1;%0A            if (System.getenv(TRACING_SAMPLING_RATE) != null) %7B%0A                try %7B%0A                    tracingRate = Integer.parseInt(System.getenv(TRACING_SAMPLING_RATE));%0A                %7D catch (final NumberFormatException e) %7B%0A                    LOGGER.warn(%22Can't parse value of environment property 
 TRAC
@@ -1722,21 +1722,131 @@
 _RAT
-AT
 E%22, 
-0) != 0
+e);%0A                %7D%0A            %7D%0A            final String isTracingOn = String.valueOf(tracingOn && tracingRate == 1
 );%0A 

fix opentracing source configuration