KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > micronova > jsp > tag > TransactionTag


1 /*
2
3 Copyright 2003-2007 MicroNova (R)
4 All rights reserved.
5
6 Redistribution and use in source and binary forms, with or
7 without modification, are permitted provided that the following
8 conditions are met:
9
10     * Redistributions of source code must retain the above copyright
11     notice, this list of conditions and the following disclaimer.
12
13     * Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17     * Neither the name of MicroNova nor the names of its contributors
18     may be used to endorse or promote products derived from this
19     software without specific prior written permission.
20
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE.
32
33 */

34
35
36 package com.micronova.jsp.tag;
37
38 import java.util.*;
39 import java.util.regex.*;
40 import java.sql.*;
41 import javax.sql.*;
42 import javax.naming.*;
43 import javax.servlet.jsp.*;
44 import javax.servlet.jsp.jstl.core.Config;
45 import org.apache.taglibs.standard.tag.common.sql.TransactionTagSupport;
46 import com.micronova.util.*;
47
48 /** stackable transaction tag */
49
50 public class TransactionTag extends YuzuTag
51 {
52     protected Object JavaDoc _dataSource;
53     protected int _isolation;
54     protected int _isolationBefore;
55
56     protected Connection _connection;
57     protected boolean _doesOwnConnection;
58
59     /** utility to open connection from given datasource spec */
60
61     public static Connection openConnection(Object JavaDoc spec) throws Exception JavaDoc
62     {
63         Connection connection = null;
64
65         if (spec instanceof DataSource)
66         {
67             connection = ((DataSource)spec).getConnection();
68         }
69         else
70         {
71             String JavaDoc specString = (String JavaDoc)spec;
72
73             if (specString.startsWith("jdbc:"))
74             {
75                 List parts = StringUtil.split(specString, ',');
76                 
77                 int partsLength = parts.size();
78
79                 String JavaDoc jdbcURL = parts.get(0).toString();
80                 String JavaDoc driverClassName = parts.get(1).toString();
81
82                 String JavaDoc user = null;
83                 String JavaDoc password = null;
84
85                 if (partsLength >= 3)
86                 {
87                     user = parts.get(2).toString();
88                 }
89
90                 if (partsLength >= 4)
91                 {
92                     password = parts.get(3).toString();
93                 }
94
95                 Class.forName(driverClassName, true, Thread.currentThread().getContextClassLoader()).newInstance();
96
97                 if ((user == null) && (password == null))
98                 {
99                     connection = DriverManager.getConnection(jdbcURL);
100                 }
101                 else
102                 {
103                     connection = DriverManager.getConnection(jdbcURL, user, password);
104                 }
105             }
106             else
107             {
108                 Context context = new InitialContext();
109                 Context environment = (Context)context.lookup("java:comp/env");
110
111                 DataSource dataSource = (DataSource)environment.lookup(specString);
112
113                 connection = dataSource.getConnection();
114             }
115         }
116
117         return connection;
118     }
119
120     protected static TransactionTag getAncestorTransactionTag(YuzuTag tag)
121     {
122         return (TransactionTag)tag.getAncestorTag("com.micronova.jsp.tag.TransactionTag");
123     }
124
125     protected static TransactionTagSupport getAncestorTransactionTagSupport(YuzuTag tag)
126     {
127         return (TransactionTagSupport)tag.getAncestorTag("org.apache.taglibs.standard.tag.common.sql.TransactionTagSupport");
128     }
129
130     /** returns given tag's ancestor's connection if possible, otherwise null. m:transaction takes precedence over sql:transaction */
131
132     public static Connection getAncestorConnection(YuzuTag tag) throws Exception JavaDoc
133     {
134         TransactionTag transactionTag = getAncestorTransactionTag(tag);
135         
136         if (transactionTag != null)
137         {
138             return transactionTag.getConnection();
139         }
140
141         TransactionTagSupport transactionTagSupport = getAncestorTransactionTagSupport(tag);
142
143         if (transactionTagSupport != null)
144         {
145             return transactionTagSupport.getSharedConnection();
146         }
147
148         return null;
149     }
150
151     protected void init()
152     {
153         super.init();
154
155         _dataSource = null;
156         _isolation = Connection.TRANSACTION_NONE;
157         _isolationBefore = Connection.TRANSACTION_NONE;
158         _connection = null;
159         _doesOwnConnection = false;
160     }
161
162     protected Connection getConnection(Object JavaDoc spec) throws Exception JavaDoc
163     {
164         Connection connection = _connection;
165
166         if (connection != null)
167         {
168             return connection;
169         }
170         else
171         {
172             if (spec == null)
173             {
174                 spec = getConfiguration(Config.SQL_DATA_SOURCE, null);
175             }
176
177             connection = openConnection(spec);
178         }
179
180         return connection;
181     }
182
183     protected Connection getConnection() throws Exception JavaDoc
184     {
185         return getConnection(_dataSource);
186     }
187
188     public void initBody() throws Exception JavaDoc
189     {
190         Connection connection = null;
191
192         Object JavaDoc dataSource = _dataSource;
193
194         if (dataSource == null)
195         {
196             connection = getAncestorConnection(this);
197         }
198
199         if (connection != null)
200         {
201             _doesOwnConnection = false;
202             _connection = connection;
203             _isolationBefore = connection.getTransactionIsolation();
204         }
205         else
206         {
207             _doesOwnConnection = true;
208             connection = getConnection(dataSource);
209             connection.setAutoCommit(false);
210
211             _connection = connection;
212         }
213
214         if (connection != null)
215         {
216             int isolation = _isolation;
217
218             if (isolation != Connection.TRANSACTION_NONE)
219             {
220                 connection.setTransactionIsolation(isolation);
221             }
222         }
223
224         super.initBody();
225     }
226     
227     public void afterBody() throws Exception JavaDoc
228     {
229         super.afterBody();
230
231         if (_doesOwnConnection)
232         {
233             _connection.commit();
234         }
235     }
236
237     public void doCatch(Throwable JavaDoc t) throws Throwable JavaDoc
238     {
239         boolean doesOwnConnection = _doesOwnConnection;
240
241         if (doesOwnConnection)
242         {
243             _connection.rollback();
244         }
245
246         super.doCatch(t);
247     }
248
249     public void doFinally()
250     {
251         try
252         {
253             if (_doesOwnConnection)
254             {
255                 try
256                 {
257                     _connection.close();
258                 }
259                 catch (Exception JavaDoc ee)
260                 {
261                 }
262             }
263             else
264             {
265                 _connection.setTransactionIsolation(_isolationBefore);
266             }
267         }
268         catch (Exception JavaDoc e)
269         {
270         }
271
272         super.doFinally();
273     }
274
275     public void setDataSource(Object JavaDoc expression) throws Exception JavaDoc
276     {
277         Object JavaDoc dataSource = evaluateAttribute("dataSource", expression, Object JavaDoc.class);
278         
279         if (!isEmptyString(dataSource))
280         {
281             _dataSource = dataSource;
282         }
283     }
284
285     public void setIsolation(Object JavaDoc expression) throws Exception JavaDoc
286     {
287         String JavaDoc isolationSpec = (String JavaDoc)evaluateAttribute("dataSource", expression, String JavaDoc.class);
288
289         if ("read_committed".equals(isolationSpec))
290         {
291             _isolation = Connection.TRANSACTION_READ_COMMITTED;
292         }
293         else if ("read_uncommitted".equals(isolationSpec))
294         {
295             _isolation = Connection.TRANSACTION_READ_UNCOMMITTED;
296         }
297         else if ("repeatable_read".equals(isolationSpec))
298         {
299             _isolation = Connection.TRANSACTION_REPEATABLE_READ;
300         }
301         else if ("serializable".equals(isolationSpec))
302         {
303             _isolation = Connection.TRANSACTION_SERIALIZABLE;
304         }
305         else if ("none".equals(isolationSpec))
306         {
307             _isolation = Connection.TRANSACTION_NONE;
308         }
309         else
310         {
311             throw new JspTagException("unsupported transaction isolation:" + isolationSpec);
312         }
313     }
314 }
315
Popular Tags