001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Portions Copyright Apache Software Foundation.
007: *
008: * The contents of this file are subject to the terms of either the GNU
009: * General Public License Version 2 only ("GPL") or the Common Development
010: * and Distribution License("CDDL") (collectively, the "License"). You
011: * may not use this file except in compliance with the License. You can obtain
012: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
013: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
014: * language governing permissions and limitations under the License.
015: *
016: * When distributing the software, include this License Header Notice in each
017: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
018: * Sun designates this particular file as subject to the "Classpath" exception
019: * as provided by Sun in the GPL Version 2 section of the License file that
020: * accompanied this code. If applicable, add the following below the License
021: * Header, with the fields enclosed by brackets [] replaced by your own
022: * identifying information: "Portions Copyrighted [year]
023: * [name of copyright owner]"
024: *
025: * Contributor(s):
026: *
027: * If you wish your version of this file to be governed by only the CDDL or
028: * only the GPL Version 2, indicate your decision by adding "[Contributor]
029: * elects to include this software in this distribution under the [CDDL or GPL
030: * Version 2] license." If you don't indicate a single choice of license, a
031: * recipient has the option to distribute your version of this file under
032: * either the CDDL, the GPL Version 2 or to extend the choice of license to
033: * its licensees as provided above. However, if you add GPL Version 2 code
034: * and therefore, elected the GPL Version 2 license, then the option applies
035: * only if the new code is made subject to such option by the copyright
036: * holder.
037: */
038:
039: package org.apache.taglibs.standard.tag.common.sql;
040:
041: import java.sql.Connection;
042: import java.sql.PreparedStatement;
043: import java.sql.SQLException;
044: import java.util.ArrayList;
045: import java.util.List;
046:
047: import javax.servlet.jsp.JspException;
048: import javax.servlet.jsp.JspTagException;
049: import javax.servlet.jsp.PageContext;
050: import javax.servlet.jsp.jstl.sql.SQLExecutionTag;
051: import javax.servlet.jsp.tagext.BodyTagSupport;
052: import javax.servlet.jsp.tagext.TryCatchFinally;
053: import javax.sql.DataSource;
054:
055: import org.apache.taglibs.standard.resources.Resources;
056: import org.apache.taglibs.standard.tag.common.core.Util;
057:
058: /**
059: * <p>Tag handler for <Update> in JSTL.
060: *
061: * @author Hans Bergsten
062: * @author Justyna Horwat
063: */
064:
065: public abstract class UpdateTagSupport extends BodyTagSupport implements
066: TryCatchFinally, SQLExecutionTag {
067:
068: private String var;
069: private int scope;
070:
071: /*
072: * The following properties take expression values, so the
073: * setter methods are implemented by the expression type
074: * specific subclasses.
075: */
076: protected Object rawDataSource;
077: protected boolean dataSourceSpecified;
078: protected String sql;
079:
080: /*
081: * Instance variables that are not for attributes
082: */
083: private Connection conn;
084: private List parameters;
085: private boolean isPartOfTransaction;
086:
087: //*********************************************************************
088: // Constructor and initialization
089:
090: public UpdateTagSupport() {
091: super ();
092: init();
093: }
094:
095: private void init() {
096: rawDataSource = null;
097: sql = null;
098: conn = null;
099: parameters = null;
100: isPartOfTransaction = dataSourceSpecified = false;
101: scope = PageContext.PAGE_SCOPE;
102: var = null;
103: }
104:
105: //*********************************************************************
106: // Accessor methods
107:
108: /**
109: * Setter method for the name of the variable to hold the
110: * result.
111: */
112: public void setVar(String var) {
113: this .var = var;
114: }
115:
116: /**
117: * Setter method for the scope of the variable to hold the
118: * result.
119: */
120: public void setScope(String scopeName) {
121: scope = Util.getScope(scopeName);
122: }
123:
124: //*********************************************************************
125: // Tag logic
126:
127: /**
128: * Prepares for execution by setting the initial state, such as
129: * getting the <code>Connection</code>
130: */
131: public int doStartTag() throws JspException {
132:
133: try {
134: conn = getConnection();
135: } catch (SQLException e) {
136: throw new JspException(sql + ": " + e.getMessage(), e);
137: }
138:
139: return EVAL_BODY_BUFFERED;
140: }
141:
142: /**
143: * <p>Execute the SQL statement, set either through the <code>sql</code>
144: * attribute or as the body, and save the result as a variable
145: * named by the <code>var</code> attribute in the scope specified
146: * by the <code>scope</code> attribute, as an object that implements
147: * the Result interface.
148: *
149: * <p>The connection used to execute the statement comes either
150: * from the <code>DataSource</code> specified by the
151: * <code>dataSource</code> attribute, provided by a parent action
152: * element, or is retrieved from a JSP scope attribute
153: * named <code>javax.servlet.jsp.jstl.sql.dataSource</code>.
154: */
155: public int doEndTag() throws JspException {
156: /*
157: * Use the SQL statement specified by the sql attribute, if any,
158: * otherwise use the body as the statement.
159: */
160: String sqlStatement = null;
161: if (sql != null) {
162: sqlStatement = sql;
163: } else if (bodyContent != null) {
164: sqlStatement = bodyContent.getString();
165: }
166: if (sqlStatement == null || sqlStatement.trim().length() == 0) {
167: throw new JspTagException(Resources
168: .getMessage("SQL_NO_STATEMENT"));
169: }
170:
171: int result = 0;
172: try {
173: PreparedStatement ps = conn.prepareStatement(sqlStatement);
174: setParameters(ps, parameters);
175: result = ps.executeUpdate();
176: } catch (Throwable e) {
177: throw new JspException(
178: sqlStatement + ": " + e.getMessage(), e);
179: }
180: if (var != null)
181: pageContext.setAttribute(var, Integer.valueOf(result),
182: scope);
183: return EVAL_PAGE;
184: }
185:
186: /**
187: * Just rethrows the Throwable.
188: */
189: public void doCatch(Throwable t) throws Throwable {
190: throw t;
191: }
192:
193: /**
194: * Close the <code>Connection</code>, unless this action is used
195: * as part of a transaction.
196: */
197: public void doFinally() {
198: if (conn != null && !isPartOfTransaction) {
199: try {
200: conn.close();
201: } catch (SQLException e) {
202: // Not much we can do
203: }
204: }
205:
206: parameters = null;
207: conn = null;
208: }
209:
210: //*********************************************************************
211: // Public utility methods
212:
213: /**
214: * Called by nested parameter elements to add PreparedStatement
215: * parameter values.
216: */
217: public void addSQLParameter(Object o) {
218: if (parameters == null) {
219: parameters = new ArrayList();
220: }
221: parameters.add(o);
222: }
223:
224: //*********************************************************************
225: // Private utility methods
226:
227: private Connection getConnection() throws JspException,
228: SQLException {
229: // Fix: Add all other mechanisms
230: Connection conn = null;
231: isPartOfTransaction = false;
232:
233: TransactionTagSupport parent = (TransactionTagSupport) findAncestorWithClass(
234: this , TransactionTagSupport.class);
235: if (parent != null) {
236: if (dataSourceSpecified) {
237: throw new JspTagException(Resources
238: .getMessage("ERROR_NESTED_DATASOURCE"));
239: }
240: conn = parent.getSharedConnection();
241: isPartOfTransaction = true;
242: } else {
243: if ((rawDataSource == null) && dataSourceSpecified) {
244: throw new JspException(Resources
245: .getMessage("SQL_DATASOURCE_NULL"));
246: }
247: DataSource dataSource = DataSourceUtil.getDataSource(
248: rawDataSource, pageContext);
249: try {
250: conn = dataSource.getConnection();
251: } catch (Exception ex) {
252: throw new JspException(Resources.getMessage(
253: "DATASOURCE_INVALID", ex.toString()));
254: }
255: }
256:
257: return conn;
258: }
259:
260: private void setParameters(PreparedStatement ps, List parameters)
261: throws SQLException {
262: if (parameters != null) {
263: for (int i = 0; i < parameters.size(); i++) {
264: /* The first parameter has index 1. If a null
265: * is passed to setObject the parameter will be
266: * set to JDBC null so an explicit call to
267: * ps.setNull is not required.
268: */
269: ps.setObject(i + 1, parameters.get(i));
270: }
271: }
272: }
273: }
|