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 */
041@Deprecated
042public class ServiceClassLoader extends ClassLoader {
043
044    public static ClassLoader getLoader() {
045        BundleContext ctx = Activator.getInstance().getContext();
046        String vendor = ctx.getProperty(Constants.FRAMEWORK_VENDOR);
047        if (vendor != null && vendor.contains("Nuxeo")) {
048            // support fake nuxeo osgi adapter
049            return Activator.class.getClassLoader();
050        } else {
051            ServiceClassLoader loader = new ServiceClassLoader(ctx.getBundle());
052            loader.addResourceLoader(RuntimeDelegateImpl.class.getClassLoader());
053            loader.addResourceLoader(UriBuilderImpl.class.getClassLoader());
054            return loader;
055        }
056    }
057
058    protected Bundle bundle;
059
060    protected List<ClassLoader> loaders;
061
062    public ServiceClassLoader(Bundle bundle) {
063        this.bundle = bundle;
064        this.loaders = new ArrayList<>();
065    }
066
067    public void addResourceLoader(ClassLoader cl) {
068        loaders.add(cl);
069    }
070
071    @Override
072    protected URL findResource(String name) {
073        URL url = null;
074        for (ClassLoader cl : loaders) {
075            url = cl.getResource(name);
076            if (url != null) {
077                break;
078            }
079        }
080        return url;
081    }
082
083    @Override
084    protected Enumeration<URL> findResources(String name) throws IOException {
085        ArrayList<Enumeration<URL>> enums = new ArrayList<>();
086        for (ClassLoader cl : loaders) {
087            enums.add(cl.getResources(name));
088        }
089        return new CompoundEnumeration<>(enums.toArray(new Enumeration[enums.size()]));
090    }
091
092    @Override
093    protected synchronized Class<?> loadClass(String name, boolean resolve) throws ClassNotFoundException {
094        return bundle.loadClass(name);
095    }
096}