Search in sources :

Example 6 with OutOfScopeException

use of com.google.inject.OutOfScopeException in project roboguice by roboguice.

the class ScopeRequestIntegrationTest method testNullReplacement.

public final void testNullReplacement() throws Exception {
    Injector injector = Guice.createInjector(new ServletModule() {

        @Override
        protected void configureServlets() {
            bindConstant().annotatedWith(Names.named(SomeObject.INVALID)).to(SHOULDNEVERBESEEN);
            bind(SomeObject.class).in(RequestScoped.class);
        }
    });
    Callable<SomeObject> callable = injector.getInstance(Caller.class);
    try {
        assertNotNull(callable.call());
        fail();
    } catch (ProvisionException pe) {
        assertTrue(pe.getCause() instanceof OutOfScopeException);
    }
    // Validate that an actual null entry in the map results in a null injected object.
    Map<Key<?>, Object> map = Maps.newHashMap();
    map.put(Key.get(SomeObject.class), null);
    callable = ServletScopes.scopeRequest(injector.getInstance(Caller.class), map);
    assertNull(callable.call());
}
Also used : ProvisionException(com.google.inject.ProvisionException) Injector(com.google.inject.Injector) OutOfScopeException(com.google.inject.OutOfScopeException) Key(com.google.inject.Key)

Example 7 with OutOfScopeException

use of com.google.inject.OutOfScopeException in project gerrit by GerritCodeReview.

the class IdentifiedUser method materializedCopy.

/**
   * Returns a materialized copy of the user with all dependencies.
   *
   * <p>Invoke all providers and factories of dependent objects and store the references to a copy
   * of the current identified user.
   *
   * @return copy of the identified user
   */
public IdentifiedUser materializedCopy() {
    CapabilityControl capabilities = getCapabilities();
    Provider<SocketAddress> remotePeer;
    try {
        remotePeer = Providers.of(remotePeerProvider.get());
    } catch (OutOfScopeException | ProvisionException e) {
        remotePeer = new Provider<SocketAddress>() {

            @Override
            public SocketAddress get() {
                throw e;
            }
        };
    }
    return new IdentifiedUser(new CapabilityControl.Factory() {

        @Override
        public CapabilityControl create(CurrentUser user) {
            return capabilities;
        }
    }, authConfig, realm, anonymousCowardName, Providers.of(canonicalUrl.get()), accountCache, groupBackend, disableReverseDnsLookup, remotePeer, state, realUser);
}
Also used : ProvisionException(com.google.inject.ProvisionException) CapabilityControl(com.google.gerrit.server.account.CapabilityControl) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) OutOfScopeException(com.google.inject.OutOfScopeException) Provider(com.google.inject.Provider)

Aggregations

OutOfScopeException (com.google.inject.OutOfScopeException)7 ProvisionException (com.google.inject.ProvisionException)6 Injector (com.google.inject.Injector)4 Key (com.google.inject.Key)4 AbstractModule (com.google.inject.AbstractModule)3 Provider (com.google.inject.Provider)3 ImmutableList (com.google.common.collect.ImmutableList)2 Scope (com.google.inject.Scope)2 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CapabilityControl (com.google.gerrit.server.account.CapabilityControl)1 GerritRequestModule (com.google.gerrit.server.config.GerritRequestModule)1 RequestScopePropagator (com.google.gerrit.server.util.RequestScopePropagator)1 ThreadLocalRequestScopePropagator (com.google.gerrit.server.util.ThreadLocalRequestScopePropagator)1 InetAddress (java.net.InetAddress)1