/src/common/jdbc/java/jdbcx/EdbcDataSource.java

https://github.com/jansepar/Ingres · Java · 738 lines · 293 code · 105 blank · 340 comment · 63 complexity · d10c7c8c290cafc17830db5c38d5bfe2 MD5 · raw file

  1. /*
  2. ** Copyright (c) 2004 Ingres Corporation
  3. */
  4. package ca.edbc.jdbcx;
  5. /*
  6. ** Name: EdbcDataSource.java
  7. **
  8. ** Description:
  9. ** Implements the EDBC JDBC extension class EdbcDataSource.
  10. **
  11. ** History:
  12. ** 26-Jan-01 (gordy)
  13. ** Created.
  14. ** 18-Apr-01 (gordy)
  15. ** Made fields protected and added getHost() method for sub-
  16. ** classes which need their own connections independent of
  17. ** the standard connection described by the property values.
  18. */
  19. import java.util.Date;
  20. import java.util.Properties;
  21. import java.io.ObjectInputStream;
  22. import java.io.PrintWriter;
  23. import java.io.Serializable;
  24. import java.io.IOException;
  25. import java.sql.Connection;
  26. import java.sql.SQLException;
  27. import javax.naming.Reference;
  28. import javax.naming.Referenceable;
  29. import javax.naming.BinaryRefAddr;
  30. import javax.naming.StringRefAddr;
  31. import javax.naming.NamingException;
  32. import javax.sql.DataSource;
  33. import ca.edbc.jdbc.EdbcConnect;
  34. import ca.edbc.jdbc.EdbcTrace;
  35. import ca.edbc.jdbc.EdbcConst;
  36. /*
  37. ** Name: EdbcDataSource
  38. **
  39. ** Description:
  40. ** EDBC JDBC extension class which implements the JDBC
  41. ** DataSource interface.
  42. **
  43. ** The following data source properties are supported:
  44. **
  45. ** dataSourceName DataSource name.
  46. ** description DataSource description
  47. ** serverName Target machine name.
  48. ** portNumber Numeric port ID.
  49. ** portName Symbolic port ID.
  50. ** databaseName Database.
  51. ** user Username for connection
  52. ** password Password for connection.
  53. ** roleName Role used in DBMS.
  54. ** groupName Group used in DBMS.
  55. ** dbmsUser Username used in DBMS.
  56. ** dbmsPassword Password used in DBMS.
  57. ** connectionPool Server connection pooling ('off' or 'on').
  58. ** autocommitMode Autocommit mode ('dbms', 'single', 'multi').
  59. ** selectLoops Use select loops not cursors ('off' or 'on').
  60. **
  61. ** Interface Methods:
  62. **
  63. ** getConnection Open a new connection.
  64. ** getLoginTimeout Returns login timeout value.
  65. ** setLoginTimeout Set login timeout value.
  66. ** getLogWriter Returns tracing output writer.
  67. ** setLogWriter Set tracing output writer.
  68. **
  69. ** getReference Get JNDI Reference to EdbcDataSource object.
  70. **
  71. ** Public Methods:
  72. **
  73. ** getDataSourceName Property getter/setter methods
  74. ** setDataSourceName
  75. ** getDescription
  76. ** setDescription
  77. ** getServerName
  78. ** setServername
  79. ** getPortNumber
  80. ** setPortNumber
  81. ** getDatabaseName
  82. ** setDatabaseName
  83. ** getUser
  84. ** setUser
  85. ** getPassword
  86. ** setPassword
  87. ** getRoleName
  88. ** setRoleName
  89. ** getGroupName
  90. ** setGroupName
  91. ** getDbmsUser
  92. ** setDbmsUser
  93. ** getDbmsPassword
  94. ** setDbmsPassword
  95. ** getConnectionPool
  96. ** setConnectionPool
  97. ** getAutocommitMode
  98. ** setAutocommitMode
  99. ** getSelectLoops
  100. ** setSelectLoops
  101. **
  102. ** Protected Methods:
  103. **
  104. ** initReference Init a JNDI Reference for EdbcDataSource.
  105. ** getHost Returns target host identifier.
  106. **
  107. ** Private Data:
  108. **
  109. ** dataSourceName Property values
  110. ** description
  111. ** serverName
  112. ** portNumber
  113. ** portName
  114. ** databaseName
  115. ** user
  116. ** password
  117. ** roleName
  118. ** groupName
  119. ** dbmsUser
  120. ** dbmsPassword
  121. ** connectionPool
  122. ** autocommitMode
  123. ** selectLoops
  124. **
  125. ** title Class title for tracing.
  126. ** tr_id Class ID for tracing.
  127. ** trace DS Tracing.
  128. ** conn_trace Connection tracing.
  129. **
  130. ** History:
  131. ** 26-Jan-01 (gordy)
  132. ** Created.
  133. ** 18-Apr-01 (gordy)
  134. ** Made fields protected and added getHost() method for sub-
  135. ** classes which need their own connections independent of
  136. ** the standard connection described by the property values.
  137. */
  138. public class
  139. EdbcDataSource
  140. implements DataSource, Serializable, Referenceable
  141. {
  142. /*
  143. ** Tracing
  144. */
  145. protected String title = "EDBC-DataSource";
  146. protected String tr_id = "EdbcDS";
  147. protected transient TraceDS trace = null;
  148. protected transient TraceDS conn_trace = null;
  149. /*
  150. ** Property values.
  151. */
  152. protected String dataSourceName = null;
  153. protected String description = null;
  154. protected String serverName = null;
  155. protected int portNumber = 0;
  156. protected String portName = null;
  157. protected String databaseName = null;
  158. protected String user = null;
  159. protected String password = null;
  160. protected String roleName = null;
  161. protected String groupName = null;
  162. protected String dbmsUser = null;
  163. protected String dbmsPassword = null;
  164. protected String connectionPool = null;
  165. protected String autocommitMode = null;
  166. protected String selectLoops = null;
  167. /*
  168. ** DataSource info
  169. */
  170. protected int timeout = 0;
  171. /*
  172. ** Name: EdbcDataSource
  173. **
  174. ** Description:
  175. ** Class constructor (needed for object factory).
  176. **
  177. ** Input:
  178. ** None.
  179. **
  180. ** Output:
  181. ** None.
  182. **
  183. ** Returns:
  184. ** None.
  185. **
  186. ** History:
  187. ** 26-Jan-01 (gordy)
  188. ** Created.
  189. */
  190. public
  191. EdbcDataSource()
  192. {
  193. initialize();
  194. }
  195. private void
  196. initialize()
  197. {
  198. trace = new TraceDS( "EDBCX" );
  199. conn_trace = new TraceDS( EdbcTrace.edbcTraceID );
  200. return;
  201. }
  202. /*
  203. ** Property getter/setter methods.
  204. **
  205. ** Each supported property has methods to set and retrieve
  206. ** the property value.
  207. */
  208. public String
  209. getDataSourceName()
  210. { return( dataSourceName ); }
  211. public void
  212. setDataSourceName( String dataSourceName )
  213. { this.dataSourceName = dataSourceName; }
  214. public String
  215. getDescription()
  216. { return( description ); }
  217. public void
  218. setDescription( String description )
  219. { this.description = description; }
  220. public String
  221. getServerName()
  222. { return( serverName ); }
  223. public void
  224. setServerName( String serverName )
  225. { this.serverName = serverName; }
  226. public int
  227. getPortNumber()
  228. { return( portNumber ); }
  229. public void
  230. setPortNumber( int portNumber )
  231. { this.portNumber = portNumber; }
  232. public String
  233. getPortName()
  234. { return( portName ); }
  235. public void
  236. setPortName( String portName )
  237. { this.portName = portName; }
  238. public String
  239. getDatabaseName()
  240. { return( databaseName ); }
  241. public void
  242. setDatabaseName( String databaseName )
  243. { this.databaseName = databaseName; }
  244. public String
  245. getUser()
  246. { return( user ); }
  247. public void
  248. setUser( String user )
  249. { this.user = user; }
  250. public String
  251. getPassword()
  252. { return( password ); }
  253. public void
  254. setPassword( String password )
  255. { this.password = password; }
  256. public String
  257. getRoleName()
  258. { return( roleName ); }
  259. public void
  260. setRoleName( String roleName )
  261. { this.roleName = roleName; }
  262. public String
  263. getGroupName()
  264. { return( groupName ); }
  265. public void
  266. setGroupName( String groupName )
  267. { this.groupName = groupName; }
  268. public String
  269. getDbmsUser()
  270. { return( dbmsUser ); }
  271. public void
  272. setDbmsUser( String dbmsUser )
  273. { this.dbmsUser = dbmsUser; }
  274. public String
  275. getDbmsPassword()
  276. { return( dbmsPassword ); }
  277. public void
  278. setDbmsPassword( String dbmsPassword )
  279. { this.dbmsPassword = dbmsPassword; }
  280. public String
  281. getConnectionPool()
  282. { return( connectionPool ); }
  283. public void
  284. setConnectionPool( String connectionPool )
  285. { this.connectionPool = connectionPool; }
  286. public String
  287. getAutocommitMode()
  288. { return( autocommitMode ); }
  289. public void
  290. setAutocommitMode( String autocommitMode )
  291. { this.autocommitMode = autocommitMode; }
  292. public String
  293. getSelectLoops()
  294. { return( selectLoops ); }
  295. public void
  296. setSelectLoops( String selectLoops )
  297. { this.selectLoops = selectLoops; }
  298. /*
  299. ** Name: getConnection
  300. **
  301. ** Description:
  302. ** Open a new connection and return a JDBC Connection object.
  303. **
  304. ** Input:
  305. ** None.
  306. **
  307. ** Output:
  308. ** None.
  309. **
  310. ** Returns:
  311. ** Connection A new connection object.
  312. **
  313. ** History:
  314. ** 26-Jan-01 (gordy)
  315. ** Created.
  316. */
  317. public Connection
  318. getConnection()
  319. throws SQLException
  320. {
  321. return( getConnection( null, null ) );
  322. } // getConnection
  323. /*
  324. ** Name: getConnection
  325. **
  326. ** Description:
  327. ** Open a new connection, allocate and return a JDBC Connection
  328. ** object.
  329. **
  330. ** Input:
  331. ** username User ID.
  332. ** password Password
  333. **
  334. ** Output:
  335. ** None.
  336. **
  337. ** Returns:
  338. ** Connection A new connection object.
  339. **
  340. ** History:
  341. ** 26-Jan-01 (gordy)
  342. ** Created.
  343. ** 18-Apr-01 (gordy)
  344. ** Extracted host:port formatting to getHost() method.
  345. */
  346. public Connection
  347. getConnection( String user, String password )
  348. throws SQLException
  349. {
  350. Connection conn;
  351. Properties info = new Properties();
  352. String host = getHost();
  353. if ( trace.enabled() ) trace.log(title + ".getConnection(" + user + ")");
  354. if ( user == null ) user = this.user;
  355. if ( password == null ) password = this.password;
  356. if ( databaseName != null )
  357. info.setProperty( EdbcConst.cp_db, databaseName );
  358. if ( user != null )
  359. info.setProperty( EdbcConst.cp_usr, user );
  360. if ( password != null )
  361. info.setProperty( EdbcConst.cp_pwd, password );
  362. if ( roleName != null )
  363. info.setProperty( EdbcConst.cp_role, roleName );
  364. if ( groupName != null )
  365. info.setProperty( EdbcConst.cp_grp, groupName );
  366. if ( dbmsUser != null )
  367. info.setProperty( EdbcConst.cp_dbusr, dbmsUser );
  368. if ( dbmsPassword != null )
  369. info.setProperty( EdbcConst.cp_dbpwd, dbmsPassword );
  370. if ( connectionPool != null )
  371. info.setProperty( EdbcConst.cp_pool, connectionPool );
  372. if ( autocommitMode != null )
  373. info.setProperty( EdbcConst.cp_xacm, autocommitMode );
  374. if ( selectLoops != null )
  375. info.setProperty( EdbcConst.cp_loop, selectLoops );
  376. if ( trace.enabled( 2 ) )
  377. trace.write( tr_id + ".getConnection: " + host + ", '" +
  378. databaseName + "', '" + user + "'" );
  379. conn = new EdbcConnect( host, info, conn_trace, timeout );
  380. if ( trace.enabled() ) trace.log( title + ".getConnection(): " + conn );
  381. return( conn );
  382. } // getConnection
  383. /*
  384. ** Name: getHost
  385. **
  386. ** Description:
  387. ** Returns the target host identifier composed of the
  388. ** machine (serverName) and port (portName or portNumber)
  389. ** formatted as needed for the EDBC Driver.
  390. **
  391. ** Input:
  392. ** None.
  393. **
  394. ** Output:
  395. ** None.
  396. **
  397. ** Returns:
  398. ** String Host ID.
  399. **
  400. ** History:
  401. ** 18-Apr-01 (gordy)
  402. ** Created.
  403. */
  404. protected String
  405. getHost()
  406. {
  407. String host = serverName + ":";
  408. if ( portName != null )
  409. host += portName;
  410. else
  411. host += Integer.toString( portNumber );
  412. return( host );
  413. } // getHost
  414. /*
  415. ** Name: getLoginTimeout
  416. **
  417. ** Description:
  418. ** Returns the login timeout value in seconds.
  419. **
  420. ** Input:
  421. ** None.
  422. **
  423. ** Output:
  424. ** None.
  425. **
  426. ** Returns:
  427. ** int Timeout in seconds.
  428. **
  429. ** History:
  430. ** 26-Jan-01 (gordy)
  431. ** Created.
  432. */
  433. public int
  434. getLoginTimeout()
  435. throws SQLException
  436. {
  437. return( timeout );
  438. } // getLoginTimeout
  439. /*
  440. ** Name: setLoginTimeout
  441. **
  442. ** Description:
  443. ** Set the login timeout value in seconds.
  444. **
  445. ** Input:
  446. ** seconds Timeout value.
  447. **
  448. ** Output:
  449. ** None.
  450. **
  451. ** Returns:
  452. ** void.
  453. **
  454. ** History:
  455. ** 26-Jan-01 (gordy)
  456. ** Created.
  457. */
  458. public void
  459. setLoginTimeout( int seconds )
  460. throws SQLException
  461. {
  462. timeout = seconds;
  463. return;
  464. } // setLoginTimeout
  465. /*
  466. ** Name: getLogWriter
  467. **
  468. ** Description:
  469. ** Returns the DataSource trace output writer.
  470. **
  471. ** Input:
  472. ** None.
  473. **
  474. ** Output:
  475. ** None.
  476. **
  477. ** Returns:
  478. ** PrintWriter Tracing output writer.
  479. **
  480. ** History:
  481. ** 26-Jan-01 (gordy)
  482. ** Created.
  483. */
  484. public PrintWriter
  485. getLogWriter()
  486. throws SQLException
  487. {
  488. return( trace.getWriter() );
  489. } // getLogWriter
  490. /*
  491. ** Name: setLogWriter
  492. **
  493. ** Description:
  494. ** Set the DataSource trace output writer.
  495. **
  496. ** Input:
  497. ** out Tracing output writer.
  498. **
  499. ** Output:
  500. ** None.
  501. **
  502. ** Returns:
  503. ** void.
  504. **
  505. ** History:
  506. ** 26-Jan-01 (gordy)
  507. ** Created.
  508. */
  509. public void
  510. setLogWriter( PrintWriter out )
  511. throws SQLException
  512. {
  513. trace.setWriter( out );
  514. if ( out != null )
  515. {
  516. /*
  517. ** Write initial trace message to log.
  518. */
  519. Date now = new Date();
  520. out.println( EdbcConst.driverID + ": " + now );
  521. }
  522. if ( conn_trace.getWriter() != out )
  523. {
  524. /*
  525. ** Since the current connection tracing may be used
  526. ** by a connection, a new trace object is created
  527. ** instead of just setting the trace log.
  528. */
  529. conn_trace = new TraceDS( EdbcTrace.edbcTraceID );
  530. conn_trace.setWriter( out );
  531. }
  532. return;
  533. } // setLogWriter
  534. /*
  535. ** Name: getReference
  536. **
  537. ** Description:
  538. ** Returns a JNDI Reference to this object. Encodes the property
  539. ** values to be retrieved by the getObjectInstance() method of the
  540. ** class EdbcDSFactory.
  541. **
  542. ** Input:
  543. ** None.
  544. **
  545. ** Output:
  546. ** None.
  547. **
  548. ** Returns:
  549. ** Reference A JNDI Reference.
  550. **
  551. ** History:
  552. ** 26-Jan-01 (gordy)
  553. ** Created.
  554. */
  555. public Reference
  556. getReference()
  557. throws NamingException
  558. {
  559. Reference ref = new Reference( this.getClass().getName(),
  560. "ca.edbc.jdbcx.EdbcDSFactory", null );
  561. initReference( ref );
  562. return( ref );
  563. } // getReference
  564. /*
  565. ** Name: initReference
  566. **
  567. ** Description:
  568. ** Initialize a Reference object to represent this EdbcDataSource.
  569. **
  570. ** Input:
  571. ** None.
  572. **
  573. ** Output:
  574. ** ref JNDI Reference.
  575. **
  576. ** Returns:
  577. ** void.
  578. **
  579. ** History:
  580. ** 26-Jan-01 (gordy)
  581. ** Created.
  582. */
  583. protected void
  584. initReference( Reference ref )
  585. {
  586. if ( dataSourceName != null )
  587. ref.add( new StringRefAddr( "dataSourceName", dataSourceName ) );
  588. if ( description != null )
  589. ref.add( new StringRefAddr( "description", description ) );
  590. if ( serverName != null )
  591. ref.add( new StringRefAddr( "serverName", serverName ) );
  592. if ( portName != null )
  593. ref.add( new StringRefAddr( "portName", portName ) );
  594. if ( databaseName != null )
  595. ref.add( new StringRefAddr( "databaseName", databaseName ) );
  596. if ( user != null )
  597. ref.add( new StringRefAddr( "user", user ) );
  598. if ( password != null )
  599. ref.add( new StringRefAddr( "password", password ) );
  600. if ( roleName != null )
  601. ref.add( new StringRefAddr( "roleName", roleName ) );
  602. if ( groupName != null )
  603. ref.add( new StringRefAddr( "groupName", groupName ) );
  604. if ( dbmsUser != null )
  605. ref.add( new StringRefAddr( "dbmsUser", dbmsUser ) );
  606. if ( dbmsPassword != null )
  607. ref.add( new StringRefAddr( "dbmsPassword", dbmsPassword ) );
  608. if ( connectionPool != null )
  609. ref.add( new StringRefAddr( "connectionPool", connectionPool ) );
  610. if ( autocommitMode != null )
  611. ref.add( new StringRefAddr( "autocommitMode", autocommitMode ) );
  612. if ( selectLoops != null )
  613. ref.add( new StringRefAddr( "selectLoops", selectLoops ) );
  614. if ( portNumber > 0 )
  615. {
  616. byte ival[] = new byte[ 4 ];
  617. ival[0] = (byte)portNumber;
  618. ival[1] = (byte)(portNumber >> 8);
  619. ival[2] = (byte)(portNumber >> 16);
  620. ival[3] = (byte)(portNumber >> 24);
  621. ref.add( new BinaryRefAddr( "portNumber", ival ) );
  622. }
  623. return;
  624. } // initReference
  625. private void
  626. readObject( ObjectInputStream in )
  627. throws IOException, ClassNotFoundException
  628. {
  629. in.defaultReadObject();
  630. initialize();
  631. return;
  632. }
  633. } // class EdbcDataSource