KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > sql > DataSourceUtil


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.standard.tag.common.sql;
18
19 import javax.naming.Context JavaDoc;
20 import javax.naming.InitialContext JavaDoc;
21 import javax.naming.NamingException JavaDoc;
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.JspTagException JavaDoc;
24 import javax.servlet.jsp.PageContext JavaDoc;
25 import javax.servlet.jsp.jstl.core.Config;
26 import javax.sql.DataSource JavaDoc;
27
28 import org.apache.taglibs.standard.resources.Resources;
29
30
31 /**
32  * <p>A simple <code>DataSource</code> utility for the standard
33  * <code>DriverManager</code> class.
34  *
35  * TO DO: need to cache DataSource
36  *
37  * @author Justyna Horwat
38  */

39 public class DataSourceUtil {
40
41     private static final String JavaDoc ESCAPE = "\\";
42     private static final String JavaDoc TOKEN = ",";
43
44     /**
45      * If dataSource is a String first do JNDI lookup.
46      * If lookup fails parse String like it was a set of JDBC parameters
47      * Otherwise check to see if dataSource is a DataSource object and use as
48      * is
49      */

50     static DataSource JavaDoc getDataSource(Object JavaDoc rawDataSource, PageContext JavaDoc pc)
51     throws JspException JavaDoc
52     {
53     DataSource JavaDoc dataSource = null;
54
55         if (rawDataSource == null) {
56             rawDataSource = Config.find(pc, Config.SQL_DATA_SOURCE);
57         }
58
59     if (rawDataSource == null) {
60         return null;
61     }
62
63         /*
64      * If the 'dataSource' attribute's value resolves to a String
65      * after rtexpr/EL evaluation, use the string as JNDI path to
66      * a DataSource
67      */

68         if (rawDataSource instanceof String JavaDoc) {
69             try {
70                 Context JavaDoc ctx = new InitialContext JavaDoc();
71                 // relative to standard JNDI root for J2EE app
72
Context JavaDoc envCtx = (Context JavaDoc) ctx.lookup("java:comp/env");
73                 dataSource = (DataSource JavaDoc) envCtx.lookup((String JavaDoc) rawDataSource);
74             } catch (NamingException JavaDoc ex) {
75                 dataSource = getDataSource((String JavaDoc) rawDataSource);
76             }
77         } else if (rawDataSource instanceof DataSource JavaDoc) {
78             dataSource = (DataSource JavaDoc) rawDataSource;
79         } else {
80         throw new JspException JavaDoc(
81                 Resources.getMessage("SQL_DATASOURCE_INVALID_TYPE"));
82     }
83
84     return dataSource;
85     }
86
87     /**
88      * Parse JDBC parameters and setup dataSource appropriately
89      */

90     private static DataSource JavaDoc getDataSource(String JavaDoc params)
91     throws JspException JavaDoc
92     {
93         DataSourceWrapper dataSource = new DataSourceWrapper();
94
95         String JavaDoc[] paramString = new String JavaDoc[4];
96         int escCount = 0;
97         int aryCount = 0;
98         int begin = 0;
99
100         for(int index=0; index < params.length(); index++) {
101             char nextChar = params.charAt(index);
102             if (TOKEN.indexOf(nextChar) != -1) {
103                 if (escCount == 0) {
104                     paramString[aryCount] = params.substring(begin,index).trim();
105                     begin = index + 1;
106                     if (++aryCount > 4) {
107                         throw new JspTagException JavaDoc(
108                             Resources.getMessage("JDBC_PARAM_COUNT"));
109                     }
110                 }
111             }
112             if (ESCAPE.indexOf(nextChar) != -1) {
113                 escCount++;
114             }
115             else {
116                 escCount = 0;
117             }
118         }
119         paramString[aryCount] = params.substring(begin).trim();
120
121     // use the JDBC URL from the parameter string
122
dataSource.setJdbcURL(paramString[0]);
123
124     // try to load a driver if it's present
125
if (paramString[1] != null) {
126             try {
127                 dataSource.setDriverClassName(paramString[1]);
128             } catch (Exception JavaDoc ex) {
129                 throw new JspTagException JavaDoc(
130                     Resources.getMessage("DRIVER_INVALID_CLASS",
131                      ex.toString()), ex);
132             }
133     }
134
135     // set the username and password
136
dataSource.setUserName(paramString[2]);
137         dataSource.setPassword(paramString[3]);
138
139     return dataSource;
140     }
141
142 }
143
Popular Tags