1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.dbcp;
  17. import java.sql.Connection;
  18. import java.sql.SQLException;
  19. import javax.sql.DataSource;
  20. /**
  21. * A {@link DataSource}-based implementation of {@link ConnectionFactory}.
  22. *
  23. * @author Rodney Waldhoff
  24. * @version $Revision: 1.7 $ $Date: 2004/02/28 12:18:17 $
  25. */
  26. public class DataSourceConnectionFactory implements ConnectionFactory {
  27. public DataSourceConnectionFactory(DataSource source) {
  28. this(source,null,null);
  29. }
  30. public DataSourceConnectionFactory(DataSource source, String uname, String passwd) {
  31. _source = source;
  32. _uname = uname;
  33. _passwd = passwd;
  34. }
  35. public Connection createConnection() throws SQLException {
  36. if(null == _uname && null == _passwd) {
  37. return _source.getConnection();
  38. } else {
  39. return _source.getConnection(_uname,_passwd);
  40. }
  41. }
  42. protected String _uname = null;
  43. protected String _passwd = null;
  44. protected DataSource _source = null;
  45. }