KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > proxool > configuration > ServletConfigurator


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.proxool.configuration;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.logicalcobwebs.proxool.ProxoolException;
11 import org.logicalcobwebs.proxool.ProxoolFacade;
12
13 import javax.servlet.ServletConfig JavaDoc;
14 import javax.servlet.ServletException JavaDoc;
15 import javax.servlet.http.HttpServlet JavaDoc;
16 import java.io.File JavaDoc;
17 import java.util.Enumeration JavaDoc;
18 import java.util.Properties JavaDoc;
19
20 /**
21  * <p>Allows you to configure Proxool using a servlet. There are three
22  * different ways:
23  *
24  * The init parameters
25  * can either directly configure Proxool (in a similar fashion to the
26  * PropertyConfigurator) or they can point to separate XML or
27  * property files. For example:</p>
28  *
29  * <p><b>1. XML file</b> delegates to {@link JAXPConfigurator} passing
30  * in the filename. If the filename is not absolute then it is prepended
31  * with the application directory.</p>
32  *
33   *<pre>
34  * &lt;servlet&gt;
35  * &lt;servlet-name&gt;ServletConfigurator&lt;/servlet-name&gt;
36  * &lt;servlet-class&gt;org.logicalcobwebs.proxool.configuration.ServletConfigurator&lt;/servlet-class&gt;
37  * &lt;init-param&gt;
38  * &lt;param-name&gt;xmlFile&lt;/param-name&gt;
39  * &lt;param-value&gt;WEB-INF/proxool.xml&lt;/param-value&gt;
40  * &lt;/init-param&gt;
41  * &lt;/servlet&gt;
42  * </pre>
43  *
44  * <b>2. Property file</b> delegates to {@link PropertyConfigurator}
45  * passing in the filename. If the filename is not absolute then it is prepended
46  * with the application directory.
47  *
48  *<pre>
49  * &lt;servlet&gt;
50  * &lt;servlet-name&gt;ServletConfigurator&lt;/servlet-name&gt;
51  * &lt;servlet-class&gt;org.logicalcobwebs.proxool.configuration.ServletConfigurator&lt;/servlet-class&gt;
52  * &lt;init-param&gt;
53  * &lt;param-name&gt;propertyFile&lt;/param-name&gt;
54  * &lt;param-value&gt;WEB-INF/proxool.properties&lt;/param-value&gt;
55  * &lt;/init-param&gt;
56  * &lt;/servlet&gt;
57  * </pre>
58  *
59  * <b>3. Init parameters</b> delegates to {@link PropertyConfigurator}
60  * by passing in a new Properties object based on the servlet's init
61  * parameters.
62  *
63  *<pre>
64  * &lt;servlet&gt;
65  * &lt;servlet-name&gt;ServletConfigurator&lt;/servlet-name&gt;
66  * &lt;servlet-class&gt;org.logicalcobwebs.proxool.configuration.ServletConfigurator&lt;/servlet-class&gt;
67  * &lt;init-param&gt;
68  * &lt;param-name&gt;jdbc-0.proxool.alias&lt;/param-name&gt;
69  * &lt;param-value&gt;test&lt;/param-value&gt;
70  * &lt;/init-param&gt;
71  * &lt;init-param&gt;
72  * &lt;param-name&gt;jdbc-0.proxool.driver-url&lt;/param-name&gt;
73  * &lt;param-value&gt;jdbc:hsqldb:.&lt;/param-value&gt;
74  * &lt;/init-param&gt;
75  * &lt;init-param&gt;
76  * &lt;param-name&gt;jdbc-0.proxool.driver-class&lt;/param-name&gt;
77  * &lt;param-value&gt;org.hsqldb.jdbcDriver&lt;/param-value&gt;
78  * &lt;/init-param&gt;
79  * &lt;/servlet&gt;
80  * </pre>
81  *
82  * <p>It will also automatically shutdown Proxool. See
83  * {@link #destroy}.</p>
84  *
85  * @version $Revision: 1.7 $, $Date: 2006/01/18 14:39:58 $
86  * @author bill
87  * @author $Author: billhorsman $ (current maintainer)
88  * @since Proxool 0.7
89  */

