View Javadoc

1   /*
2    * Copyright (c) 2005, Henri Yandell
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or 
6    * without modification, are permitted provided that the 
7    * following conditions are met:
8    * 
9    * + Redistributions of source code must retain the above copyright notice, 
10   *   this list of conditions and the following disclaimer.
11   * 
12   * + Redistributions in binary form must reproduce the above copyright notice, 
13   *   this list of conditions and the following disclaimer in the documentation 
14   *   and/or other materials provided with the distribution.
15   * 
16   * + Neither the name of Simple-JNDI nor the names of its contributors 
17   *   may be used to endorse or promote products derived from this software 
18   *   without specific prior written permission.
19   * 
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
23   * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
30   * POSSIBILITY OF SUCH DAMAGE.
31   */
32  
33  package org.osjava.sj.loader;
34  
35  import java.sql.Connection;
36  import java.sql.SQLException;
37  import javax.sql.DataSource;
38  import java.sql.DriverManager;
39  import java.io.PrintWriter;
40  import java.util.Properties;
41  
42  /***
43   * A basic implementation of a DataSource. 
44   */
45  public class SJDataSource implements DataSource {
46  
47      private PrintWriter pw;
48      private String username;
49      private String password;
50      private String url;
51      private String driver;
52  
53      // for pooling
54      private Properties properties;
55  
56      /***
57       * if a connection pool has been built, its url is stored in here
58       */
59      private String poolUrl = null;
60  
61      public SJDataSource(String driver, String url, String username, String password, Properties properties) {
62          ensureLoaded(driver);
63          this.driver = driver;
64          this.url = url;
65          this.pw = new PrintWriter(System.err);
66          this.username = username;
67          this.password = password;
68          this.properties = properties;
69      }
70  
71      // Method from Apache Commons DbUtils
72      private static boolean ensureLoaded(String name) {
73          try {
74              Class.forName(name).newInstance();
75              return true;
76          } catch (Exception e) {
77              return false;
78          }
79      }
80  
81      public Connection getConnection() throws SQLException {
82          return this.getConnection(this.username, this.password);
83      }
84  
85      /***
86       * returns a connection to the database specified in the properties and
87       * creates a connection pool, if neccessary 
88       */
89      public Connection getConnection(String username, String password) throws SQLException {
90          String tmpUrl = this.url;
91  
92          String pool = properties.getProperty("pool");
93          if (pool != null) {  // we want a connection name named like the pool property
94              synchronized (SJDataSource.class) {
95                  if (poolUrl == null) {  // we didn't create a connection pool already, so do it now
96                      PoolSetup.setupConnection(pool, url, username, password, properties);
97                      poolUrl = PoolSetup.getUrl(pool);
98                  }
99              }
100             tmpUrl = poolUrl;  // url is now a pooling link
101         }
102 
103         if(username == null || password == null) {
104             return DriverManager.getConnection(tmpUrl);
105         }
106         return DriverManager.getConnection(tmpUrl, username, password);
107     }
108 
109     public PrintWriter getLogWriter() throws SQLException {
110         return pw;
111     }
112 
113     public int getLoginTimeout() throws SQLException {
114         return 0;
115     }
116 
117     public void setLogWriter(PrintWriter pw) throws SQLException {
118         this.pw = pw;
119     }
120 
121     public void setLoginTimeout(int timeout) throws SQLException {
122         // ignored
123     }
124 
125     public String toString() {
126         return driver + "::::" + url + "::::" + username;
127     }
128 
129     public boolean equals(Object obj) {
130         if(obj == null) { 
131             return false;
132         }
133         if(obj.getClass() != this.getClass()) {
134             return false;
135         }
136         SJDataSource other = (SJDataSource) obj;
137 
138         return other.url.equals(this.url) &&
139                other.driver.equals(this.driver) &&
140                other.username.equals(this.username);
141     }
142 
143     public int hashCode() {
144         return this.url.hashCode() & this.username.hashCode() & this.driver.hashCode();
145     }
146 
147     // Added by JDK 1.6 via java.sql.Wrapper
148     public boolean isWrapperFor(Class<?> iface) throws SQLException {
149          return false;
150     }
151 
152     // Added by JDK 1.6 via java.sql.Wrapper
153     public <T>  T unwrap(Class<T> iface) throws SQLException {
154         throw new SQLException("This object is not a wrapper");
155     }
156 
157 }
158