1 /*
2 * Copyright 2001,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.commons.scaffold.sql;
18
19
20 import java.sql.SQLException;
21
22 import javax.servlet.http.HttpServlet;
23 import javax.sql.DataSource;
24
25
26 /**
27 * Connection adaptor for a connection pool that is exposed
28 * through the servlet context, like the Struts generic
29 * connection pool.
30 * <p>
31 * The ConnectionServlet will automatically set a reference
32 * to itself if an instance of this class is specified
33 * as the ConnectionAdaptor.
34 *
35 * @author Ted Husted
36 * @version $Revision: 155464 $ $Date: 2005-02-26 13:26:54 +0000 (Sat, 26 Feb 2005) $
37 */
38 public class ServletAdaptor extends ConnectionAdaptor {
39
40
41 /**
42 * Field to store a link to a servlet in the application.
43 */
44 private static HttpServlet servlet = null;
45
46
47 /**
48 * Set a reference to a servlet in the application
49 * to allow access to the servlet context.
50 */
51 public void setServlet(HttpServlet servlet) {
52 ServletAdaptor.servlet = servlet;
53 }
54
55
56 /**
57 * Return datasource from the servlet context using
58 * key as the attribute name.
59 *
60 * @param key The attribute name for the resource.
61 * If null is passed, null is returned.
62 * @returns null or the datasource object related to "key"
63 */
64 protected DataSource getDataSource(String key)
65 throws SQLException {
66
67 if (null==key) return null;
68
69 return (DataSource)
70 servlet.getServletContext().getAttribute(key);
71
72 }
73
74
75 // Required
76 public ServletAdaptor() {
77
78 if (null==pool) pool = this;
79
80 }
81
82 } // end ServletAdaptor
83