7 Replies Latest reply on Aug 21, 2008 12:26 PM by Markus Brandstätter

    Access to persistence context from AbstractResource or Servlet

    Marcos LoisBermudez Newbie

      Hello i have some problems getting org.jboss.seam.web.AbstractResource to work, i also try to use a pure servlet to stream data to client with the same problem.


      If i stream data from filesystem no problem with both aproaches, but when i try to stream data that is stored in database (I access data trought Hibernate JPA implementation), i can't get a entity manager, i can only solve this on pure Servlet getting a EnityManagerFactory from JNDI.


      So is posible to inject the persistenceContext in AbstractResource instance or pure Servlet? I try out most of the examples and nothing work.


      This is my code, running on JBoss 4.2.2GA:


      components.xml


      <persistence:managed-persistence-context name="entityManager" auto-create="true" persistence-unit-jndi-name="java:/axudasEntityManagerFactory" />



      persistence.xml


      <persistence-unit name="axudas">
          <provider>org.hibernate.ejb.HibernatePersistence</provider>
          <jta-data-source>java:/axudasDatasource</jta-data-source>
          <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
            <property name="hibernate.hbm2ddl.auto" value="validate"/>
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="jboss.entity.manager.factory.jndi.name" value="java:/axudasEntityManagerFactory"/>
          </properties>
      </persistence-unit>



      MediaServlet.java


      public class MediaServlet extends javax.servlet.http.HttpServlet implements
              javax.servlet.Servlet {
          static final long serialVersionUID = 1L;
      
          /*
           * (non-Java-doc)
           *
           * @see javax.servlet.http.HttpServlet#HttpServlet()
           */
          public MediaServlet() {
              super();
          }
      
          /*
           * (non-Java-doc)
           *
           * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
           *      HttpServletResponse response)
           */
          protected void doGet(HttpServletRequest request,
                  HttpServletResponse response) throws ServletException, IOException {
              response.setContentType("application/octet-stream");
              response.addHeader("Content-disposition",
                      "attachment; filename=\"image.jpg\"");
              response.addHeader("Cache-Control", "no-cache");
              response.setStatus(200);
             
              // perform JNDI lookup to obtain container-managed entity manager
              EntityManager entityManager;
             
              Context initCtx;
              try {
                  initCtx = new InitialContext();
                  entityManager = ((EntityManagerFactory) initCtx
                          .lookup("java:axudasEntityManagerFactory")).createEntityManager();
              } catch (NamingException e) {
                  throw new ServletException(e);
              }
      
              Prueba pb = entityManager.find(Prueba.class, 10848);
      
              response.flushBuffer();
              ServletOutputStream out = response.getOutputStream();
      
              InputStream in;
              try {
                  in = pb.getData().getBinaryStream();
              } catch (SQLException e) {
                  throw new ServletException(e);
              }
              // FileInputStream in = new FileInputStream(new
              // File("C:/Marcos/SIVSA/Download/eclipse-cpp-ganymede-win32.zip"));
              byte[] buff = new byte[1024];
              int nread = 0;
      
              while ((nread = in.read(buff)) > 0) {
                  out.write(buff, 0, nread);
                  out.flush();
                  response.flushBuffer();
              }
      
              out.close();
              in.close();
          }
      }



      MediaResource.java


      @Scope(ScopeType.APPLICATION)
      @Name("myResource")
      @BypassInterceptors
      public class Resource extends AbstractResource {
         
          @In
          private EntityManagerFactory entityManagerFactory;
      
          @Override
          public String getResourcePath() {
              return "/myresource";
          }
      
      
          @Override
          public void getResource(final HttpServletRequest request, final HttpServletResponse response)
                  throws ServletException, IOException {
      
              new ContextualHttpServletRequest(request) {
                  @Override
                  public void process() throws IOException, ServletException {
                      response.setContentType("application/octet-stream");
                      response.addHeader("Content-disposition", "attachment; filename=\"image.jpg\"");
                      response.addHeader("Cache-Control", "no-cache");
                      response.setStatus(200);
                     
                      response.flushBuffer();
                      ServletOutputStream out = response.getOutputStream();
                     
                      EntityManager entityManager = entityManagerFactory.createEntityManager();
                     
                      Prueba pb = entityManager.find(Prueba.class, 10848);
                     
                      InputStream in;
                      try {
                          in = pb.getData().getBinaryStream();
                      } catch (SQLException e) {
                          throw new ServletException(e);
                      }
      //                FileInputStream in = new FileInputStream(new File("C:/Marcos/SIVSA/Download/eclipse-cpp-ganymede-win32.zip"));
                      byte[] buff = new byte[1024];
                      int nread = 0;
                     
                      while((nread = in.read(buff)) > 0) {
                          out.write(buff, 0, nread);
                          out.flush();
                          response.flushBuffer();
                      }
                     
                      out.close();
                      in.close();
                  }
              }.run();
          }
      }




      So how inyect the persistence context in a clean way to AbstractResource or Servlet?


      Marcos Lois