1 16 17 package org.apache.taglibs.standard.tag.common.sql; 18 19 import java.sql.Connection ; 20 import java.sql.PreparedStatement ; 21 import java.sql.ResultSet ; 22 import java.sql.SQLException ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 26 import javax.servlet.jsp.JspException ; 27 import javax.servlet.jsp.JspTagException ; 28 import javax.servlet.jsp.PageContext ; 29 import javax.servlet.jsp.jstl.core.Config; 30 import javax.servlet.jsp.jstl.sql.Result; 31 import javax.servlet.jsp.jstl.sql.SQLExecutionTag; 32 import javax.servlet.jsp.tagext.BodyTagSupport ; 33 import javax.servlet.jsp.tagext.TryCatchFinally ; 34 import javax.sql.DataSource ; 35 36 import org.apache.taglibs.standard.resources.Resources; 37 import org.apache.taglibs.standard.tag.common.core.Util; 38 39 40 46 47 public abstract class QueryTagSupport extends BodyTagSupport 48 implements TryCatchFinally , SQLExecutionTag { 49 50 private String var; 51 private int scope; 52 53 58 protected Object rawDataSource; 59 protected boolean dataSourceSpecified; 60 protected String sql; 61 protected int maxRows; 62 protected boolean maxRowsSpecified; 63 protected int startRow; 64 65 68 private Connection conn; 69 private List parameters; 70 private boolean isPartOfTransaction; 71 72 73 76 public QueryTagSupport() { 77 super(); 78 init(); 79 } 80 81 private void init() { 82 startRow = 0; 83 maxRows = -1; 84 maxRowsSpecified = dataSourceSpecified = false; 85 isPartOfTransaction = false; 86 conn = null; 87 rawDataSource = null; 88 parameters = null; 89 sql = null; 90 var = null; 91 scope = PageContext.PAGE_SCOPE; 92 } 93 94 95 98 102 public void setVar(String var) { 103 this.var = var; 104 } 105 106 110 public void setScope(String scopeName) { 111 scope = Util.getScope(scopeName); 112 } 113 114 117 121 public void addSQLParameter(Object o) { 122 if (parameters == null) { 123 parameters = new ArrayList (); 124 } 125 parameters.add(o); 126 } 127 128 131 135 public int doStartTag() throws JspException { 136 137 if (!maxRowsSpecified) { 138 Object obj = Config.find(pageContext, Config.SQL_MAX_ROWS); 139 if (obj != null) { 140 if (obj instanceof Integer ) { 141 maxRows = ((Integer ) obj).intValue(); 142 } else if (obj instanceof String ) { 143 try { 144 maxRows = Integer.parseInt((String ) obj); 145 } catch (NumberFormatException nfe) { 146 throw new JspException ( 147 Resources.getMessage("SQL_MAXROWS_PARSE_ERROR", 148 (String ) obj), 149 nfe); 150 } 151 } else { 152 throw new JspException ( 153 Resources.getMessage("SQL_MAXROWS_INVALID")); 154 } 155 } 156 } 157 158 try { 159 conn = getConnection(); 160 } catch (SQLException e) { 161 throw new JspException (sql + ": " + e.getMessage(), e); 162 } 163 164 return EVAL_BODY_BUFFERED; 165 } 166 167 180 public int doEndTag() throws JspException { 181 185 String sqlStatement = null; 186 if (sql != null) { 187 sqlStatement = sql; 188 } 189 else if (bodyContent != null) { 190 sqlStatement = bodyContent.getString(); 191 } 192 if (sqlStatement == null || sqlStatement.trim().length() == 0) { 193 throw new JspTagException ( 194 Resources.getMessage("SQL_NO_STATEMENT")); 195 } 196 199 if ((startRow < 0) || (maxRows < -1)) { 200 throw new JspException ( 201 Resources.getMessage("PARAM_BAD_VALUE")); 202 } 203 204 Result result = null; 205 212 PreparedStatement ps = null; 213 try { 214 ps = conn.prepareStatement(sqlStatement); 215 setParameters(ps, parameters); 216 ResultSet rs = ps.executeQuery(); 217 result = new ResultImpl(rs, startRow, maxRows); 218 } 219 catch (Throwable e) { 220 throw new JspException (sqlStatement + ": " + e.getMessage(), e); 221 } finally { 222 if (ps != null) { 223 try { 224 ps.close(); 225 } catch (SQLException sqe) { 226 throw new JspException (sqe.getMessage(), sqe); 227 } 228 } 229 } 230 pageContext.setAttribute(var, result, scope); 231 return EVAL_PAGE; 232 } 233 234 237 public void doCatch(Throwable t) throws Throwable { 238 throw t; 239 } 240 241 245 public void doFinally() { 246 if (conn != null && !isPartOfTransaction) { 247 try { 248 conn.close(); 249 } catch (SQLException e) {} } 251 252 conn = null; 253 parameters = null; 254 } 255 256 257 260 private Connection getConnection() throws JspException , SQLException { 261 Connection conn = null; 263 isPartOfTransaction = false; 264 265 TransactionTagSupport parent = (TransactionTagSupport) 266 findAncestorWithClass(this, TransactionTagSupport.class); 267 if (parent != null) { 268 if (dataSourceSpecified) { 269 throw new JspTagException ( 270 Resources.getMessage("ERROR_NESTED_DATASOURCE")); 271 } 272 conn = parent.getSharedConnection(); 273 isPartOfTransaction = true; 274 } else { 275 if ((rawDataSource == null) && dataSourceSpecified) { 276 throw new JspException ( 277 Resources.getMessage("SQL_DATASOURCE_NULL")); 278 } 279 DataSource dataSource = DataSourceUtil.getDataSource(rawDataSource, 280 pageContext); 281 try { 282 conn = dataSource.getConnection(); 283 } catch (Exception ex) { 284 throw new JspException ( 285 Resources.getMessage("DATASOURCE_INVALID", 286 ex.toString())); 287 } 288 } 289 290 return conn; 291 } 292 293 private void setParameters(PreparedStatement ps, List parameters) 294 throws SQLException 295 { 296 if (parameters != null) { 297 for (int i = 0; i < parameters.size(); i++) { 298 303 ps.setObject(i + 1, parameters.get(i)); 304 } 305 } 306 } 307 } 308 | Popular Tags |