1 21 package net.sf.hajdbc.sql; 22 23 import java.sql.Connection ; 24 25 import javax.naming.Context ; 26 import javax.naming.InitialContext ; 27 import javax.naming.NamingException ; 28 import javax.sql.DataSource ; 29 30 import net.sf.hajdbc.ActiveDatabaseMBean; 31 import net.sf.hajdbc.InactiveDatabaseMBean; 32 import net.sf.hajdbc.Messages; 33 34 39 public class DataSourceDatabase extends AbstractDatabase<DataSource> implements InactiveDataSourceDatabaseMBean 40 { 41 protected String name; 42 43 46 public String getName() 47 { 48 return this.name; 49 } 50 51 55 public void setName(String name) 56 { 57 this.checkDirty(this.name, name); 58 this.name = name; 59 } 60 61 67 public Connection connect(DataSource dataSource) throws java.sql.SQLException 68 { 69 return (this.user != null) ? dataSource.getConnection(this.user, this.password) : dataSource.getConnection(); 70 } 71 72 75 public DataSource createConnectionFactory() 76 { 77 try 78 { 79 Context context = new InitialContext (this.properties); 80 81 Object object = context.lookup(this.name); 82 83 return DataSource.class.cast(object); 84 } 85 catch (ClassCastException e) 86 { 87 throw new IllegalArgumentException (e.toString(), e); 88 } 89 catch (NamingException e) 90 { 91 throw new IllegalArgumentException (Messages.getMessage(Messages.JNDI_LOOKUP_FAILED, this.name), e); 92 } 93 } 94 95 98 public Class <DataSource> getConnectionFactoryClass() 99 { 100 return DataSource.class; 101 } 102 103 106 public Class <? extends ActiveDatabaseMBean> getActiveMBeanClass() 107 { 108 return ActiveDataSourceDatabaseMBean.class; 109 } 110 111 114 public Class <? extends InactiveDatabaseMBean> getInactiveMBeanClass() 115 { 116 return InactiveDataSourceDatabaseMBean.class; 117 } 118 } 119 | Popular Tags |