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.SQLException ; 22 import java.util.ArrayList ; 23 import java.util.List ; 24 25 import javax.servlet.jsp.JspException ; 26 import javax.servlet.jsp.JspTagException ; 27 import javax.servlet.jsp.PageContext ; 28 import javax.servlet.jsp.jstl.sql.SQLExecutionTag; 29 import javax.servlet.jsp.tagext.BodyTagSupport ; 30 import javax.servlet.jsp.tagext.TryCatchFinally ; 31 import javax.sql.DataSource ; 32 33 import org.apache.taglibs.standard.resources.Resources; 34 import org.apache.taglibs.standard.tag.common.core.Util; 35 36 42 43 public abstract class UpdateTagSupport extends BodyTagSupport 44 implements TryCatchFinally , SQLExecutionTag { 45 46 private String var; 47 private int scope; 48 49 54 protected Object rawDataSource; 55 protected boolean dataSourceSpecified; 56 protected String sql; 57 58 61 private Connection conn; 62 private List parameters; 63 private boolean isPartOfTransaction; 64 65 66 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 88 92 public void setVar(String var) { 93 this.var = var; 94 } 95 96 100 public void setScope(String scopeName) { 101 scope = Util.getScope(scopeName); 102 } 103 104 105 108 112 public int doStartTag() throws JspException { 113 114 try { 115 conn = getConnection(); 116 } catch (SQLException e) { 117 throw new JspException (sql + ": " + e.getMessage(), e); 118 } 119 120 return EVAL_BODY_BUFFERED; 121 } 122 123 136 public int doEndTag() throws JspException { 137 141 String 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 ( 150 Resources.getMessage("SQL_NO_STATEMENT")); 151 } 152 153 int result = 0; 154 PreparedStatement ps = null; 155 try { 156 ps = conn.prepareStatement(sqlStatement); 157 setParameters(ps, parameters); 158 result = ps.executeUpdate(); 159 } 160 catch (Throwable e) { 161 throw new JspException (sqlStatement + ": " + e.getMessage(), e); 162 } finally { 163 if (ps != null) { 164 try { 165 ps.close(); 166 } catch (SQLException sqe) { 167 throw new JspException (sqe.getMessage(), sqe); 168 } 169 } 170 } 171 if (var != null) 172 pageContext.setAttribute(var, new Integer (result), scope); 173 return EVAL_PAGE; 174 } 175 176 179 public void doCatch(Throwable t) throws Throwable { 180 throw t; 181 } 182 183 187 public void doFinally() { 188 if (conn != null && !isPartOfTransaction) { 189 try { 190 conn.close(); 191 } catch (SQLException e) { 192 } 194 } 195 196 parameters = null; 197 conn = null; 198 } 199 200 201 204 208 public void addSQLParameter(Object o) { 209 if (parameters == null) { 210 parameters = new ArrayList (); 211 } 212 parameters.add(o); 213 } 214 215 216 219 private Connection getConnection() throws JspException , SQLException { 220 Connection 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 ( 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 ( 236 Resources.getMessage("SQL_DATASOURCE_NULL")); 237 } 238 DataSource dataSource = DataSourceUtil.getDataSource(rawDataSource, 239 pageContext); 240 try { 241 conn = dataSource.getConnection(); 242 } catch (Exception ex) { 243 throw new JspException ( 244 Resources.getMessage("DATASOURCE_INVALID", 245 ex.toString())); 246 } 247 } 248 249 return conn; 250 } 251 252 private void setParameters(PreparedStatement ps, List parameters) 253 throws SQLException 254 { 255 if (parameters != null) { 256 for (int i = 0; i < parameters.size(); i++) { 257 262 ps.setObject(i + 1, parameters.get(i)); 263 } 264 } 265 } 266 } 267 | Popular Tags |