Search in sources :

Example 1 with ServiceChooserFactory

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

the class ServiceCallServiceChooserConfiguration method newInstance.

// *************************************************************************
// Factory
// *************************************************************************
@Override
public ServiceChooser newInstance(CamelContext camelContext) throws Exception {
    ObjectHelper.notNull(factoryKey, "ServiceChooser factoryKey");
    ServiceChooser answer;
    // First try to find the factory from the registry.
    ServiceChooserFactory factory = CamelContextHelper.lookup(camelContext, factoryKey, ServiceChooserFactory.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 (ServiceChooserFactory.class.isAssignableFrom(type)) {
                factory = (ServiceChooserFactory) camelContext.getInjector().newInstance(type);
            } else {
                throw new NoFactoryAvailableException("Resolving ServiceChooser: " + factoryKey + " detected type conflict: Not a ServiceChooserFactory implementation. Found: " + type.getName());
            }
        }
        try {
            Map<String, Object> parameters = new HashMap<>();
            IntrospectionSupport.getProperties(this, parameters, null, false);
            parameters.put("properties", getPropertiesAsMap(camelContext));
            IntrospectionSupport.setProperties(factory, parameters);
            answer = factory.newInstance(camelContext);
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }
    return answer;
}
Also used : ServiceChooser(org.apache.camel.cloud.ServiceChooser) HashMap(java.util.HashMap) ServiceChooserFactory(org.apache.camel.cloud.ServiceChooserFactory) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException) NoFactoryAvailableException(org.apache.camel.NoFactoryAvailableException)

Aggregations

HashMap (java.util.HashMap)1 NoFactoryAvailableException (org.apache.camel.NoFactoryAvailableException)1 ServiceChooser (org.apache.camel.cloud.ServiceChooser)1 ServiceChooserFactory (org.apache.camel.cloud.ServiceChooserFactory)1