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