package at.bestsolution.efxclipse.runtime.workbench.internal;
import org.eclipse.osgi.service.datalocation.Location;
import org.eclipse.osgi.service.debug.DebugOptions;
import org.eclipse.osgi.service.debug.DebugTrace;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Filter;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.service.packageadmin.PackageAdmin;
import org.osgi.util.tracker.ServiceTracker;
public class WorkbenchJFXActivator implements BundleActivator {
public static final String PI_RENDERERS = "at.bestsolution.efxclipse.runtime.workbench"; //$NON-NLS-1$
private BundleContext context;
private ServiceTracker pkgAdminTracker;
private ServiceTracker locationTracker;
private static WorkbenchJFXActivator activator;
private ServiceTracker debugTracker;
private DebugTrace trace;
/**
* Get the default activator.
*
* @return a BundleActivator
*/
public static WorkbenchJFXActivator getDefault() {
return activator;
}
/**
* @return this bundles context
*/
public BundleContext getContext() {
return context;
}
public void start(BundleContext context) throws Exception {
activator = this;
this.context = context;
}
public void stop(BundleContext context) throws Exception {
if (pkgAdminTracker != null) {
pkgAdminTracker.close();
pkgAdminTracker = null;
}
}
public Bundle getBundle() {
if (context == null)
return null;
return context.getBundle();
}
/**
* @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();
}
/**
* @return the instance Location service
*/
public Location getInstanceLocation() {
if (locationTracker == null) {
Filter filter = null;
try {
filter = context.createFilter(Location.INSTANCE_FILTER);
} catch (InvalidSyntaxException e) {
// ignore this. It should never happen as we have tested the
// above format.
}
locationTracker = new ServiceTracker(context, filter, null);
locationTracker.open();
}
return (Location) locationTracker.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;
}
public static void trace(String option, String msg, Throwable error) {
final DebugOptions debugOptions = activator.getDebugOptions();
if (debugOptions.isDebugEnabled()
&& debugOptions.getBooleanOption(PI_RENDERERS + option, false)) {
System.out.println(msg);
if (error != null) {
error.printStackTrace(System.out);
}
}
activator.getTrace().trace(option, msg, error);
}
public DebugOptions getDebugOptions() {
if (debugTracker == null) {
if (context == null)
return null;
debugTracker = new ServiceTracker(context, DebugOptions.class
.getName(), null);
debugTracker.open();
}
return (DebugOptions) debugTracker.getService();
}
public DebugTrace getTrace() {
if (trace == null) {
trace = getDebugOptions().newDebugTrace(PI_RENDERERS);
}
return trace;
}
}