2 Replies Latest reply on Apr 25, 2013 3:50 AM by hongweichen

    How to get the bundlecontext in servlet which is in webapp

    hongweichen

      Hi all,

        I want to get the bundlecontext in servlet which is in webapp,

         my code is:

       

          public class HelloWorldServlet extends HttpServlet {

       

       

                private static final long serialVersionUID = 5423372534281029927L;

       

                private TimeService service;

       

                protected void doGet(HttpServletRequest req, HttpServletResponse resp)

                                    throws ServletException, IOException {

                          Bundle bundle = ((BundleReference) TimeService.class.getClassLoader()).getBundle();

                          String message = service.getTime();

              PrintWriter out = resp.getWriter();

              out.println(message);

              out.close();

                }

       

                }

       

                but i catch the exception:

                16:51:33,223 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/Spring].[test]] (http--192.168.6.80-8080-1) Servlet.service() for servlet test threw exception: java.lang.ClassCastException: org.jboss.modules.ModuleClassLoader cannot be cast to org.osgi.framework.BundleReference

                at com.fsj.spring.web.HelloWorldServlet.doGet(HelloWorldServlet.java:30) [classes:]

                at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]

                at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]

                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]

                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]

                at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) [org.springframework.web-3.0.5.RELEASE.jar:3.0.5.RELEASE]

                at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) [org.springframework.web-3.0.5.RELEASE.jar:3.0.5.RELEASE]

                at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.13.Final.jar:]

                at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]

                at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]

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

                at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]

                at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]

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

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

                at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]

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

                at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]

                at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]

                at java.lang.Thread.run(Thread.java:662) [rt.jar:1.6.0_35]

       

       

                TimeService is the interface of a bundle, and the webapp is the non-bundle app, my jboss is jboss-as-7.1.1.Final.

       

                is anybody can help me to workout this problem?

       

                Thanks

        • 1. Re: How to get the bundlecontext in servlet which is in webapp
          thomas.diesler

          This would be the case if TimeService was not part of or wired to a bundle deployment

          • 2. Re: How to get the bundlecontext in servlet which is in webapp
            hongweichen

            Hi Thomas,

                The above problem has fixed, after add the <module name="org.jboss.osgi.framework" export="true" /> in module dependencies.

                But i catch the another exception:

                      java.lang.ClassCastException: com.example.time.local.service.LocalTimeService cannot be cast to com.example.time.service.TimeService

                My code:

             

            public class HelloWorldServlet extends HttpServlet {

             

             

                      private static final long serialVersionUID = 5423372534281029927L;

             

             

                      @Resource(name="BundleContext")   

                private BundleContext context;

             

                      @SuppressWarnings({ "rawtypes", "unchecked" })

                      protected void doGet(HttpServletRequest req, HttpServletResponse resp)

                                          throws ServletException, IOException {

                                ServiceReference helloServiceReference = context.getServiceReference(TimeService.class.getName());

                                TimeService service = (TimeService)context.getService(helloServiceReference); 

             

                                String message = service.getTime();

                    PrintWriter out = resp.getWriter();

                    out.println(message);

                    out.close();

                      }

            }

             

            Note: The LocalTimeService is implements TimeService.

             

                I think this maybe because of the classloader is different, I have tried add the <module name="org.jboss.osgi.framework" export="true" /> in module.xml and add the <module name="org.jboss.osgi.framework" export="true" /> in jboss-deployment-structure.xml, these two methods dont solve this issue.

             

                Can u help me to work out this problem,

             

                 Thanks.