Search in sources :

Example 76 with CreationException

use of com.google.inject.CreationException in project graylog2-server by Graylog2.

the class CmdLineTool method setupInjector.

protected Injector setupInjector(NamedConfigParametersModule configModule, Module... otherModules) {
    try {
        final ImmutableList.Builder<Module> modules = ImmutableList.builder();
        modules.add(configModule);
        modules.addAll(getSharedBindingsModules());
        modules.addAll(getCommandBindings());
        modules.addAll(Arrays.asList(otherModules));
        modules.add(new Module() {

            @Override
            public void configure(Binder binder) {
                binder.bind(String.class).annotatedWith(Names.named("BootstrapCommand")).toInstance(commandName);
            }
        });
        return GuiceInjectorHolder.createInjector(modules.build());
    } catch (CreationException e) {
        annotateInjectorCreationException(e);
        return null;
    } catch (Exception e) {
        LOG.error("Injector creation failed!", e);
        return null;
    }
}
Also used : Binder(com.google.inject.Binder) ImmutableList(com.google.common.collect.ImmutableList) CreationException(com.google.inject.CreationException) Module(com.google.inject.Module) PluginModule(org.graylog2.plugin.PluginModule) NamedConfigParametersModule(com.github.joschi.jadconfig.guice.NamedConfigParametersModule) ParameterException(com.github.joschi.jadconfig.ParameterException) NodeIdPersistenceException(org.graylog2.plugin.system.NodeIdPersistenceException) RepositoryException(com.github.joschi.jadconfig.RepositoryException) ValidationException(com.github.joschi.jadconfig.ValidationException) AccessDeniedException(java.nio.file.AccessDeniedException) CreationException(com.google.inject.CreationException)

Example 77 with CreationException

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

the class BaseInit method createSiteInit.

private SiteInit createSiteInit() {
    final ConsoleUI ui = getConsoleUI();
    final Path sitePath = getSitePath();
    final List<Module> m = new ArrayList<>();
    final SecureStoreInitData secureStoreInitData = discoverSecureStoreClass();
    final String currentSecureStoreClassName = getConfiguredSecureStoreClass();
    if (secureStoreInitData != null && currentSecureStoreClassName != null && !currentSecureStoreClassName.equals(secureStoreInitData.className)) {
        String err = String.format("Different secure store was previously configured: %s. " + "Use SwitchSecureStore program to switch between implementations.", currentSecureStoreClassName);
        throw die(err);
    }
    m.add(new GerritServerConfigModule());
    m.add(new InitModule(standalone, initDb));
    m.add(new AbstractModule() {

        @Override
        protected void configure() {
            bind(ConsoleUI.class).toInstance(ui);
            bind(Path.class).annotatedWith(SitePath.class).toInstance(sitePath);
            List<String> plugins = MoreObjects.firstNonNull(getInstallPlugins(), new ArrayList<String>());
            bind(new TypeLiteral<List<String>>() {
            }).annotatedWith(InstallPlugins.class).toInstance(plugins);
            bind(new TypeLiteral<Boolean>() {
            }).annotatedWith(InstallAllPlugins.class).toInstance(installAllPlugins());
            bind(PluginsDistribution.class).toInstance(pluginsDistribution);
            String secureStoreClassName;
            if (secureStoreInitData != null) {
                secureStoreClassName = secureStoreInitData.className;
            } else {
                secureStoreClassName = currentSecureStoreClassName;
            }
            if (secureStoreClassName != null) {
                ui.message("Using secure store: %s\n", secureStoreClassName);
            }
            bind(SecureStoreInitData.class).toProvider(Providers.of(secureStoreInitData));
            bind(String.class).annotatedWith(SecureStoreClassName.class).toProvider(Providers.of(secureStoreClassName));
            bind(SecureStore.class).toProvider(SecureStoreProvider.class).in(SINGLETON);
            bind(new TypeLiteral<List<String>>() {
            }).annotatedWith(LibraryDownload.class).toInstance(getSkippedDownloads());
            bind(Boolean.class).annotatedWith(LibraryDownload.class).toInstance(skipAllDownloads());
            bind(MetricMaker.class).to(DisabledMetricMaker.class);
        }
    });
    try {
        return Guice.createInjector(PRODUCTION, m).getInstance(SiteInit.class);
    } catch (CreationException ce) {
        final Message first = ce.getErrorMessages().iterator().next();
        Throwable why = first.getCause();
        if (why instanceof Die) {
            throw (Die) why;
        }
        final StringBuilder buf = new StringBuilder(ce.getMessage());
        while (why != null) {
            buf.append("\n");
            buf.append(why.getMessage());
            why = why.getCause();
            if (why != null) {
                buf.append("\n  caused by ");
            }
        }
        throw die(buf.toString(), new RuntimeException("InitInjector failed", ce));
    }
}
Also used : Path(java.nio.file.Path) SitePath(com.google.gerrit.server.config.SitePath) Die(com.google.gerrit.common.Die) Message(com.google.inject.spi.Message) ArrayList(java.util.ArrayList) CreationException(com.google.inject.CreationException) SecureStore(com.google.gerrit.server.securestore.SecureStore) ConsoleUI(com.google.gerrit.pgm.init.api.ConsoleUI) AbstractModule(com.google.inject.AbstractModule) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) TypeLiteral(com.google.inject.TypeLiteral) List(java.util.List) ArrayList(java.util.ArrayList) Module(com.google.inject.Module) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) IndexModule(com.google.gerrit.server.index.IndexModule) AbstractModule(com.google.inject.AbstractModule) DisabledMetricMaker(com.google.gerrit.metrics.DisabledMetricMaker)

