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      }
65  
66      //*********************************************************************
67      // Accessor methods
68  
69      /**
70       * Sets the name of the variable to hold the
71       * result.
72       */
73      public void setVar(String var) {
74          this.var = var;
75      }
76  
77      /***
78       * Sets the scope of the variable to hold the
79       * result.
80       */
81      public void setScope(String scopeName) {
82          this.scope = scopeName;
83      }
84  
85      /***
86       * Sets the SQL DataSource. DataSource can be
87       * a String or a DataSource object.
88       */
89      public void setDataSource(Object dataSource) {
90          this.rawDataSource = dataSource;
91          this.dataSourceSpecified = true;
92      }
93  
94      /***
95       * Sets the SQL statement to use for the
96       * query. The statement may contain parameter markers
97       * (question marks, ?). If so, the parameter values must
98       * be set using nested value elements.
99       */
100     public void setSql(String sql) {
101         this.sql = sql;
102     }
103 
104 
105     //**********************************************************************
106     // Public utility methods
107 
108     /**
109      * Called by nested parameter elements to add PreparedStatement
110      * parameter values.
111      */
112     public void addSQLParameter(Object o) {
113         if (parameters == null) {
114             parameters = new ArrayList();
115         }
116         parameters.add(o);
117     }
118 
119     //**********************************************************************
120     // Protected utility methods
121 
122     /**
123      * @return true if there are SQL parameters
124      */
125     protected boolean hasParameters() {
126         return parameters != null && parameters.size() > 0;
127     }
128 
129     protected void clearParameters() {
130         parameters = null;
131     }
132 
133     protected Connection getConnection() throws JellyTagException, SQLException {
134         // Fix: Add all other mechanisms
135         Connection conn = null;
136         isPartOfTransaction = false;
137 
138         TransactionTag parent =
139             (TransactionTag) findAncestorWithClass(TransactionTag.class);
140         if (parent != null) {
141             if (dataSourceSpecified) {
142                 throw new JellyTagException(Resources.getMessage("ERROR_NESTED_DATASOURCE"));
143             }
144             conn = parent.getSharedConnection();
145             isPartOfTransaction = true;
146         }
147         else {
148             if ((rawDataSource == null) && dataSourceSpecified) {
149                 throw new JellyTagException(Resources.getMessage("SQL_DATASOURCE_NULL"));
150             }
151             DataSource dataSource = DataSourceUtil.getDataSource(rawDataSource, context);
152             try {
153                 conn = dataSource.getConnection();
154             }
155             catch (Exception ex) {
156                 throw new JellyTagException(
157                     Resources.getMessage("DATASOURCE_INVALID", ex.getMessage()));
158             }
159         }
160 
161         return conn;
162     }
163 
164     protected void setParameters(PreparedStatement ps)
165         throws SQLException {
166         if (parameters != null) {
167             for (int i = 0; i < parameters.size(); i++) {
168                 // The first parameter has index 1
169                 ps.setObject(i + 1, parameters.get(i));
170             }
171         }
172     }
173 }