glassfish
  1. glassfish
  2. GLASSFISH-19388

ClassCastException when using @OSGiService in a enterprise web application

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Critical Critical
    • Resolution: Invalid
    • Affects Version/s: 3.1.2.2
    • Fix Version/s: None
    • Component/s: OSGi-JavaEE
    • Labels:
      None
    • Environment:

      Ubuntu 12.04 LTS 64bit

      Description

      TestServlet.java
      @WebServlet(name = "TestUI", urlPatterns = "/")
      public class TestServlet extends HttpServlet {
      	@Inject
      	@OSGiService(dynamic = true)
      	private SampleOSGiService sampleOSGiService;
      
      	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      		PrintStream writer = new PrintStream(response.getOutputStream());
                      writer.println(sampleOSGiService.sayHello());
              }
      }
      

      The SampleOSGiService is exported by a plain-old osgi-bundle. I have attached a sample project. Below is the stack trace I get from the server logs.

      java.lang.ClassCastException
      	at java.lang.Class.cast(Class.java:2990)
      	at org.glassfish.osgicdi.impl.OSGiServiceFactory.getBundleContext(OSGiServiceFactory.java:191)
      	at org.glassfish.osgicdi.impl.OSGiServiceFactory.lookupService(OSGiServiceFactory.java:127)
      	at org.glassfish.osgicdi.impl.OSGiServiceFactory.access$100(OSGiServiceFactory.java:72)
      	at org.glassfish.osgicdi.impl.OSGiServiceFactory$DynamicInvocationHandler.invoke(OSGiServiceFactory.java:232)
      	at $Proxy230.sayHello(Unknown Source)
      	at com.ifp.vault.web.TestServlet.doGet(TestServlet.java:59)
      	at javax.servlet.http.HttpServlet.service(HttpServlet.java:668)
      	at javax.servlet.http.HttpServlet.service(HttpServlet.java:770)
      	at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1550)
      	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
      	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
      	at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
      	at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
      	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:161)
      	at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:331)
      	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:231)
      	at com.sun.enterprise.v3.services.impl.ContainerMapper$AdapterCallable.call(ContainerMapper.java:317)
      	at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:195)
      	at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:860)
      	at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:757)
      	at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1056)
      	at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:229)
      	at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
      	at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
      	at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
      	at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
      	at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
      	at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
      	at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
      	at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
      	at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
      	at java.lang.Thread.run(Thread.java:662)
      
      

        Activity

        Hide
        Ranie Jade Ramiso added a comment -

        Sorry about that, I was supposed to remove that. A correct sample would be really helpful. Thanks

        Show
        Ranie Jade Ramiso added a comment - Sorry about that, I was supposed to remove that. A correct sample would be really helpful. Thanks
        Hide
        Ranie Jade Ramiso added a comment -

        Solved my problem, I was missing an import to "javax.inject".

        Show
        Ranie Jade Ramiso added a comment - Solved my problem, I was missing an import to "javax.inject".
        Hide
        Sanjeeb Sahoo added a comment -

        Tang is correct. You need to deploy the WAR as an OSGi bundle to make it a WAB. Thanks Tang for debugging.

        I can't deploy your sample, because it requires mysql drivers which I don't have in my env. I don't see anything particularly wrong in your app for you to get NPE. May be you are not deploying the bundles correctly.

        Show
        Sanjeeb Sahoo added a comment - Tang is correct. You need to deploy the WAR as an OSGi bundle to make it a WAB. Thanks Tang for debugging. I can't deploy your sample, because it requires mysql drivers which I don't have in my env. I don't see anything particularly wrong in your app for you to get NPE. May be you are not deploying the bundles correctly.
        Hide
        Ranie Jade Ramiso added a comment -

        @Sanjeeb NPE was caused because CDI did not work. So the instance is never injected. I just need to add "javax.inject" in the osgi import-package header.

        Show
        Ranie Jade Ramiso added a comment - @Sanjeeb NPE was caused because CDI did not work. So the instance is never injected. I just need to add "javax.inject" in the osgi import-package header.
        Hide
        Sanjeeb Sahoo added a comment -

        Yes, I also hand edited the bundles to not use JavaDB and had to fix the imports in the WAB.

        Show
        Sanjeeb Sahoo added a comment - Yes, I also hand edited the bundles to not use JavaDB and had to fix the imports in the WAB.

          People

          • Assignee:
            Sanjeeb Sahoo
            Reporter:
            Ranie Jade Ramiso
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: