ozeigermann    2004/05/25 07:29:23

  Added:       transaction/example/mapConnector/src/conf web.xml
                        jboss-web.xml
               transaction/example/mapConnector/src/java/connector
                        TestServlet.java
  Log:
  Added example for Map Connector
  
  Revision  Changes    Path
  1.1                  
jakarta-commons-sandbox/transaction/example/mapConnector/src/conf/web.xml
  
  Index: web.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" 
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd";>
  <web-app id="WebApp">
        <servlet>
                <servlet-name>Test</servlet-name>
                <display-name>Test</display-name>
                
<servlet-class>org.apache.commons.transaction.jca.TestServlet</servlet-class>
        </servlet>
        <servlet-mapping>
                <servlet-name>Test</servlet-name>
                <url-pattern>/</url-pattern>
        </servlet-mapping>
     <resource-ref >
        <res-ref-name>Map</res-ref-name>
        
<res-type>org.apache.commons.transaction.jca.MapManagedConnectionFactory</res-type>
        <res-auth>Container</res-auth>
     </resource-ref>
  </web-app>
  
  
  
  1.1                  
jakarta-commons-sandbox/transaction/example/mapConnector/src/conf/jboss-web.xml
  
  Index: jboss-web.xml
  ===================================================================
  <?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE jboss-web PUBLIC "-//JBoss//DTD Web Application 2.2//EN" 
"http://www.jboss.org/j2ee/dtd/jboss-web.dtd";>
  
  <jboss-web>
  
     <!-- Resource references -->
     <resource-ref>
        <res-ref-name>Map</res-ref-name>
        <jndi-name>java:Map</jndi-name>
     </resource-ref>
  </jboss-web>
  
  
  
  
  1.1                  
jakarta-commons-sandbox/transaction/example/mapConnector/src/java/connector/TestServlet.java
  
  Index: TestServlet.java
  ===================================================================
  package connector;
  
  import java.io.*;
  import java.util.Map;
  
  import javax.resource.ResourceException;
  import javax.servlet.*;
  import javax.servlet.http.*;
  import javax.transaction.SystemException;
  import javax.transaction.UserTransaction;
  
  import javax.naming.InitialContext;
  import javax.naming.Context;
  
  import org.apache.commons.transaction.memory.jca.*;
  
  /**
   * Implementation of the test servlet.
   */
  public class TestServlet extends HttpServlet {
      // Reference to the factory
      private MapConnectionFactory _factory;
  
      /**
       * <code>init()</code> stores the factory for efficiency since JNDI
       * is relatively slow.
       */
      public void init() throws ServletException {
          try {
              Context ic = new InitialContext();
  
              _factory = (MapConnectionFactory) ic.lookup("java:comp/env/Map");
          } catch (Exception e) {
              throw new ServletException(e);
          }
      }
  
      /**
       * Use the connection.  All JCA connections must use the following
       * pattern to ensure the connection is closed even when exceptions
       * occur.
       */
      public void service(HttpServletRequest request, HttpServletResponse response)
          throws IOException, ServletException {
          response.setContentType("text/html");
          PrintWriter out = response.getWriter();
  
          MapConnection conn1 = null;
          MapConnection conn2 = null;
  
          UserTransaction tx = null;
          try {
              Context ic = new InitialContext();
              tx = (UserTransaction) ic.lookup("java:comp/UserTransaction");
  
              tx.begin();
  
              System.out.println("Tx: " + tx);
              out.println("Tx: " + tx + "<br>");
  
              System.out.println("Factory: " + _factory);
              out.println("Factory: " + _factory + "<br>");
  
              conn1 = (MapConnection) _factory.getConnection(new 
MapConnectionSpec("map1"));
              conn2 = (MapConnection) _factory.getConnection(new 
MapConnectionSpec("map2"));
              out.println("Connection1: " + conn1 + "<br>");
              System.out.println("Connection1: " + conn1);
              out.println("Connection2: " + conn2 + "<br>");
              System.out.println("Connection2: " + conn2);
  
              Map map1 = conn1.getMap();
              Map map2 = conn2.getMap();
              out.println("Map1: " + map1 + "<br>");
              System.out.println("Map1: " + map1);
              out.println("Map2: " + map2 + "<br>");
              System.out.println("Map2: " + map2);
  
              map1.put("Olli", "Molli");
              map1.remove("Berti");
  
              map2.put("Walter", "Alter");
              map2.put("Gundel", "Flunder");
              map2.remove("Hertha");
  
              tx.commit();
          } catch (Exception e) {
              if (tx != null)
                  try {
                      tx.rollback();
                  } catch (IllegalStateException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  } catch (SecurityException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  } catch (SystemException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  }
              System.out.println(e);
              e.printStackTrace();
              throw new ServletException(e);
          } finally {
              if (conn1 != null)
                  try {
                      conn1.close();
                  } catch (ResourceException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  }
              if (conn2 != null)
                  try {
                      conn2.close();
                  } catch (ResourceException e1) {
                      // TODO Auto-generated catch block
                      e1.printStackTrace();
                  }
          }
      }
  }
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to