KickJava   Java API By Example, From Geeks To Geeks.

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


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.jsp.JspException JavaDoc;
20 import javax.servlet.jsp.JspTagException JavaDoc;
21 import javax.servlet.jsp.PageContext JavaDoc;
22 import javax.servlet.jsp.jstl.core.Config;
23 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
24 import javax.sql.DataSource JavaDoc;
25
26 import org.apache.taglibs.standard.resources.Resources;
27 import org.apache.taglibs.standard.tag.common.core.Util;
28
29
30 /**
31  * <p>Tag handler for &lt;SetDataSource&gt; in JSTL, used to create
32  * a simple DataSource for prototyping.</p>
33  *
34  * @author Hans Bergsten
35  * @author Justyna Horwat
36  */

37 public class SetDataSourceTagSupport extends TagSupport JavaDoc {
38
39     protected Object JavaDoc dataSource;
40     protected boolean dataSourceSpecified;
41     protected String JavaDoc jdbcURL;
42     protected String JavaDoc driverClassName;
43     protected String JavaDoc userName;
44     protected String JavaDoc password;
45
46     private int scope;
47     private String JavaDoc var;
48
49
50     //*********************************************************************
51
// Constructor and initialization
52

53     public SetDataSourceTagSupport() {
54     super();
55     init();
56     }
57
58     private void init() {
59     dataSource = null;
60     dataSourceSpecified = false;
61     jdbcURL = driverClassName = userName = password = null;
62     var = null;
63     scope = PageContext.PAGE_SCOPE;
64     }
65
66
67     //*********************************************************************
68
// Accessor methods
69

70     /**
71      * Setter method for the scope of the variable to hold the
72      * result.
73      *
74      */

75     public void setScope(String JavaDoc scope) {
76         this.scope = Util.getScope(scope);
77     }
78
79     public void setVar(String JavaDoc var) {
80     this.var = var;
81     }
82
83
84     //*********************************************************************
85
// Tag logic
86

87     public int doStartTag() throws JspException JavaDoc {
88         DataSource JavaDoc ds;
89
90         if (dataSource != null) {
91             ds = DataSourceUtil.getDataSource(dataSource, pageContext);
92         } else {
93         if (dataSourceSpecified) {
94         throw new JspException JavaDoc(
95                     Resources.getMessage("SQL_DATASOURCE_NULL"));
96         }
97
98             DataSourceWrapper dsw = new DataSourceWrapper();
99             try {
100                 // set driver class iff provided by the tag
101
if (driverClassName != null) {
102                     dsw.setDriverClassName(driverClassName);
103                 }
104             }
105             catch (Exception JavaDoc e) {
106                 throw new JspTagException JavaDoc(
107                     Resources.getMessage("DRIVER_INVALID_CLASS",
108                      e.toString()), e);
109             }
110             dsw.setJdbcURL(jdbcURL);
111             dsw.setUserName(userName);
112             dsw.setPassword(password);
113         ds = (DataSource JavaDoc) dsw;
114         }
115
116         if (var != null) {
117         pageContext.setAttribute(var, ds, scope);
118         } else {
119             Config.set(pageContext, Config.SQL_DATA_SOURCE, ds, scope);
120         }
121
122     return SKIP_BODY;
123     }
124
125     // Releases any resources we may have (or inherit)
126
public void release() {
127     init();
128     }
129 }
130
Popular Tags