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

com.netflix.fabricator.guice.mapping.ComponentManagerBinding Maven / Gradle / Ivy

There is a newer version: 0.28.0
Show newest version
package com.netflix.fabricator.guice.mapping;

import com.google.inject.Binding;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import com.google.inject.util.Types;
import com.netflix.fabricator.ConfigurationNode;
import com.netflix.fabricator.component.ComponentManager;

import java.lang.reflect.Method;

/**
 * Created by hyuan on 1/16/14.
 */
public class ComponentManagerBinding implements BindingReslove {
    private String propertyName;

    public ComponentManagerBinding(String propertyName) {
        this.propertyName = propertyName;
    }

    @Override
    public boolean execute(String name, Object obj, ConfigurationNode node, Class argType, Injector injector, Method method) throws Exception {
        TypeLiteral> managerLiteral = (TypeLiteral>) TypeLiteral.get(Types.newParameterizedType(ComponentManager.class, argType));
        Binding> managerBinding = injector.getExistingBinding(Key.get(managerLiteral));
        if (managerBinding != null) {
            ComponentManager manager = managerBinding.getProvider().get();
            try {
                method.invoke(obj, manager.get(name));
                return true;
            }
            catch (Exception e) {
                throw new Exception(String.format(
                        "Unable to get component '%s' (%s) for property '%s' must be one of %s",
                        name, argType.getSimpleName(), propertyName, manager.getIds()), e);
            }
        }
        return false;    
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy