KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > dbtags > connection > ConnectionTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.dbtags.connection;
18
19 import java.io.IOException JavaDoc;
20 import java.sql.Connection JavaDoc;
21 import java.sql.DriverManager JavaDoc;
22 import java.sql.SQLException JavaDoc;
23
24 import javax.naming.Context JavaDoc;
25 import javax.naming.InitialContext JavaDoc;
26 import javax.naming.NamingException JavaDoc;
27 import javax.servlet.jsp.JspTagException JavaDoc;
28 import javax.servlet.jsp.tagext.BodyTagSupport JavaDoc;
29 import javax.sql.DataSource JavaDoc;
30
31 /**
32  * <p>JSP tag connection, used to get a
33  * java.sql.Connection object from the DriverManager.</p>
34  *
35  * <p>JSP Tag Lib Descriptor
36  * <pre>
37  * &lt;name>connection&lt;/name>
38  * &lt;tagclass>org.apache.taglibs.dbtags.connection.ConnectionTag&lt;/tagclass>
39  * &lt;bodycontent>JSP&lt;/bodycontent>
40  * &lt;teiclass>org.apache.taglibs.dbtags.connection.ConnectionTEI&lt;/teiclass>
41  * &lt;info>Opens a connection based on either a url in the body of the tag
42  * or by using the "datasource" tag attribute to reference to a
43  * javax.sql.DataSource page attribute. driver (optional),
44  * userid (optional), and password (optional) are also set in the
45  * body of the tag.&lt;/info>
46  * &lt;attribute>
47  * &lt;name>id&lt;/name>
48  * &lt;required>true&lt;/required>
49  * &lt;rtexprvalue>false&lt;/rtexprvalue>
50  * &lt;/attribute>
51  * &lt;attribute>
52  * &lt;name>jndiName&lt;/name>
53  * &lt;required>false&lt;/required>
54  * &lt;rtexprvalue>false&lt;/rtexprvalue>
55  * &lt;/attribute>
56  * &lt;attribute>
57  * &lt;name>dataSource</name>
58  * &lt;required>false</required>
59  * &lt;rtexprvalue>false</rtexprvalue>
60  * &lt;/attribute>
61  * </pre>
62  *
63  * @author Morgan Delagrange
64  */

65 public class ConnectionTag extends BodyTagSupport JavaDoc {
66
67   private String JavaDoc _dataSourceName = null;
68   private String JavaDoc _url = null;
69   private String JavaDoc _driver = null;
70   private String JavaDoc _userId = null;
71   private String JavaDoc _password = null;
72   private String JavaDoc _jndiName = null;
73
74   /**
75    * Set the name of a javax.sql.DataSource page attribute
76    * which can create a database connection
77    *
78    * @param dataSourceName
79    * name of a javax.sql.DataSource page attribute
80    */

81   public void setDataSource(String JavaDoc dataSourceName) {
82     _dataSourceName = dataSourceName;
83   }
84
85   /**
86    * URL of the database to access.
87    *
88    * @param url database URL
89    */

90   public void setUrl(String JavaDoc url) {
91     _url = url;
92   }
93
94   /**
95    * java.sql.Driver for the database. This method is
96    * optional if you have already loaded the Driver in
97    * the JVM.
98    *
99    * @param driver class name of the java.sql.Driver
100    */

101   public void setDriver(String JavaDoc driver) {
102     _driver = driver;
103   }
104
105   /**
106    * jndi named datasource used for connecting to the database via a jndi lookup
107    *
108    * @param jndiName jndi name for the jdbc datasourcer
109    */

110   public void setJndiName(String JavaDoc jndiName) {
111     _jndiName = jndiName;
112   }
113
114   /**
115    * User id for the database. Optional if the user id
116    * is already encoded in the URL, or if it is not
117    * required by the database.
118    *
119    * @param userId user id for the database.
120    */

121   public void setUserId(String JavaDoc userId) {
122     _userId = userId;
123   }
124
125   /**
126    * Password for the database. Optional if the
127    * password is already encoded in the URL, or if
128    * it is not required by the database.
129    *
130    * @param password password for the given user id
131    */

132   public void setPassword(String JavaDoc password) {
133     _password = password;
134   }
135
136   public int doStartTag() throws JspTagException JavaDoc {
137     return EVAL_BODY_TAG;
138   }
139
140   public int doAfterBody() throws JspTagException JavaDoc
141   {
142     // Automatically trim the contents of this tag
143
try {
144       getPreviousOut().write(getBodyContent().getString().trim());
145     } catch (IOException JavaDoc e) {
146       throw new JspTagException JavaDoc(e.toString());
147     }
148     return EVAL_PAGE;
149   }
150
151   public int doEndTag() throws JspTagException JavaDoc{
152
153     DataSource JavaDoc dataSource = null;
154
155     try {
156       if (_driver != null) {
157         Class.forName(_driver);
158       }
159
160       Connection JavaDoc conn = null;
161
162       if (_jndiName != null) {
163     try {
164           Context JavaDoc ctx = new InitialContext JavaDoc();
165       dataSource = (DataSource JavaDoc)ctx.lookup(_jndiName);
166     } catch (NamingException JavaDoc ne) {
167         throw new JspTagException JavaDoc(ne.toString());
168     }
169     if (_userId != null) {
170           conn = dataSource.getConnection(_userId, _password);
171         } else {
172           conn = dataSource.getConnection();
173         }
174       } else if (_dataSourceName != null) {
175         dataSource = (DataSource JavaDoc) pageContext.findAttribute(_dataSourceName);
176         if ( dataSource == null ) {
177           throw new JspTagException JavaDoc("Did not find a DataSource bean named " + _dataSourceName );
178         }
179         
180         if (_userId != null) {
181           conn = dataSource.getConnection(_userId, _password);
182         } else {
183           conn = dataSource.getConnection();
184         }
185       } else if (_url != null) {
186         if (_userId != null) {
187           conn = DriverManager.getConnection(_url, _userId, _password);
188         } else {
189           conn = DriverManager.getConnection(_url);
190         }
191       } else {
192         throw new JspTagException JavaDoc("Cannot connect to database, no database URL or DataSource.");
193       }
194
195       pageContext.setAttribute(getId(),conn);
196     } catch (SQLException JavaDoc e) {
197       throw new JspTagException JavaDoc(e.toString());
198     } catch (ClassNotFoundException JavaDoc e) {
199       throw new JspTagException JavaDoc("Driver class '" + _driver + "' not found\n" +
200                                 e.toString());
201     }
202
203     return EVAL_PAGE;
204   }
205
206   public void release() {
207     _dataSourceName = null;
208     _url = null;
209     _driver = null;
210     _userId = null;
211     _password = null;
212   }
213
214 }
215
Popular Tags