private ClassLoader getClassLoader()

in components/camel-jcache-osgi/src/main/java/org/apache/camel/component/jcache/osgi/OSGiCacheManager.java [63:111]


    private ClassLoader getClassLoader(String providerName) throws Exception {
        if (providerName == null || !getConfiguration().isLookupProviders()) {
            return null;
        }

        final BundleContext bc = FrameworkUtil.getBundle(JCacheHelper.class).getBundleContext();
        final ClassLoader bcl = bc.getBundle().adapt(BundleWiring.class).getClassLoader();
        final ClassLoader acl = getConfiguration().getApplicationContextClassLoader();

        for (final Bundle bundle : bc.getBundles()) {
            URL spi = bundle.getResource("META-INF/services/javax.cache.spi.CachingProvider");
            if (spi != null) {
                try (BufferedReader in = new BufferedReader(new InputStreamReader(spi.openStream()))) {
                    if (ObjectHelper.equal(providerName, in.readLine())) {
                        return new ClassLoader(bcl) {
                            @Override
                            protected Class<?> findClass(String name) throws ClassNotFoundException {
                                try {
                                    return acl.loadClass(name);
                                } catch (ClassNotFoundException e) {
                                    return bundle.loadClass(name);
                                }
                            }

                            @Override
                            protected URL findResource(String name) {
                                URL resource = acl.getResource(name);
                                if (resource == null) {
                                    resource = bundle.getResource(name);
                                }
                                return resource;
                            }

                            @Override
                            protected Enumeration findResources(String name) throws IOException {
                                try {
                                    return acl.getResources(name);
                                } catch (IOException e) {
                                    return bundle.getResources(name);
                                }
                            }
                        };
                    }
                }
            }
        }

        return null;
    }