1 16 17 package org.apache.taglibs.dbtags.connection; 18 19 import java.io.IOException ; 20 import java.sql.Connection ; 21 import java.sql.DriverManager ; 22 import java.sql.SQLException ; 23 24 import javax.naming.Context ; 25 import javax.naming.InitialContext ; 26 import javax.naming.NamingException ; 27 import javax.servlet.jsp.JspTagException ; 28 import javax.servlet.jsp.tagext.BodyTagSupport ; 29 import javax.sql.DataSource ; 30 31 65 public class ConnectionTag extends BodyTagSupport { 66 67 private String _dataSourceName = null; 68 private String _url = null; 69 private String _driver = null; 70 private String _userId = null; 71 private String _password = null; 72 private String _jndiName = null; 73 74 81 public void setDataSource(String dataSourceName) { 82 _dataSourceName = dataSourceName; 83 } 84 85 90 public void setUrl(String url) { 91 _url = url; 92 } 93 94 101 public void setDriver(String driver) { 102 _driver = driver; 103 } 104 105 110 public void setJndiName(String jndiName) { 111 _jndiName = jndiName; 112 } 113 114 121 public void setUserId(String userId) { 122 _userId = userId; 123 } 124 125 132 public void setPassword(String password) { 133 _password = password; 134 } 135 136 public int doStartTag() throws JspTagException { 137 return EVAL_BODY_TAG; 138 } 139 140 public int doAfterBody() throws JspTagException 141 { 142 try { 144 getPreviousOut().write(getBodyContent().getString().trim()); 145 } catch (IOException e) { 146 throw new JspTagException (e.toString()); 147 } 148 return EVAL_PAGE; 149 } 150 151 public int doEndTag() throws JspTagException { 152 153 DataSource dataSource = null; 154 155 try { 156 if (_driver != null) { 157 Class.forName(_driver); 158 } 159 160 Connection conn = null; 161 162 if (_jndiName != null) { 163 try { 164 Context ctx = new InitialContext (); 165 dataSource = (DataSource )ctx.lookup(_jndiName); 166 } catch (NamingException ne) { 167 throw new JspTagException (ne.toString()); 168 } 169 if (_userId != null) { 170 conn = dataSource.getConnection(_userId, _password); 171 } else { 172 conn = dataSource.getConnection(); 173 } 174 } else if (_dataSourceName != null) { 175 dataSource = (DataSource ) pageContext.findAttribute(_dataSourceName); 176 if ( dataSource == null ) { 177 throw new JspTagException ("Did not find a DataSource bean named " + _dataSourceName ); 178 } 179 180 if (_userId != null) { 181 conn = dataSource.getConnection(_userId, _password); 182 } else { 183 conn = dataSource.getConnection(); 184 } 185 } else if (_url != null) { 186 if (_userId != null) { 187 conn = DriverManager.getConnection(_url, _userId, _password); 188 } else { 189 conn = DriverManager.getConnection(_url); 190 } 191 } else { 192 throw new JspTagException ("Cannot connect to database, no database URL or DataSource."); 193 } 194 195 pageContext.setAttribute(getId(),conn); 196 } catch (SQLException e) { 197 throw new JspTagException (e.toString()); 198 } catch (ClassNotFoundException e) { 199 throw new JspTagException ("Driver class '" + _driver + "' not found\n" + 200 e.toString()); 201 } 202 203 return EVAL_PAGE; 204 } 205 206 public void release() { 207 _dataSourceName = null; 208 _url = null; 209 _driver = null; 210 _userId = null; 211 _password = null; 212 } 213 214 } 215 | Popular Tags |