Skip to content

Commit

Permalink
Merge pull request #345 from elandau/di
Browse files Browse the repository at this point in the history
Use the injector to create objects instead of newInstance
  • Loading branch information
elandau committed Jun 2, 2016
2 parents f1eb71e + 05a1e54 commit 076c6b1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
23 changes: 18 additions & 5 deletions karyon2-admin/src/main/java/netflix/admin/AdminConfigImpl.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package netflix.admin;

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

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

import javax.inject.Singleton;
import javax.servlet.Filter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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

@Singleton
public class AdminConfigImpl implements AdminContainerConfig {
private static final Logger logger = LoggerFactory.getLogger(AdminConfigImpl.class);
Expand Down Expand Up @@ -38,6 +42,17 @@ public class AdminConfigImpl implements AdminContainerConfig {
public static final String NETFLIX_ADMIN_CTX_FILTERS = "netflix.admin.additional.filters";
public static final String DEFAULT_CONTEXT_FILTERS = "";

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);
Expand Down Expand Up @@ -90,11 +105,9 @@ public List<Filter> additionalFilters() {
final String[] filterClasses = rootContextFilters.split(",");
for (String filterClass : filterClasses) {
try {
getClass().getClassLoader();
final Class<?> filterCls = Class.forName(filterClass, false, getClass().getClassLoader());
if (Filter.class.isAssignableFrom(filterCls)) {
Filter filter = (Filter) filterCls.newInstance();
filters.add(filter);
filters.add((Filter)(injector == null ? filterCls.newInstance() : injector.getInstance(filterCls)));
}
} catch (InstantiationException | IllegalAccessException e) {
logger.warn("Filter class can not be instantiated " + filterClass);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package netflix.adminresources;


import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.inject.Injector;
import com.google.inject.Singleton;
import com.netflix.config.ConfigurationManager;
import com.netflix.governator.lifecycle.ClasspathScanner;
Expand All @@ -18,6 +18,7 @@
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import javax.inject.Inject;

@Singleton
public class AdminPageRegistry {
Expand All @@ -28,12 +29,19 @@ public class AdminPageRegistry {
public static final String DEFAULT_ADMIN_PAGE_ANNOTATION = "netflix.adminresources.AdminPage";

private Map<String, AdminPageInfo> baseServerPageInfoMap;


private Injector injector;
public AdminPageRegistry() {
this.baseServerPageInfoMap = new ConcurrentHashMap<>();
this.injector = null;
}

@Inject
public AdminPageRegistry(Injector injector) {
this.baseServerPageInfoMap = new ConcurrentHashMap<>();
this.injector = injector;
}

public void add(AdminPageInfo baseServerPageInfo) {
Preconditions.checkNotNull(baseServerPageInfo);
baseServerPageInfoMap.put(baseServerPageInfo.getPageId(), baseServerPageInfo);
Expand Down Expand Up @@ -72,8 +80,7 @@ public void registerAdminPagesWithClasspathScan() {
if (derivedFromAbstractBaseServePageInfo(baseServerAdminPageClass) ||
implementsAdminPageInfo(baseServerAdminPageClass)) {
try {
AdminPageInfo baseServerPageInfo = (AdminPageInfo) baseServerAdminPageClass.newInstance();
add(baseServerPageInfo);
add((AdminPageInfo) (injector == null ? baseServerAdminPageClass.newInstance() : injector.getInstance(baseServerAdminPageClass)));
} catch (Exception e) {
LOG.warn(String.format("Exception registering %s admin page", baseServerAdminPageClass.getName()), e);
}
Expand Down

0 comments on commit 076c6b1

Please sign in to comment.