KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ziclix > python > sql > connect > Lookup


1 /*
2  * Jython Database Specification API 2.0
3  *
4  * $Id: Lookup.java,v 1.3 2005/02/23 04:26:19 bzimmer Exp $
5  *
6  * Copyright (c) 2001 brian zimmer <bzimmer@ziclix.com>
7  *
8  */

9 package com.ziclix.python.sql.connect;
10
11 import java.sql.*;
12 import java.util.*;
13 import java.lang.reflect.Field JavaDoc;
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 /**
22  * Establish a connection through a JNDI lookup. The bound object can be either a <code>DataSource</code>,
23  * <code>ConnectionPooledDataSource</code>, <code>Connection</code> or a <code>String</code>. If it's a
24  * <code>String</code> the value is passed to the DriverManager to obtain a connection, otherwise the
25  * <code>Connection</code> is established using the object.
26  *
27  * @author brian zimmer
28  * @author last revised by $Author: bzimmer $
29  * @version $Revision: 1.3 $
30  */

31 public class Lookup extends PyObject {
32
33     private static final PyString _doc = new PyString("establish a connection through a JNDI lookup");
34
35     /**
36      * Constructor Lookup
37      */

38     public Lookup() {
39     }
40
41     /**
42      * Method __findattr__
43      *
44      * @param name
45      * @return PyObject
46      */

47     public PyObject __findattr__(String JavaDoc name) {
48
49         if ("__doc__".equals(name)) {
50             return _doc;
51         }
52
53         return super.__findattr__(name);
54     }
55
56     /**
57      * Expects a single PyString argument which is the JNDI name of the bound Connection or DataSource.
58      * If any keywords are passed, an attempt is made to match the keyword to a static final Field on
59      * javax.naming.Context. If the Field is found, the value of the Field is substituted as the key
60      * and the value of the keyword is the value put in the Hashtable environment. If the Field is not
61      * found, the key is the keyword with no substitutions.
62      */

63     public PyObject __call__(PyObject[] args, String JavaDoc[] keywords) {
64
65         Object JavaDoc ref = null;
66         Connection connection = null;
67         Hashtable env = new Hashtable();
68
69         // figure out the correct params
70
PyArgParser parser = new PyArgParser(args, keywords);
71         Object JavaDoc jndiName = parser.arg(0).__tojava__(String JavaDoc.class);
72
73         if ((jndiName == null) || (jndiName == Py.NoConversion)) {
74             throw zxJDBC.makeException(zxJDBC.DatabaseError, "lookup name is null");
75         }
76
77         // add any Context properties
78
String JavaDoc[] kws = parser.kws();
79
80         for (int i = 0; i < kws.length; i++) {
81             String JavaDoc keyword = kws[i], fieldname = null;
82             Object JavaDoc value = parser.kw(keyword).__tojava__(Object JavaDoc.class);
83
84             try {
85                 Field JavaDoc field = Context.class.getField(keyword);
86
87                 fieldname = (String JavaDoc) field.get(Context.class);
88             } catch (IllegalAccessException JavaDoc e) {
89                 throw zxJDBC.makeException(zxJDBC.ProgrammingError, e);
90             } catch (NoSuchFieldException JavaDoc 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 JavaDoc) 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 JavaDoc) {
119                 connection = DriverManager.getConnection(((String JavaDoc) 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     /**
143      * Method toString
144      *
145      * @return String
146      */

147     public String JavaDoc toString() {
148         return "<lookup object instance at " + Py.id(this) + ">";
149     }
150
151     // __class__ boilerplate -- see PyObject for details
152

153     /**
154      * Field __class__
155      */

156     public static PyClass __class__;
157
158     /**
159      * Method getPyClass
160      *
161      * @return PyClass
162      */

163     protected PyClass getPyClass() {
164         return __class__;
165     }
166 }
167
Popular Tags