Search in sources :

Example 46 with TypeLiteral

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

the class PluginBindings method configure.

@Override
protected void configure() {
    final Multibinder<Plugin> pluginbinder = Multibinder.newSetBinder(binder(), Plugin.class);
    final Multibinder<PluginMetaData> pluginMetaDataBinder = Multibinder.newSetBinder(binder(), PluginMetaData.class);
    // Make sure there is a binding for the plugin rest resource classes to avoid binding errors when running
    // without plugins.
    MapBinder.newMapBinder(binder(), new TypeLiteral<String>() {
    }, new TypeLiteral<Class<? extends PluginRestResource>>() {
    }).permitDuplicates();
    for (final Plugin plugin : plugins) {
        pluginbinder.addBinding().toInstance(plugin);
        for (final PluginModule pluginModule : plugin.modules()) {
            binder().install(pluginModule);
        }
        pluginMetaDataBinder.addBinding().toInstance(plugin.metadata());
    }
}
Also used : TypeLiteral(com.google.inject.TypeLiteral) PluginMetaData(org.graylog2.plugin.PluginMetaData) PluginModule(org.graylog2.plugin.PluginModule) Plugin(org.graylog2.plugin.Plugin)

Example 47 with TypeLiteral

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

the class NamingService method initialize.

public static void initialize() {
    namingDebug = Debug.getInstance("amNaming");
    platformProperties = SystemProperties.getAll();
    server_proto = platformProperties.getProperty("com.iplanet.am.server.protocol", "");
    server_host = platformProperties.getProperty("com.iplanet.am.server.host", "");
    server_port = platformProperties.getProperty(Constants.AM_SERVER_PORT, "");
    PrivilegedAction<SSOToken> adminAction = InjectorHolder.getInstance(Key.get(new TypeLiteral<PrivilegedAction<SSOToken>>() {
    }));
    sso = AccessController.doPrivileged(adminAction);
    try {
        ssmNaming = new ServiceSchemaManager(NAMING_SERVICE, sso);
        ssmPlatform = new ServiceSchemaManager(PLATFORM_SERVICE, sso);
        serviceRevNumber = ssmPlatform.getRevisionNumber();
    } catch (SMSException | SSOException e) {
        throw new IllegalStateException(e);
    }
    ServiceListeners.Action action = new ServiceListeners.Action() {

        @Override
        public void performUpdate() {
            try {
                updateNamingTable();
                WebtopNaming.updateNamingTable();
            } catch (Exception ex) {
                namingDebug.error("Error occured in updating naming table", ex);
            }
        }
    };
    ServiceListeners builder = InjectorHolder.getInstance(ServiceListeners.class);
    builder.config(NAMING_SERVICE).global(action).schema(action).listen();
    builder.config(PLATFORM_SERVICE).global(action).schema(action).listen();
}
Also used : ServiceListeners(com.iplanet.services.naming.ServiceListeners) PrivilegedAction(java.security.PrivilegedAction) SSOToken(com.iplanet.sso.SSOToken) TypeLiteral(com.google.inject.TypeLiteral) SMSException(com.sun.identity.sm.SMSException) SSOException(com.iplanet.sso.SSOException) ServiceSchemaManager(com.sun.identity.sm.ServiceSchemaManager) ServerEntryNotFoundException(com.iplanet.services.naming.ServerEntryNotFoundException) SMSException(com.sun.identity.sm.SMSException) MalformedURLException(java.net.MalformedURLException) SSOException(com.iplanet.sso.SSOException)

Example 48 with TypeLiteral

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

the class PrivateInternals_DynamicTypes method dynamicItemsOf.

public static Map<TypeLiteral<?>, DynamicItem<?>> dynamicItemsOf(Injector src) {
    Map<TypeLiteral<?>, DynamicItem<?>> m = new HashMap<>();
    for (Map.Entry<Key<?>, Binding<?>> e : src.getBindings().entrySet()) {
        TypeLiteral<?> type = e.getKey().getTypeLiteral();
        if (type.getRawType() == DynamicItem.class) {
            ParameterizedType p = (ParameterizedType) type.getType();
            m.put(TypeLiteral.get(p.getActualTypeArguments()[0]), (DynamicItem<?>) e.getValue().getProvider().get());
        }
    }
    if (m.isEmpty()) {
        return Collections.emptyMap();
    }
    return Collections.unmodifiableMap(m);
}
Also used : Binding(com.google.inject.Binding) ParameterizedType(java.lang.reflect.ParameterizedType) TypeLiteral(com.google.inject.TypeLiteral) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap) Key(com.google.inject.Key)

Example 49 with TypeLiteral

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

the class Passwd method getSysInjector.

