View Javadoc

1   /*
2    * Copyright 2002,2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.commons.jelly.tags.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.jstl.sql.SQLExecutionTag;
26  import javax.sql.DataSource;
27  
28  import org.apache.commons.jelly.JellyTagException;
29  import org.apache.commons.jelly.TagSupport;
30  import org.apache.commons.jelly.tags.Resources;
31  
32  /***
33   * <p>Abstract base class for any SQL related tag in JSTL.
34   *
35   * @author Hans Bergsten
36   * @author Justyna Horwat
37   * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
38   */
39  
40  public abstract class SqlTagSupport extends TagSupport implements SQLExecutionTag {
41  
42      protected String var;
43      protected String scope = "page";
44  
45      /*
46       * The following properties take expression values, so the
47       * setter methods are implemented by the expression type
48       * specific subclasses.
49       */
50      protected Object rawDataSource;
51      protected boolean dataSourceSpecified;
52      protected String sql;
53  
54      /*
55       * Instance variables that are not for attributes
56       */
57      private List parameters;
58      protected boolean isPartOfTransaction;
59  
60      //**********************************************************************
61      // Constructor and initialization
62  
63      public SqlTagSupport() {
64  		super.setEscapeText(false);
65      }
66  
67      //*********************************************************************
68      // Accessor methods
69  
70      /**
71       * Sets the name of the variable to hold the
72       * result.
73       */
74      public void setVar(String var) {
75          this.var = var;
76      }
77  
78      /***
79       * Sets the scope of the variable to hold the
80       * result.
81       */
82      public void setScope(String scopeName) {
83          this.scope = scopeName;
84      }
85  
86      /***
87       * Sets the SQL DataSource. DataSource can be
88       * a String or a DataSource object.
89       */
90      public void setDataSource(Object dataSource) {
91          this.rawDataSource = dataSource;
92          this.dataSourceSpecified = true;
93      }
94  
95      /***
96       * Sets the SQL statement to use for the
97       * query. The statement may contain parameter markers
98       * (question marks, ?). If so, the parameter values must
99       * be set using nested value elements.
100      */
101     public void setSql(String sql) {
102         this.sql = sql;
103     }
104 
105 
106     //**********************************************************************
107     // Public utility methods
108 
109     /**
110      * Called by nested parameter elements to add PreparedStatement
111      * parameter values.
112      */
113     public void addSQLParameter(Object o) {
114         if (parameters == null) {
115             parameters = new ArrayList();
116         }
117         parameters.add(o);
118     }
119 
120     //**********************************************************************
121     // Protected utility methods
122 
123     /**
124      * @return true if there are SQL parameters
125      */
126     protected boolean hasParameters() {
127         return parameters != null && parameters.size() > 0;
128     }
129 
130     protected void clearParameters() {
131         parameters = null;
132     }
133 
134     protected Connection getConnection() throws JellyTagException, SQLException {
135         // Fix: Add all other mechanisms
136         Connection conn = null;
137         isPartOfTransaction = false;
138 
139         TransactionTag parent =
140             (TransactionTag) findAncestorWithClass(TransactionTag.class);
141         if (parent != null) {
142             if (dataSourceSpecified) {
143                 throw new JellyTagException(Resources.getMessage("ERROR_NESTED_DATASOURCE"));
144             }
145             conn = parent.getSharedConnection();
146             isPartOfTransaction = true;
147         }
148         else {
149             if ((rawDataSource == null) && dataSourceSpecified) {
150                 throw new JellyTagException(Resources.getMessage("SQL_DATASOURCE_NULL"));
151             }
152             DataSource dataSource = DataSourceUtil.getDataSource(rawDataSource, context);
153             try {
154                 conn = dataSource.getConnection();
155             }
156             catch (Exception ex) {
157                 throw new JellyTagException(
158                     Resources.getMessage("DATASOURCE_INVALID", ex.getMessage()));
159             }
160         }
161 
162         return conn;
163     }
164 
165     protected void setParameters(PreparedStatement ps)
166         throws SQLException {
167         if (parameters != null) {
168             for (int i = 0; i < parameters.size(); i++) {
169                 // The first parameter has index 1
170                 ps.setObject(i + 1, parameters.get(i));
171             }
172         }
173     }
174 }