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 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.runtime.osgi;
023
024import java.net.URL;
025
026import org.nuxeo.runtime.RuntimeService;
027import org.nuxeo.runtime.api.Framework;
028import org.nuxeo.runtime.model.impl.DefaultRuntimeContext;
029import org.osgi.framework.Bundle;
030import org.osgi.framework.Constants;
031
032/**
033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
034 */
035public class OSGiRuntimeContext extends DefaultRuntimeContext {
036
037    protected final Bundle bundle;
038
039    protected String hostBundleId;
040
041    protected Bundle hostBundle;
042
043    public OSGiRuntimeContext(Bundle bundle) {
044        this(Framework.getRuntime(), bundle);
045    }
046
047    public OSGiRuntimeContext(RuntimeService runtime, Bundle bundle) {
048        super(runtime);
049        this.bundle = bundle;
050        // workaround to correctly handle fragment class loaders
051        hostBundleId = (String) bundle.getHeaders().get(Constants.FRAGMENT_HOST);
052        if (hostBundleId != null) {
053            int p = hostBundleId.indexOf(';');
054            if (p > -1) { // remove version or other extra information if any
055                hostBundleId = hostBundleId.substring(0, p);
056            }
057        }
058    }
059
060    @Override
061    public Bundle getBundle() {
062        return bundle;
063    }
064
065    @Override
066    public URL getResource(String name) {
067        URL url = null;
068        if (hostBundleId != null) {
069            url = getHostBundle().getResource(name);
070        } else {
071            url = bundle.getResource(name);
072        }
073        if (url == null) {
074            url = Framework.getResourceLoader().getResource(name);
075        }
076        return url;
077    }
078
079    @Override
080    public URL getLocalResource(String name) {
081        URL url = null;
082        if (hostBundleId != null) {
083            url = getHostBundle().getEntry(name);
084        } else {
085            url = bundle.getEntry(name);
086        }
087        if (url == null) {
088            url = Framework.getResourceLoader().getResource(name);
089        }
090        return url;
091    }
092
093    @Override
094    public Class<?> loadClass(String className) throws ClassNotFoundException {
095        try {
096            if (hostBundleId != null) { // workaround to handle fragment bundles that doesn't have class loaders
097                return getHostBundle().loadClass(className);
098            }
099            return bundle.loadClass(className);
100        } catch (ClassNotFoundException e) {
101            return Framework.getResourceLoader().loadClass(className);
102        }
103    }
104
105    public Bundle getHostBundle() {
106        if (hostBundleId != null) {
107            if (hostBundle == null && runtime instanceof OSGiRuntimeService) {
108                hostBundle = ((OSGiRuntimeService) runtime).findHostBundle(bundle);
109            }
110        }
111        return hostBundle;
112    }
113
114}