/resteasy-2.3.2.Final/resteasy-guice/src/main/java/org/jboss/resteasy/plugins/guice/ModuleProcessor.java

# · Java · 76 lines · 66 code · 10 blank · 0 comment · 4 complexity · ec54e66eeb676d2bf43691c4a5005cae MD5 · raw file

  1. package org.jboss.resteasy.plugins.guice;
  2. import com.google.inject.Binding;
  3. import com.google.inject.Guice;
  4. import com.google.inject.Injector;
  5. import com.google.inject.Module;
  6. import com.google.inject.Stage;
  7. import org.jboss.resteasy.logging.Logger;
  8. import org.jboss.resteasy.spi.Registry;
  9. import org.jboss.resteasy.spi.ResourceFactory;
  10. import org.jboss.resteasy.spi.ResteasyProviderFactory;
  11. import org.jboss.resteasy.util.GetRestful;
  12. import javax.ws.rs.ext.Provider;
  13. import java.lang.reflect.Type;
  14. public class ModuleProcessor
  15. {
  16. private final static Logger logger = Logger.getLogger(ModuleProcessor.class);
  17. private final Registry registry;
  18. private final ResteasyProviderFactory providerFactory;
  19. public ModuleProcessor(final Registry registry, final ResteasyProviderFactory providerFactory)
  20. {
  21. this.registry = registry;
  22. this.providerFactory = providerFactory;
  23. }
  24. public void process(final Module... modules)
  25. {
  26. final Injector injector = Guice.createInjector(modules);
  27. processInjector(injector);
  28. }
  29. public void process(final Stage stage, final Module... modules)
  30. {
  31. final Injector injector = Guice.createInjector(stage, modules);
  32. processInjector(injector);
  33. }
  34. public void process(final Iterable<Module> modules)
  35. {
  36. final Injector injector = Guice.createInjector(modules);
  37. processInjector(injector);
  38. }
  39. public void process(final Stage stage, final Iterable<Module> modules)
  40. {
  41. final Injector injector = Guice.createInjector(stage, modules);
  42. processInjector(injector);
  43. }
  44. private void processInjector(final Injector injector)
  45. {
  46. for (final Binding<?> binding : injector.getBindings().values())
  47. {
  48. final Type type = binding.getKey().getTypeLiteral().getType();
  49. if (type instanceof Class)
  50. {
  51. final Class<?> beanClass = (Class) type;
  52. if (GetRestful.isRootResource(beanClass))
  53. {
  54. final ResourceFactory resourceFactory = new GuiceResourceFactory(binding.getProvider(), beanClass);
  55. logger.info("registering factory for {0}", beanClass.getName());
  56. registry.addResourceFactory(resourceFactory);
  57. }
  58. if (beanClass.isAnnotationPresent(Provider.class))
  59. {
  60. logger.info("registering provider instance for {0}", beanClass.getName());
  61. providerFactory.registerProviderInstance(binding.getProvider().get());
  62. }
  63. }
  64. }
  65. }
  66. }