KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > rt > 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  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

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

68   public void setSql(String JavaDoc sql)
69   {
70     _sql = sql;
71   }
72
73   /**
74    * Sets the variable name.
75    */

76   public void setVar(String JavaDoc var)
77   {
78     _var = var;
79   }
80
81   /**
82    * Sets the scope.
83    */

84   public void setScope(String JavaDoc scope)
85   {
86     _scope = scope;
87   }
88
89   /**
90    * Sets the data source.
91    */

92   public void setDataSource(Object JavaDoc dataSource)
93   {
94     _dataSource = dataSource;
95   }
96
97   /**
98    * Sets the maximum number of rows.
99    */

100   public void setMaxRows(int maxRows)
101   {
102     _maxRows = maxRows;
103   }
104
105   /**
106    * Sets the start row.
107    */

108   public void setStartRow(int startRow)
109   {
110     _startRow = startRow;
111   }
112
113   /**
114    * Adds a parameter.
115    */

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