KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > sql > UpdateTagSupport


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.standard.tag.common.sql;
18
19 import java.sql.Connection JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.List JavaDoc;
24
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.JspTagException JavaDoc;
27 import javax.servlet.jsp.PageContext JavaDoc;
28 import javax.servlet.jsp.jstl.sql.SQLExecutionTag;
29 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
30 import javax.servlet.jsp.tagext.TryCatchFinally JavaDoc;
31 import javax.sql.DataSource JavaDoc;
32
33 import org.apache.taglibs.standard.resources.Resources;
34 import org.apache.taglibs.standard.tag.common.core.Util;
35
36 /**
37  * <p>Tag handler for &lt;Update&gt; in JSTL.
38  *
39  * @author Hans Bergsten
40  * @author Justyna Horwat
41  */

42
43 public abstract class UpdateTagSupport extends BodyTagSupport JavaDoc
44     implements TryCatchFinally JavaDoc, SQLExecutionTag {
45
46     private String JavaDoc var;
47     private int scope;
48
49     /*
50      * The following properties take expression values, so the
51      * setter methods are implemented by the expression type
52      * specific subclasses.
53      */

54     protected Object JavaDoc rawDataSource;
55     protected boolean dataSourceSpecified;
56     protected String JavaDoc sql;
57
58     /*
59      * Instance variables that are not for attributes
60      */

61     private Connection JavaDoc conn;
62     private List JavaDoc parameters;
63     private boolean isPartOfTransaction;
64
65
66     //*********************************************************************
67
// Constructor and initialization
68

69     public UpdateTagSupport() {
70     super();
71     init();
72     }
73
74     private void init() {
75     rawDataSource = null;
76     sql = null;
77     conn = null;
78     parameters = null;
79     isPartOfTransaction = dataSourceSpecified = false;
80         scope = PageContext.PAGE_SCOPE;
81     var = null;
82     }
83
84
85     //*********************************************************************
86
// Accessor methods
87

88     /**
89      * Setter method for the name of the variable to hold the
90      * result.
91      */

92     public void setVar(String JavaDoc var) {
93     this.var = var;
94     }
95
96     /**
97      * Setter method for the scope of the variable to hold the
98      * result.
99      */

100     public void setScope(String JavaDoc scopeName) {
101         scope = Util.getScope(scopeName);
102     }
103
104
105     //*********************************************************************
106
// Tag logic
107

108     /**
109      * Prepares for execution by setting the initial state, such as
110      * getting the <code>Connection</code>
111      */

112     public int doStartTag() throws JspException JavaDoc {
113
114     try {
115         conn = getConnection();
116     } catch (SQLException JavaDoc e) {
117         throw new JspException JavaDoc(sql + ": " + e.getMessage(), e);
118     }
119
120     return EVAL_BODY_BUFFERED;
121     }
122
123     /**
124      * <p>Execute the SQL statement, set either through the <code>sql</code>
125      * attribute or as the body, and save the result as a variable
126      * named by the <code>var</code> attribute in the scope specified
127      * by the <code>scope</code> attribute, as an object that implements
128      * the Result interface.
129      *
130      * <p>The connection used to execute the statement comes either
131      * from the <code>DataSource</code> specified by the
132      * <code>dataSource</code> attribute, provided by a parent action
133      * element, or is retrieved from a JSP scope attribute
134      * named <code>javax.servlet.jsp.jstl.sql.dataSource</code>.
135      */

136     public int doEndTag() throws JspException JavaDoc {
137     /*
138      * Use the SQL statement specified by the sql attribute, if any,
139      * otherwise use the body as the statement.
140      */

141     String JavaDoc sqlStatement = null;
142     if (sql != null) {
143         sqlStatement = sql;
144     }
145     else if (bodyContent != null) {
146         sqlStatement = bodyContent.getString();
147     }
148     if (sqlStatement == null || sqlStatement.trim().length() == 0) {
149         throw new JspTagException JavaDoc(
150                 Resources.getMessage("SQL_NO_STATEMENT"));
151     }
152
153     int result = 0;
154     PreparedStatement JavaDoc ps = null;
155     try {
156         ps = conn.prepareStatement(sqlStatement);
157         setParameters(ps, parameters);
158         result = ps.executeUpdate();
159     }
160     catch (Throwable JavaDoc e) {
161         throw new JspException JavaDoc(sqlStatement + ": " + e.getMessage(), e);
162     } finally {
163         if (ps != null) {
164         try {
165             ps.close();
166         } catch (SQLException JavaDoc sqe) {
167             throw new JspException JavaDoc(sqe.getMessage(), sqe);
168         }
169         }
170     }
171     if (var != null)
172         pageContext.setAttribute(var, new Integer JavaDoc(result), scope);
173     return EVAL_PAGE;
174     }
175
176     /**
177      * Just rethrows the Throwable.
178      */

179     public void doCatch(Throwable JavaDoc t) throws Throwable JavaDoc {
180     throw t;
181     }
182
183     /**
184      * Close the <code>Connection</code>, unless this action is used
185      * as part of a transaction.
186      */

187     public void doFinally() {
188     if (conn != null && !isPartOfTransaction) {
189         try {
190         conn.close();
191         } catch (SQLException JavaDoc e) {
192         // Not much we can do
193
}
194     }
195
196     parameters = null;
197     conn = null;
198     }
199
200
201     //*********************************************************************
202
// Public utility methods
203

204     /**
205      * Called by nested parameter elements to add PreparedStatement
206      * parameter values.
207      */

208     public void addSQLParameter(Object JavaDoc o) {
209     if (parameters == null) {
210         parameters = new ArrayList JavaDoc();
211     }
212     parameters.add(o);
213     }
214
215
216     //*********************************************************************
217
// Private utility methods
218

219     private Connection JavaDoc getConnection() throws JspException JavaDoc, SQLException JavaDoc {
220     // Fix: Add all other mechanisms
221
Connection JavaDoc conn = null;
222     isPartOfTransaction = false;
223
224     TransactionTagSupport parent = (TransactionTagSupport)
225         findAncestorWithClass(this, TransactionTagSupport.class);
226     if (parent != null) {
227             if (dataSourceSpecified) {
228                 throw new JspTagException JavaDoc(
229                     Resources.getMessage("ERROR_NESTED_DATASOURCE"));
230             }
231         conn = parent.getSharedConnection();
232             isPartOfTransaction = true;
233     } else {
234         if ((rawDataSource == null) && dataSourceSpecified) {
235         throw new JspException JavaDoc(
236             Resources.getMessage("SQL_DATASOURCE_NULL"));
237         }
238         DataSource JavaDoc dataSource = DataSourceUtil.getDataSource(rawDataSource,
239                                  pageContext);
240             try {
241                 conn = dataSource.getConnection();
242             } catch (Exception JavaDoc ex) {
243                 throw new JspException JavaDoc(
244                     Resources.getMessage("DATASOURCE_INVALID",
245                      ex.toString()));
246             }
247     }
248
249     return conn;
250     }
251
252     private void setParameters(PreparedStatement JavaDoc ps, List JavaDoc parameters)
253         throws SQLException JavaDoc
254     {
255     if (parameters != null) {
256         for (int i = 0; i < parameters.size(); i++) {
257                 /* The first parameter has index 1. If a null
258                  * is passed to setObject the parameter will be
259                  * set to JDBC null so an explicit call to
260                  * ps.setNull is not required.
261                  */

262                 ps.setObject(i + 1, parameters.get(i));
263         }
264     }
265     }
266 }
267
Popular Tags