Skip to content

Commit

Permalink
supporting registering of stereotype handlers on guice. Fixes gh-366
Browse files Browse the repository at this point in the history
  • Loading branch information
lucascs committed Feb 25, 2012
1 parent 81f16d6 commit b746c63
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
Expand Up @@ -33,11 +33,10 @@
import br.com.caelum.vraptor.ioc.Cacheable;
import br.com.caelum.vraptor.ioc.ComponentFactory;
import br.com.caelum.vraptor.ioc.ComponentFactoryIntrospector;
import br.com.caelum.vraptor.ioc.StereotypeHandler;

import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.inject.Binder;
import com.google.inject.Inject;
Expand All @@ -47,6 +46,7 @@
import com.google.inject.TypeLiteral;
import com.google.inject.binder.ScopedBindingBuilder;
import com.google.inject.matcher.Matchers;
import com.google.inject.multibindings.Multibinder;
import com.google.inject.spi.TypeEncounter;
import com.google.inject.spi.TypeListener;
import com.google.inject.util.Types;
Expand All @@ -68,12 +68,18 @@ public class GuiceComponentRegistry implements ComponentRegistry {
private final Set<Class<?>> boundClasses = new HashSet<Class<?>>();
private final Set<Class<?>> listTypes = new HashSet<Class<?>>();

public GuiceComponentRegistry(Binder binder) {
private final Multibinder<StereotypeHandler> stereotypeHandlers;

public GuiceComponentRegistry(Binder binder, Multibinder<StereotypeHandler> stereotypeHandlers) {
this.binder = binder;
this.stereotypeHandlers = stereotypeHandlers;
}
public void register(Class requiredType, Class componentType) {
boundClasses.add(requiredType);
logger.debug("Binding {} to {}", requiredType, componentType);
if (StereotypeHandler.class.isAssignableFrom(requiredType)) {
stereotypeHandlers.addBinding().to(requiredType);
}
ScopedBindingBuilder binding = bindToConstructor(requiredType, componentType);
if (defaultScope(componentType)) {
binding.in(GuiceProvider.REQUEST);
Expand Down
Expand Up @@ -38,6 +38,7 @@
import com.google.inject.Module;
import com.google.inject.Stage;
import com.google.inject.TypeLiteral;
import com.google.inject.multibindings.Multibinder;
import com.google.inject.util.Modules;

/**
Expand Down Expand Up @@ -110,7 +111,7 @@ private void executeStereotypeHandlers() {
protected Module customModule() {
return new Module() {
public void configure(Binder binder) {
ComponentRegistry registry = new GuiceComponentRegistry(binder);
ComponentRegistry registry = new GuiceComponentRegistry(binder, Multibinder.newSetBinder(binder, StereotypeHandler.class));
BasicConfiguration config = new BasicConfiguration(context);

// using the new vraptor.scan
Expand Down
Expand Up @@ -92,7 +92,7 @@ protected void configure() {

bind(Container.class).toInstance(container);

GuiceComponentRegistry registry = new GuiceComponentRegistry(binder());
GuiceComponentRegistry registry = new GuiceComponentRegistry(binder(), Multibinder.newSetBinder(binder(), StereotypeHandler.class));

bind(ComponentRegistry.class).toInstance(registry);

Expand All @@ -103,10 +103,10 @@ protected void configure() {
for (Class converter : BaseComponents.getBundledConverters()) {
registry.register(converter, converter);
}


for (Class handler : BaseComponents.getStereotypeHandlers()) {
registry.register(handler, handler);
Multibinder<StereotypeHandler> stereotypeHandlers = Multibinder.newSetBinder(binder(), StereotypeHandler.class);
stereotypeHandlers.addBinding().to(handler);
}

for (Entry<Class<?>, Class<?>> entry : BaseComponents.getCachedComponents().entrySet()) {
Expand Down

0 comments on commit b746c63

Please sign in to comment.