Example 78 with CreationException

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

the class CheckedProviderTest method testManyMethods.

public void testManyMethods() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                install(ThrowingProviderBinder.forModule(this));
            }

            @SuppressWarnings("unused")
            @CheckedProvides(ManyMethods.class)
            String foo() {
                return null;
            }
        });
        fail();
    } catch (CreationException ce) {
        assertEquals(ManyMethods.class.getName() + " may not declare any new methods, but declared " + Arrays.asList(ManyMethods.class.getDeclaredMethods()), Iterables.getOnlyElement(ce.getErrorMessages()).getMessage());
    }
}
Also used : CreationException(com.google.inject.CreationException) AbstractModule(com.google.inject.AbstractModule)

Example 79 with CreationException

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

the class CheckedProviderTest method testCxtorWithSuperclassExceptionFails.

public void testCxtorWithSuperclassExceptionFails() {
    try {
        Guice.createInjector(new AbstractModule() {

            @Override
            protected void configure() {
                ThrowingProviderBinder.create(binder()).bind(RemoteProvider.class, Foo.class).providing(SuperclassExceptionFoo.class);
            }
        });
        fail();
    } catch (CreationException ce) {
        assertEquals(IOException.class.getName() + " is not compatible with the exceptions ([" + RemoteException.class + ", " + BindException.class + "]) declared in the CheckedProvider interface (" + RemoteProvider.class.getName() + ")", Iterables.getOnlyElement(ce.getErrorMessages()).getMessage());
    }
}
Also used : BindException(java.net.BindException) CreationException(com.google.inject.CreationException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) AbstractModule(com.google.inject.AbstractModule)

Example 80 with CreationException

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

the class ThrowingProviderTest method testProviderMethodWithWrongException.

public void testProviderMethodWithWrongException() {
    try {
        Guice.createInjector(new AbstractModule() {

            protected void configure() {
                install(ThrowingProviderBinder.forModule(this));
            }

            @SuppressWarnings("unused")
            @CheckedProvides(RemoteProvider.class)
            String foo() throws InterruptedException {
                return null;
            }
        });
        fail();
    } catch (CreationException ce) {
        assertEquals(InterruptedException.class.getName() + " is not compatible with the exceptions ([" + RemoteException.class + "]) declared in the CheckedProvider interface (" + RemoteProvider.class.getName() + ")", Iterables.getOnlyElement(ce.getErrorMessages()).getMessage());
    }
}
Also used : CreationException(com.google.inject.CreationException) RemoteException(java.rmi.RemoteException) AbstractModule(com.google.inject.AbstractModule)

Aggregations

CreationException (com.google.inject.CreationException)169 AbstractModule (com.google.inject.AbstractModule)163 Module (com.google.inject.Module)26 RemoteException (java.rmi.RemoteException)18 BindException (java.net.BindException)12 IOException (java.io.IOException)9 List (java.util.List)8 ImmutableList (com.google.common.collect.ImmutableList)7 PrivateModule (com.google.inject.PrivateModule)7 TooManyListenersException (java.util.TooManyListenersException)6 Provider (com.google.inject.Provider)5 Message (com.google.inject.spi.Message)5 ArrayList (java.util.ArrayList)5 Injector (com.google.inject.Injector)4 Provides (com.google.inject.Provides)4 AccessException (java.rmi.AccessException)4 Key (com.google.inject.Key)3 TypeLiteral (com.google.inject.TypeLiteral)3 JCommander (com.beust.jcommander.JCommander)2 Console (com.beust.jcommander.internal.Console)2