Skip to content

Commit

Permalink
Adding initialization injector binding of ServletContext (primaraly to
Browse files Browse the repository at this point in the history
support Shiro)
  • Loading branch information
sskrla committed Aug 27, 2014
1 parent 1637b47 commit 1f3c92c
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import com.google.inject.*;
import com.google.inject.name.Names;
import io.dropwizard.Configuration;
import io.dropwizard.jetty.MutableServletContextHandler;
import io.dropwizard.setup.Environment;
import org.apache.commons.lang.ClassUtils;
import org.apache.commons.lang.reflect.FieldUtils;

import javax.servlet.ServletContext;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
Expand Down Expand Up @@ -38,8 +40,20 @@ protected void configure() {
}

bindConfigs(configurationClass, new String[]{}, Lists.<Class<?>>newArrayList());

bindContext("application", environment.getApplicationContext());
}

/**
* Bind some of the context objects to be injectable. Annotated with a {@link com.google.inject.name.Names} to
* prevent collisions for any that the {@link com.google.inject.servlet.ServletModule} may bind later.
*/
private void bindContext(String name, MutableServletContextHandler context) {
bind(ServletContext.class)
.annotatedWith(Names.named(name))
.toInstance(context.getServletContext());
}

private void bindConfigs(Class<?> config, String[] path, List<Class<?>> visited) {
List<Class<?>> classes = Lists.newArrayList(ClassUtils.getAllSuperclasses(config));
classes.add(config);
Expand Down

0 comments on commit 1f3c92c

Please sign in to comment.