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 javax.sql.DataSource;
20  
21  import org.apache.commons.jelly.JellyTagException;
22  import org.apache.commons.jelly.TagSupport;
23  import org.apache.commons.jelly.XMLOutput;
24  import org.apache.commons.jelly.tags.Resources;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  /***
29   * <p>Tag handler for &lt;SetDataSource&gt; in JSTL, used to create
30   * a simple DataSource for prototyping.</p>
31   *
32   * @author Hans Bergsten
33   * @author Justyna Horwat
34   */
35  public class SetDataSourceTag extends TagSupport {
36  
37      /*** The Log to which logging calls will be made. */
38      private static final Log log = LogFactory.getLog(SetDataSourceTag.class);
39  
40      protected Object dataSource;
41      protected boolean dataSourceSpecified;
42      protected String jdbcURL;
43      protected String driverClassName;
44      protected String userName;
45      protected String password;
46  
47      private String scope = "page";
48      private String var;
49  
50      //**********************************************************************
51      // Constructor and initialization
52  
53      public SetDataSourceTag() {
54      }
55  
56      //*********************************************************************
57      // Accessor methods
58  
59      /**
60       * Sets the scope of the variable to hold the
61       * result.
62       *
63       */
64      public void setScope(String scope) {
65          this.scope = scope;
66      }
67  
68      public void setVar(String var) {
69          this.var = var;
70      }
71  
72      public void setDataSource(Object dataSource) {
73          this.dataSource = dataSource;
74          this.dataSourceSpecified = true;
75      }
76  
77      public void setDriver(String driverClassName) {
78          this.driverClassName = driverClassName;
79      }
80  
81      public void setUrl(String jdbcURL) {
82          this.jdbcURL = jdbcURL;
83      }
84  
85      public void setUser(String userName) {
86          this.userName = userName;
87      }
88  
89      public void setPassword(String password) {
90          this.password = password;
91      }
92  
93      //**********************************************************************
94      // Tag logic
95  
96      public void doTag(XMLOutput output) throws JellyTagException {
97          DataSource ds = null;
98  
99          if (dataSource != null) {
100             ds = DataSourceUtil.getDataSource(dataSource, context);
101         }
102         else {
103             if (dataSourceSpecified) {
104                 throw new JellyTagException(Resources.getMessage("SQL_DATASOURCE_NULL"));
105             }
106 
107             DataSourceWrapper dsw = new DataSourceWrapper();
108             try {
109                 // set driver class iff provided by the tag
110                 if (driverClassName != null) {
111                     dsw.setDriverClassName(driverClassName);
112                 }
113             }
114             catch (Exception e) {
115                 log.error( "Could not load driver class: " + e, e );
116                 throw new JellyTagException(
117                     Resources.getMessage("DRIVER_INVALID_CLASS", e.getMessage()));
118             }
119             dsw.setJdbcURL(jdbcURL);
120             dsw.setUserName(userName);
121             dsw.setPassword(password);
122             ds = (DataSource) dsw;
123         }
124 
125         if (var != null) {
126             context.setVariable(var, ds);
127         }
128         else {
129             context.setVariable("org.apache.commons.jelly.sql.DataSource", ds);
130         }
131     }
132 }