001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bstefanescu
018 */
019package org.nuxeo.ecm.webengine.jaxrs.servlet;
020
021import java.io.IOException;
022import java.net.URL;
023import java.util.ArrayList;
024import java.util.Enumeration;
025import java.util.List;
026
027import org.nuxeo.ecm.webengine.jaxrs.Activator;
028import org.osgi.framework.Bundle;
029import org.osgi.framework.BundleContext;
030import org.osgi.framework.Constants;
031
032import com.sun.jersey.api.uri.UriBuilderImpl;
033import com.sun.jersey.server.impl.provider.RuntimeDelegateImpl;
034
035/**
036 * Support for jersey ServiceFinder lookups in an OSGi environment.
037 *
038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
039 * @deprecated This class is deprecated since jersey 1.11 which fixed OSGi class loader problems.
040 */
041public class ServiceClassLoader extends ClassLoader {
042
043    public static ClassLoader getLoader() {
044        BundleContext ctx = Activator.getInstance().getContext();
045        String vendor = ctx.getProperty(Constants.FRAMEWORK_VENDOR);
046        if (vendor != null && vendor.contains("Nuxeo")) {
047            // support fake nuxeo osgi adapter
048            return Activator.class.getClassLoader();
049        } else {
050            ServiceClassLoader loader = new ServiceClassLoader(ctx.getBundle());
051            loader.addResourceLoader(RuntimeDelegateImpl.class.getClassLoader());
052            loader.addResourceLoader(UriBuilderImpl.class.getClassLoader());
053            return loader;
054        }
055    }
056
057    protected Bundle bundle;
058
059    protected List<ClassLoader> loaders;
060
061    public ServiceClassLoader(Bundle bundle) {
062        this.bundle = bundle;
063        this.loaders = new ArrayList<ClassLoader>();
064    }
065
066    public void addResourceLoader(ClassLoader cl) {
067        loaders.add(cl);
068    }
069
070    @Override
071    protected URL findResource(String name) {
072        URL url = null;
073        for (ClassLoader cl : loaders) {
074            url = cl.getResource(name);
075            if (url != null) {
076                break;
077            }
078        }
079        return url;
080    }
081
082    @Override
083    protected Enumeration<URL> findResources(String name) throws IOException {
084        ArrayList<Enumeration<URL>> enums = new ArrayList<Enumeration<URL>>();
085        for (ClassLoader cl : loaders) {
086            enums.add(cl.getResources(name));
087        }
088        return new CompoundEnumeration<URL>(enums.toArray(new Enumeration[enums.size()]));
089    }
090
091    @Override
092    protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
093        return bundle.loadClass(name);
094    }
095}