KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > el > SqlQueryTag


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.jstl.el;
31
32 import com.caucho.el.Expr;
33 import com.caucho.jstl.ResultImpl;
34 import com.caucho.log.Log;
35 import com.caucho.util.L10N;
36
37 import javax.el.ELContext;
38 import javax.naming.InitialContext JavaDoc;
39 import javax.naming.NamingException JavaDoc;
40 import javax.servlet.jsp.JspException JavaDoc;
41 import javax.servlet.jsp.PageContext JavaDoc;
42 import javax.servlet.jsp.jstl.core.Config;
43 import javax.servlet.jsp.jstl.sql.Result;
44 import javax.servlet.jsp.jstl.sql.SQLExecutionTag;
45 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
46 import javax.sql.DataSource JavaDoc;
47 import java.sql.Connection JavaDoc;
48 import java.sql.PreparedStatement JavaDoc;
49 import java.sql.ResultSet JavaDoc;
50 import java.sql.Statement JavaDoc;
51 import java.sql.Types JavaDoc;
52 import java.util.ArrayList JavaDoc;
53 import java.util.logging.Level JavaDoc;
54 import java.util.logging.Logger JavaDoc;
55
56 public class SqlQueryTag extends BodyTagSupport JavaDoc implements SQLExecutionTag {
57   private static final Logger JavaDoc log = Log.open(SqlQueryTag.class);
58   private static final L10N L = new L10N(SqlQueryTag.class);
59   
60   private Expr _sql;
61   private String JavaDoc _var;
62   private String JavaDoc _scope;
63   private Expr _dataSource;
64   private Expr _maxRows;
65   private Expr _startRow;
66
67   private ArrayList JavaDoc<Object JavaDoc> _params;
68
69   /**
70    * Sets the JSP-EL expression for the SQL.
71    */

72   public void setSql(Expr sql)
73   {
74     _sql = sql;
75   }
76
77   /**
78    * Sets the variable name.
79    */

80   public void setVar(String JavaDoc var)
81   {
82     _var = var;
83   }
84
85   /**
86    * Sets the scope.
87    */

88   public void setScope(String JavaDoc scope)
89   {
90     _scope = scope;
91   }
92
93   /**
94    * Sets the data source.
95    */

96   public void setDataSource(Expr dataSource)
97   {
98     _dataSource = dataSource;
99   }
100
101   /**
102    * Sets the maximum number of rows.
103    */

104   public void setMaxRows(Expr maxRows)
105   {
106     _maxRows = maxRows;
107   }
108
109   /**
110    * Sets the start row.
111    */

112   public void setStartRow(Expr startRow)
113   {
114     _startRow = startRow;
115   }
116
117   /**
118    * Adds a parameter.
119    */

120   public void addSQLParameter(Object JavaDoc value)
121   {
122     if (_params == null)
123       _params = new ArrayList JavaDoc<Object JavaDoc>();
124     
125     _params.add(value);
126   }
127
128   public int doEndTag() throws JspException JavaDoc
129   {
130     Connection JavaDoc conn = null;
131     boolean isTransaction = false;
132     ELContext env = pageContext.getELContext();
133     
134     try {
135       String JavaDoc sql;
136
137       if (_sql != null)
138         sql = _sql.evalString(env);
139       else
140         sql = bodyContent.getString();
141
142       conn = (Connection JavaDoc) pageContext.getAttribute("caucho.jstl.sql.conn");
143       if (conn != null)
144         isTransaction = true;
145
146       if (! isTransaction) {
147         DataSource JavaDoc ds;
148
149         if (_dataSource != null)
150           ds = getDataSource(pageContext, _dataSource.evalObject(env));
151         else
152           ds = getDataSource(pageContext, null);
153
154         conn = ds.getConnection();
155       }
156
157       Object JavaDoc value = null;
158
159       ResultSet JavaDoc rs;
160
161       ArrayList JavaDoc params = _params;
162       _params = null;
163       Statement JavaDoc stmt;
164
165       if (params == null) {
166         stmt = conn.createStatement();
167         rs = stmt.executeQuery(sql);
168       }
169       else {
170         PreparedStatement JavaDoc pstmt = conn.prepareStatement(sql);
171         stmt = pstmt;
172
173         for (int i = 0; i < params.size(); i++) {
174           Object JavaDoc paramValue = params.get(i);
175
176       if (paramValue == null)
177         pstmt.setNull(i + 1, Types.VARCHAR);
178       else
179         pstmt.setObject(i + 1, paramValue);
180         }
181
182         rs = pstmt.executeQuery();
183       }
184
185       if (_startRow != null) {
186         int startRow = (int) _startRow.evalLong(env);
187
188         while (startRow-- > 0 && rs.next()) {
189         }
190       }
191
192       Result result;
193       if (_maxRows != null)
194         result = new ResultImpl(rs, (int) _maxRows.evalLong(env));
195       else
196         result = new ResultImpl(rs, -1);
197
198       rs.close();
199       stmt.close();
200
201       CoreSetTag.setValue(pageContext, _var, _scope, result);
202     } catch (Exception JavaDoc e) {
203       throw new JspException JavaDoc(e);
204     } finally {
205       if (! isTransaction && conn != null) {
206         try {
207           conn.close();
208         } catch (Exception JavaDoc e) {
209           log.log(Level.FINE, e.toString(), e);
210         }
211       }
212     }
213
214     return EVAL_PAGE;
215   }
216
217   public static DataSource JavaDoc getDataSource(PageContext JavaDoc pageContext,
218                                          Object JavaDoc ds)
219     throws JspException JavaDoc
220   {
221     if (ds == null)
222       ds = Config.find(pageContext, Config.SQL_DATA_SOURCE);
223
224     if (ds instanceof DataSource JavaDoc)
225       return (DataSource JavaDoc) ds;
226     else if (! (ds instanceof String JavaDoc))
227       throw new JspException JavaDoc(L.l("`{0}' is an invalid DataSource.", ds));
228
229     String JavaDoc key = (String JavaDoc) ds;
230
231     try {
232       String JavaDoc jndiName;
233       
234       if (key.startsWith("java:comp/"))
235         jndiName = key;
236       else
237         jndiName = "java:comp/env/" + key;
238
239       Object JavaDoc value = new InitialContext JavaDoc().lookup(jndiName);
240
241       if (value instanceof DataSource JavaDoc)
242         return (DataSource JavaDoc) value;
243     } catch (NamingException JavaDoc e) {
244     }
245     
246     throw new JspException JavaDoc(L.l("`{0}' is an invalid DataSource.", ds));
247   }
248 }
249
Popular Tags