/*******************************************************************************
* Copyright (c) 2008 Olivier Moises
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Olivier Moises- initial API and implementation
* Pavel Erofeev - locator for platform:/resource URIs
*******************************************************************************/
package org.eclipse.wazaabi.locator.platform.resource;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.service.packageadmin.PackageAdmin;
import org.osgi.util.tracker.ServiceTracker;
public class Activator implements BundleActivator {
// The shared instance
private static Activator activator;
private static BundleContext context;
private ServiceTracker pkgAdminTracker;
public static Activator getDefault() {
return activator;
}
static BundleContext getContext() {
return context;
}
/*
* (non-Javadoc)
*
* @see
* org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext
* )
*/
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
activator = this;
}
/*
* (non-Javadoc)
*
* @see
* org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext bundleContext) throws Exception {
Activator.context = null;
activator = null;
if (pkgAdminTracker != null) {
pkgAdminTracker.close();
pkgAdminTracker = null;
}
}
/**
* @return the PackageAdmin service from this bundle
*/
public PackageAdmin getBundleAdmin() {
if (pkgAdminTracker == null) {
if (context == null)
return null;
pkgAdminTracker = new ServiceTracker(context,
PackageAdmin.class.getName(), null);
pkgAdminTracker.open();
}
return (PackageAdmin) pkgAdminTracker.getService();
}
/**
* @param bundleName
* the bundle id
* @return A bundle if found, or <code>null</code>
*/
public Bundle getBundleForName(String bundleName) {
Bundle[] bundles = getBundleAdmin().getBundles(bundleName, null);
if (bundles == null)
return null;
// Return the first bundle that is not installed or uninstalled
for (int i = 0; i < bundles.length; i++) {
if ((bundles[i].getState() & (Bundle.INSTALLED | Bundle.UNINSTALLED)) == 0) {
return bundles[i];
}
}
return null;
}
}