Search in sources :

Example 1 with ConfigurationException

use of com.google.inject.ConfigurationException in project druid by druid-io.

the class JettyServerModule method initializeServer.

static void initializeServer(Injector injector, Lifecycle lifecycle, final Server server) {
    JettyServerInitializer initializer = injector.getInstance(JettyServerInitializer.class);
    try {
        initializer.initialize(server, injector);
    } catch (ConfigurationException e) {
        throw new ProvisionException(Iterables.getFirst(e.getErrorMessages(), null).getMessage());
    }
    lifecycle.addHandler(new Lifecycle.Handler() {

        @Override
        public void start() throws Exception {
            server.start();
        }

        @Override
        public void stop() {
            try {
                server.stop();
            } catch (Exception e) {
                log.warn(e, "Unable to stop Jetty server.");
            }
        }
    });
}
Also used : ProvisionException(com.google.inject.ProvisionException) ConfigurationException(com.google.inject.ConfigurationException) Lifecycle(io.druid.java.util.common.lifecycle.Lifecycle) ServletException(javax.servlet.ServletException) ProvisionException(com.google.inject.ProvisionException) ConfigurationException(com.google.inject.ConfigurationException)

Example 2 with ConfigurationException

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

the class InjectionPoint method forConstructorOf.

/**
   * Returns a new injection point for the injectable constructor of {@code type}.
   *
   * @param type a concrete type with exactly one constructor annotated {@literal @}{@link Inject},
   *     or a no-arguments constructor that is not private.
   * @throws ConfigurationException if there is no injectable constructor, more than one injectable
   *     constructor, or if parameters of the injectable constructor are malformed, such as a
   *     parameter with multiple binding annotations.
   */
public static InjectionPoint forConstructorOf(TypeLiteral<?> type) {
    Class<?> rawType = getRawType(type.getType());
    Errors errors = new Errors(rawType);
    Constructor<?> injectableConstructor = null;
    for (Constructor<?> constructor : rawType.getDeclaredConstructors()) {
        boolean optional;
        Inject guiceInject = constructor.getAnnotation(Inject.class);
        if (guiceInject == null) {
            javax.inject.Inject javaxInject = constructor.getAnnotation(javax.inject.Inject.class);
            if (javaxInject == null) {
                continue;
            }
            optional = false;
        } else {
            optional = guiceInject.optional();
        }
        if (optional) {
            errors.optionalConstructor(constructor);
        }
        if (injectableConstructor != null) {
            errors.tooManyConstructors(rawType);
        }
        injectableConstructor = constructor;
        checkForMisplacedBindingAnnotations(injectableConstructor, errors);
    }
    errors.throwConfigurationExceptionIfErrorsExist();
    if (injectableConstructor != null) {
        return new InjectionPoint(type, injectableConstructor);
    }
    // If no annotated constructor is found, look for a no-arg constructor instead.
    try {
        Constructor<?> noArgConstructor = rawType.getDeclaredConstructor();
        // Disallow private constructors on non-private classes (unless they have @Inject)
        if (Modifier.isPrivate(noArgConstructor.getModifiers()) && !Modifier.isPrivate(rawType.getModifiers())) {
            errors.missingConstructor(rawType);
            throw new ConfigurationException(errors.getMessages());
        }
        checkForMisplacedBindingAnnotations(noArgConstructor, errors);
        return new InjectionPoint(type, noArgConstructor);
    } catch (NoSuchMethodException e) {
        errors.missingConstructor(rawType);
        throw new ConfigurationException(errors.getMessages());
    }
}
Also used : Inject(com.google.inject.Inject) Errors(com.google.inject.internal.Errors) ConfigurationException(com.google.inject.ConfigurationException)

Example 3 with ConfigurationException

use of com.google.inject.ConfigurationException in project dsl-devkit by dsldevkit.

the class ResourceServiceProviderLocator method getResourceServiceProviderById.

/**
 * Finds the {@link IResourceServiceProvider} for a language by given its id.
 *
 * @param languageId
 *          the language id (grammar name)
 * @return the {@link IResourceServiceProvider} for the given language id
 */
