Skip to content

Commit

Permalink
Merge pull request #353 from Netflix/ihaken-jettyconnectors
Browse files Browse the repository at this point in the history
Add configuration option to add additional connectors
  • Loading branch information
elandau authored Jun 29, 2017
2 parents 8c2c714 + e651e68 commit ee93a2e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
58 changes: 33 additions & 25 deletions karyon2-admin/src/main/java/netflix/admin/AdminConfigImpl.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
package netflix.admin;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import com.google.inject.Injector;
import com.netflix.config.ConfigurationManager;
import com.netflix.config.util.ConfigurationUtils;
import org.mortbay.jetty.Connector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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;
import java.util.*;
import java.util.Map.Entry;

@Singleton
public class AdminConfigImpl implements AdminContainerConfig {
Expand Down Expand Up @@ -49,6 +43,9 @@ public class AdminConfigImpl implements AdminContainerConfig {
public static final String NETFLIX_ADMIN_CTX_FILTERS = ADMIN_PREFIX + "additional.filters";
public static final String DEFAULT_CONTEXT_FILTERS = "";

public static final String NETFLIX_ADMIN_CTX_CONNECTORS = ADMIN_PREFIX + "additional.connectors";
public static final String DEFAULT_CONTEXT_CONNECTORS = "";

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

Expand Down Expand Up @@ -115,26 +112,37 @@ public Map<String, Object> getJerseyConfigProperties() {

@Override
public List<Filter> additionalFilters() {
String rootContextFilters = ConfigurationManager.getConfigInstance().getString(NETFLIX_ADMIN_CTX_FILTERS, DEFAULT_CONTEXT_FILTERS);
return getInstancesFromClassList(
ConfigurationManager.getConfigInstance().getString(NETFLIX_ADMIN_CTX_FILTERS, DEFAULT_CONTEXT_FILTERS),
Filter.class);
}

@Override
public List<Connector> additionalConnectors() {
return getInstancesFromClassList(
ConfigurationManager.getConfigInstance().getString(NETFLIX_ADMIN_CTX_CONNECTORS, DEFAULT_CONTEXT_CONNECTORS),
Connector.class);
}

if (rootContextFilters.isEmpty()) {
private <T> List<T> getInstancesFromClassList(String classList, Class<T> clazz) {
if (classList.isEmpty()) {
return Collections.emptyList();
}

List<Filter> filters = new ArrayList<>();
final String[] filterClasses = rootContextFilters.split(",");
for (String filterClass : filterClasses) {
List<T> instances = new ArrayList<>();
final String[] classNames = classList.split(",");
for (String className : classNames) {
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)));
final Class<?> implClass = Class.forName(className, false, getClass().getClassLoader());
if (clazz.isAssignableFrom(implClass)) {
instances.add(clazz.cast(injector == null ? implClass.newInstance() : injector.getInstance(implClass)));
}
} catch (InstantiationException | IllegalAccessException e) {
logger.warn("Filter class can not be instantiated " + filterClass);
logger.warn("Class can not be instantiated " + className);
} catch (ClassNotFoundException e) {
logger.warn("Filter class not found " + filterClass);
logger.warn("Class not found " + className);
}
}
return filters;
return instances;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javax.servlet.Filter;

import com.google.inject.ImplementedBy;
import org.mortbay.jetty.Connector;

@ImplementedBy(AdminConfigImpl.class)
public interface AdminContainerConfig {
Expand All @@ -18,6 +19,7 @@ public interface AdminContainerConfig {
boolean shouldScanClassPathForPluginDiscovery();
int listenPort();
List<Filter> additionalFilters();
List<Connector> additionalConnectors();
Map<String, Object> getJerseyConfigProperties();

}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.mortbay.jetty.Server;
import org.mortbay.jetty.handler.HandlerCollection;
import org.mortbay.jetty.handler.ResourceHandler;
import org.mortbay.jetty.security.SslSocketConnector;
import org.mortbay.jetty.servlet.Context;
import org.mortbay.jetty.servlet.DefaultServlet;
import org.mortbay.jetty.servlet.FilterHolder;
Expand Down Expand Up @@ -198,6 +199,9 @@ public Resource getResource(String path) throws MalformedURLException {
HandlerCollection handlers = new HandlerCollection();
handlers.setHandlers(new Handler[]{resource_handler, adminTemplatesResHandler, adminDataResHandler, rootHandler});
server.setHandler(handlers);
for (Connector connector : adminContainerConfig.additionalConnectors()) {
server.addConnector(connector);
}

server.start();

Expand Down

0 comments on commit ee93a2e

Please sign in to comment.