com.netflix.ribbon.guice.RibbonResourceProvider Maven / Gradle / Ivy
/*
* Copyright 2014 Netflix, Inc.
*
* Licensed 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 com.netflix.ribbon.guice;
import com.google.inject.Inject;
import com.google.inject.spi.BindingTargetVisitor;
import com.google.inject.spi.ProviderInstanceBinding;
import com.google.inject.spi.ProviderWithExtensionVisitor;
import com.google.inject.spi.Toolable;
import com.netflix.ribbon.RibbonResourceFactory;
/**
* Guice Provider extension to create a ribbon resource from an injectable
* RibbonResourceFactory.
*
* @author elandau
*
* @param Type of the client API interface class
*/
public class RibbonResourceProvider implements ProviderWithExtensionVisitor {
private RibbonResourceFactory factory;
private final Class contract;
public RibbonResourceProvider(Class contract) {
this.contract = contract;
}
@Override
public T get() {
// TODO: Get name from annotations (not only class name of contract)
return factory.from(contract);
}
/**
* This is needed for 'initialize(injector)' below to be called so the provider
* can get the injector after it is instantiated.
*/
@Override
public V acceptExtensionVisitor(
BindingTargetVisitor visitor,
ProviderInstanceBinding extends B> binding) {
return visitor.visit(binding);
}
@Inject
@Toolable
protected void initialize(RibbonResourceFactory factory) {
this.factory = factory;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy