KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jstl > el > SqlSetDataSourceTag


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

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 JavaDoc;
37 import javax.servlet.jsp.jstl.core.Config;
38 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
39 import javax.sql.DataSource JavaDoc;
40 import java.io.PrintWriter JavaDoc;
41 import java.sql.Connection JavaDoc;
42 import java.sql.Driver JavaDoc;
43 import java.sql.SQLException JavaDoc;
44 import java.util.Properties JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 public class SqlSetDataSourceTag extends TagSupport JavaDoc {
48   private static final Logger JavaDoc 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 JavaDoc _var;
59   private String JavaDoc _scope;
60
61   /**
62    * Sets the JSP-EL expression for the dataSource.
63    */

64   public void setDataSource(Expr dataSource)
65   {
66     _dataSource = dataSource;
67   }
68
69   /**
70    * Sets the JSP-EL expression for the URL.
71    */

72   public void setUrl(Expr url)
73   {
74     _url = url;
75   }
76
77   /**
78    * Sets the JSP-EL expression for the driver.
79    */

80   public void setDriver(Expr driver)
81   {
82     _driver = driver;
83   }
84
85   /**
86    * Sets the JSP-EL expression for the user.
87    */

88   public void setUser(Expr user)
89   {
90     _user = user;
91   }
92
93   /**
94    * Sets the JSP-EL expression for the password.
95    */

96   public void setPassword(Expr password)
97   {
98     _password = password;
99   }
100
101   /**
102    * Sets the variable name.
103    */

104   public void setVar(String JavaDoc var)
105   {
106     _var = var;
107   }
108
109   /**
110    * Sets the scope.
111    */

112   public void setScope(String JavaDoc scope)
113   {
114     _scope = scope;
115   }
116
117   public int doStartTag() throws JspException JavaDoc
118   {
119     try {
120       String JavaDoc var = _var;
121
122       if (var == null)
123     var = Config.SQL_DATA_SOURCE;
124
125       ELContext env = pageContext.getELContext();
126
127       DataSource JavaDoc dataSource = null;
128
129       if (_dataSource != null) {
130     Object JavaDoc 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 JavaDoc e) {
145       throw new JspException JavaDoc(e);
146     }
147   }
148
149   public static DataSource JavaDoc openDataSource(String JavaDoc driverClass, String JavaDoc url,
150                       String JavaDoc user, String JavaDoc password)
151     throws Exception JavaDoc
152   {
153     ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
154     
155     Class JavaDoc cl = Class.forName(driverClass, false, loader);
156     Driver JavaDoc driver = (Driver JavaDoc) cl.newInstance();
157     
158     return new DataSourceAdapter(driver, url, user, password);
159   }
160
161   public static class DataSourceAdapter implements DataSource JavaDoc {
162     private Driver JavaDoc _driver;
163     private String JavaDoc _url;
164     private String JavaDoc _user;
165     private String JavaDoc _password;
166
167     public DataSourceAdapter(Driver JavaDoc driver, String JavaDoc url,
168                  String JavaDoc user, String JavaDoc password)
169     {
170       _driver = driver;
171       _url = url;
172       _user = user;
173       _password = password;
174     }
175
176     public Connection JavaDoc getConnection(String JavaDoc user, String JavaDoc password)
177       throws SQLException JavaDoc
178     {
179       Properties JavaDoc props = new Properties JavaDoc();
180       props.put("user", user);
181       props.put("password", user);
182       
183       return _driver.connect(_url, props);
184     }
185
186     public Connection JavaDoc getConnection()
187       throws SQLException JavaDoc
188     {
189       Properties JavaDoc props = new Properties JavaDoc();
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 JavaDoc out)
199     {
200     }
201
202     public PrintWriter JavaDoc 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