View Javadoc
1 /* 2 * The Apache Software License, Version 1.1 3 * 4 * Copyright (c) 1999 The Apache Software Foundation. All rights 5 * reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * 3. The end-user documentation included with the redistribution, if 20 * any, must include the following acknowlegement: 21 * "This product includes software developed by the 22 * Apache Software Foundation (http://www.apache.org/)." 23 * Alternately, this acknowlegement may appear in the software itself, 24 * if and wherever such third-party acknowlegements normally appear. 25 * 26 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software 27 * Foundation" must not be used to endorse or promote products derived 28 * from this software without prior written permission. For written 29 * permission, please contact apache@apache.org. 30 * 31 * 5. Products derived from this software may not be called "Apache" 32 * nor may "Apache" appear in their names without prior written 33 * permission of the Apache Group. 34 * 35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 46 * SUCH DAMAGE. 47 * ==================================================================== 48 * 49 * This software consists of voluntary contributions made by many 50 * individuals on behalf of the Apache Software Foundation. For more 51 * information on the Apache Software Foundation, please see 52 * <http://www.apache.org/>;. 53 * 54 */ 55 56 package org.apache.commons.jelly.tags.sql; 57 58 import javax.sql.DataSource; 59 import javax.naming.InitialContext; 60 import javax.naming.Context; 61 import javax.naming.NamingException; 62 63 import org.apache.commons.jelly.JellyContext; 64 import org.apache.commons.jelly.JellyException; 65 import org.apache.commons.jelly.tags.Resources; 66 67 import org.apache.commons.logging.Log; 68 import org.apache.commons.logging.LogFactory; 69 70 /*** 71 * <p>A simple <code>DataSource</code> utility for the standard 72 * <code>DriverManager</code> class. 73 * 74 * TO DO: need to cache DataSource 75 * 76 * @author Justyna Horwat 77 */ 78 public class DataSourceUtil { 79 80 private static final String ESCAPE = "//"; 81 private static final String TOKEN = ","; 82 83 /*** The Log to which logging calls will be made. */ 84 private static final Log log = LogFactory.getLog(DataSourceUtil.class); 85 86 /*** 87 * If dataSource is a String first do JNDI lookup. 88 * If lookup fails parse String like it was a set of JDBC parameters 89 * Otherwise check to see if dataSource is a DataSource object and use as 90 * is 91 */ 92 static DataSource getDataSource(Object rawDataSource, JellyContext pc) 93 throws JellyException { 94 DataSource dataSource = null; 95 96 if (rawDataSource == null) { 97 rawDataSource = pc.getVariable("org.apache.commons.jelly.sql.DataSource"); 98 } 99 100 if (rawDataSource == null) { 101 return null; 102 } 103 104 /* 105 * If the 'dataSource' attribute's value resolves to a String 106 * after rtexpr/EL evaluation, use the string as JNDI path to 107 * a DataSource 108 */ 109 if (rawDataSource instanceof String) { 110 try { 111 Context ctx = new InitialContext(); 112 // relative to standard JNDI root for J2EE app 113 Context envCtx = (Context) ctx.lookup("java:comp/env"); 114 dataSource = (DataSource) envCtx.lookup((String) rawDataSource); 115 } 116 catch (NamingException ex) { 117 dataSource = getDataSource((String) rawDataSource); 118 } 119 } 120 else if (rawDataSource instanceof DataSource) { 121 dataSource = (DataSource) rawDataSource; 122 } 123 else { 124 throw new JellyException(Resources.getMessage("SQL_DATASOURCE_INVALID_TYPE")); 125 } 126 127 return dataSource; 128 } 129 130 /*** 131 * Parse JDBC parameters and setup dataSource appropriately 132 */ 133 private static DataSource getDataSource(String params) throws JellyException { 134 DataSourceWrapper dataSource = new DataSourceWrapper(); 135 136 String[] paramString = new String[4]; 137 int escCount = 0; 138 int aryCount = 0; 139 int begin = 0; 140 141 for (int index = 0; index < params.length(); index++) { 142 char nextChar = params.charAt(index); 143 if (TOKEN.indexOf(nextChar) != -1) { 144 if (escCount == 0) { 145 paramString[aryCount] = params.substring(begin, index); 146 begin = index + 1; 147 if (++aryCount > 4) { 148 throw new JellyException(Resources.getMessage("JDBC_PARAM_COUNT")); 149 } 150 } 151 } 152 if (ESCAPE.indexOf(nextChar) != -1) { 153 escCount++; 154 } 155 else { 156 escCount = 0; 157 } 158 } 159 paramString[aryCount] = params.substring(begin); 160 161 // use the JDBC URL from the parameter string 162 dataSource.setJdbcURL(paramString[0]); 163 164 // try to load a driver if it's present 165 if (paramString[1] != null) { 166 try { 167 dataSource.setDriverClassName(paramString[1]); 168 } 169 catch (Exception ex) { 170 throw new JellyException( 171 Resources.getMessage("DRIVER_INVALID_CLASS", ex.getMessage())); 172 } 173 } 174 175 // set the username and password 176 dataSource.setUserName(paramString[2]); 177 dataSource.setPassword(paramString[3]); 178 179 return dataSource; 180 } 181 182 }

This page was automatically generated by Maven