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.core.storage.sql;
020
021import java.io.IOException;
022import java.io.InputStream;
023import java.net.URL;
024
025import org.osgi.framework.BundleActivator;
026import org.osgi.framework.BundleContext;
027
028/**
029 * Needed to lookup local bundle resources - which should use Bundle API.
030 *
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 */
033public class Activator implements BundleActivator {
034
035    private static volatile Activator instance;
036
037    private BundleContext context;
038
039    public static URL getResource(String path) {
040        Activator a = instance;
041        if (a != null) {
042            return a.context.getBundle().getResource(path);
043        }
044        return Thread.currentThread().getContextClassLoader().getResource(path);
045    }
046
047    public static InputStream getResourceAsStream(String path) throws IOException {
048        URL url = getResource(path);
049        return url != null ? url.openStream() : null;
050    }
051
052    public static Activator getInstance() {
053        return instance;
054    }
055
056    public BundleContext getContext() {
057        return context;
058    }
059
060    @Override
061    public void start(BundleContext context) {
062        this.context = context;
063        instance = this;
064    }
065
066    @Override
067    public void stop(BundleContext context) {
068        instance = null;
069        this.context = null;
070    }
071
072}