private ClassLoader getClassLoader()

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


    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()))) {
                    String currentLine;
                    while( (currentLine = in.readLine()) != null) {
                        if (ObjectHelper.equal(providerName, currentLine)) {
                            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;
    }