1 28 29 package com.caucho.jstl.el; 30 31 import com.caucho.el.Expr; 32 import com.caucho.log.Log; 33 import com.caucho.util.L10N; 34 35 import javax.el.ELContext; 36 import javax.servlet.jsp.JspException ; 37 import javax.servlet.jsp.jstl.core.Config; 38 import javax.servlet.jsp.tagext.TagSupport ; 39 import javax.sql.DataSource ; 40 import java.io.PrintWriter ; 41 import java.sql.Connection ; 42 import java.sql.Driver ; 43 import java.sql.SQLException ; 44 import java.util.Properties ; 45 import java.util.logging.Logger ; 46 47 public class SqlSetDataSourceTag extends TagSupport { 48 private static final Logger log = Log.open(SqlSetDataSourceTag.class); 49 private static final L10N L = new L10N(SqlSetDataSourceTag.class); 50 51 private Expr _dataSource; 52 53 private Expr _url; 54 private Expr _driver; 55 private Expr _user; 56 private Expr _password; 57 58 private String _var; 59 private String _scope; 60 61 64 public void setDataSource(Expr dataSource) 65 { 66 _dataSource = dataSource; 67 } 68 69 72 public void setUrl(Expr url) 73 { 74 _url = url; 75 } 76 77 80 public void setDriver(Expr driver) 81 { 82 _driver = driver; 83 } 84 85 88 public void setUser(Expr user) 89 { 90 _user = user; 91 } 92 93 96 public void setPassword(Expr password) 97 { 98 _password = password; 99 } 100 101 104 public void setVar(String var) 105 { 106 _var = var; 107 } 108 109 112 public void setScope(String scope) 113 { 114 _scope = scope; 115 } 116 117 public int doStartTag() throws JspException 118 { 119 try { 120 String var = _var; 121 122 if (var == null) 123 var = Config.SQL_DATA_SOURCE; 124 125 ELContext env = pageContext.getELContext(); 126 127 DataSource dataSource = null; 128 129 if (_dataSource != null) { 130 Object ds = _dataSource.evalObject(env); 131 132 dataSource = SqlQueryTag.getDataSource(pageContext, ds); 133 } 134 else { 135 dataSource = openDataSource(_driver.evalString(env), 136 _url.evalString(env), 137 _user != null ? _user.evalString(env) : null, 138 _password != null ? _password.evalString(env) : null); 139 } 140 141 CoreSetTag.setValue(pageContext, var, _scope, dataSource); 142 143 return SKIP_BODY; 144 } catch (Exception e) { 145 throw new JspException (e); 146 } 147 } 148 149 public static DataSource openDataSource(String driverClass, String url, 150 String user, String password) 151 throws Exception 152 { 153 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 154 155 Class cl = Class.forName(driverClass, false, loader); 156 Driver driver = (Driver ) cl.newInstance(); 157 158 return new DataSourceAdapter(driver, url, user, password); 159 } 160 161 public static class DataSourceAdapter implements DataSource { 162 private Driver _driver; 163 private String _url; 164 private String _user; 165 private String _password; 166 167 public DataSourceAdapter(Driver driver, String url, 168 String user, String password) 169 { 170 _driver = driver; 171 _url = url; 172 _user = user; 173 _password = password; 174 } 175 176 public Connection getConnection(String user, String password) 177 throws SQLException 178 { 179 Properties props = new Properties (); 180 props.put("user", user); 181 props.put("password", user); 182 183 return _driver.connect(_url, props); 184 } 185 186 public Connection getConnection() 187 throws SQLException 188 { 189 Properties props = new Properties (); 190 if (_user != null) 191 props.put("user", _user); 192 if (_password != null) 193 props.put("password", _password); 194 195 return _driver.connect(_url, props); 196 } 197 198 public void setLogWriter(PrintWriter out) 199 { 200 } 201 202 public PrintWriter getLogWriter() 203 { 204 return null; 205 } 206 207 public void setLoginTimeout(int timeout) 208 { 209 } 210 211 public int getLoginTimeout() 212 { 213 return -1; 214 } 215 } 216 } 217 | Popular Tags |