View Javadoc
1   /*
2    * Copyright (C) 2003-2012 David E. Berry
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this library; if not, write to the Free Software
16   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17   *
18   * A copy of the GNU Lesser General Public License may also be found at
19   * http://www.gnu.org/licenses/lgpl.txt
20   */
21  package org.synchronoss.cpo.jdbc;
22  
23  import org.synchronoss.cpo.CpoException;
24  import org.synchronoss.cpo.helper.CpoClassLoader;
25  
26  import javax.sql.DataSource;
27  import java.sql.*;
28  import java.util.Properties;
29  
30  /**
31   * Collects the info required to instantiate a DataSource from a JDBC Driver
32   *
33   * Provides the DataSourceInfo factory method getDataSource which instantiates the DataSource
34   *
35   * @author dberry
36   */
37  public class DriverJdbcDataSourceInfo extends AbstractJdbcDataSource {
38  
39    private static final int URL_CONNECTION = 1;
40    private static final int URL_PROPS_CONNECTION = 2;
41    private static final int URL_USER_PASSWORD_CONNECTION = 3;
42    private int connectionType = 0;
43    private String url = null;
44    private String username = null;
45    private String password = null;
46    private Properties properties = null;
47  
48    /**
49     * Creates a DriverJdbcDataSourceInfo from a Jdbc Driver
50     *
51     * @param driver The text name of the driver
52     * @param url - The url that points to the database.
53     */
54    public DriverJdbcDataSourceInfo(String driver, String url) throws CpoException {
55      super(url);
56      loadDriver(driver);
57      connectionType = URL_CONNECTION;
58      this.url = url;
59    }
60  
61    /**
62     * Creates a DriverJdbcDataSourceInfo from a Jdbc Driver
63     *
64     * @param driver The text name of the driver
65     * @param url - The url that points to the database.
66     * @param properties - The connection properties for connecting to the database
67     */
68    public DriverJdbcDataSourceInfo(String driver, String url, Properties properties) throws CpoException {
69      super(url, properties);
70      loadDriver(driver);
71      connectionType = URL_PROPS_CONNECTION;
72      this.url = url;
73      this.properties = properties;
74    }
75  
76    /**
77     * Creates a DriverJdbcDataSourceInfo from a Jdbc Driver
78     *
79     * @param driver The text name of the driver
80     * @param url - The url that points to the database.
81     * @param username - The username for connecting to the database
82     * @param password - The password for connectinf to the database
83     */
84    public DriverJdbcDataSourceInfo(String driver, String url, String username, String password) throws CpoException {
85      super(url + username);
86      loadDriver(driver);
87      connectionType = URL_USER_PASSWORD_CONNECTION;
88      this.url = url;
89      this.username = username;
90      this.password = password;
91    }
92  
93    @Override
94    protected DataSource createDataSource() throws CpoException {
95      return this;
96    }
97  
98    @Override
99    public Connection getConnection() throws SQLException {
100     return makeNewConnection();
101   }
102 
103   private Connection makeNewConnection() throws SQLException {
104     Connection connection = null;
105     switch (connectionType) {
106       case DriverJdbcDataSourceInfo.URL_CONNECTION:
107         connection = DriverManager.getConnection(url);
108         break;
109       case DriverJdbcDataSourceInfo.URL_PROPS_CONNECTION:
110         connection = DriverManager.getConnection(url, properties);
111         break;
112       case DriverJdbcDataSourceInfo.URL_USER_PASSWORD_CONNECTION:
113         connection = DriverManager.getConnection(url, username, password);
114         break;
115       default:
116         throw new SQLException("Invalid Connection Type");
117     }
118     return connection;
119   }
120 
121   @Override
122   public synchronized String toString() {
123     StringBuilder info = new StringBuilder();
124     info.append("JdbcDataSource(");
125     info.append(getDataSourceName());
126     info.append(")");
127     return (info.toString());
128   }
129 
130   private void loadDriver(String driver) throws CpoException {
131     try {
132       CpoClassLoader.forName(driver);
133     } catch (ClassNotFoundException cnfe) {
134       throw new CpoException("Could Not Load Driver" + driver);
135     }
136   }
137 }