90 public class ServletConfigurator extends HttpServlet JavaDoc {
91
92     private static final Log LOG = LogFactory.getLog(ServletConfigurator.class);
93
94     private static final String JavaDoc XML_FILE_PROPERTY = "xmlFile";
95
96     private static final String JavaDoc PROPERTY_FILE_PROPERTY = "propertyFile";
97
98     private static final String JavaDoc AUTO_SHUTDOWN_PROPERTY = "autoShutdown";
99
100     private boolean autoShutdown = true;
101
102     public void init(ServletConfig JavaDoc servletConfig) throws ServletException JavaDoc {
103         super.init(servletConfig);
104
105         String JavaDoc appDir = servletConfig.getServletContext().getRealPath("/");
106
107         Properties JavaDoc properties = new Properties JavaDoc();
108
109         Enumeration JavaDoc names = servletConfig.getInitParameterNames();
110         while (names.hasMoreElements()) {
111             String JavaDoc name = (String JavaDoc) names.nextElement();
112             String JavaDoc value = servletConfig.getInitParameter(name);
113
114             if (name.equals(XML_FILE_PROPERTY)) {
115                 try {
116                     File JavaDoc file = new File JavaDoc(value);
117                     if (file.isAbsolute()) {
118                         JAXPConfigurator.configure(value, false);
119                     } else {
120                         JAXPConfigurator.configure(appDir + File.separator + value, false);
121                     }
122                 } catch (ProxoolException e) {
123                     LOG.error("Problem configuring " + value, e);
124                 }
125             } else if (name.equals(PROPERTY_FILE_PROPERTY)) {
126                 try {
127                     File JavaDoc file = new File JavaDoc(value);
128                     if (file.isAbsolute()) {
129                         PropertyConfigurator.configure(value);
130                     } else {
131                         PropertyConfigurator.configure(appDir + File.separator + value);
132                     }
133                 } catch (ProxoolException e) {
134                     LOG.error("Problem configuring " + value, e);
135                 }
136             } else if (name.equals(AUTO_SHUTDOWN_PROPERTY)) {
137                 autoShutdown = Boolean.valueOf(value).booleanValue();
138             } else if (name.startsWith(PropertyConfigurator.PREFIX)) {
139                 properties.setProperty(name, value);
140             }
141         }
142
143         if (properties.size() > 0) {
144             try {
145                 PropertyConfigurator.configure(properties);
146             } catch (ProxoolException e) {
147                 LOG.error("Problem configuring using init properties", e);
148             }
149         }
150     }
151
152     /**
153      * Shuts down Proxool by removing all connection pools. If you want
154      * to disable this behaviour then use:
155      * <pre>
156      * &lt;init-param&gt;
157      * &lt;param-name&gt;autoShutdown&lt;/param-name&gt;
158      * &lt;param-value&gt;false&lt;/param-value&gt;
159      * &lt;/init-param&gt;
160      * </pre>
161      */

162     public void destroy() {
163         if (autoShutdown) {
164             ProxoolFacade.shutdown(0);
165         }
166     }
167 }
168
169
170 /*
171  Revision history:
172  $Log: ServletConfigurator.java,v $
173  Revision 1.7 2006/01/18 14:39:58 billhorsman
174  Unbundled Jakarta's Commons Logging.
175
176  Revision 1.6 2003/03/10 15:26:54 billhorsman
177  refactoringn of concurrency stuff (and some import
178  optimisation)
179
180  Revision 1.5 2003/03/03 11:12:00 billhorsman
181  fixed licence
182
183  Revision 1.4 2003/02/07 17:26:25 billhorsman
184  use shutdown() instead of removeAllConnectionPools()
185
186  Revision 1.3 2003/02/06 17:41:05 billhorsman
187  now uses imported logging
188
189  Revision 1.2 2003/02/06 15:45:26 billhorsman
190  trivial doc changes
191
192  Revision 1.1 2003/02/05 15:03:49 billhorsman
193  new configuration servlet.
194
195  */
Popular Tags