%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
org.apache.commons.jelly.tags.sql.DataSourceUtil |
|
|
1 | /* |
|
2 | * Copyright 2002,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.commons.jelly.tags.sql; |
|
18 | ||
19 | import javax.sql.DataSource; |
|
20 | import javax.naming.InitialContext; |
|
21 | import javax.naming.Context; |
|
22 | import javax.naming.NamingException; |
|
23 | ||
24 | import org.apache.commons.jelly.JellyContext; |
|
25 | import org.apache.commons.jelly.JellyTagException; |
|
26 | import org.apache.commons.jelly.tags.Resources; |
|
27 | ||
28 | import org.apache.commons.logging.Log; |
|
29 | import org.apache.commons.logging.LogFactory; |
|
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 | 0 | public class DataSourceUtil { |
40 | ||
41 | private static final String ESCAPE = "\\"; |
|
42 | private static final String TOKEN = ","; |
|
43 | ||
44 | /** The Log to which logging calls will be made. */ |
|
45 | 0 | private static final Log log = LogFactory.getLog(DataSourceUtil.class); |
46 | ||
47 | /** |
|
48 | * If dataSource is a String first do JNDI lookup. |
|
49 | * If lookup fails parse String like it was a set of JDBC parameters |
|
50 | * Otherwise check to see if dataSource is a DataSource object and use as |
|
51 | * is |
|
52 | */ |
|
53 | static DataSource getDataSource(Object rawDataSource, JellyContext pc) |
|
54 | throws JellyTagException { |
|
55 | 0 | DataSource dataSource = null; |
56 | ||
57 | 0 | if (rawDataSource == null) { |
58 | 0 | rawDataSource = pc.getVariable("org.apache.commons.jelly.sql.DataSource"); |
59 | } |
|
60 | ||
61 | 0 | if (rawDataSource == null) { |
62 | 0 | return null; |
63 | } |
|
64 | ||
65 | /* |
|
66 | * If the 'dataSource' attribute's value resolves to a String |
|
67 | * after rtexpr/EL evaluation, use the string as JNDI path to |
|
68 | * a DataSource |
|
69 | */ |
|
70 | 0 | if (rawDataSource instanceof String) { |
71 | try { |
|
72 | 0 | Context ctx = new InitialContext(); |
73 | // relative to standard JNDI root for J2EE app |
|
74 | 0 | Context envCtx = (Context) ctx.lookup("java:comp/env"); |
75 | 0 | dataSource = (DataSource) envCtx.lookup((String) rawDataSource); |
76 | 0 | } |
77 | catch (NamingException ex) { |
|
78 | 0 | dataSource = getDataSource((String) rawDataSource); |
79 | 0 | } |
80 | } |
|
81 | 0 | else if (rawDataSource instanceof DataSource) { |
82 | 0 | dataSource = (DataSource) rawDataSource; |
83 | } |
|
84 | else { |
|
85 | 0 | throw new JellyTagException(Resources.getMessage("SQL_DATASOURCE_INVALID_TYPE")); |
86 | } |
|
87 | ||
88 | 0 | return dataSource; |
89 | } |
|
90 | ||
91 | /** |
|
92 | * Parse JDBC parameters and setup dataSource appropriately |
|
93 | */ |
|
94 | private static DataSource getDataSource(String params) throws JellyTagException { |
|
95 | 0 | DataSourceWrapper dataSource = new DataSourceWrapper(); |
96 | ||
97 | 0 | String[] paramString = new String[4]; |
98 | 0 | int escCount = 0; |
99 | 0 | int aryCount = 0; |
100 | 0 | int begin = 0; |
101 | ||
102 | 0 | for (int index = 0; index < params.length(); index++) { |
103 | 0 | char nextChar = params.class="keyword">charAt(index); |
104 | 0 | if (TOKEN.indexOf(nextChar) != -1) { |
105 | 0 | if (escCount == 0) { |
106 | 0 | paramString[aryCount] = params.substring(begin, index); |
107 | 0 | begin = index + 1; |
108 | 0 | if (++aryCount > 4) { |
109 | 0 | throw new JellyTagException(Resources.getMessage("JDBC_PARAM_COUNT")); |
110 | } |
|
111 | } |
|
112 | } |
|
113 | 0 | if (ESCAPE.indexOf(nextChar) != -1) { |
114 | 0 | escCount++; |
115 | } |
|
116 | else { |
|
117 | 0 | escCount = 0; |
118 | } |
|
119 | } |
|
120 | 0 | paramString[aryCount] = params.substring(begin); |
121 | ||
122 | // use the JDBC URL from the parameter string |
|
123 | 0 | dataSource.setJdbcURL(paramString[0]); |
124 | ||
125 | // try to load a driver if it's present |
|
126 | 0 | if (paramString[1] != null) { |
127 | try { |
|
128 | 0 | dataSource.setDriverClassName(paramString[1]); |
129 | 0 | } |
130 | catch (Exception ex) { |
|
131 | 0 | throw new JellyTagException( |
132 | Resources.getMessage("DRIVER_INVALID_CLASS", ex.getMessage())); |
|
133 | } |
|
134 | } |
|
135 | ||
136 | // set the username and password |
|
137 | 0 | dataSource.setUserName(paramString[2]); |
138 | 0 | dataSource.setPassword(paramString[3]); |
139 | ||
140 | 0 | return dataSource; |
141 | } |
|
142 | ||
143 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |