Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ConnectionServlet |
|
| 6.0;6 |
1 | /* | |
2 | * Copyright 2000-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.http; | |
18 | ||
19 | import java.io.IOException; | |
20 | ||
21 | import javax.servlet.ServletException; | |
22 | ||
23 | import org.apache.commons.logging.Log; | |
24 | import org.apache.commons.logging.LogFactory; | |
25 | import org.apache.commons.scaffold.sql.ConnectionAdaptor; | |
26 | import org.apache.commons.scaffold.sql.ServletAdaptor; | |
27 | ||
28 | ||
29 | /** | |
30 | * Initialize a <code>ConnectionAdaptor</code> for this application. | |
31 | * <p> | |
32 | * The servlet itself is <b>not</b> involved in managing the connection. | |
33 | * The ConnectionAdaptor is a standalone object. | |
34 | * This servlet simply provides a means to initialize and load a | |
35 | * ConnectionAdaptor for use by your application. | |
36 | * <p> | |
37 | * This servlet can be configured to use an alternate adaptor or | |
38 | * adaptor key using initialization parameters: | |
39 | * <p> | |
40 | * <b>adaptor</b> The default adaptor is | |
41 | * <code>org.apache.scaffold.sql.ConnectionAdaptor</code>. | |
42 | * Any sublcass of ConnectionAdaptor can be specified instead. | |
43 | * <p> | |
44 | * <b>adaptor.key</b> The default adaptor key is <code>DATA_SOURCE</code>. | |
45 | * Another key can be specified instead. This is the attribute to use | |
46 | * when looking up the datasource in JNDI, the servlet context, or | |
47 | * whatever. | |
48 | * <p> | |
49 | * To use the Struts generic connection pool, specify the adaptor as | |
50 | * <code>org.apache.scaffold.sql.ServletAdaptor</code> and the | |
51 | * adaptor.key as <code>org.apache.struts.action.DATA_SOURCE</code> | |
52 | * <p> | |
53 | * To use the Resin connection pool, specify the <b>res-ref-name</b> | |
54 | * in the Resin conf as the <b>adaptor.key</b>. | |
55 | * <p> | |
56 | * To use Poolman without JNDI, use the Poolman adaptor in the | |
57 | * sql package, and specify the dbName from the poolman.xml as the | |
58 | * <b>adaptor.key</b> | |
59 | * | |
60 | * @see org.apache.scaffold.sql.ConnectionAdaptor | |
61 | * @see org.apache.scaffold.sql.Servletdaptor | |
62 | * @see org.apache.scaffold.sql.PoolmanAdaptor | |
63 | * | |
64 | * @author Ted Husted | |
65 | * @author Steve Raeburn | |
66 | * | |
67 | */ | |
68 | 0 | public class ConnectionServlet extends ResourceServlet { |
69 | ||
70 | // ------------------------------------------------------- Class variables | |
71 | ||
72 | /** | |
73 | * Commons logging instance | |
74 | */ | |
75 | 0 | private static Log log = LogFactory.getLog(ResourceServlet.class); |
76 | ||
77 | ||
78 | /** | |
79 | * Default parameter for setting a custom adaptor type | |
80 | * in the servlet configuration. | |
81 | */ | |
82 | 0 | private static String ADAPTOR_PARAMETER = "adaptor"; |
83 | ||
84 | ||
85 | /** | |
86 | * Default type to use if parameter not set | |
87 | * ["org.apache.scaffold.sql.ConnectionAdaptor"]. | |
88 | */ | |
89 | 0 | private static String ADAPTOR_DEFAULT = |
90 | "org.apache.commons.scaffold.sql.ConnectionAdaptor"; | |
91 | ||
92 | ||
93 | /** | |
94 | * Default parameter for setting the default attribute | |
95 | * name, or key, used to retrieve the datasource. | |
96 | * <p> | |
97 | * For the Struts generic connection pool, you would set | |
98 | * this to "org.apache.struts.action.DATA_SOURCE" | |
99 | * [Action.DATA_SOURCE_KEY]. | |
100 | */ | |
101 | 0 | private static String ADAPTOR_KEY = "adaptor.key"; |
102 | ||
103 | ||
104 | /** | |
105 | * Field to hold our instance of the connection adaptor. | |
106 | * Can be initialized to another ConnectionAdaptor at startup. | |
107 | */ | |
108 | private static ConnectionAdaptor adaptor; | |
109 | ||
110 | ||
111 | // ------------------------------------------------------ Logging Messages | |
112 | ||
113 | ||
114 | /** | |
115 | * Message for verbose logging. | |
116 | */ | |
117 | 0 | private static String ADAPTOR_LOADING_EVENT = |
118 | "Loading ConnectionAdaptor"; | |
119 | ||
120 | ||
121 | /** | |
122 | * Message to log if connector initialization fails. | |
123 | */ | |
124 | 0 | private static String ADAPTOR_FAILED_EVENT = |
125 | "*** ConnectionAdaptor failed to load: "; | |
126 | ||
127 | ||
128 | // ----------------------------------------------------------------------- | |
129 | ||
130 | /** | |
131 | * Initialize the ConnectionAdaptor for this application. | |
132 | * <p> | |
133 | * An alternate ConnectionAdaptor can be specified in the servlet config | |
134 | * by specifying the class type as the <b>adaptor</b> initialization | |
135 | * parameter. | |
136 | * | |
137 | * @exception IOException if an input/output error is encountered | |
138 | * @exception ServletException if we cannot initialize these resources | |
139 | */ | |
140 | protected void initDefault() throws IOException, ServletException { | |
141 | ||
142 | 0 | super.initDefault(); |
143 | 0 | if (log.isDebugEnabled()) { |
144 | 0 | log(ADAPTOR_LOADING_EVENT); |
145 | } | |
146 | ||
147 | // Check for an adaptor parameter | |
148 | 0 | String adaptorClass = |
149 | getInitString(ADAPTOR_PARAMETER,ADAPTOR_DEFAULT); | |
150 | ||
151 | // Create the default or custom adaptor | |
152 | try { | |
153 | 0 | adaptor = (ConnectionAdaptor) |
154 | Class.forName(adaptorClass).newInstance(); | |
155 | } | |
156 | ||
157 | 0 | catch (Throwable t) { |
158 | 0 | log.error(ADAPTOR_FAILED_EVENT,t); |
159 | 0 | t.printStackTrace(); |
160 | 0 | throw new ServletException(t); |
161 | 0 | } |
162 | ||
163 | // Check for an adaptor key | |
164 | 0 | String key = getInitString(ADAPTOR_KEY,null); |
165 | 0 | if (null!=key) adaptor.setKey(key); |
166 | ||
167 | // ** kludge alert ** | |
168 | // If its a servlet adaptor, give it a servlet reference | |
169 | 0 | if (adaptor instanceof ServletAdaptor) { |
170 | // Give adaptor access to servlet context. | |
171 | 0 | ServletAdaptor s = (ServletAdaptor) adaptor; |
172 | 0 | s.setServlet(this); |
173 | } | |
174 | 0 | } |
175 | ||
176 | } // end ConnectionServlet |