public IResourceServiceProvider getResourceServiceProviderById(final String languageId) {
    ImmutableMap<Map<String, Object>, ? extends Function<String, IResourceServiceProvider>> resourceProvidersMap = getProviderMaps();
    for (Map.Entry<Map<String, Object>, ? extends Function<String, IResourceServiceProvider>> mapEntry : resourceProvidersMap.entrySet()) {
        Map<String, Object> map = mapEntry.getKey();
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            try {
                IResourceServiceProvider resourceServiceProvider = mapEntry.getValue().apply(entry.getKey());
                if (resourceServiceProvider == null) {
                    continue;
                }
                IGrammarAccess grammarAccess = resourceServiceProvider.get(IGrammarAccess.class);
                if (grammarAccess != null && grammarAccess.getGrammar().getName().equals(languageId)) {
                    return resourceServiceProvider;
                }
            // CHECKSTYLE:OFF
            } catch (ConfigurationException ex) {
            // CHECKSTYLE:ON
            // ignore
            }
        }
    }
    return null;
}
Also used : IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IGrammarAccess(org.eclipse.xtext.IGrammarAccess) ConfigurationException(com.google.inject.ConfigurationException) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 4 with ConfigurationException

use of com.google.inject.ConfigurationException in project dsl-devkit by dsldevkit.

the class CheckCfgUtil method getAllLanguages.

/**
 * Gets the all languages available in the workbench.
 *
 * @return set of all languages
 */
public Set<String> getAllLanguages() {
    Set<String> languages = new HashSet<String>();
    for (String extension : Registry.INSTANCE.getExtensionToFactoryMap().keySet()) {
        final URI dummyUri = URI.createURI("foo:/foo." + extension);
        IResourceServiceProvider resourceServiceProvider = Registry.INSTANCE.getResourceServiceProvider(dummyUri);
        // By checking that description manager is AbstractCachingResourceDescriptionManager we exclude technical languages of the framework
        if (resourceServiceProvider != null && resourceServiceProvider.getResourceDescriptionManager() instanceof AbstractCachingResourceDescriptionManager) {
            try {
                IGrammarAccess grammarAccess = resourceServiceProvider.get(IGrammarAccess.class);
                if (grammarAccess != null && grammarAccess.getGrammar() != null) {
                    languages.add(grammarAccess.getGrammar().getName());
                }
            } catch (ConfigurationException e) {
            // Will happen if no binding for IGrammarAccess was present.
            }
        }
    }
    return languages;
}
Also used : AbstractCachingResourceDescriptionManager(com.avaloq.tools.ddk.xtext.resource.AbstractCachingResourceDescriptionManager) IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IGrammarAccess(org.eclipse.xtext.IGrammarAccess) ConfigurationException(com.google.inject.ConfigurationException) URI(org.eclipse.emf.common.util.URI) HashSet(java.util.HashSet)

Example 5 with ConfigurationException

use of com.google.inject.ConfigurationException in project google-gin by gwtplus.

the class FactoryBindingTest method testTwoUnnamedStringAnnotations.

public void testTwoUnnamedStringAnnotations() {
    try {
        new FactoryBinding(Collections.<Key<?>, TypeLiteral<?>>emptyMap(), Key.get(TwoUnnamedStringsFactory.class), CONTEXT, new GuiceUtil(createInjectableCollector()), null);
        fail("Expected ConfigurationException.");
    } catch (ConfigurationException e) {
        assertTrue(e.getMessage().contains("has more than one parameter of type " + "java.lang.String annotated with @Assisted(\"\")."));
    }
}
Also used : GuiceUtil(com.google.gwt.inject.rebind.util.GuiceUtil) ConfigurationException(com.google.inject.ConfigurationException)

Aggregations

ConfigurationException (com.google.inject.ConfigurationException)62 Injector (com.google.inject.Injector)16 Errors (com.google.inject.internal.Errors)13 InjectionPoint (com.google.inject.spi.InjectionPoint)8 Method (java.lang.reflect.Method)7 AbstractModule (com.google.inject.AbstractModule)5 Inject (com.google.inject.Inject)5 ErrorsException (com.google.inject.internal.ErrorsException)5 Annotation (java.lang.annotation.Annotation)5 TypeLiteral (com.google.inject.TypeLiteral)4 Map (java.util.Map)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 GuiceUtil (com.google.gwt.inject.rebind.util.GuiceUtil)3 Module (com.google.inject.Module)3 Message (com.google.inject.spi.Message)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Binding (com.google.inject.Binding)2