Search in sources :

Example 41 with ProvisionException

use of com.google.inject.ProvisionException in project OpenAM by OpenRock.

the class ConfigLoader method validateClass.

/**
     * Validates that the specified class can be loaded and implements the proper interface so that we don't have to do
     * that for every request.
     *
     * @param cfg
     * @return the class of the RequestHandler to be used for this client config, or null if the class is invalid.
     */
@SuppressWarnings("unchecked")
private Class validateClass(ClientConfig cfg) {
    Class clazz = null;
    try {
        clazz = Class.forName(cfg.getAccessRequestHandlerClassname());
    } catch (final ClassNotFoundException e) {
        LOG.error("Unable to load Handler Class '" + cfg.getAccessRequestHandlerClassname() + "' for RADIUS client '" + cfg.getName() + "'. Requests from this client will be ignored.", e);
        return null;
    }
    Object inst = null;
    try {
        inst = InjectorHolder.getInstance(clazz);
    } catch (ConfigurationException | ProvisionException e) {
        LOG.error("Unable to instantiate Handler Class '" + cfg.getAccessRequestHandlerClassname() + "' for RADIUS client '" + cfg.getName() + "'. Requests from this client will be ignored.", e);
        return null;
    }
    AccessRequestHandler handler = null;
    try {
        handler = (AccessRequestHandler) inst;
    } catch (final ClassCastException e) {
        LOG.error("Unable to use Handler Class '" + cfg.getAccessRequestHandlerClassname() + "' for RADIUS client '" + cfg.getName() + "'. Requests from this client will be ignored.", e);
        return null;
    }
    return clazz;
}
Also used : ProvisionException(com.google.inject.ProvisionException) ConfigurationException(com.google.inject.ConfigurationException) AccessRequestHandler(org.forgerock.openam.radius.server.spi.AccessRequestHandler)

Example 42 with ProvisionException

use of com.google.inject.ProvisionException in project metacat by Netflix.

the class SNSNotificationServiceImplProvider method get.

/**
     * {@inheritDoc}
     */
@Override
public NotificationService get() {
    if (this.config.isSnsNotificationEnabled()) {
        final String tableArn = this.config.getSnsTopicTableArn();
        if (StringUtils.isEmpty(tableArn)) {
            throw new ProvisionException("SNS Notifications are enabled but no table ARN provided. Unable to configure.");
        }
        final String partitionArn = this.config.getSnsTopicPartitionArn();
        if (StringUtils.isEmpty(partitionArn)) {
            throw new ProvisionException("SNS Notifications are enabled but no partition ARN provided. Unable to configure.");
        }
        log.info("SNS notifications are enabled. Providing SNSNotificationServiceImpl implementation.");
        return new SNSNotificationServiceImpl(new AmazonSNSClient(), tableArn, partitionArn, this.mapper, registry);
    } else {
        log.info("SNS notifications are not enabled. Ignoring and providing default implementation.");
        return new DefaultNotificationServiceImpl();
    }
}
Also used : ProvisionException(com.google.inject.ProvisionException) DefaultNotificationServiceImpl(com.netflix.metacat.main.services.notifications.DefaultNotificationServiceImpl) AmazonSNSClient(com.amazonaws.services.sns.AmazonSNSClient)

Example 43 with ProvisionException

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

the class MultibinderTest method testConcurrentMutation_bindingsDiffentAtInjectorCreation.

/*
   * Verify through gratuitous mutation that key hashCode snapshots and whatnot happens at the right
   * times, by binding two lists that are different at injector creation, but compare equal when the
   * module is configured *and* when the set is instantiated.
   */
public void testConcurrentMutation_bindingsDiffentAtInjectorCreation() {
    // We initially bind two equal lists
    final List<String> list1 = Lists.newArrayList();
    final List<String> list2 = Lists.newArrayList();
    Module module = new AbstractModule() {

        @Override
        protected void configure() {
            Multibinder<List<String>> multibinder = Multibinder.newSetBinder(binder(), listOfStrings);
            multibinder.addBinding().toInstance(list1);
            multibinder.addBinding().toInstance(list2);
        }
    };
    List<Element> elements = Elements.getElements(module);
    // Now we change the lists so they no longer match, and create the injector.
    list1.add("A");
    list2.add("B");
    Injector injector = Guice.createInjector(Elements.getModule(elements));
    // Now we change the lists so they compare equal again, and create the set.
    list1.add(1, "B");
    list2.add(0, "A");
    try {
        injector.getInstance(Key.get(setOfListOfStrings));
        fail();
    } catch (ProvisionException e) {
        assertEquals(1, e.getErrorMessages().size());
        assertContains(Iterables.getOnlyElement(e.getErrorMessages()).getMessage().toString(), "Set injection failed due to duplicated element \"[A, B]\"");
    }
    // Finally, we change the lists again so they are once more different, and ensure the set
    // contains both.
    list1.remove("A");
    list2.remove("B");
    Set<List<String>> set = injector.getInstance(Key.get(setOfListOfStrings));
    assertEquals(ImmutableSet.of(ImmutableList.of("A"), ImmutableList.of("B")), set);
}
Also used : ProvisionException(com.google.inject.ProvisionException) Injector(com.google.inject.Injector) Element(com.google.inject.spi.Element) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Example 44 with ProvisionException

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

the class MultibinderTest method testMultibinderSetForbidsDuplicateElements.

public void testMultibinderSetForbidsDuplicateElements() {
    Module module1 = new AbstractModule() {

        protected void configure() {
            final Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class);
            multibinder.addBinding().toProvider(Providers.of("A"));
        }
    };
    Module module2 = new AbstractModule() {

        protected void configure() {
            final Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class);
            multibinder.addBinding().toInstance("A");
        }
    };
    Injector injector = Guice.createInjector(module1, module2);
    try {
        injector.getInstance(Key.get(setOfString));
        fail();
    } catch (ProvisionException expected) {
        assertContains(expected.getMessage(), "1) Set injection failed due to duplicated element \"A\"", "Bound at " + module1.getClass().getName(), "Bound at " + module2.getClass().getName());
    }
    // But we can still visit the module!
    assertSetVisitor(Key.get(setOfString), Key.get(collectionOfProvidersOfStrings), stringType, setOf(module1, module2), MODULE, false, 0, instance("A"), instance("A"));
}
Also used : ProvisionException(com.google.inject.ProvisionException) Injector(com.google.inject.Injector) Module(com.google.inject.Module) AbstractModule(com.google.inject.AbstractModule) AbstractModule(com.google.inject.AbstractModule)

Example 45 with ProvisionException

use of com.google.inject.ProvisionException 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)

Aggregations

ProvisionException (com.google.inject.ProvisionException)57 Injector (com.google.inject.Injector)22 AbstractModule (com.google.inject.AbstractModule)20 Module (com.google.inject.Module)11 Provider (com.google.inject.Provider)9 OutOfScopeException (com.google.inject.OutOfScopeException)6 TypeLiteral (com.google.inject.TypeLiteral)6 Key (com.google.inject.Key)5 Message (com.google.inject.spi.Message)5 ImmutableList (com.google.common.collect.ImmutableList)4 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)4 OrmException (com.google.gwtorm.server.OrmException)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Provides (com.google.inject.Provides)3 Dependency (com.google.inject.spi.Dependency)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Method (java.lang.reflect.Method)3 Path (java.nio.file.Path)3