Search in sources :

Example 1 with LoadBalancerFactory

use of org.apache.camel.cloud.LoadBalancerFactory in project camel by apache.

the class ServiceCallLoadBalancerConfiguration method newInstance.

// *************************************************************************
// Factory
// *************************************************************************
@Override
public LoadBalancer newInstance(CamelContext camelContext) throws Exception {
    ObjectHelper.notNull(factoryKey, "LoadBalancer factoryKey");
    LoadBalancer answer;
    // First try to find the factory from the registry.
    LoadBalancerFactory factory = CamelContextHelper.lookup(camelContext, factoryKey, LoadBalancerFactory.class);
    if (factory != null) {
        // If a factory is found in the registry do not re-configure it as
        // it should be pre-configured.
        answer = factory.newInstance(camelContext);
    } else {
        Class<?> type;
        try {
            // Then use Service factory.
            type = camelContext.getFactoryFinder(RESOURCE_PATH).findClass(factoryKey);
        } catch (Exception e) {
            throw new NoFactoryAvailableException(RESOURCE_PATH + factoryKey, e);
        }
        if (type != null) {
            if (LoadBalancerFactory.class.isAssignableFrom(type)) {
                factory = (LoadBalancerFactory) camelContext.getInjector().newInstance(type);
            } else {
                throw new IllegalArgumentException("Resolving LoadBalancer: " + factoryKey + " detected type conflict: Not a LoadBalancerFactory implementation. Found: " + type.getName());
            }
        }
        try {
            Map<String, Object> parameters = new HashMap<>();
            IntrospectionSupport.getProperties(this, parameters, null, false);
            parameters.put("properties", getPropertiesAsMap(camelContext));
            postProcessFactoryParameters(camelContext, parameters);
            IntrospectionSupport.setProperties(factory, parameters);
            answer = factory.newInstance(camelContext);
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }
    return answer;
}
Also used : HashMap(java.util.HashMap) LoadBalancer(org.apache.camel.cloud.LoadBalancer) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) LoadBalancerFactory(org.apache.camel.cloud.LoadBalancerFactory) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException)

Aggregations

HashMap (java.util.HashMap)1 NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)1 LoadBalancer (org.apache.camel.cloud.LoadBalancer)1 LoadBalancerFactory (org.apache.camel.cloud.LoadBalancerFactory)1