001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package connector;
018:
019: import java.io.*;
020: import java.util.Map;
021:
022: import javax.resource.ResourceException;
023: import javax.servlet.*;
024: import javax.servlet.http.*;
025: import javax.transaction.SystemException;
026: import javax.transaction.UserTransaction;
027:
028: import javax.naming.InitialContext;
029: import javax.naming.Context;
030:
031: import org.apache.commons.transaction.memory.jca.*;
032:
033: /**
034: * Implementation of the test servlet.
035: *
036: * @version $Id: TestServlet.java 493628 2007-01-07 01:42:48Z joerg $
037: */
038: public class TestServlet extends HttpServlet {
039: // Reference to the factory
040: private MapConnectionFactory _factory;
041:
042: /**
043: * <code>init()</code> stores the factory for efficiency since JNDI
044: * is relatively slow.
045: */
046: public void init() throws ServletException {
047: try {
048: Context ic = new InitialContext();
049:
050: _factory = (MapConnectionFactory) ic
051: .lookup("java:comp/env/Map");
052: } catch (Exception e) {
053: throw new ServletException(e);
054: }
055: }
056:
057: /**
058: * Use the connection. All JCA connections must use the following
059: * pattern to ensure the connection is closed even when exceptions
060: * occur.
061: */
062: public void service(HttpServletRequest request,
063: HttpServletResponse response) throws IOException,
064: ServletException {
065: response.setContentType("text/html");
066: PrintWriter out = response.getWriter();
067:
068: MapConnection conn1 = null;
069: MapConnection conn2 = null;
070:
071: UserTransaction tx = null;
072: try {
073: Context ic = new InitialContext();
074: tx = (UserTransaction) ic
075: .lookup("java:comp/UserTransaction");
076:
077: tx.begin();
078:
079: System.out.println("Tx: " + tx);
080: out.println("Tx: " + tx + "<br>");
081:
082: System.out.println("Factory: " + _factory);
083: out.println("Factory: " + _factory + "<br>");
084:
085: conn1 = (MapConnection) _factory
086: .getConnection(new MapConnectionSpec("map1"));
087: conn2 = (MapConnection) _factory
088: .getConnection(new MapConnectionSpec("map2"));
089: out.println("Connection1: " + conn1 + "<br>");
090: System.out.println("Connection1: " + conn1);
091: out.println("Connection2: " + conn2 + "<br>");
092: System.out.println("Connection2: " + conn2);
093:
094: Map map1 = conn1.getMap();
095: Map map2 = conn2.getMap();
096: out.println("Map1: " + map1 + "<br>");
097: System.out.println("Map1: " + map1);
098: out.println("Map2: " + map2 + "<br>");
099: System.out.println("Map2: " + map2);
100:
101: map1.put("Olli", "Molli");
102: map1.remove("Berti");
103:
104: map2.put("Walter", "Alter");
105: map2.put("Gundel", "Flunder");
106: map2.remove("Hertha");
107:
108: tx.commit();
109: } catch (Exception e) {
110: if (tx != null)
111: try {
112: tx.rollback();
113: } catch (IllegalStateException e1) {
114: // TODO Auto-generated catch block
115: e1.printStackTrace();
116: } catch (SecurityException e1) {
117: // TODO Auto-generated catch block
118: e1.printStackTrace();
119: } catch (SystemException e1) {
120: // TODO Auto-generated catch block
121: e1.printStackTrace();
122: }
123: System.out.println(e);
124: e.printStackTrace();
125: throw new ServletException(e);
126: } finally {
127: if (conn1 != null)
128: try {
129: conn1.close();
130: } catch (ResourceException e1) {
131: // TODO Auto-generated catch block
132: e1.printStackTrace();
133: }
134: if (conn2 != null)
135: try {
136: conn2.close();
137: } catch (ResourceException e1) {
138: // TODO Auto-generated catch block
139: e1.printStackTrace();
140: }
141: }
142: }
143: }
|