001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 */
012package org.nuxeo.ecm.webengine.jaxrs.servlet;
013
014import java.io.IOException;
015import java.net.URL;
016import java.util.ArrayList;
017import java.util.Enumeration;
018import java.util.List;
019
020import org.nuxeo.ecm.webengine.jaxrs.Activator;
021import org.osgi.framework.Bundle;
022import org.osgi.framework.BundleContext;
023import org.osgi.framework.Constants;
024
025import com.sun.jersey.api.uri.UriBuilderImpl;
026import com.sun.jersey.server.impl.provider.RuntimeDelegateImpl;
027
028/**
029 * Support for jersey ServiceFinder lookups in an OSGi environment.
030 *
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 * @deprecated This class is deprecated since jersey 1.11 which fixed OSGi class loader problems.
033 */
034public class ServiceClassLoader extends ClassLoader {
035
036    public static ClassLoader getLoader() {
037        BundleContext ctx = Activator.getInstance().getContext();
038        String vendor = ctx.getProperty(Constants.FRAMEWORK_VENDOR);
039        if (vendor != null && vendor.contains("Nuxeo")) {
040            // support fake nuxeo osgi adapter
041            return Activator.class.getClassLoader();
042        } else {
043            ServiceClassLoader loader = new ServiceClassLoader(ctx.getBundle());
044            loader.addResourceLoader(RuntimeDelegateImpl.class.getClassLoader());
045            loader.addResourceLoader(UriBuilderImpl.class.getClassLoader());
046            return loader;
047        }
048    }
049
050    protected Bundle bundle;
051
052    protected List<ClassLoader> loaders;
053
054    public ServiceClassLoader(Bundle bundle) {
055        this.bundle = bundle;
056        this.loaders = new ArrayList<ClassLoader>();
057    }
058
059    public void addResourceLoader(ClassLoader cl) {
060        loaders.add(cl);
061    }
062
063    @Override
064    protected URL findResource(String name) {
065        URL url = null;
066        for (ClassLoader cl : loaders) {
067            url = cl.getResource(name);
068            if (url != null) {
069                break;
070            }
071        }
072        return url;
073    }
074
075    @Override
076    protected Enumeration<URL> findResources(String name) throws IOException {
077        ArrayList<Enumeration<URL>> enums = new ArrayList<Enumeration<URL>>();
078        for (ClassLoader cl : loaders) {
079            enums.add(cl.getResources(name));
080        }
081        return new CompoundEnumeration<URL>(enums.toArray(new Enumeration[enums.size()]));
082    }
083
084    @Override
085    protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
086        return bundle.loadClass(name);
087    }
088}