KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > standard > tag > common > sql > DriverTag


1 /*
2  * Copyright 1999-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.taglibs.standard.tag.common.sql;
18
19 import javax.servlet.ServletContext JavaDoc;
20 import javax.servlet.jsp.JspException JavaDoc;
21 import javax.servlet.jsp.JspTagException JavaDoc;
22 import javax.servlet.jsp.PageContext JavaDoc;
23 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
24
25 /**
26  * <p>Tag handler for &lt;Driver&gt; in JSTL, used to create
27  * a simple DataSource for prototyping.</p>
28  *
29  * @author Hans Bergsten
30  */

31 public class DriverTag extends TagSupport JavaDoc {
32     private static final String JavaDoc DRIVER_CLASS_NAME =
33     "javax.servlet.jsp.jstl.sql.driver";
34     private static final String JavaDoc JDBC_URL =
35     "javax.servlet.jsp.jstl.sql.jdbcURL";
36     private static final String JavaDoc USER_NAME =
37     "javax.servlet.jsp.jstl.sql.userName";
38     private static final String JavaDoc PASSWORD =
39     "javax.servlet.jsp.jstl.sql.password";
40
41     private String JavaDoc driverClassName;
42     private String JavaDoc jdbcURL;
43     private int scope = PageContext.PAGE_SCOPE;
44     private String JavaDoc userName;
45     private String JavaDoc var;
46
47     //*********************************************************************
48
// Accessor methods
49

50     public void setDriver(String JavaDoc driverClassName) {
51     this.driverClassName = driverClassName;
52     }
53
54     public void setJdbcURL(String JavaDoc jdbcURL) {
55     this.jdbcURL = jdbcURL;
56     }
57
58     /**
59      * Setter method for the scope of the variable to hold the
60      * result.
61      *
62      */

63     public void setScope(String JavaDoc 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 JavaDoc userName) {
79     this.userName = userName;
80     }
81
82     public void setVar(String JavaDoc var) {
83     this.var = var;
84     }
85
86     //*********************************************************************
87
// Tag logic
88

89     public int doStartTag() throws JspException JavaDoc {
90     DataSourceWrapper ds = new DataSourceWrapper();
91     try {
92     ds.setDriverClassName(getDriverClassName());
93     }
94     catch (Exception JavaDoc e) {
95         throw new JspTagException JavaDoc("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     //*********************************************************************
107
// Private utility methods
108

109     private String JavaDoc getDriverClassName() {
110     if (driverClassName != null) {
111         return driverClassName;
112     }
113     ServletContext JavaDoc application = pageContext.getServletContext();
114     return application.getInitParameter(DRIVER_CLASS_NAME);
115     }
116
117     private String JavaDoc getJdbcURL() {
118     if (jdbcURL != null) {
119         return jdbcURL;
120     }
121     ServletContext JavaDoc application = pageContext.getServletContext();
122     return application.getInitParameter(JDBC_URL);
123     }
124
125     private String JavaDoc getUserName() {
126     if (userName != null) {
127         return userName;
128     }
129     ServletContext JavaDoc application = pageContext.getServletContext();
130     return application.getInitParameter(USER_NAME);
131     }
132
133     private String JavaDoc getPassword() {
134     ServletContext JavaDoc application = pageContext.getServletContext();
135     return application.getInitParameter(PASSWORD);
136     }
137 }
138
Popular Tags