All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.elasticsearch.common.inject.Injectors Maven / Gradle / Ivy

There is a newer version: 8.15.1
Show newest version
/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.common.inject;

import com.google.common.collect.Sets;
import org.elasticsearch.common.inject.matcher.Matcher;
import org.elasticsearch.common.inject.name.Names;
import org.elasticsearch.common.inject.spi.Message;

import java.lang.reflect.Type;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

/**
 *
 */
public class Injectors {

    public static Throwable getFirstErrorFailure(CreationException e) {
        if (e.getErrorMessages().isEmpty()) {
            return e;
        }
        // return the first message that has root cause, probably an actual error
        for (Message message : e.getErrorMessages()) {
            if (message.getCause() != null) {
                return message.getCause();
            }
        }
        return e;
    }

    /**
     * Returns an instance of the given type with the {@link org.elasticsearch.common.inject.name.Named}
     * annotation value.
     * 

* This method allows you to switch this code * injector.getInstance(Key.get(type, Names.named(name))); *

* to the more concise * Injectors.getInstance(injector, type, name); */ public static T getInstance(Injector injector, java.lang.Class type, String name) { return injector.getInstance(Key.get(type, Names.named(name))); } /** * Returns a collection of all instances of the given base type * * @param baseClass the base type of objects required * @param the base type * @return a set of objects returned from this injector */ public static Set getInstancesOf(Injector injector, Class baseClass) { Set answer = Sets.newHashSet(); Set, Binding>> entries = injector.getBindings().entrySet(); for (Entry, Binding> entry : entries) { Key key = entry.getKey(); Class keyType = getKeyType(key); if (keyType != null && baseClass.isAssignableFrom(keyType)) { Binding binding = entry.getValue(); Object value = binding.getProvider().get(); if (value != null) { T castValue = baseClass.cast(value); answer.add(castValue); } } } return answer; } /** * Returns a collection of all instances matching the given matcher * * @param matcher matches the types to return instances * @return a set of objects returned from this injector */ public static Set getInstancesOf(Injector injector, Matcher matcher) { Set answer = Sets.newHashSet(); Set, Binding>> entries = injector.getBindings().entrySet(); for (Entry, Binding> entry : entries) { Key key = entry.getKey(); Class keyType = getKeyType(key); if (keyType != null && matcher.matches(keyType)) { Binding binding = entry.getValue(); Object value = binding.getProvider().get(); answer.add((T) value); } } return answer; } /** * Returns a collection of all of the providers matching the given matcher * * @param matcher matches the types to return instances * @return a set of objects returned from this injector */ public static Set> getProvidersOf(Injector injector, Matcher matcher) { Set> answer = Sets.newHashSet(); Set, Binding>> entries = injector.getBindings().entrySet(); for (Entry, Binding> entry : entries) { Key key = entry.getKey(); Class keyType = getKeyType(key); if (keyType != null && matcher.matches(keyType)) { Binding binding = entry.getValue(); answer.add((Provider) binding.getProvider()); } } return answer; } /** * Returns a collection of all providers of the given base type * * @param baseClass the base type of objects required * @param the base type * @return a set of objects returned from this injector */ public static Set> getProvidersOf(Injector injector, Class baseClass) { Set> answer = Sets.newHashSet(); Set, Binding>> entries = injector.getBindings().entrySet(); for (Entry, Binding> entry : entries) { Key key = entry.getKey(); Class keyType = getKeyType(key); if (keyType != null && baseClass.isAssignableFrom(keyType)) { Binding binding = entry.getValue(); answer.add((Provider) binding.getProvider()); } } return answer; } /** * Returns true if a binding exists for the given matcher */ public static boolean hasBinding(Injector injector, Matcher matcher) { return !getBindingsOf(injector, matcher).isEmpty(); } /** * Returns true if a binding exists for the given base class */ public static boolean hasBinding(Injector injector, Class baseClass) { return !getBindingsOf(injector, baseClass).isEmpty(); } /** * Returns true if a binding exists for the given key */ public static boolean hasBinding(Injector injector, Key key) { Binding binding = getBinding(injector, key); return binding != null; } /** * Returns the binding for the given key or null if there is no such binding */ public static Binding getBinding(Injector injector, Key key) { Map, Binding> bindings = injector.getBindings(); Binding binding = bindings.get(key); return binding; } /** * Returns a collection of all of the bindings matching the given matcher * * @param matcher matches the types to return instances * @return a set of objects returned from this injector */ public static Set> getBindingsOf(Injector injector, Matcher matcher) { Set> answer = Sets.newHashSet(); Set, Binding>> entries = injector.getBindings().entrySet(); for (Entry, Binding> entry : entries) { Key key = entry.getKey(); Class keyType = getKeyType(key); if (keyType != null && matcher.matches(keyType)) { answer.add(entry.getValue()); } } return answer; } /** * Returns a collection of all bindings of the given base type * * @param baseClass the base type of objects required * @return a set of objects returned from this injector */ public static Set> getBindingsOf(Injector injector, Class baseClass) { Set> answer = Sets.newHashSet(); Set, Binding>> entries = injector.getBindings().entrySet(); for (Entry, Binding> entry : entries) { Key key = entry.getKey(); Class keyType = getKeyType(key); if (keyType != null && baseClass.isAssignableFrom(keyType)) { answer.add(entry.getValue()); } } return answer; } /** * Returns the key type of the given key */ public static Class getKeyType(Key key) { Class keyType = null; TypeLiteral typeLiteral = key.getTypeLiteral(); Type type = typeLiteral.getType(); if (type instanceof Class) { keyType = (Class) type; } return keyType; } public static void close(Injector injector) { } public static void cleanCaches(Injector injector) { ((InjectorImpl) injector).clearCache(); if (injector.getParent() != null) { cleanCaches(injector.getParent()); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy