001 /** 002 * Copyright (c) 2003 Daffodil Software Ltd all rights reserved, 003 * Modifications Copyright (c) 2008 Regiscope Digital Imaging Co, LLC, All rights reserved. 004 * This program is free software; you can redistribute it and/or modify 005 * it under the terms of version 2 of the GNU General Public License as 006 * published by the Free Software Foundation. 007 * There are special exceptions to the terms and conditions of the GPL 008 * as it is applied to this software. See the GNU General Public License for more details. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program; if not, write to the Free Software 017 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 018 */ 019 020 021 package org.dbreplicator.replication; 022 023 import javax.sql.*; 024 import java.lang.reflect.*; 025 import java.sql.*; 026 027 028 public class DBDataSource { 029 private String dataBaseName, 030 user, 031 password, 032 dBServerName, 033 dBPortNo, 034 vendorName, 035 connectionMode, 036 databaseName, 037 hostName, 038 portNo, 039 Daffodil_ProductName = "DaffodilDB", 040 Oracle_ProductName = "Oracle", 041 SqlServer_ProductName = "Microsoft SQL Server", 042 PointBase_ProductName = "PointBase", 043 Cloudscape_ProductName = "Apache Derby", 044 PostgreSQL_ProductName = "PostgreSQL", 045 DB2_ProductName = "DB2/NT", 046 DB2_6000_ProductName = "DB2/6000", 047 DB2_AS_400_ProductName = "DB2 UDB", 048 Sybase_ASE = "Adaptive Server Enterprise", 049 Sybase_ASA = "Adaptive Server AnyWhere", 050 FireBird_ProductName = "Firebird" 051 ; 052 public DBDataSource(String dataBaseName0, String user0, String password0, String dBServerName0,String dBPortNo0,String vendorName0) { 053 dataBaseName =dataBaseName0; 054 user =user0; 055 password =password0; 056 dBServerName=dBServerName0; 057 dBPortNo=dBPortNo0; 058 vendorName =vendorName0; 059 } 060 061 062 public DataSource getDataSource() throws 063 RepException { 064 DataSource dataSource = null; 065 Class calass = null; 066 try { 067 System.out.println(System.getProperty("java.class.path")); 068 if (vendorName.equalsIgnoreCase(Daffodil_ProductName)) { 069 if (connectionMode.startsWith("Embedded")) { 070 calass = Class.forName("in.co.daffodil.db.jdbc.RmiDaffodilDBDataSource"); 071 dataSource = (DataSource) (calass.newInstance()); 072 setDataSrcPropDaffoEmbedded(calass, dataSource, hostName, databaseName, user, password); 073 } 074 else { 075 calass = Class.forName("in.co.daffodil.db.RMI.RMIDaffodilDBDataSource"); 076 dataSource = (DataSource) (calass.newInstance()); 077 setDataSrcPropDaffoServer(calass, dataSource, hostName, databaseName,portNo, user, password); 078 } 079 080 } 081 else if (vendorName.startsWith("M")|| vendorName.startsWith("SQL")) { 082 calass = Class.forName("com.microsoft.jdbcx.sqlserver.SQLServerDataSource"); 083 dataSource = (DataSource) (calass.newInstance()); 084 System.out.println(" dataSource :: "+dataSource.getClass()); 085 setDataSourceProperties(calass, dataSource, hostName, dataBaseName,portNo, user, password); 086 } 087 else if (vendorName.equalsIgnoreCase(Oracle_ProductName)) { 088 calass = Class.forName("oracle.jdbc.pool.OracleDataSource"); 089 dataSource = (DataSource) (calass.newInstance()); 090 setDataSourceProperties(calass, dataSource, hostName, dataBaseName,portNo, user, password); 091 } 092 else if (vendorName.startsWith("DB2")) { 093 calass = Class.forName("com.ibm.db2.jdbc.db2connectionPoolDataSource"); 094 dataSource = (DataSource) (calass.newInstance()); 095 setDataSourceProperties(calass, dataSource, hostName, dataBaseName,portNo, user, password); 096 } 097 else if (vendorName.equalsIgnoreCase(Cloudscape_ProductName)) { 098 calass = Class.forName("org.apache.derby.jdbc.ClientDataSource"); 099 dataSource = (DataSource) (calass.newInstance()); 100 setDataSourceProperties(calass, dataSource, hostName, dataBaseName,portNo, user, password); 101 } 102 else if (vendorName.equalsIgnoreCase(PostgreSQL_ProductName)) { 103 calass = Class.forName(" org.postgresql.jdbc2.optional.PoolingDataSource"); 104 dataSource = (DataSource) (calass.newInstance()); 105 setDataSourceProperties(calass, dataSource, hostName, dataBaseName,portNo, user, password); 106 } 107 else if (vendorName.equalsIgnoreCase(FireBird_ProductName)) { 108 calass = Class.forName(" org.firebirdsql.pool.FBWrappingDataSource"); 109 dataSource = (DataSource) (calass.newInstance()); 110 setDataSourceProperties(calass, dataSource, hostName, dataBaseName,portNo, user, password); 111 } 112 else if (vendorName.equalsIgnoreCase(FireBird_ProductName)) { 113 calass = Class.forName("com.evermind.sql.DriverManagerDataSource"); 114 dataSource = (DataSource) (calass.newInstance()); 115 setDataSourceProperties(calass, dataSource, hostName, dataBaseName,portNo, user, password); 116 } 117 118 119 } 120 catch (Exception e) { 121 System.err.println("PROBLE IN MAKING THE INSTANCE OF DATASOURCE CLASS"); 122 e.printStackTrace(); 123 } 124 return dataSource; 125 } 126 127 private void setDataSourcePropertiesSQlServer(Class calass, DataSource dataSoruce, 128 String hostName, String dataBaseName, 129 String user, 130 String password) throws RepException { 131 try { 132 // Set Host Name 133 calass.getMethod("setServerName", new Class[] {String.class}).invoke(dataSoruce, new Object[] {hostName}); 134 // Set the Database Name 135 calass.getMethod("setDatabaseName", new Class[] {String.class}).invoke(dataSoruce, new Object[] {dataBaseName}); 136 // Set user Name 137 calass.getMethod("setUser", new Class[] {String.class}).invoke(dataSoruce, new Object[] {user}); 138 // Set password 139 calass.getMethod("setPassword", new Class[] {String.class}).invoke(dataSoruce, new Object[] {password}); 140 } 141 catch (InvocationTargetException ex) { 142 throw new RepException("REP001", new Object[] {ex.getMessage()}); 143 } 144 catch (IllegalArgumentException ex) { 145 throw new RepException("REP001", new Object[] {ex.getMessage()}); 146 } 147 catch (IllegalAccessException ex) { 148 throw new RepException("REP001", new Object[] {ex.getMessage()}); 149 } 150 catch (SecurityException ex) { 151 throw new RepException("REP001", new Object[] {ex.getMessage()}); 152 } 153 catch (NoSuchMethodException ex) { 154 throw new RepException("REP001", new Object[] {ex.getMessage()}); 155 } 156 157 } 158 159 160 private void setDataSourceProperties(Class calass, DataSource dataSoruce, 161 String hostName, String dataBaseName, 162 String portNumber, String user, 163 String password) throws RepException { 164 try { 165 166 Method methodName = calass.getMethod("setServerName", new Class[] {String.class}); 167 methodName.invoke(dataSoruce, new Object[] {"sube"}); 168 System.out.println(" Set Server Name :: "+methodName); 169 170 171 methodName = calass.getMethod("setDatabaseName", new Class[] {String.class}); 172 System.out.println(" Database Name :: "+methodName); 173 methodName.invoke(dataSoruce, new Object[] {dataBaseName}); 174 175 176 methodName = calass.getMethod("setUser", new Class[] {String.class}); 177 System.out.println(" setUser : "+methodName); 178 methodName.invoke(dataSoruce, new Object[] {user}); 179 180 methodName = calass.getMethod("setPassword", new Class[] {String.class}); 181 System.out.println(" setPassword :: "+methodName); 182 methodName.invoke(dataSoruce, new Object[] {password}); 183 } 184 catch (InvocationTargetException ex) { 185 throw new RepException("REP001", new Object[] {ex.getMessage()}); 186 } 187 catch (IllegalArgumentException ex) { 188 throw new RepException("REP001", new Object[] {ex.getMessage()}); 189 } 190 catch (IllegalAccessException ex) { 191 throw new RepException("REP001", new Object[] {ex.getMessage()}); 192 } 193 catch (SecurityException ex) { 194 throw new RepException("REP001", new Object[] {ex.getMessage()}); 195 } 196 catch (NoSuchMethodException ex) { 197 throw new RepException("REP001", new Object[] {ex.getMessage()}); 198 } 199 200 } 201 202 203 private void setDataSrcPropDaffoServer(Class calass, DataSource dataSoruce, 204 String hostName, String dataBaseName, 205 String portNumber, String user, 206 String password) throws RepException { 207 try { 208 // Set the HostName 209 calass.getMethod("setHostName", new Class[] {String.class}).invoke(dataSoruce, new Object[] {hostName}); 210 //Set DatabaseName 211 calass.getMethod("setDatabaseName", new Class[] {String.class}).invoke(dataSoruce, new Object[] {dataBaseName}); 212 //Set port Number 213 calass.getMethod("setPortNumber", new Class[] {String.class}).invoke(dataSoruce, new Object[] {portNumber}); 214 //Set user 215 calass.getMethod("setUser", new Class[] {String.class}).invoke(dataSoruce, new Object[] {user}); 216 //Set password 217 calass.getMethod("setPassword", new Class[] {String.class}).invoke(dataSoruce, new Object[] {password}); 218 } 219 catch (InvocationTargetException ex) { 220 throw new RepException("REP001", new Object[] {ex.getMessage()}); 221 } 222 catch (IllegalArgumentException ex) { 223 RepConstants.writeERROR_FILE(ex); 224 throw new RepException("REP001", new Object[] {ex.getMessage()}); 225 } 226 catch (IllegalAccessException ex) { 227 RepConstants.writeERROR_FILE(ex); 228 throw new RepException("REP001", new Object[] {ex.getMessage()}); 229 } 230 catch (SecurityException ex) { 231 RepConstants.writeERROR_FILE(ex); 232 throw new RepException("REP001", new Object[] {ex.getMessage()}); 233 } 234 catch (NoSuchMethodException ex) { 235 RepConstants.writeERROR_FILE(ex); 236 throw new RepException("REP001", new Object[] {ex.getMessage()}); 237 } 238 239 } 240 241 242 private void setDataSrcPropDaffoEmbedded(Class calass, DataSource dataSoruce, 243 String hostName, String dataBaseName, 244 String user, 245 String password) throws RepException { 246 try { 247 // Set Server Name 248 calass.getMethod("setServerName", new Class[] {String.class}).invoke(dataSoruce, new Object[] {hostName}); 249 // Set Database Name 250 calass.getMethod("setDatabaseName", new Class[] {String.class}).invoke(dataSoruce, new Object[] {dataBaseName}); 251 // Set user Name 252 calass.getMethod("setUser", new Class[] {String.class}).invoke(dataSoruce, new Object[] {user}); 253 // Set password 254 calass.getMethod("setPassword", new Class[] {String.class}).invoke(dataSoruce, new Object[] {password}); 255 } 256 catch (InvocationTargetException ex) { 257 throw new RepException("REP001", new Object[] {ex.getMessage()}); 258 } 259 catch (IllegalArgumentException ex) { 260 throw new RepException("REP001", new Object[] {ex.getMessage()}); 261 } 262 catch (IllegalAccessException ex) { 263 throw new RepException("REP001", new Object[] {ex.getMessage()}); 264 } 265 catch (SecurityException ex) { 266 throw new RepException("REP001", new Object[] {ex.getMessage()}); 267 } 268 catch (NoSuchMethodException ex) { 269 throw new RepException("REP001", new Object[] {ex.getMessage()}); 270 } 271 272 } 273 274 275 public static void main(String[] args) { 276 277 try { 278 279 280 DBDataSource dbs = new DBDataSource("dataBaseName0", " user0", "password0", "dBServerName0","dBPortNo0","vendorName0"); 281 DataSource ds = dbs.getDataSource(); 282 283 System.out.println(" User Name = "+ds.getConnection().getMetaData().getUserName()); 284 Connection conn =ds.getConnection(); 285 /* bjt */ 286 System.out.println(" Connection Timeout = "+ds.getLoginTimeout()); 287 System.out.println(" Connection "+conn); 288 Statement stmt =conn.createStatement(); 289 System.out.println(" Statement : "+stmt); 290 stmt.execute("Create table t(c1 integer)"); 291 System.out.println(" Table created successfully "); 292 stmt.execute(" Drop table t"); 293 System.out.println(" Table dropped successfully "); 294 295 } 296 catch (Exception ex) { 297 ex.printStackTrace(); 298 } 299 300 } 301 302 303 }

