7 Replies Latest reply on Nov 28, 2010 9:53 AM by jp00

    Injecting bean into servlet

    misqu23.misqu23.gmail.com

      Hi


      In our project as a view presentation technology we use gwt, but on te server side we use ejb beans and servlets. Till now we have been using havily guice as dependency injection framework, but java ee6 has great new features such as cdi, bean validation and jpa 2 (criteria api) so we decided to switch to java ee6 and abandon foreign frameworks such as guice.


      Weld documentation says that the only think we need to change in our ear we need to add empty beans.ml files into appropriate places. I doesn't worked (lots of exceptions) with our source ear.


      So I decided to check the weld step by step (we are using jboss 6.0.0 M1) :
      First I took examples from weld distribution, they are working perfectly.
      Then I wrote simple world servlet with injection :


      The servlet code :



      public class TestCDI extends HttpServlet {
      
           private static final long serialVersionUID = -1638107271201388827L;
      
           @Inject
           private HiThere hi;
      
           @Override
           protected void doGet(HttpServletRequest req, HttpServletResponse resp)
                     throws ServletException, IOException {
              resp.setContentType("text/html");
              PrintWriter out = resp.getWriter();
      
              out.println("<html>");
              out.println("<head>");
              out.println("<title>Hola</title>");
              out.println("</head>");
              out.println("<body bgcolor=\"white\">");
      
              out.println("<h1>"+hi.getMessage()+"</h1>");
      
              out.println("</body>");
              out.println("</html>");     }
      }
      
      



      HiThere bean looks like this :



      public class HiThere {
      
           public String getMessage() {
                return "Hellol";
           }
      }
      
      



      Pretty simple. When I deploy this as war (exploded or not) with empty beans.xml and following web.xml :



      <?xml version="1.0" encoding="UTF-8"?>
      <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           id="smartoffice" version="2.5">
           <display-name>smartoffice</display-name>
           <welcome-file-list>
                <welcome-file>index.html</welcome-file>
           </welcome-file-list>
      
           <session-config>
                <session-timeout>30</session-timeout>
           </session-config>
      
           <servlet>
                <servlet-name>testCDI</servlet-name>
                <servlet-class>pl.scentia.smartoffice.server.handler.TestCDI</servlet-class>
           </servlet>
           
           <servlet-mapping>
                <servlet-name>testCDI</servlet-name>
                <url-pattern>/testcdi</url-pattern>
           </servlet-mapping>
      </web-app>
      


      The injection works perfectly, but when I put this war into the very basic ear application (which has only this ear packaged) with the following application.xml :



      <?xml version="1.0" encoding="UTF-8"?>
      <application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:application="http://java.sun.com/xml/ns/javaee/application_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd" id="SmartOffice" version="5">
        <display-name>pl.scentia.smartoffice.ear</display-name>
        <module>
          <web>
            <web-uri>gui.war</web-uri>
            <context-root>/smartoffice</context-root>
          </web>
        </module>
      </application>
      



      The application is deployed properly (without any error) but when I'm invoking the servlet I get following null pointer exception


      15:04:12,478 ERROR [[testCDI]] Servlet.service() for servlet testCDI threw exception
      java.lang.NullPointerException
           at pl.scentia.smartoffice.server.handler.TestCDI.doGet(TestCDI.java:46)
           at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
           at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:68)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
           at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
           at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
           at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
           at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
           at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:183)
           at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:95)
           at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
           at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
           at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
           at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
           at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
           at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
           at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
           at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
           at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
           at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
           at java.lang.Thread.run(Thread.java:636)
      
      


      Here is the full listing of the content of the ear :




      Archive:  smartoffice.ear
      Zip file size: 3420 bytes, number of entries: 4
      META-INF/
      META-INF/application.xml
      gui.war
      META-INF/MANIFEST.MF
      



      and listing of gui.war :



      Archive:  gui.war
      Zip file size: 4546 bytes, number of entries: 15
      META-INF/
      META-INF/MANIFEST.MF
      WEB-INF/
      WEB-INF/beans.xml
      WEB-INF/classes/
      WEB-INF/classes/pl/
      WEB-INF/classes/pl/scentia/
      WEB-INF/classes/pl/scentia/smartoffice/
      WEB-INF/classes/pl/scentia/smartoffice/server/
      WEB-INF/classes/pl/scentia/smartoffice/server/handler/
      WEB-INF/classes/pl/scentia/smartoffice/server/handler/HiThere.class
      WEB-INF/classes/pl/scentia/smartoffice/server/handler/TestCDI.class
      WEB-INF/lib/
      WEB-INF/web.xml
      index.html
      


      I don't know what am I doing wrong, during deployment everything looks ok, but at the runtime injection doesn't work.


      I thought that I have to change the definition of the application.xml to 6 and web.xml to 3 (even if the examples doesn't declare), but after changing application.xml and web.xml to :


      <application xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:application="http://java.sun.com/xml/ns/javaee/application_6.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_6.xsd" id="SmartOffice" version="6">
      
      <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
           id="smartoffice" version="3.0">
      



      I get following exception during the deployment :


      15:48:23,410 WARN  [JBossEntityResolver] Trying to resolve systemId as a non-file URL: http://java.sun.com/xml/ns/javaee/application_6.xsd
      15:48:24,288 WARN  [JBossEntityResolver] Trying to resolve systemId as a non-file URL: http://java.sun.com/xml/ns/javaee
      15:48:25,350 WARN  [XsdBinder] 103:47 schema_reference.4: Failed to read schema document 'http://java.sun.com/xml/ns/javaee', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
      15:48:25,353 ERROR [ProfileDeployAction] Failed to add deployment: smartoffice.ear
      org.jboss.deployers.spi.DeploymentException: Exception determining structure: AbstractVFSDeployment(smartoffice.ear)
           at org.jboss.deployers.spi.DeploymentException.rethrowAsDeploymentException(DeploymentException.java:49)
           at org.jboss.deployers.structure.spi.helpers.AbstractStructuralDeployers.determineStructure(AbstractStructuralDeployers.java:85)
           at org.jboss.deployers.plugins.main.MainDeployerImpl.determineStructure(MainDeployerImpl.java:1001)
           at org.jboss.deployers.plugins.main.MainDeployerImpl.determineDeploymentContext(MainDeployerImpl.java:437)
           at org.jboss.deployers.plugins.main.MainDeployerImpl.addDeployment(MainDeployerImpl.java:387)
           at org.jboss.deployers.plugins.main.MainDeployerImpl.addDeployment(MainDeployerImpl.java:297)
           at org.jboss.system.server.profileservice.repository.MainDeployerAdapter.addDeployment(MainDeployerAdapter.java:86)
           at org.jboss.system.server.profileservice.repository.ProfileDeployAction.install(ProfileDeployAction.java:61)
           at org.jboss.system.server.profileservice.repository.AbstractProfileAction.install(AbstractProfileAction.java:53)
           at org.jboss.system.server.profileservice.repository.AbstractProfileService.install(AbstractProfileService.java:403)
           at org.jboss.dependency.plugins.AbstractControllerContext.install(AbstractControllerContext.java:348)
           at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:1633)
           at org.jboss.dependency.plugins.AbstractController.incrementState(AbstractController.java:935)
           at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:1083)
           at org.jboss.dependency.plugins.AbstractController.resolveContexts(AbstractController.java:985)
           at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:775)
           at org.jboss.dependency.plugins.AbstractController.install(AbstractController.java:540)
           at org.jboss.system.server.profileservice.repository.AbstractProfileService.registerProfile(AbstractProfileService.java:308)
           at org.jboss.system.server.profileservice.ProfileServiceBootstrap.start(ProfileServiceBootstrap.java:258)
           at org.jboss.system.server.profileservice.ProfileServiceBootstrap.start(ProfileServiceBootstrap.java:97)
           at org.jboss.bootstrap.impl.base.server.AbstractServer.startBootstraps(AbstractServer.java:860)
           at org.jboss.bootstrap.impl.base.server.AbstractServer$StartServerTask.run(AbstractServer.java:441)
           at java.lang.Thread.run(Thread.java:636)
      Caused by: java.lang.RuntimeException: Error determining structure: smartoffice.ear
           at org.jboss.deployment.EARStructure.determineStructure(EARStructure.java:307)
           at org.jboss.deployers.vfs.plugins.structure.StructureDeployerWrapper.determineStructure(StructureDeployerWrapper.java:73)
           at org.jboss.deployers.vfs.plugins.structure.VFSStructuralDeployersImpl.doDetermineStructure(VFSStructuralDeployersImpl.java:196)
           at org.jboss.deployers.vfs.plugins.structure.VFSStructuralDeployersImpl.determineStructure(VFSStructuralDeployersImpl.java:221)
           at org.jboss.deployers.structure.spi.helpers.AbstractStructuralDeployers.determineStructure(AbstractStructuralDeployers.java:77)
           ... 21 more
      Caused by: org.jboss.xb.binding.JBossXBException: Failed to parse source: Failed to parse schema for nsURI=http://java.sun.com/xml/ns/javaee, baseURI=null, schemaLocation=http://java.sun.com/xml/ns/javaee/application_6.xsd
           at org.jboss.xb.binding.parser.sax.SaxJBossXBParser.parse(SaxJBossXBParser.java:203)
           at org.jboss.xb.binding.parser.sax.SaxJBossXBParser.parse(SaxJBossXBParser.java:183)
           at org.jboss.xb.binding.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:161)
           at org.jboss.deployment.EARStructure.determineStructure(EARStructure.java:162)
           ... 25 more
      Caused by: org.jboss.xb.binding.JBossXBRuntimeException: Failed to parse schema for nsURI=http://java.sun.com/xml/ns/javaee, baseURI=null, schemaLocation=http://java.sun.com/xml/ns/javaee/application_6.xsd
           at org.jboss.xb.binding.resolver.AbstractMutableSchemaResolver.resolve(AbstractMutableSchemaResolver.java:347)
           at org.jboss.xb.binding.sunday.unmarshalling.SundayContentHandler.startElement(SundayContentHandler.java:279)
           at org.jboss.xb.binding.parser.sax.SaxJBossXBParser$DelegatingContentHandler.startElement(SaxJBossXBParser.java:401)
           at org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)
           at org.apache.xerces.xinclude.XIncludeHandler.startElement(Unknown Source)
           at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
           at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
           at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
           at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
           at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
           at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
           at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
           at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
           at org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
           at org.jboss.xb.binding.parser.sax.SaxJBossXBParser.parse(SaxJBossXBParser.java:199)
           ... 28 more
      Caused by: org.jboss.xb.binding.JBossXBRuntimeException: -1:-1 160:49 src-resolve: Cannot resolve the name 'javaee:descriptionGroup' to a(n) 'group' component.
           at org.jboss.xb.binding.sunday.unmarshalling.XsdBinderTerminatingErrorHandler.handleError(XsdBinderTerminatingErrorHandler.java:40)
           at org.apache.xerces.impl.xs.XMLSchemaLoader.reportDOMFatalError(Unknown Source)
           at org.apache.xerces.impl.xs.XSLoaderImpl.load(Unknown Source)
           at org.jboss.xb.binding.Util.loadSchema(Util.java:389)
           at org.jboss.xb.binding.sunday.unmarshalling.XsdBinder.bind(XsdBinder.java:177)
           at org.jboss.xb.binding.sunday.unmarshalling.XsdBinder.bind(XsdBinder.java:148)
           at org.jboss.xb.binding.resolver.AbstractMutableSchemaResolver.resolve(AbstractMutableSchemaResolver.java:339)
           ... 42 more
      
      



      Please help me, what am I doing wrong ?


      Marcin