Twitter github

Am I Headless?

Through my daily browsing of the Eclipse newsgroups, I saw a post about how do detect whether a bundle is running headless. I had to do something similar several months ago, but I figure I would post a solution for people whoever come across this issue in their bundle-development lives.

The gist of the solution is to detect whether the SWT bundle is installed and running. Here’s a basic snippet on how to do this:

Bundle b = Platform.getBundle("org.eclipse.swt");
if (b==null || b.getState() != Bundle.ACTIVE) {
System.out.println("sweet, headless mode!");
}

If you don’t care for the dependency on the Platform class and the bundle it comes from, you can do a “pure OSGi” solution of using PackageAdmin. I recommend using getBundles(…) from PackageAdmin to get access to the SWT bundle to do a similar check.

That’s it… I’m not sure if a more elegant solution exists.