1 2 3 Previous Next 31 Replies Latest reply: Apr 24, 2013 4:43 AM by hongwei chen RSS

    How to get the BundleContext inside Web Application?

    Nikhil Joshi Newbie

      Hi,

       

      I wan to access a OSGI Bundle from a Web Application (a resteasy service).

      For this I tried the approch mentioned by Thomas Diesler in his webnair - Invoking OSGI service from a web app.

       

      I used annotation 

       

       

      @Resource

      private BundleContext context;

       

       

       

       

      I am just wondering how webApp gets the instace of the BundleContext, when WAR is deployed in JEE container?

      when I deploy and run the WAR on AS 7 server, I get context as NULL.

       

      Does it require any other configuration at Application or server level ?

       

      Thanks,

      Nikhil

        • 2. Re: How to get the BundleContext inside Web Application?
          Nikhil Joshi Newbie

          Hi Thomas, Thanks for reply.

           

          I am using 7.0.1.Final version.  Is there any way to access the OSGI bundle from Web App with 7.0.1 ?

           

          Also I could not find the download for 7.1.0 Alpha at http://www.jboss.org/jbossas/downloads/

          From where I can download that ?

          • 3. Re: How to get the BundleContext inside Web Application?
            William Bendrot Newbie

            Hi Thomas. Is anyone reporting problems compiling 7.1 Alpha 1? First error comes up during Arquillian build and everything tanks from there.

            • 4. Re: How to get the BundleContext inside Web Application?
              Thomas Diesler Master

              Works for me, for Jenkins, and everybody else AFAIK.

              • 5. Re: How to get the BundleContext inside Web Application?
                Thomas Diesler Master

                You can build it from here https://github.com/jbossas/jboss-as/tree/7.1.0.Alpha1

                 

                Or click the large Download button on https://github.com/jbossas/jboss-as

                Is there any way to access the OSGI bundle from Web App with 7.0.1

                 

                Have a look at the BundleContextProvider. It relies on a feature that a ClassLoader that is associated with a Bundle must implement BundleReference.

                • 6. Re: How to get the BundleContext inside Web Application?
                  Nikhil Joshi Newbie

                  Hi Thomas,

                   

                  I am able to build from the 7.1.0.Alpha1 source code. Thanks for sharing it.

                   

                  However while running code I get following error.

                   

                  Allocate exception for servlet SimpleClientServlet: java.lang.IllegalArgumentException: Can not set org.osgi.framework.BundleContext field sample.SimpleClientServlet.context to org.jboss.osgi.framework.internal.SystemBundleContext

                   

                  Do you have any clue about this error ??

                   

                  - Nikhil

                  • 7. Re: How to get the BundleContext inside Web Application?
                    Thomas Diesler Master

                    Could you please show us the full stack trace?

                    • 8. Re: How to get the BundleContext inside Web Application?
                      Nikhil Joshi Newbie

                      Here is the stack trace,

                       

                      12:16:04,070 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/MyRestService].[SimpleClientServlet]] (http--127.0.0.1-8080-1) Allocate exception for servlet SimpleClientServlet: java.lang.IllegalArgumentException: Can not set org.osgi.framework.BundleContext field sample.SimpleClientServlet.context to org.jboss.osgi.framework.internal.SystemBundleContext

                          at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source) [:1.6.0_25]

                          at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source) [:1.6.0_25]

                          at sun.reflect.UnsafeObjectFieldAccessorImpl.set(Unknown Source) [:1.6.0_25]

                          at java.lang.reflect.Field.set(Unknown Source) [:1.6.0_25]

                          at org.jboss.as.ee.component.ManagedReferenceFieldInjectionInterceptor.processInvocation(ManagedReferenceFieldInjectionInterceptor.java:64)

                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]

                          at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]

                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]

                          at org.jboss.as.ee.component.ManagedReferenceInterceptor.processInvocation(ManagedReferenceInterceptor.java:53)

                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]

                          at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]

                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]

                          at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45)

                          at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:287) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]

                          at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61) [jboss-invocation-1.1.0.Final.jar:1.1.0.Final]

                          at org.jboss.as.ee.component.BasicComponent.constructComponentInstance(BasicComponent.java:152)

                          at org.jboss.as.ee.component.BasicComponent.createInstance(BasicComponent.java:77)

                          at org.jboss.as.web.deployment.component.WebComponentInstantiator$1.<init>(WebComponentInstantiator.java:57) [jboss-as-web-7.1.0.Alpha1.jar:7.1.0.Alpha1]

                          at org.jboss.as.web.deployment.component.WebComponentInstantiator.getReference(WebComponentInstantiator.java:55) [jboss-as-web-7.1.0.Alpha1.jar:7.1.0.Alpha1]

                          at org.jboss.as.web.deployment.WebInjectionContainer.instantiate(WebInjectionContainer.java:99) [jboss-as-web-7.1.0.Alpha1.jar:7.1.0.Alpha1]

                          at org.jboss.as.web.deployment.WebInjectionContainer.newInstance(WebInjectionContainer.java:78) [jboss-as-web-7.1.0.Alpha1.jar:7.1.0.Alpha1]

                          at org.jboss.as.web.deployment.WebInjectionContainer.newInstance(WebInjectionContainer.java:72) [jboss-as-web-7.1.0.Alpha1.jar:7.1.0.Alpha1]

                          at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1156) [jbossweb-7.0.1.Final.jar:7.1.0.Alpha1]

                          at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:952) [jbossweb-7.0.1.Final.jar:7.1.0.Alpha1]

                          at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:188) [jbossweb-7.0.1.Final.jar:7.1.0.Alpha1]

                          at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.1.Final.jar:7.1.0.Alpha1]

                          at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:139) [jboss-as-web-7.1.0.Alpha1.jar:7.1.0.Alpha1]

                          at org.jboss.as.web.NamingValve.invoke(NamingValve.java:57) [jboss-as-web-7.1.0.Alpha1.jar:7.1.0.Alpha1]

                          at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:154) [jbossweb-7.0.1.Final.jar:7.1.0.Alpha1]

                          at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.1.Final.jar:7.1.0.Alpha1]

                          at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.1.Final.jar:7.1.0.Alpha1]

                          at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362) [jbossweb-7.0.1.Final.jar:7.1.0.Alpha1]

                          at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.1.Final.jar:7.1.0.Alpha1]

                          at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:667) [jbossweb-7.0.1.Final.jar:7.1.0.Alpha1]

                          at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:952) [jbossweb-7.0.1.Final.jar:7.1.0.Alpha1]

                          at java.lang.Thread.run(Unknown Source) [:1.6.0_25]

                      • 9. Re: How to get the BundleContext inside Web Application?
                        Thomas Diesler Master

                        I was hoping to see why it cannot set org.osgi.framework.BundleContext field sample.SimpleClientServlet.context to org.jboss.osgi.framework.internal.SystemBundleContext, but that is not obvious.

                         

                        You could now create a jira issue and attach some code that allows us to reproduce what you are seeing. How is your deployment different from what we use in the test suite?

                        • 10. Re: How to get the BundleContext inside Web Application?
                          Nikhil Joshi Newbie

                          Hi Thomas,

                           

                          I have created a JIRA for this https://issues.jboss.org/browse/AS7-1974

                           

                          For deployment, I have configured the server at JBOSS developer studio M5 and running my app using option "Run on server".

                          Or importing WAR to server's deployment folder.

                           

                          Thx,

                          Nikhil

                          • 11. Re: How to get the BundleContext inside Web Application?
                            diego loro Newbie

                            Hi all,

                             

                            I'm using jboss-as-7.1.0.Beta1b but I'm not able to get BundleContext from my ManagedBean inside a Webapp (the Webapp is not a bundle and it contains also JEE components, like EJB3.0).

                            The two suggested ways fail:

                             

                            - "@Resource" >>

                            java.lang.IllegalArgumentException: Can not set org.osgi.framework.BundleContext field managed.ManagedBean.bundleContext to org.jboss.osgi.framework.internal.SystemBundleContext

                            sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146)

                            sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150)

                            sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:63)

                            java.lang.reflect.Field.set(Field.java:657)

                            org.jboss.as.ee.component.ManagedReferenceFieldInjectionInterceptorFactory$ManagedReferenceFieldInjectionInterceptor.processInvocation(ManagedReferenceFieldInjectionInterceptorFactory.java:111)

                             

                            https://issues.jboss.org/browse/AS7-1974 workaround >>

                            org.jboss.modules.ModuleClassLoader cannot be cast to org.osgi.framework.BundleReference

                            How to reference Jboss Osgi library instead of include osgi libs inside my Webapp as Thomas said in is comment?

                            Is this bug really closed? https://issues.jboss.org/browse/AS7-2021

                             


                            Is it possible to call bundleContext or services with JNDI lookup from a webapp (servlet, ws, managedBean...)?

                            So I would like to know how to access Osgi Bundle Context from JEE App and Web App in Jboss 7.1.

                            Any ideas?

                            Thanks a lot,

                            Diego

                            • 12. Re: How to get the BundleContext inside Web Application?
                              diego loro Newbie

                              Any news about this thread?
                              Does anyone know if and how you can access the BundleContext from Enterprise Application (from servlets, web services, ejb ...)?
                              To me it still does not work using @ Resource annotation.

                              Thank you,
                              Diego

                              • 13. Re: How to get the BundleContext inside Web Application?
                                Rastko Soskic Newbie

                                Hi, this is only post I've found that tackles this matter.

                                 

                                To avoid adding new thread for mostly same topic I'll add here.


                                I am using JBoss 7.1.0-Final and I've tried to inject BundleContext into managed bean for JSF app.

                                 

                                I've used same principle as above:


                                @Resource

                                private BundleContext context;


                                However, I'm getting class not found exception for BundleContext...
                                My project (maven) does reference osgi core but with "provided" scope as I supposed osgi classes are already available with osgi container.


                                Perhaps I am doing wrong and my war should contain osgi core jar?

                                 

                                Due to that, I am not sure BundleContext even can be injected into managed bean (maybe it works only with ejbs)?
                                Maybe my whole concept is wrong?

                                 


                                Edit:

                                after playing a bit with app, it seems that I am having the same problem with IllegalArgumentException and UnsafeFieldAccessorImpl...

                                 

                                I suppose diego has walked the same way and came up with including osgi jar into web app...

                                 

                                So it seems that I should simply ask the same question. Is there actually some way to inject BundleContext of jboss into, in my case,
                                managed bean...

                                 

                                Any help/suggestion/tip is welcome and appreciated.

                                 

                                Thank you very much,

                                • 14. Re: How to get the BundleContext inside Web Application?
                                  Rastko Soskic Newbie

                                  I've turned to workaround Thomas proposed with BundleContextProvider...

                                   

                                  I am using managed bean in web app, and I've set it up in same way as here: https://github.com/tdiesler/jbosgi/blob/7bc79a65d3eb3549c76a5bbcd3c506689394ef7d/testsuite/jbossas/webapp/pom.xml

                                  I've deployed BundleContextProvider as bundle to jboss successfully, and for web app I've set bundle with BundleContextProvider scope to provided.

                                   

                                  However, again in managed bean I'm getting ClassNotFound exception for BundleContextProvider... so.. am I missing something? Is there something additional what should be set?

                                   

                                  Thanks

                                  1 2 3 Previous Next