1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
32
33
34
35
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
50
51
52
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
63
64
65
66
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
78
79
80
81
82
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 }