Search in sources :

Example 1 with ServiceDiscoveryFactory

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

the class ServiceCallServiceDiscoveryConfiguration method newInstance.

// *************************************************************************
// Factory
// *************************************************************************
@Override
public ServiceDiscovery newInstance(CamelContext camelContext) throws Exception {
    ObjectHelper.notNull(factoryKey, "ServiceDiscovery factoryKey");
    ServiceDiscovery answer;
    // First try to find the factory from the registry.
    ServiceDiscoveryFactory factory = CamelContextHelper.lookup(camelContext, factoryKey, ServiceDiscoveryFactory.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 (ServiceDiscoveryFactory.class.isAssignableFrom(type)) {
                factory = (ServiceDiscoveryFactory) camelContext.getInjector().newInstance(type);
            } else {
                throw new IllegalArgumentException("Resolving ServiceDiscovery: " + factoryKey + " detected type conflict: Not a ServiceDiscoveryFactory 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 : ServiceDiscoveryFactory(org.apache.camel.cloud.ServiceDiscoveryFactory) HashMap(java.util.HashMap) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) ServiceDiscovery(org.apache.camel.cloud.ServiceDiscovery)

Aggregations

HashMap (java.util.HashMap)1 NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)1 ServiceDiscovery (org.apache.camel.cloud.ServiceDiscovery)1 ServiceDiscoveryFactory (org.apache.camel.cloud.ServiceDiscoveryFactory)1