/**
 * Copyright (C) 2015 The Gravitee team (http://gravitee.io)
 *
 * 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 io.gravitee.gateway.core.reporter.impl;

import io.gravitee.gateway.api.reporter.Reporter;
import io.gravitee.gateway.core.plugin.PluginContextFactory;
import io.gravitee.gateway.core.plugin.PluginHandler;
import io.gravitee.gateway.core.reporter.ReporterManager;
import io.gravitee.plugin.api.Plugin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.util.Assert;
import org.springframework.util.StringValueResolver;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @author David BRASSELY (brasseld at gmail.com)
 */
public class ReporterManagerImpl implements ReporterManager, PluginHandler, EmbeddedValueResolverAware {

    private final Logger LOGGER = LoggerFactory.getLogger(ReporterManagerImpl.class);

    private final Collection<Reporter> reporters = new ArrayList<>();

    private StringValueResolver stringValueResolver;

    @Autowired
    private PluginContextFactory pluginContextFactory;

    @Override
    public Collection<Reporter> getReporters() {
        return reporters;
    }

    @Override
    public boolean canHandle(Plugin plugin) {
        try {
            Assert.isAssignable(Reporter.class, plugin.clazz());
            return true;
        } catch (Exception iae) {
            return false;
        }
    }

    @Override
    public void handle(Plugin plugin) {
        boolean enabled = isEnabled(plugin);
        if (enabled) {
            try {
                ApplicationContext context = pluginContextFactory.create(plugin);
                reporters.add(context.getBean(Reporter.class));
            } catch (Exception iae) {
                LOGGER.error("Unexpected error while create reporter instance", iae);
                // Be sure that the context does not exist anymore.
                pluginContextFactory.remove(plugin);
            }
        } else {
            LOGGER.warn("Plugin {} is disabled. Please have a look to your configuration to re-enable it", plugin.id());
        }
    }

    private boolean isEnabled(Plugin reporterPlugin) {
        String value = stringValueResolver.resolveStringValue("${reporter." + reporterPlugin.id() + ".enabled:true}");
        LOGGER.debug("Plugin {} configuration: {}", reporterPlugin.id(), value);
        return Boolean.parseBoolean(value);
    }

    @Override
    public void setEmbeddedValueResolver(StringValueResolver resolver) {
        stringValueResolver = resolver;
    }
}

@@ -1142,40 +1142,26 @@
 k.co
-ntext.EmbeddedValueResolverAware
+re.env.Environment
 ;%0Aim
@@ -1201,61 +1201,8 @@
 ert;
-%0Aimport org.springframework.util.StringValueResolver;
 %0A%0Aim
@@ -1393,36 +1393,8 @@
 dler
-, EmbeddedValueResolverAware
  %7B%0A%0A
@@ -1559,55 +1559,54 @@
     
-private StringValueResolver stringValueResolver
+@Autowired%0A    private Environment environment
 ;%0A%0A 
@@ -2779,65 +2779,51 @@
     
-String value = stringValueResolver.resolveStringValue(%22$%7B
+boolean enabled = environment.getProperty(%22
 repo
@@ -2866,15 +2866,30 @@
 bled
-:
+%22, Boolean.class, 
 true
-%7D%22
 );%0A 
@@ -2960,21 +2960,23 @@
 n.id(), 
-value
+enabled
 );%0A     
@@ -2989,169 +2989,15 @@
 urn 
-Boolean.parseBoolean(value);%0A    %7D%0A%0A    @Override%0A    public void setEmbeddedValueResolver(StringValueResolver resolver) %7B%0A        stringValueResolver = resolver
+enabled
 ;%0A  

Use Spring Environment to retrieve reporter state from configuration