1 21 22 package uk.org.primrose.pool.datasource; 23 24 import uk.org.primrose.pool.jmx.*; 25 import java.io.PrintWriter ; 26 import java.io.IOException ; 27 import java.sql.Connection ; 28 import java.sql.SQLException ; 29 import javax.sql.DataSource ; 30 import javax.naming.*; 31 import java.util.ArrayList ; 32 import uk.org.primrose.vendor.jboss.jndi.FakeSerializableArrayList; 33 34 public class PoolDataSource implements DataSource { 35 36 private String poolName = ""; 37 38 public void setPoolName(String poolName) { 39 this.poolName = poolName; 40 } 41 42 public String getPoolName() { 43 return poolName; 44 } 45 46 49 public Connection getConnection() throws SQLException { 50 Connection conn = null; 51 try { 52 Context ctx = new InitialContext(); 53 ArrayList al = null; 54 Object o = null; 55 56 try { 57 o = ctx.lookup("java:comp/env/masterPool"); 58 } catch (Exception e) { 59 o = ctx.lookup("masterPool"); 60 } 61 62 String s = o.getClass().getName(); 63 if (o instanceof ArrayList ) { 64 al = (ArrayList )o; 65 } else if (s.indexOf("FakeSerializableArrayList") != -1) { 66 FakeSerializableArrayList alf = (FakeSerializableArrayList)o; 67 al = alf.initialize(); 68 } else if (s.indexOf("BindPrimroseFactory") != -1) { 69 if (s.indexOf("resin") != -1) { 70 uk.org.primrose.vendor.resin.jndi.BindPrimroseFactory brf = (uk.org.primrose.vendor.resin.jndi.BindPrimroseFactory)o; 71 al = brf.getPools(); 72 } else if (s.indexOf("jetty") != -1) { 73 if (s.indexOf("BindPrimroseFactoryV6") != -1) { 74 uk.org.primrose.vendor.jetty.BindPrimroseFactoryV6 brf = (uk.org.primrose.vendor.jetty.BindPrimroseFactoryV6)o; 75 al = brf.getPools(); 76 } else { 77 uk.org.primrose.vendor.jetty.BindPrimroseFactory brf = (uk.org.primrose.vendor.jetty.BindPrimroseFactory)o; 78 al = brf.getPools(); 79 80 } 81 } 82 83 } else { 84 throw new SQLException ("Cannot get list of pools available, so cannot return connection"); 85 } 86 87 94 97 if (al.size() == 0) { 98 throw new SQLException ("No Pool Queues have been loaded - primrose initialisation problem ?"); 99 } 100 101 for (int i = 0; i < al.size(); i++) { 102 Queue q = (Queue)al.get(i); 103 if (q.getName().equals(poolName)) { 104 conn = q.getConnection(); 105 } 106 } 107 108 if (conn == null) { 109 throw new SQLException ("Cannot find valid connection object from pools - perhaps db is down ?"); 110 111 } 112 return conn; 113 } catch (Exception e) { 114 e.printStackTrace(System.err); 115 throw new SQLException ("Error retrieving list of pools available, or extracting valid connection - so cannot return connection"); 116 } 117 118 } 119 120 123 public Connection getConnection(String username, String password) throws SQLException { 124 return getConnection(); 125 } 126 127 130 public int getLoginTimeout() { 131 return -1; 132 } 133 134 137 public PrintWriter getLogWriter() { 138 return null; 139 } 140 141 144 public void setLoginTimeout(int seconds) { 145 146 } 147 148 151 public void setLogWriter(PrintWriter out) { 152 153 } 154 155 } 156 | Popular Tags |