1 9 package com.ziclix.python.sql.connect; 10 11 import java.sql.*; 12 import java.util.*; 13 import java.lang.reflect.Field ; 14 import javax.sql.*; 15 import javax.naming.*; 16 17 import org.python.core.*; 18 import com.ziclix.python.sql.*; 19 import com.ziclix.python.sql.util.*; 20 21 31 public class Lookup extends PyObject { 32 33 private static final PyString _doc = new PyString("establish a connection through a JNDI lookup"); 34 35 38 public Lookup() { 39 } 40 41 47 public PyObject __findattr__(String name) { 48 49 if ("__doc__".equals(name)) { 50 return _doc; 51 } 52 53 return super.__findattr__(name); 54 } 55 56 63 public PyObject __call__(PyObject[] args, String [] keywords) { 64 65 Object ref = null; 66 Connection connection = null; 67 Hashtable env = new Hashtable(); 68 69 PyArgParser parser = new PyArgParser(args, keywords); 71 Object jndiName = parser.arg(0).__tojava__(String .class); 72 73 if ((jndiName == null) || (jndiName == Py.NoConversion)) { 74 throw zxJDBC.makeException(zxJDBC.DatabaseError, "lookup name is null"); 75 } 76 77 String [] kws = parser.kws(); 79 80 for (int i = 0; i < kws.length; i++) { 81 String keyword = kws[i], fieldname = null; 82 Object value = parser.kw(keyword).__tojava__(Object .class); 83 84 try { 85 Field field = Context.class.getField(keyword); 86 87 fieldname = (String ) field.get(Context.class); 88 } catch (IllegalAccessException e) { 89 throw zxJDBC.makeException(zxJDBC.ProgrammingError, e); 90 } catch (NoSuchFieldException e) { 91 fieldname = keyword; 92 } 93 94 env.put(fieldname, value); 95 } 96 97 InitialContext context = null; 98 99 try { 100 context = new InitialContext(env); 101 ref = context.lookup((String ) jndiName); 102 } catch (NamingException e) { 103 throw zxJDBC.makeException(zxJDBC.DatabaseError, e); 104 } finally { 105 if (context != null) { 106 try { 107 context.close(); 108 } catch (NamingException e) { 109 } 110 } 111 } 112 113 if (ref == null) { 114 throw zxJDBC.makeException(zxJDBC.ProgrammingError, "object [" + jndiName + "] not found in JNDI"); 115 } 116 117 try { 118 if (ref instanceof String ) { 119 connection = DriverManager.getConnection(((String ) ref)); 120 } else if (ref instanceof Connection) { 121 connection = (Connection) ref; 122 } else if (ref instanceof DataSource) { 123 connection = ((DataSource) ref).getConnection(); 124 } else if (ref instanceof ConnectionPoolDataSource) { 125 connection = ((ConnectionPoolDataSource) ref).getPooledConnection().getConnection(); 126 } 127 } catch (SQLException e) { 128 throw zxJDBC.makeException(zxJDBC.DatabaseError, e); 129 } 130 131 try { 132 if ((connection == null) || connection.isClosed()) { 133 throw zxJDBC.makeException(zxJDBC.DatabaseError, "unable to establish connection"); 134 } 135 136 return new PyConnection(connection); 137 } catch (SQLException e) { 138 throw zxJDBC.makeException(zxJDBC.DatabaseError, e); 139 } 140 } 141 142 147 public String toString() { 148 return "<lookup object instance at " + Py.id(this) + ">"; 149 } 150 151 153 156 public static PyClass __class__; 157 158 163 protected PyClass getPyClass() { 164 return __class__; 165 } 166 } 167 | Popular Tags |