1 //$Id: InjectedDataSourceConnectionProvider.java 11171 2007-02-08 03:40:51Z epbernard $
2 package org.hibernate.ejb.connection;
3
4 import java.util.Properties;
5 import java.sql.Connection;
6 import java.sql.SQLException;
7 import javax.sql.DataSource;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.hibernate.HibernateException;
12 import org.hibernate.cfg.Environment;
13 import org.hibernate.connection.DatasourceConnectionProvider;
14
15 /**
16 * A connection provider that uses an injected <tt>DataSource</tt>.
17 * Setters has to be called before configure()
18 *
19 * @author Emmanuel Bernard
20 * @see org.hibernate.connection.ConnectionProvider
21 */
22 public class InjectedDataSourceConnectionProvider extends DatasourceConnectionProvider {
23 //TODO make datasource connection provider properties protected in 3.3
24 private String user;
25 private String pass;
26
27 private static final Log log = LogFactory.getLog( InjectedDataSourceConnectionProvider.class );
28
29 public void setDataSource(DataSource ds) {
30 super.setDataSource( ds );
31 }
32
33 public void configure(Properties props) throws HibernateException {
34 user = props.getProperty( Environment.USER );
35 pass = props.getProperty( Environment.PASS );
36
37 if ( getDataSource() == null ) throw new HibernateException( "No datasource provided" );
38 log.info( "Using provided datasource" );
39 }
40
41 @Override
42 public Connection getConnection() throws SQLException {
43 if (user != null || pass != null) {
44 return getDataSource().getConnection(user, pass);
45 }
46 else {
47 return getDataSource().getConnection();
48 }
49 }
50 }