Search in sources :

Example 1 with OutOfScopeException

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

the class CheckedProviderTest method testProvisionExceptionOnDependenciesOfCxtor.

public void testProvisionExceptionOnDependenciesOfCxtor() throws Exception {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            ThrowingProviderBinder.create(binder()).bind(RemoteProvider.class, Foo.class).providing(ProvisionExceptionFoo.class);
            bindScope(BadScope.class, new Scope() {

                @Override
                public <T> Provider<T> scope(Key<T> key, Provider<T> unscoped) {
                    return new Provider<T>() {

                        @Override
                        public T get() {
                            throw new OutOfScopeException("failure");
                        }
                    };
                }
            });
        }
    });
    try {
        injector.getInstance(Key.get(remoteProviderOfFoo)).get();
        fail();
    } catch (ProvisionException pe) {
        assertEquals(2, pe.getErrorMessages().size());
        List<Message> messages = Lists.newArrayList(pe.getErrorMessages());
        assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure", messages.get(0).getMessage());
        assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure", messages.get(1).getMessage());
    }
}
Also used : ProvisionException(com.google.inject.ProvisionException) Scope(com.google.inject.Scope) Injector(com.google.inject.Injector) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) OutOfScopeException(com.google.inject.OutOfScopeException) Key(com.google.inject.Key) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider)

Example 2 with OutOfScopeException

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

the class CheckedProviderTest method testProvisionExceptionOnDependenciesOfCxtor.

public void testProvisionExceptionOnDependenciesOfCxtor() throws Exception {
    Injector injector = Guice.createInjector(new AbstractModule() {

        @Override
        protected void configure() {
            ThrowingProviderBinder.create(binder()).bind(RemoteProvider.class, Foo.class).providing(ProvisionExceptionFoo.class);
            bindScope(BadScope.class, new Scope() {

                @Override
                public <T> Provider<T> scope(final Key<T> key, Provider<T> unscoped) {
                    return new Provider<T>() {

                        @Override
                        public T get() {
                            throw new OutOfScopeException("failure: " + key.toString());
                        }
                    };
                }
            });
        }
    });
    try {
        injector.getInstance(Key.get(remoteProviderOfFoo)).get();
        fail();
    } catch (ProvisionException pe) {
        assertEquals(2, pe.getErrorMessages().size());
        List<Message> messages = Lists.newArrayList(pe.getErrorMessages());
        assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure: " + Key.get(Unscoped1.class), messages.get(0).getMessage());
        assertEquals("Error in custom provider, com.google.inject.OutOfScopeException: failure: " + Key.get(Unscoped2.class), messages.get(1).getMessage());
    }
}
Also used : OutOfScopeException(com.google.inject.OutOfScopeException) AbstractModule(com.google.inject.AbstractModule) Provider(com.google.inject.Provider) ProvisionException(com.google.inject.ProvisionException) Scope(com.google.inject.Scope) Injector(com.google.inject.Injector) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) Key(com.google.inject.Key)

Example 3 with OutOfScopeException

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

the class IdentifiedUser method guessHost.

private String guessHost() {
    String host = null;
    SocketAddress remotePeer = null;
    try {
        remotePeer = remotePeerProvider.get();
    } catch (OutOfScopeException | ProvisionException e) {
    // Leave null.
    }
    if (remotePeer instanceof InetSocketAddress) {
        InetSocketAddress sa = (InetSocketAddress) remotePeer;
        InetAddress in = sa.getAddress();
        host = in != null ? getHost(in) : sa.getHostName();
    }
    if (Strings.isNullOrEmpty(host)) {
        return "unknown";
    }
    return host;
}
Also used : ProvisionException(com.google.inject.ProvisionException) InetSocketAddress(java.net.InetSocketAddress) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) OutOfScopeException(com.google.inject.OutOfScopeException) InetAddress(java.net.InetAddress)

Example 4 with OutOfScopeException

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

the class InProcessProtocol method module.

static Module module() {
    return new AbstractModule() {

        @Override
        public void configure() {
            install(new GerritRequestModule());
            bind(RequestScopePropagator.class).to(Propagator.class);
            bindScope(RequestScoped.class, InProcessProtocol.REQUEST);
        }

        @Provides
        @RemotePeer
        SocketAddress getSocketAddress() {
            // something.
            throw new OutOfScopeException("No remote peer in acceptance tests");
        }
    };
}
Also used : GerritRequestModule(com.google.gerrit.server.config.GerritRequestModule) RequestScopePropagator(com.google.gerrit.server.util.RequestScopePropagator) ThreadLocalRequestScopePropagator(com.google.gerrit.server.util.ThreadLocalRequestScopePropagator) OutOfScopeException(com.google.inject.OutOfScopeException) AbstractModule(com.google.inject.AbstractModule)

Example 5 with OutOfScopeException

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

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)

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