1 2 3 Previous Next 31 Replies Latest reply on Apr 24, 2013 4:43 AM by hongweichen Go to original post
      • 30. Re: How to get the BundleContext inside Web Application?

        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.

        • 31. Re: How to get the BundleContext inside Web Application?


              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;




              private BundleContext context;


                    private TimeService service;


                    protected void doGet(HttpServletRequest req, HttpServletResponse resp)

                                        throws ServletException, IOException {

                              String message = service.getTime();

                  PrintWriter out = resp.getWriter();






              public void init(ServletConfig config) throws ServletException {



                  ServiceTracker tracker = new ServiceTracker(context, TimeService.class.getName(), null) {




                      public Object addingService(ServiceReference sref) {

                          System.out.println("Adding service");

                          service = (TimeService) super.addingService(sref);

                          return service;





                      public void removedService(ServiceReference sref, Object sinst) {

                          super.removedService(sref, service);

                          System.out.println("Removing service");

                          service = null;






              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?



          1 2 3 Previous Next