1 16 17 package org.apache.taglibs.standard.tag.common.sql; 18 19 import javax.servlet.ServletContext ; 20 import javax.servlet.jsp.JspException ; 21 import javax.servlet.jsp.JspTagException ; 22 import javax.servlet.jsp.PageContext ; 23 import javax.servlet.jsp.tagext.TagSupport ; 24 25 31 public class DriverTag extends TagSupport { 32 private static final String DRIVER_CLASS_NAME = 33 "javax.servlet.jsp.jstl.sql.driver"; 34 private static final String JDBC_URL = 35 "javax.servlet.jsp.jstl.sql.jdbcURL"; 36 private static final String USER_NAME = 37 "javax.servlet.jsp.jstl.sql.userName"; 38 private static final String PASSWORD = 39 "javax.servlet.jsp.jstl.sql.password"; 40 41 private String driverClassName; 42 private String jdbcURL; 43 private int scope = PageContext.PAGE_SCOPE; 44 private String userName; 45 private String var; 46 47 50 public void setDriver(String driverClassName) { 51 this.driverClassName = driverClassName; 52 } 53 54 public void setJdbcURL(String jdbcURL) { 55 this.jdbcURL = jdbcURL; 56 } 57 58 63 public void setScope(String scopeName) { 64 if ("page".equals(scopeName)) { 65 scope = PageContext.PAGE_SCOPE; 66 } 67 else if ("request".equals(scopeName)) { 68 scope = PageContext.REQUEST_SCOPE; 69 } 70 else if ("session".equals(scopeName)) { 71 scope = PageContext.SESSION_SCOPE; 72 } 73 else if ("application".equals(scopeName)) { 74 scope = PageContext.APPLICATION_SCOPE; 75 } 76 } 77 78 public void setUserName(String userName) { 79 this.userName = userName; 80 } 81 82 public void setVar(String var) { 83 this.var = var; 84 } 85 86 89 public int doStartTag() throws JspException { 90 DataSourceWrapper ds = new DataSourceWrapper(); 91 try { 92 ds.setDriverClassName(getDriverClassName()); 93 } 94 catch (Exception e) { 95 throw new JspTagException ("Invalid driver class name: " + 96 e.toString(), e); 97 } 98 ds.setJdbcURL(getJdbcURL()); 99 ds.setUserName(getUserName()); 100 ds.setPassword(getPassword()); 101 pageContext.setAttribute(var, ds, scope); 102 return SKIP_BODY; 103 } 104 105 106 109 private String getDriverClassName() { 110 if (driverClassName != null) { 111 return driverClassName; 112 } 113 ServletContext application = pageContext.getServletContext(); 114 return application.getInitParameter(DRIVER_CLASS_NAME); 115 } 116 117 private String getJdbcURL() { 118 if (jdbcURL != null) { 119 return jdbcURL; 120 } 121 ServletContext application = pageContext.getServletContext(); 122 return application.getInitParameter(JDBC_URL); 123 } 124 125 private String getUserName() { 126 if (userName != null) { 127 return userName; 128 } 129 ServletContext application = pageContext.getServletContext(); 130 return application.getInitParameter(USER_NAME); 131 } 132 133 private String getPassword() { 134 ServletContext application = pageContext.getServletContext(); 135 return application.getInitParameter(PASSWORD); 136 } 137 } 138 | Popular Tags |