From: brian z. <bz...@us...> - 2001-11-20 04:55:21
|
Update of /cvsroot/jython/jython/com/ziclix/python/sql/connect In directory usw-pr-cvs1:/tmp/cvs-serv7094/com/ziclix/python/sql/connect Added Files: Connectx.java Connect.java Lookup.java Log Message: initial zxJDBC checkin --- NEW FILE: Connectx.java --- /* * Jython Database Specification API 2.0 * * $Id: Connectx.java,v 1.1 2001/11/20 04:55:18 bzimmer Exp $ * * Copyright (c) 2001 brian zimmer <bz...@zi...> * */ package com.ziclix.python.sql.connect; import java.sql.*; import java.util.*; import javax.sql.*; import java.lang.reflect.*; import org.python.core.*; import com.ziclix.python.sql.*; import com.ziclix.python.sql.util.*; /** * Connect using through a javax.sql.DataSource or javax.sql.ConnectionPooledDataSource. * * @author brian zimmer * @author last revised by $Author: bzimmer $ * @version $Revision: 1.1 $ */ public class Connectx extends PyObject { private final String SET = "set"; private final PyString doc = new PyString("establish a connection through a javax.sql.DataSource or javax.sql.ConnectionPooledDataSource"); /** * Constructor Connectx * */ public Connectx() {} /** * Method __findattr__ * * @param String name * * @return PyObject * */ public PyObject __findattr__(String name) { if ("__doc__".equals(name)) { return doc; } return super.__findattr__(name); } /** * Construct a javax.sql.DataSource or javax.sql.ConnectionPooledDataSource */ public PyObject __call__(PyObject[] args, String[] keywords) { Connection c = null; PyConnection pc = null; Object datasource = null; PyArgParser parser = new PyArgParser(args, keywords); try { String _class = (String)parser.arg(0).__tojava__(String.class); datasource = Class.forName(_class).newInstance(); } catch (Exception e) { throw zxJDBC.makeException(zxJDBC.DatabaseError, "unable to instantiate datasource"); } String[] kws = parser.kws(); Class clazz = datasource.getClass(); for (int i = 0; i < kws.length; i++) { String methodName = kws[i]; if (methodName == null) { continue; } Object value = parser.kw(kws[i]).__tojava__(Object.class); if (methodName.length() > SET.length()) { if (!SET.equals(methodName.substring(0, SET.length()))) { // prepend "set" invoke(datasource, SET + methodName.substring(0, 1).toUpperCase() + methodName.substring(1), value); } else { // starts with "set" so just pass it on invoke(datasource, methodName, value); } } else { // shorter than "set" so it can't be a full method name invoke(datasource, SET + methodName.substring(0, 1).toUpperCase() + methodName.substring(1), value); } } try { if (datasource instanceof DataSource) { c = ((DataSource)datasource).getConnection(); } else if (datasource instanceof ConnectionPoolDataSource) { c = ((ConnectionPoolDataSource)datasource).getPooledConnection().getConnection(); } } catch (SQLException e) { throw zxJDBC.makeException(zxJDBC.DatabaseError, e.getMessage()); } try { if ((c == null) || c.isClosed()) { throw zxJDBC.makeException(zxJDBC.DatabaseError, "unable to establish connection"); } pc = new PyConnection(c); } catch (SQLException e) { throw zxJDBC.makeException(zxJDBC.DatabaseError, e.getMessage()); } return pc; } /** * Method toString * * @return String * */ public String toString() { return "<connectx object instance at " + Py.id(this) + ">"; } /** * Method invoke * * @param Object src * @param String methodName * @param Object value * */ protected void invoke(Object src, String methodName, Object value) { Method method = null; StringBuffer exceptionMsg = new StringBuffer("method [").append(methodName).append("] using arg type ["); exceptionMsg.append(value.getClass()).append("], value [").append(value.toString()).append("]"); try { method = getMethod(src.getClass(), methodName, value.getClass()); if (method == null) { throw zxJDBC.newError("no such " + exceptionMsg); } method.invoke(src, new Object[]{ value }); } catch (IllegalAccessException e) { throw zxJDBC.newError("illegal access for " + exceptionMsg); } catch (InvocationTargetException e) { throw zxJDBC.newError("invocation target exception for " + exceptionMsg); } return; } /** * Try to find the method by the given name. If failing that, see if the valueClass * is perhaps a primitive and attempt to recurse using the primitive type. Failing * that return null. */ protected Method getMethod(Class srcClass, String methodName, Class valueClass) { Method method = null; try { method = srcClass.getMethod(methodName, new Class[]{ valueClass }); } catch (NoSuchMethodException e) { Class primitive = null; try { Field f = valueClass.getField("TYPE"); primitive = (Class)f.get(valueClass); } catch (NoSuchFieldException ex) {} catch (IllegalAccessException ex) {} catch (ClassCastException ex) {} if ((primitive != null) && primitive.isPrimitive()) { return getMethod(srcClass, methodName, primitive); } } return method; } // __class__ boilerplate -- see PyObject for details /** Field __class__ */ public static PyClass __class__; /** * Method getPyClass * * @return PyClass * */ protected PyClass getPyClass() { return __class__; } } --- NEW FILE: Connect.java --- /* * Jython Database Specification API 2.0 * * $Id: Connect.java,v 1.1 2001/11/20 04:55:18 bzimmer Exp $ * * Copyright (c) 2001 brian zimmer <bz...@zi...> * */ package com.ziclix.python.sql.connect; import java.sql.*; import java.util.*; import org.python.core.*; import com.ziclix.python.sql.*; import com.ziclix.python.sql.util.*; /** * Connect using DriverManager. * * @author brian zimmer * @author last revised by $Author: bzimmer $ * @version $Revision: 1.1 $ */ public class Connect extends PyObject { private static final PyString _doc = new PyString("establish a connection through java.sql.DriverManager"); /** * Default empty constructor. */ public Connect() {} /** * Method __findattr__ * * @param String name * * @return PyObject * */ public PyObject __findattr__(String name) { if ("__doc__".equals(name)) { return _doc; } return super.__findattr__(name); } /** * Establish a connection through DriverManager. */ public PyObject __call__(PyObject[] args, String[] keywords) { Connection c = null; PyArgParser parser = new PyArgParser(args, keywords); Object arg = parser.arg(0).__tojava__(Connection.class); if (arg == Py.NoConversion) { Properties props = new Properties(); String url = null, user = null, password = null, driver = null; url = (String)parser.arg(0).__tojava__(String.class); user = (String)parser.arg(1).__tojava__(String.class); password = (String)parser.arg(2).__tojava__(String.class); driver = (String)parser.arg(3).__tojava__(String.class); if (url == null) { throw zxJDBC.makeException(zxJDBC.DatabaseError, "no url specified"); } if (driver == null) { throw zxJDBC.makeException(zxJDBC.DatabaseError, "no driver specified"); } // the value can't be null props.put("user", (user == null) ? "" : user); props.put("password", (password == null) ? "" : password); String[] kws = parser.kws(); for (int i = 0; i < kws.length; i++) { Object value = parser.kw(kws[i]).__tojava__(Object.class); props.put(kws[i], value); } try { Class.forName(driver); } catch (ClassNotFoundException e) { throw zxJDBC.makeException(zxJDBC.DatabaseError, "driver not found"); } try { c = DriverManager.getConnection(url, props); } catch (SQLException e) { throw zxJDBC.makeException(zxJDBC.DatabaseError, e.getMessage()); } } else { c = (Connection)arg; } try { if ((c == null) || c.isClosed()) { throw zxJDBC.makeException(zxJDBC.DatabaseError, "unable to establish connection"); } return new PyConnection(c); } catch (SQLException e) { throw zxJDBC.makeException(zxJDBC.DatabaseError, e.getMessage()); } } /** * Method toString * * @return String * */ public String toString() { return "<connect object instance at " + Py.id(this) + ">"; } // __class__ boilerplate -- see PyObject for details /** Field __class__ */ public static PyClass __class__; /** * Method getPyClass * * @return PyClass * */ protected PyClass getPyClass() { return __class__; } } --- NEW FILE: Lookup.java --- /* * Jython Database Specification API 2.0 * * $Id: Lookup.java,v 1.1 2001/11/20 04:55:18 bzimmer Exp $ * * Copyright (c) 2001 brian zimmer <bz...@zi...> * */ package com.ziclix.python.sql.connect; import java.sql.*; import java.util.*; import java.lang.reflect.Field; import javax.sql.*; import javax.naming.*; import org.python.core.*; import com.ziclix.python.sql.*; import com.ziclix.python.sql.util.*; /** * Establish a connection through a JNDI lookup. The bound object can be either a <code>DataSource</code>, * <code>ConnectionPooledDataSource</code>, <code>Connection</code> or a <code>String</code>. If it's a * <code>String</code> the value is passed to the DriverManager to obtain a connection, otherwise the * <code>Connection</code> is established using the object. * * @author brian zimmer * @author last revised by $Author: bzimmer $ * @version $Revision: 1.1 $ */ public class Lookup extends PyObject { private static final PyString _doc = new PyString("establish a connection through a JNDI lookup"); /** * Constructor Lookup * */ public Lookup() {} /** * Method __findattr__ * * @param String name * * @return PyObject * */ public PyObject __findattr__(String name) { if ("__doc__".equals(name)) { return _doc; } return super.__findattr__(name); } /** * Expects a single PyString argument which is the JNDI name of the bound Connection or DataSource. * If any keywords are passed, an attempt is made to match the keyword to a static final Field on * javax.naming.Context. If the Field is found, the value of the Field is substituted as the key * and the value of the keyword is the value put in the Hashtable environment. If the Field is not * found, the key is the keyword with no substitutions. */ public PyObject __call__(PyObject[] args, String[] keywords) { Object ref = null; Connection connection = null; Hashtable env = new Hashtable(); // figure out the correct params PyArgParser parser = new PyArgParser(args, keywords); Object jndiName = parser.arg(0).__tojava__(String.class); if ((jndiName == null) || (jndiName == Py.NoConversion)) { throw zxJDBC.makeException(zxJDBC.DatabaseError, "lookup name is null"); } // add any Context properties String[] kws = parser.kws(); for (int i = 0; i < kws.length; i++) { String keyword = kws[i], fieldname = null; Object value = parser.kw(keyword).__tojava__(Object.class); try { Field field = Context.class.getField(keyword); fieldname = (String)field.get(Context.class); } catch (IllegalAccessException e) { throw zxJDBC.makeException(zxJDBC.ProgrammingError, e.getMessage()); } catch (NoSuchFieldException e) { fieldname = keyword; } env.put(fieldname, value); } InitialContext context = null; try { context = new InitialContext(env); ref = context.lookup((String)jndiName); } catch (NamingException e) { throw zxJDBC.makeException(zxJDBC.DatabaseError, e.getMessage()); } finally { if (context != null) { try { context.close(); } catch (NamingException e) {} } } if (ref == null) { throw zxJDBC.makeException(zxJDBC.ProgrammingError, "object [" + jndiName + "] not found in JNDI"); } try { if (ref instanceof String) { connection = DriverManager.getConnection(((String)ref)); } else if (ref instanceof Connection) { connection = (Connection)ref; } else if (ref instanceof DataSource) { connection = ((DataSource)ref).getConnection(); } else if (ref instanceof ConnectionPoolDataSource) { connection = ((ConnectionPoolDataSource)ref).getPooledConnection().getConnection(); } } catch (SQLException e) { throw zxJDBC.makeException(zxJDBC.DatabaseError, e.getMessage()); } try { if ((connection == null) || connection.isClosed()) { throw zxJDBC.makeException(zxJDBC.DatabaseError, "unable to establish connection"); } return new PyConnection(connection); } catch (SQLException e) { throw zxJDBC.makeException(zxJDBC.DatabaseError, e.getMessage()); } } /** * Method toString * * @return String * */ public String toString() { return "<lookup object instance at " + Py.id(this) + ">"; } // __class__ boilerplate -- see PyObject for details /** Field __class__ */ public static PyClass __class__; /** * Method getPyClass * * @return PyClass * */ protected PyClass getPyClass() { return __class__; } } |