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 org.apache.commons.jelly.JellyTagException; 20 import org.apache.commons.jelly.TagSupport; 21 import org.apache.commons.jelly.XMLOutput; 22 23 /*** 24 * <p>Tag handler for <Driver> in JSTL, used to create 25 * a simple DataSource for prototyping.</p> 26 * 27 * @author Hans Bergsten 28 */ 29 public class DriverTag extends TagSupport { 30 private static final String DRIVER_CLASS_NAME = 31 "javax.servlet.jsp.jstl.sql.driver"; 32 private static final String JDBC_URL = "javax.servlet.jsp.jstl.sql.jdbcURL"; 33 private static final String USER_NAME = "javax.servlet.jsp.jstl.sql.userName"; 34 private static final String PASSWORD = "javax.servlet.jsp.jstl.sql.password"; 35 36 private String driverClassName; 37 private String jdbcURL; 38 private String scope = "page"; 39 private String userName; 40 private String var; 41 42 //********************************************************************** 43 // Accessor methods 44 45 public void setDriver(String driverClassName) { 46 this.driverClassName = driverClassName; 47 } 48 49 public void setJdbcURL(String jdbcURL) { 50 this.jdbcURL = jdbcURL; 51 } 52 53 /** 54 * Sets the scope of the variable to hold the 55 * result. 56 * 57 */ 58 public void setScope(String scopeName) { 59 this.scope = scopeName; 60 } 61 62 public void setUserName(String userName) { 63 this.userName = userName; 64 } 65 66 public void setVar(String var) { 67 this.var = var; 68 } 69 70 //********************************************************************** 71 // Tag logic 72 73 public void doTag(XMLOutput output) throws JellyTagException { 74 DataSourceWrapper ds = new DataSourceWrapper(); 75 try { 76 ds.setDriverClassName(getDriverClassName()); 77 } 78 catch (Exception e) { 79 throw new JellyTagException("Invalid driver class name: " + e.getMessage()); 80 } 81 ds.setJdbcURL(getJdbcURL()); 82 ds.setUserName(getUserName()); 83 ds.setPassword(getPassword()); 84 context.setVariable(var, ds); 85 } 86 87 //********************************************************************* 88 // Private utility methods 89 90 private String getDriverClassName() { 91 if (driverClassName != null) { 92 return driverClassName; 93 } 94 return getInitParameter(DRIVER_CLASS_NAME); 95 } 96 97 private String getJdbcURL() { 98 if (jdbcURL != null) { 99 return jdbcURL; 100 } 101 return getInitParameter(JDBC_URL); 102 } 103 104 private String getUserName() { 105 if (userName != null) { 106 return userName; 107 } 108 return getInitParameter(USER_NAME); 109 } 110 111 private String getPassword() { 112 return getInitParameter(PASSWORD); 113 } 114 115 protected String getInitParameter(String key) { 116 return ""; 117 } 118 }