Search in sources :

Example 1 with TypeConverterLoader

use of org.apache.camel.spi.TypeConverterLoader in project camel by apache.

the class OsgiTypeConverter method createRegistry.

protected DefaultTypeConverter createRegistry() {
    // base the osgi type converter on the default type converter
    DefaultTypeConverter answer = new DefaultTypeConverter(new DefaultPackageScanClassResolver() {

        @Override
        public Set<ClassLoader> getClassLoaders() {
            // we don't need any classloaders as we use OSGi service tracker instead
            return Collections.emptySet();
        }
    }, injector, factoryFinder);
    // inject CamelContext
    answer.setCamelContext(camelContext);
    try {
        // only load the core type converters, as OSGi activator will keep track on bundles
        // being installed/uninstalled and load type converters as part of that process
        answer.loadCoreTypeConverters();
    } catch (Exception e) {
        throw new RuntimeCamelException("Error loading CoreTypeConverter due: " + e.getMessage(), e);
    }
    // Load the type converters the tracker has been tracking
    // Here we need to use the ServiceReference to check the ranking
    ServiceReference<TypeConverterLoader>[] serviceReferences = this.tracker.getServiceReferences();
    if (serviceReferences != null) {
        ArrayList<ServiceReference<TypeConverterLoader>> servicesList = new ArrayList<ServiceReference<TypeConverterLoader>>(Arrays.asList(serviceReferences));
        // Just make sure we install the high ranking fallback converter at last
        Collections.sort(servicesList);
        for (ServiceReference<TypeConverterLoader> sr : servicesList) {
            try {
                LOG.debug("loading type converter from bundle: {}", sr.getBundle().getSymbolicName());
                ((TypeConverterLoader) this.tracker.getService(sr)).load(answer);
            } catch (Throwable t) {
                throw new RuntimeCamelException("Error loading type converters from service: " + sr + " due: " + t.getMessage(), t);
            }
        }
    }
    LOG.trace("Created TypeConverter: {}", answer);
    return answer;
}
Also used : DefaultTypeConverter(org.apache.camel.impl.converter.DefaultTypeConverter) Set(java.util.Set) ArrayList(java.util.ArrayList) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) ServiceReference(org.osgi.framework.ServiceReference) TypeConverterLoader(org.apache.camel.spi.TypeConverterLoader) DefaultPackageScanClassResolver(org.apache.camel.impl.DefaultPackageScanClassResolver) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Example 2 with TypeConverterLoader

use of org.apache.camel.spi.TypeConverterLoader in project camel by apache.

the class OsgiTypeConverter method addingService.

public Object addingService(ServiceReference<TypeConverterLoader> serviceReference) {
    LOG.trace("AddingService: {}, Bundle: {}", serviceReference, serviceReference.getBundle());
    TypeConverterLoader loader = bundleContext.getService(serviceReference);
    try {
        LOG.debug("loading type converter from bundle: {}", serviceReference.getBundle().getSymbolicName());
        if (delegate != null) {
            loader.load(delegate);
        }
    } catch (Throwable t) {
        throw new RuntimeCamelException("Error loading type converters from service: " + serviceReference + " due: " + t.getMessage(), t);
    }
    return loader;
}
Also used : TypeConverterLoader(org.apache.camel.spi.TypeConverterLoader) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Aggregations

RuntimeCamelException (org.apache.camel.RuntimeCamelException)2 TypeConverterLoader (org.apache.camel.spi.TypeConverterLoader)2 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 NoTypeConversionAvailableException (org.apache.camel.NoTypeConversionAvailableException)1 DefaultPackageScanClassResolver (org.apache.camel.impl.DefaultPackageScanClassResolver)1 DefaultTypeConverter (org.apache.camel.impl.converter.DefaultTypeConverter)1 ServiceReference (org.osgi.framework.ServiceReference)1