Yes, this is a hack that predates @Resource injection
/**
* A provider for the system bundle context.
*
* [TODO] should be provided as injectable resource
*
* @author thomas.diesler@jboss.com
* @since 18-Jul-2011
*/
However, if you have class that you know it comes from a bundle, you can always do
Bundle bundle = ((BundleReference) myClass.getClassLoader()).getBundle();
This is standard API.
Hi,
I have encounter the same problem, my jboss is jboss-as-7.1.1.Final, i have depends org.jboss.osgi.framework in my webapp,
the sample code is:
public class HelloWorldServlet extends HttpServlet {
private static final long serialVersionUID = 5423372534281029927L;
@Resource(name="BundleContext")
private BundleContext context;
private TimeService service;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String message = service.getTime();
PrintWriter out = resp.getWriter();
out.println(message);
out.close();
}
@Override
public void init(ServletConfig config) throws ServletException {
ServiceTracker tracker = new ServiceTracker(context, TimeService.class.getName(), null) {
@Override
public Object addingService(ServiceReference sref) {
System.out.println("Adding service");
service = (TimeService) super.addingService(sref);
return service;
}
@Override
public void removedService(ServiceReference sref, Object sinst) {
super.removedService(sref, service);
System.out.println("Removing service");
service = null;
}
};
tracker.open();
}
}
but i catch the exception:
java.lang.IllegalArgumentException: Can not set org.osgi.framework.BundleContext field com.fsj.spring.web.HelloWorldServlet.context to org.jboss.osgi.framework.internal.SystemBundleContext
Is anybody can help me to work out this problem?
Thanks