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.io.PrintWriter;
20  import java.sql.Connection;
21  import java.sql.DriverManager;
22  import java.sql.SQLException;
23  
24  import javax.sql.DataSource;
25  
26  import org.apache.commons.jelly.tags.Resources;
27  import org.apache.commons.jelly.util.ClassLoaderUtils;
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  
31  /***
32   * <p>A simple <code>DataSource</code> wrapper for the standard
33   * <code>DriverManager</code> class.
34   *
35   * @author Hans Bergsten
36   */
37  public class DataSourceWrapper implements DataSource {
38  
39      /*** The Log to which logging calls will be made. */
40      private static final Log log = LogFactory.getLog(DataSourceWrapper.class);
41  
42      private String driverClassName;
43      private String jdbcURL;
44      private String userName;
45      private String password;
46  
47      public void setDriverClassName(String driverClassName)
48          throws ClassNotFoundException, InstantiationException, IllegalAccessException {
49  
50          if (log.isDebugEnabled()) {
51              log.debug("Loading JDBC driver: [" + driverClassName + "]");
52          }
53  
54          this.driverClassName = driverClassName;
55          ClassLoaderUtils.getClassLoader(getClass()).loadClass(driverClassName).newInstance();
56      }
57  
58      public void setJdbcURL(String jdbcURL) {
59          this.jdbcURL = jdbcURL;
60      }
61  
62      public void setUserName(String userName) {
63          this.userName = userName;
64      }
65  
66      public void setPassword(String password) {
67          this.password = password;
68      }
69  
70      /***
71       * Returns a Connection using the DriverManager and all
72       * set properties.
73       */
74      public Connection getConnection() throws SQLException {
75          Connection conn = null;
76          if (userName != null) {
77              if (log.isDebugEnabled()) {
78                  log.debug(
79                      "Creating connection from url: " + jdbcURL + " userName: " + userName);
80              }
81  
82              conn = DriverManager.getConnection(jdbcURL, userName, password);
83          }
84          else {
85              if (log.isDebugEnabled()) {
86                  log.debug("Creating connection from url: " + jdbcURL);
87              }
88  
89              conn = DriverManager.getConnection(jdbcURL);
90          }
91          if (log.isDebugEnabled()) {
92              log.debug(
93                  "Created connection: " + conn );
94          }
95          return conn;
96      }
97  
98      /***
99       * Always throws a SQLException. Username and password are set
100      * in the constructor and can not be changed.
101      */
102     public Connection getConnection(String username, String password)
103         throws SQLException {
104         throw new SQLException(Resources.getMessage("NOT_SUPPORTED"));
105     }
106 
107     /***
108      * Always throws a SQLException. Not supported.
109      */
110     public int getLoginTimeout() throws SQLException {
111         throw new SQLException(Resources.getMessage("NOT_SUPPORTED"));
112     }
113 
114     /***
115      * Always throws a SQLException. Not supported.
116      */
117     public PrintWriter getLogWriter() throws SQLException {
118         throw new SQLException(Resources.getMessage("NOT_SUPPORTED"));
119     }
120 
121     /***
122      * Always throws a SQLException. Not supported.
123      */
124     public void setLoginTimeout(int seconds) throws SQLException {
125         throw new SQLException(Resources.getMessage("NOT_SUPPORTED"));
126     }
127 
128     /***
129      * Always throws a SQLException. Not supported.
130      */
131     public synchronized void setLogWriter(PrintWriter out) throws SQLException {
132         throw new SQLException(Resources.getMessage("NOT_SUPPORTED"));
133     }
134 
135 }