private Injector getSysInjector() {
    List<Module> modules = new ArrayList<>();
    modules.add(new FactoryModule() {

        @Override
        protected void configure() {
            bind(Path.class).annotatedWith(SitePath.class).toInstance(getSitePath());
            bind(ConsoleUI.class).toInstance(ConsoleUI.getInstance(password != null));
            factory(Section.Factory.class);
            bind(Boolean.class).annotatedWith(InstallAllPlugins.class).toInstance(Boolean.FALSE);
            bind(new TypeLiteral<List<String>>() {
            }).annotatedWith(InstallPlugins.class).toInstance(new ArrayList<String>());
            bind(String.class).annotatedWith(SecureStoreClassName.class).toProvider(Providers.of(getConfiguredSecureStoreClass()));
        }
    });
    modules.add(new GerritServerConfigModule());
    return Guice.createInjector(modules);
}
Also used : SitePath(com.google.gerrit.server.config.SitePath) Path(java.nio.file.Path) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) TypeLiteral(com.google.inject.TypeLiteral) FactoryModule(com.google.gerrit.extensions.config.FactoryModule) ArrayList(java.util.ArrayList) Module(com.google.inject.Module) GerritServerConfigModule(com.google.gerrit.server.config.GerritServerConfigModule) FactoryModule(com.google.gerrit.extensions.config.FactoryModule)

Example 50 with TypeLiteral

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

the class PluginGuiceEnvironment method reattachSet.

private void reattachSet(ListMultimap<TypeLiteral<?>, ReloadableRegistrationHandle<?>> oldHandles, Map<TypeLiteral<?>, DynamicSet<?>> sets, @Nullable Injector src, Plugin newPlugin) {
    if (src == null || sets == null || sets.isEmpty()) {
        return;
    }
    for (Map.Entry<TypeLiteral<?>, DynamicSet<?>> e : sets.entrySet()) {
        @SuppressWarnings("unchecked") TypeLiteral<Object> type = (TypeLiteral<Object>) e.getKey();
        @SuppressWarnings("unchecked") DynamicSet<Object> set = (DynamicSet<Object>) e.getValue();
        // Index all old handles that match this DynamicSet<T> keyed by
        // annotations. Ignore the unique annotations, thereby favoring
        // the @Named annotations or some other non-unique naming.
        Map<Annotation, ReloadableRegistrationHandle<?>> am = new HashMap<>();
        List<ReloadableRegistrationHandle<?>> old = oldHandles.get(type);
        Iterator<ReloadableRegistrationHandle<?>> oi = old.iterator();
        while (oi.hasNext()) {
            ReloadableRegistrationHandle<?> h = oi.next();
            Annotation a = h.getKey().getAnnotation();
            if (a != null && !UNIQUE_ANNOTATION.isInstance(a)) {
                am.put(a, h);
                oi.remove();
            }
        }
        // Replace old handles with new bindings, favoring cases where there
        // is an exact match on an @Named annotation. If there is no match
        // pick any handle and replace it. We generally expect only one
        // handle of each DynamicSet type when using unique annotations, but
        // possibly multiple ones if @Named was used. Plugin authors that want
        // atomic replacement across reloads should use @Named annotations with
        // stable names that do not change across plugin versions to ensure the
        // handles are swapped correctly.
        oi = old.iterator();
        for (Binding<?> binding : bindings(src, type)) {
            @SuppressWarnings("unchecked") Binding<Object> b = (Binding<Object>) binding;
            Key<Object> key = b.getKey();
            if (key.getAnnotation() == null) {
                continue;
            }
            @SuppressWarnings("unchecked") ReloadableRegistrationHandle<Object> h1 = (ReloadableRegistrationHandle<Object>) am.remove(key.getAnnotation());
            if (h1 != null) {
                replace(newPlugin, h1, b);
            } else if (oi.hasNext()) {
                @SuppressWarnings("unchecked") ReloadableRegistrationHandle<Object> h2 = (ReloadableRegistrationHandle<Object>) oi.next();
                oi.remove();
                replace(newPlugin, h2, b);
            } else {
                newPlugin.add(set.add(b.getKey(), b.getProvider()));
            }
        }
    }
}
Also used : Binding(com.google.inject.Binding) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DynamicSet(com.google.gerrit.extensions.registration.DynamicSet) Annotation(java.lang.annotation.Annotation) TypeLiteral(com.google.inject.TypeLiteral) ReloadableRegistrationHandle(com.google.gerrit.extensions.registration.ReloadableRegistrationHandle) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) DynamicMap(com.google.gerrit.extensions.registration.DynamicMap)

Aggregations

TypeLiteral (com.google.inject.TypeLiteral)118 AbstractModule (com.google.inject.AbstractModule)43 Injector (com.google.inject.Injector)38 Module (com.google.inject.Module)15 Map (java.util.Map)14 Key (com.google.inject.Key)12 Provider (com.google.inject.Provider)12 ParameterizedType (java.lang.reflect.ParameterizedType)12 ImmutableSet (com.google.common.collect.ImmutableSet)10 Binding (com.google.inject.Binding)10 Annotation (java.lang.annotation.Annotation)10 HashMap (java.util.HashMap)10 Set (java.util.Set)10 Binder (com.google.inject.Binder)9 InjectionPoint (com.google.inject.spi.InjectionPoint)9 Method (java.lang.reflect.Method)9 Type (java.lang.reflect.Type)9 HashSet (java.util.HashSet)9 ProvisionException (com.google.inject.ProvisionException)7 FactoryModuleBuilder (com.google.inject.assistedinject.FactoryModuleBuilder)7