View Javadoc

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  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      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          DataSource dataSource = null;
56  
57          if (rawDataSource == null) {
58              rawDataSource = pc.getVariable("org.apache.commons.jelly.sql.DataSource");
59          }
60  
61          if (rawDataSource == null) {
62              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          if (rawDataSource instanceof String) {
71              try {
72                  Context ctx = new InitialContext();
73                  // relative to standard JNDI root for J2EE app
74                  Context envCtx = (Context) ctx.lookup("java:comp/env");
75                  dataSource = (DataSource) envCtx.lookup((String) rawDataSource);
76              }
77              catch (NamingException ex) {
78                  dataSource = getDataSource((String) rawDataSource);
79              }
80          }
81          else if (rawDataSource instanceof DataSource) {
82              dataSource = (DataSource) rawDataSource;
83          }
84          else {
85              throw new JellyTagException(Resources.getMessage("SQL_DATASOURCE_INVALID_TYPE"));
86          }
87  
88          return dataSource;
89      }
90  
91      /***
92       * Parse JDBC parameters and setup dataSource appropriately
93       */
94      private static DataSource getDataSource(String params) throws JellyTagException {
95          DataSourceWrapper dataSource = new DataSourceWrapper();
96  
97          String[] paramString = new String[4];
98          int escCount = 0;
99          int aryCount = 0;
100         int begin = 0;
101 
102         for (int index = 0; index < params.length(); index++) {
103             char nextChar = params.charAt(index);
104             if (TOKEN.indexOf(nextChar) != -1) {
105                 if (escCount == 0) {
106                     paramString[aryCount] = params.substring(begin, index);
107                     begin = index + 1;
108                     if (++aryCount > 4) {
109                         throw new JellyTagException(Resources.getMessage("JDBC_PARAM_COUNT"));
110                     }
111                 }
112             }
113             if (ESCAPE.indexOf(nextChar) != -1) {
114                 escCount++;
115             }
116             else {
117                 escCount = 0;
118             }
119         }
120         paramString[aryCount] = params.substring(begin);
121 
122         // use the JDBC URL from the parameter string
123         dataSource.setJdbcURL(paramString[0]);
124 
125         // try to load a driver if it's present
126         if (paramString[1] != null) {
127             try {
128                 dataSource.setDriverClassName(paramString[1]);
129             }
130             catch (Exception ex) {
131                 throw new JellyTagException(
132                     Resources.getMessage("DRIVER_INVALID_CLASS", ex.getMessage()));
133             }
134         }
135 
136         // set the username and password
137         dataSource.setUserName(paramString[2]);
138         dataSource.setPassword(paramString[3]);
139 
140         return dataSource;
141     }
142 
143 }