Skip to content

Instantly share code, notes, and snippets.

@chbaranowski
Created October 26, 2011 11:21
Show Gist options
  • Save chbaranowski/1316073 to your computer and use it in GitHub Desktop.
Save chbaranowski/1316073 to your computer and use it in GitHub Desktop.
OSGi Bundle Context - Get Bundle Context from the ClassLoader via BundleReference Version via OSGi FrameworkUtil class
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceReference;
import org.osgi.service.packageadmin.PackageAdmin;
public class MyDemoClass
{
public PackageAdmin getService()
{
// get bundle instance via the OSGi Framework Util class
BundleContext ctx = FrameworkUtil.getBundle(MyDemoClass.class).getBundleContext();
ServiceReference serviceReference = ctx.getServiceReference(PackageAdmin.class.getName());
return PackageAdmin.class.cast(ctx.getService(serviceReference));
}
}
@SrinivasChannappa
Copy link

I am using hudson build , it throwns an error Compiler Error: cannot find symbol symbol : method getBundleContext() location: interface org.osgi.framework.Bundle. Please let me know how to fix it . I am usign the below in pom

org.osgi.core org.osgi.core 4.3.0 jar compile org.apache.felix org.apache.felix.framework 5.0.1 runtime

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment