1 25 package org.objectweb.jonas.jdbc; 26 27 import java.io.PrintWriter ; 28 import java.io.Serializable ; 29 30 import java.sql.Connection ; 31 import java.sql.SQLException ; 32 33 import javax.naming.Reference ; 34 35 import javax.resource.Referenceable ; 36 import javax.resource.ResourceException ; 37 import javax.resource.spi.ConnectionManager ; 38 39 import org.objectweb.util.monolog.api.BasicLevel; 40 41 46 public class DataSourceImpl implements javax.sql.DataSource , Serializable , Referenceable { 47 48 private ConnectionManager cm; 49 50 private ManagedConnectionFactoryImpl mcf; 51 52 private PrintWriter pw; 53 54 private Reference reference; 55 56 int loginTimeout = 0; 57 58 61 private final boolean isDebugOn; 62 63 public DataSourceImpl(ManagedConnectionFactoryImpl _mcf, ConnectionManager _cm) { 64 if (_cm == null) { 65 cm = new ConnectionManagerImpl(); 66 } else { 67 cm = _cm; 68 } 69 mcf = _mcf; 70 isDebugOn = mcf.trace.isLoggable(BasicLevel.DEBUG); 71 } 72 73 public Connection getConnection() throws SQLException { 74 if (isDebugOn) { 75 mcf.trace.log(BasicLevel.DEBUG, ""); 76 } 77 try { 78 Connection con = (Connection ) cm.allocateConnection(mcf, null); 79 if (con == null) { 80 SQLException se = new SQLException ("Null connection object returned"); 81 throw se; 82 } 83 return con; 84 } catch (ResourceException re) { 85 throw new SQLException (re.getMessage()); 86 } 87 } 88 89 public Connection getConnection(String user, String pwd) throws SQLException { 90 if (isDebugOn) { 91 mcf.trace.log(BasicLevel.DEBUG, "" + user); 92 } 93 try { 94 ConnectionRequestInfoImpl info = new ConnectionRequestInfoImpl(user, pwd); 95 Connection con = (Connection ) cm.allocateConnection(mcf, info); 96 if (con == null) { 97 SQLException se = new SQLException ("Null connection object returned"); 98 throw se; 99 } 100 return con; 101 } catch (ResourceException re) { 102 throw new SQLException (re.getMessage()); 103 } 104 } 105 106 public int getLoginTimeout() throws SQLException { 107 return loginTimeout; 108 } 109 110 public PrintWriter getLogWriter() throws SQLException { 111 return pw; 112 } 113 114 public Reference getReference() { 115 return reference; 116 } 117 118 public void setLoginTimeout(int _loginTimeout) throws SQLException { 119 loginTimeout = _loginTimeout; 120 } 121 122 public void setLogWriter(PrintWriter _pw) throws SQLException { 123 pw = _pw; 124 } 125 126 public void setReference(Reference _ref) { 127 reference = _ref; 128 } 129 130 131 132 public String getMapperName() { 133 String mn = mcf.getMapperName(); 134 if (isDebugOn) { 135 mcf.trace.log(BasicLevel.DEBUG, mn); 136 } 137 return mn; 138 } 139 140 } 141 | Popular Tags |