com.google.inject.Scope Java Examples

The following examples show how to use com.google.inject.Scope. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MultipleSingletonPluginUITest.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
private boolean isSingleton(Binding<?> b) {
	return b.acceptScopingVisitor(new DefaultBindingScopingVisitor<Boolean>() {
		@Override
		public Boolean visitEagerSingleton() {
			return Boolean.TRUE;
		}

		@Override
		public Boolean visitScope(Scope scope) {
			return Scopes.SINGLETON.equals(scope);
		}

		@Override
		protected Boolean visitOther() {
			return Boolean.FALSE;
		}
	});
}
 
Example #2
Source File: GuiceScopingVisitor.java    From dropwizard-guicey with MIT License 6 votes vote down vote up
@Override
public Class<? extends Annotation> visitScope(final Scope scope) {
    Class<? extends Annotation> res = null;
    if (scope == Scopes.SINGLETON) {
        res = javax.inject.Singleton.class;
    }
    if (scope == Scopes.NO_SCOPE) {
        res = Prototype.class;
    }
    if (scope == ServletScopes.REQUEST) {
        res = RequestScoped.class;
    }
    if (scope == ServletScopes.SESSION) {
        res = SessionScoped.class;
    }
    // not supporting custom scopes
    return res;
}
 
Example #3
Source File: LazySingletonScopeTest.java    From hivemq-community-edition with Apache License 2.0 5 votes vote down vote up
@Test
public void test_lazy_singleton_scope_is_singleton() throws Exception {
    final Scope scope1 = LazySingletonScope.get();
    final Scope scope2 = LazySingletonScope.get();

    assertSame(scope1, scope2);
}
 
Example #4
Source File: LazySingletonModule.java    From hivemq-community-edition with Apache License 2.0 4 votes vote down vote up
@Override
protected void configure() {
    final Scope lazySingletonScope = LazySingletonScope.get();
    bindScope(LazySingleton.class, lazySingletonScope);
}
 
Example #5
Source File: AbstractBindingBuilder.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void in(Scope scope) {
    applyScoping(Scoping.forInstance(scope));
}
 
Example #6
Source File: GuiceServiceModule.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
@Override
protected void configure() {
  binder().bindListener(Matchers.any(), new ProvisionListener() {
    @Override
    public <T> void onProvision(ProvisionInvocation<T> provision) {
      final Binding<T> binding = provision.getBinding();
      logger.debug("provisioning {}", binding.getKey().getTypeLiteral());

      final T provisioned = provision.provision();

      if (provisioned != null && Service.class.isAssignableFrom(provisioned.getClass())) {
        final AtomicBoolean start = new AtomicBoolean(false);
        binding.acceptScopingVisitor(new DefaultBindingScopingVisitor<T>() {
          @Override
          public T visitEagerSingleton() {
            start.set(true);
            return super.visitEagerSingleton();
          }

          @Override
          public T visitScope(Scope scope) {
            if (scope instanceof SingletonScope) {
              start.set(true);
            }
            return super.visitScope(scope);
          }
        });

        if (start.get()) {
          serviceList.push(binding.getKey().getTypeLiteral().getRawType());
          try {
            logger.debug("starting {}", binding.getKey().getTypeLiteral());
            ((Service) provisioned).start();
          } catch (Exception e) {
            throwIfUnchecked(e);
            throw new RuntimeException(e);
          }
        }
      }
    }
  });
}
 
Example #7
Source File: LifecycleMatcher.java    From seed with Mozilla Public License 2.0 4 votes vote down vote up
@Override
public Boolean visitScope(Scope scope) {
    return scope == Scopes.SINGLETON;
}
 
Example #8
Source File: SagaLibModule.java    From saga-lib with Apache License 2.0 4 votes vote down vote up
/**
 * Perform binding to interface only if implementation type is not null.
 */
private <T> void bindIfNotNull(final Class<T> interfaceType, @Nullable final Class<? extends T> implementationType, final Scope scope) {
    if (implementationType != null) {
        bind(interfaceType).to(implementationType).in(scope);
    }
}
 
Example #9
Source File: ConfigModule.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
ConfigModule(Flags flags, Config config, Path outputDir, Scope documentationScope) {
  this.flags = flags;
  this.config = config;
  this.outputDir = outputDir;
  this.documentationScope = documentationScope;
}
 
Example #10
Source File: LazySingletonScope.java    From hivemq-community-edition with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the LazySingletonScope
 *
 * @return the LazySingletonScope
 */
public static Scope get() {
    return instance;
}
 
Example #11
Source File: Binders.java    From ProjectAres with GNU Affero General Public License v3.0 votes vote down vote up
default void installIn(Scope scope, Module... modules) { installIn(Scoping.forInstance(scope), modules); }