package netflix.admin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.servlet.Filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.Injector;
import com.netflix.config.ConfigurationManager;
import com.netflix.config.util.ConfigurationUtils;

@Singleton
public class AdminConfigImpl implements AdminContainerConfig {
    private static final Logger logger = LoggerFactory.getLogger(AdminConfigImpl.class);

    public static final String ADMIN_PREFIX = "netflix.admin.";
    
    public static final String NETFLIX_ADMIN_TEMPLATE_CONTEXT = ADMIN_PREFIX + "template.context";
    public static final String TEMPLATE_CONTEXT_DEFAULT = "/admin";

    public static final String NETFLIX_ADMIN_RESOURCE_CONTEXT = ADMIN_PREFIX + "resource.context";
    public static final String RESOURCE_CONTEXT_DEFAULT = "/webadmin";

    private static final String JERSEY_CORE_RESOURCES = "netflix.platform.admin.resources.core.packages";
    public static final String JERSEY_CORE_RESOURCES_DEFAULT = "netflix.adminresources;com.netflix.explorers.resources;com.netflix.explorers.providers";

    private static final String JERSEY_VIEWABLE_RESOURCES = "netflix.platform.admin.resources.viewable.packages";
    public static final String JERSEY_VIEWABLE_RESOURCES_DEFAULT = "netflix.admin;netflix.adminresources.pages;com.netflix.explorers.resources";

    public static final String CONTAINER_LISTEN_PORT = "netflix.platform.admin.resources.port";
    public static final int LISTEN_PORT_DEFAULT = 8077;

    public static final String SERVER_ENABLE_PROP_NAME = "netflix.platform.admin.resources.enable";
    public static final boolean SERVER_ENABLE_DEFAULT = true;

    public static final String NETFLIX_ADMIN_RESOURCES_ISOLATE = ADMIN_PREFIX + "resources.isolate";
    public static final boolean ISOLATE_RESOURCES_DEFAULT = false;

    public static final String NETFLIX_ADMIN_CTX_FILTERS = ADMIN_PREFIX + "additional.filters";
    public static final String DEFAULT_CONTEXT_FILTERS = "";

    private static final String JERSEY_PROPERTY_PREFIX = "com.sun.jersey.config";
    private static final String ADMIN_JERSEY_PROPERTY_PREFIX = ADMIN_PREFIX + JERSEY_PROPERTY_PREFIX;
    
    private final Injector injector;

    public AdminConfigImpl() {
        this(null);
    }
    
    @Inject
    public AdminConfigImpl(Injector injector) {
        this.injector = injector;
    }
    
    @Override
    public boolean shouldIsolateResources() {
        return ConfigurationManager.getConfigInstance().getBoolean(NETFLIX_ADMIN_RESOURCES_ISOLATE, ISOLATE_RESOURCES_DEFAULT);
    }

    @Override
    public boolean shouldEnable() {
        return ConfigurationManager.getConfigInstance().getBoolean(SERVER_ENABLE_PROP_NAME, SERVER_ENABLE_DEFAULT);
    }

    @Override
    public String templateResourceContext() {
        return ConfigurationManager.getConfigInstance().getString(NETFLIX_ADMIN_TEMPLATE_CONTEXT, TEMPLATE_CONTEXT_DEFAULT);
    }

    @Override
    public String ajaxDataResourceContext() {
        return ConfigurationManager.getConfigInstance().getString(NETFLIX_ADMIN_RESOURCE_CONTEXT, RESOURCE_CONTEXT_DEFAULT);
    }

    @Override
    public String jerseyResourcePkgList() {
        return ConfigurationManager.getConfigInstance().getString(JERSEY_CORE_RESOURCES, JERSEY_CORE_RESOURCES_DEFAULT);
    }

    @Override
    public String jerseyViewableResourcePkgList() {
        return ConfigurationManager.getConfigInstance().getString(JERSEY_VIEWABLE_RESOURCES, JERSEY_VIEWABLE_RESOURCES_DEFAULT);
    }

    @Override
    public boolean shouldScanClassPathForPluginDiscovery() {
        return true;
    }

    @Override
    public int listenPort() {
        return ConfigurationManager.getConfigInstance().getInt(CONTAINER_LISTEN_PORT, LISTEN_PORT_DEFAULT);
    }

    @Override
    public Map<String, String> getJerseyConfigProperties() {
        return ConfigurationUtils.getProperties(ConfigurationManager.getConfigInstance().subset(ADMIN_JERSEY_PROPERTY_PREFIX))
                .entrySet()
                .stream()
                .peek(entry -> logger.info("Using admin jersey config: " + entry))
                .collect(Collectors.toMap(
                    entry -> JERSEY_PROPERTY_PREFIX + "." + entry.getKey().toString(),
                    entry -> entry.getValue().toString()
                    ));
    }
    
    @Override
    public List<Filter> additionalFilters() {
        String rootContextFilters = ConfigurationManager.getConfigInstance().getString(NETFLIX_ADMIN_CTX_FILTERS, DEFAULT_CONTEXT_FILTERS);

        if (rootContextFilters.isEmpty()) {
            return Collections.emptyList();
        }

        List<Filter> filters = new ArrayList<>();
        final String[] filterClasses = rootContextFilters.split(",");
        for (String filterClass : filterClasses) {
            try {
                final Class<?> filterCls = Class.forName(filterClass, false, getClass().getClassLoader());
                if (Filter.class.isAssignableFrom(filterCls)) {
                    filters.add((Filter)(injector == null ? filterCls.newInstance() : injector.getInstance(filterCls)));
                }
            } catch (InstantiationException | IllegalAccessException e) {
                logger.warn("Filter class can not be instantiated " + filterClass);
            } catch (ClassNotFoundException e) {
                logger.warn("Filter class not found " + filterClass);
            }
        }
        return filters;
    }
}

@@ -75,16 +75,42 @@
 ctions;%0A
+import java.util.HashMap;%0A
 import j
@@ -167,24 +167,45 @@
 til.
-stream.Collector
+Map.Entry;%0Aimport java.util.Propertie
 s;%0A%0A
@@ -4092,38 +4092,104 @@
 ies() %7B%0A        
-return
+Map%3CString, String%3E result = new HashMap%3C%3E();%0A        Properties props =
  ConfigurationUt
@@ -4284,16 +4284,17 @@
 PREFIX))
+;
 %0A       
@@ -4298,209 +4298,85 @@
     
-        .entrySet()%0A                .stream()%0A                .peek(entry -%3E logger.info(%22Using admin jersey config: %22 + entry))%0A                .collect(Collectors.toMap(%0A                    entry -%3E 
+for (Entry%3CObject, Object%3E prop : props.entrySet()) %7B%0A            result.put(
 JERS
@@ -4402,21 +4402,20 @@
 + %22.%22 + 
-entry
+prop
 .getKey(
@@ -4431,43 +4431,13 @@
 g(),
-%0A                    entry -%3E entry
+ prop
 .get
@@ -4454,16 +4454,18 @@
 String()
+);
 %0A       
@@ -4465,27 +4465,26 @@
         
-   
+%7D%0A
         
  ));%0A   
@@ -4479,11 +4479,21 @@
     
- ))
+return result
 ;%0A  

Revert to java7 syntax