1 21 package net.sf.hajdbc.sql; 22 23 import java.io.PrintWriter ; 24 import java.sql.SQLException ; 25 26 import javax.naming.Reference ; 27 import javax.naming.Referenceable ; 28 import javax.naming.StringRefAddr ; 29 30 import net.sf.hajdbc.Database; 31 import net.sf.hajdbc.Operation; 32 33 34 38 public class DataSource implements javax.sql.DataSource , Referenceable 39 { 40 41 public static final String DATABASE_CLUSTER = "cluster"; 42 43 private String cluster; 44 private ConnectionFactory<javax.sql.DataSource > connectionFactory; 45 46 50 public String getCluster() 51 { 52 return this.cluster; 53 } 54 55 59 public void setCluster(String cluster) 60 { 61 this.cluster = cluster; 62 } 63 64 67 public final Reference getReference() 68 { 69 Reference ref = new Reference (this.getClass().getName(), DataSourceFactory.class.getName(), null); 70 71 ref.add(new StringRefAddr (DATABASE_CLUSTER, this.cluster)); 72 73 return ref; 74 } 75 76 80 public void setConnectionFactory(ConnectionFactory<javax.sql.DataSource > connectionFactory) 81 { 82 this.connectionFactory = connectionFactory; 83 } 84 85 88 public int getLoginTimeout() throws SQLException 89 { 90 Operation<javax.sql.DataSource , Integer > operation = new Operation<javax.sql.DataSource , Integer >() 91 { 92 public Integer execute(Database database, javax.sql.DataSource dataSource) throws SQLException 93 { 94 return dataSource.getLoginTimeout(); 95 } 96 }; 97 98 return this.connectionFactory.executeReadFromDriver(operation); 99 } 100 101 104 public void setLoginTimeout(final int seconds) throws SQLException 105 { 106 Operation<javax.sql.DataSource , Void > operation = new Operation<javax.sql.DataSource , Void >() 107 { 108 public Void execute(Database database, javax.sql.DataSource dataSource) throws SQLException 109 { 110 dataSource.setLoginTimeout(seconds); 111 112 return null; 113 } 114 }; 115 116 this.connectionFactory.executeWriteToDriver(operation); 117 } 118 119 122 public PrintWriter getLogWriter() throws SQLException 123 { 124 Operation<javax.sql.DataSource , PrintWriter > operation = new Operation<javax.sql.DataSource , PrintWriter >() 125 { 126 public PrintWriter execute(Database database, javax.sql.DataSource dataSource) throws SQLException 127 { 128 return dataSource.getLogWriter(); 129 } 130 }; 131 132 return this.connectionFactory.executeReadFromDriver(operation); 133 } 134 135 138 public void setLogWriter(final PrintWriter writer) throws SQLException 139 { 140 Operation<javax.sql.DataSource , Void > operation = new Operation<javax.sql.DataSource , Void >() 141 { 142 public Void execute(Database database, javax.sql.DataSource dataSource) throws SQLException 143 { 144 dataSource.setLogWriter(writer); 145 146 return null; 147 } 148 }; 149 150 this.connectionFactory.executeWriteToDriver(operation); 151 } 152 153 156 public java.sql.Connection getConnection() throws SQLException 157 { 158 Operation<javax.sql.DataSource , java.sql.Connection > operation = new Operation<javax.sql.DataSource , java.sql.Connection >() 159 { 160 public java.sql.Connection execute(Database database, javax.sql.DataSource dataSource) throws SQLException 161 { 162 return dataSource.getConnection(); 163 } 164 }; 165 166 return new Connection<javax.sql.DataSource >(this.connectionFactory, operation, new FileSupportImpl()); 167 } 168 169 172 public java.sql.Connection getConnection(final String user, final String password) throws SQLException 173 { 174 Operation<javax.sql.DataSource , java.sql.Connection > operation = new Operation<javax.sql.DataSource , java.sql.Connection >() 175 { 176 public java.sql.Connection execute(Database database, javax.sql.DataSource dataSource) throws SQLException 177 { 178 return dataSource.getConnection(user, password); 179 } 180 }; 181 182 return new Connection<javax.sql.DataSource >(this.connectionFactory, operation, new FileSupportImpl()); 183 } 184 } 185 | Popular Tags |