KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hsqldb > WebServer


1 /* Copyright (c) 2001-2005, The HSQL Development Group
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * Neither the name of the HSQL Development Group nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30
31
32 package org.hsqldb;
33
34 import org.hsqldb.lib.FileUtil;
35 import org.hsqldb.persist.HsqlProperties;
36 import org.hsqldb.resources.BundleHandler;
37
38 // fredt@users 20020215 - patch 1.7.0 by fredt
39
// method rorganised to use new HsqlServerProperties class
40
// unsaved@users 20021113 - patch 1.7.2 - SSL support
41
// boucherb@users 20030510 - patch 1.7.2 - SSL support moved to factory interface
42
// boucherb@users 20030510 - patch 1.7.2 - moved all common code to Server
43
// boucherb@users 20030510 - patch 1.7.2 - general lint removal
44

45 /**
46  * The HSQLDB HTTP protocol network database server. <p>
47  *
48  * WebServer has two distinct functions:<p>
49  *
50  * The primary function is to allow client/server access to HSQLDB databases
51  * via the HTTP protocol. This protocol is less efficient than the HSQL
52  * protocol used by the Server class and should be used only in situations
53  * where sandboxes or firewalls between the client and the server do not
54  * allow the use of the HSQL protocol. One example is client/server access by
55  * an applet running in browsers on remote hosts and accessing the database
56  * engine on the HTTP server from which the applet originated. From version
57  * 1.7.2, HTTP database connections are persistent and support transactions.
58  * Similar to HSQL connections, they should be explicitly closed to free the
59  * server resources. <p>
60  *
61  * The secondary function of WebServer is to act as a simple general purpose
62  * HTTP server. It is aimed to support the minimum requirements set out by
63  * the HTTP/1.0 standard. The HEAD and GET methods can be used to query and
64  * retreive static files from the HTTP server.<p>
65  *
66  * Both the database server and HTTP server functions of WebServer can be
67  * configured with the webserver.properties file. It contains entries for the
68  * database server similar to those for the HSQL protocol Server class. In
69  * addition, a list mapping different file endings to their mime types may be
70  * included in this file. (fredt@users) <p>
71  *
72  * From the command line, the options are as follows: <p>
73  * <pre>
74  * +----------------+-------------+----------+------------------------------+
75  * | OPTION | TYPE | DEFAULT | DESCRIPTION |
76  * +----------------+-------------+----------+------------------------------|
77  * | -? | -- | -- | prints this message |
78  * | -address | name|number | any | server inet address |
79  * | -port | number | 80 | port at which server listens |
80  * | -database.i | [type]spec | 0=test | path of database i |
81  * | -dbname.i | alias | -- | url alias for database i |
82  * | -silent | true|false | true | false => display all queries |
83  * | -trace | true|false | false | display JDBC trace messages |
84  * | -no_system_exit| true|false | false | do not issue System.exit() |
85  * +----------------+-------------+----------+------------------------------+
86  * </pre>
87  *
88  * Example of the webserver.properties file:
89  *
90  * <pre>
91  * server.port=80
92  * server.database.0=test
93  * server.dbname.0=...
94  * ...
95  * server.database.n=...
96  * server.dbname.n=...
97  * server.silent=true
98  *
99  * .htm=text/html
100  * .html=text/html
101  * .txt=text/plain
102  * .gif=image/gif
103  * .class=application/octet-stream
104  * .jpg=image/jpeg
105  * .jgep=image/jpeg
106  * .zip=application/x-zip-compressed
107  * </pre>
108  *
109  * <ul>
110  * <li>For server.root, use '/' as the separator, even for DOS/Windows.
111  * <li>File extensions for mime types must be lowercase and start with '.'
112  * </ul>
113  *
114  * Replaces original Hypersonic class of the same name.
115  *
116  * @author fredt@users
117  * @author boucherb@users
118  * @version 1.7.2
119  * @since 1.7.2
120  */

121 public class WebServer extends Server {
122
123     /**
124      * Handle to resource bundle providing i18n for things like
125      * HTTP error pages.
126      */

127     static int webBundleHandle = BundleHandler.getBundleHandle("webserver",
128         null);
129
130     public WebServer() {
131         super(ServerConstants.SC_PROTOCOL_HTTP);
132     }
133
134     /**
135      * Starts a new WebServer.
136      *
137      * @param args the "command line" parameters with which to start
138      * the WebServer. "-?" will cause the command line arguments
139      * help to be printed to the standard output
140      */

141     public static void main(String JavaDoc[] args) {
142
143         String JavaDoc propsPath = FileUtil.canonicalOrAbsolutePath("webserver");
144         HsqlProperties fileProps =
145             ServerConfiguration.getPropertiesFromFile(propsPath);
146         HsqlProperties props = fileProps == null ? new HsqlProperties()
147                                                  : fileProps;
148         HsqlProperties stringProps = HsqlProperties.argArrayToProps(args,
149             ServerConstants.SC_KEY_PREFIX);
150
151         if (stringProps != null) {
152             if (stringProps.getErrorKeys().length != 0) {
153                 printHelp("webserver.help");
154
155                 return;
156             }
157
158             props.addProperties(stringProps);
159         }
160
161         ServerConfiguration.translateDefaultDatabaseProperty(props);
162
163         // Standard behaviour when started from the command line
164
// is to halt the VM when the server shuts down. This may, of
165
// course, be overridden by whatever, if any, security policy
166
// is in place.
167
ServerConfiguration.translateDefaultNoSystemExitProperty(props);
168
169         // finished setting up properties;
170
Server server = new WebServer();
171
172         server.setProperties(props);
173
174         // now messages go to the channel specified in properties
175
server.print("Startup sequence initiated from main() method");
176
177         if (fileProps != null) {
178             server.print("Loaded properties from [" + propsPath
179                          + ".properties]");
180         } else {
181             server.print("Could not load properties from file");
182             server.print("Using cli/default properties only");
183         }
184
185         server.start();
186     }
187
188     /**
189      * Retrieves the name of the web page served when no page is specified.
190      * This attribute is relevant only when server protocol is HTTP(S).
191      *
192      * @return the name of the web page served when no page is specified
193      *
194      * @jmx.managed-attribute
195      * access="read-write"
196      * description="Used when server protocol is HTTP(S)"
197      */

198     public String JavaDoc getDefaultWebPage() {
199         return serverProperties.getProperty(
200             ServerConstants.SC_KEY_WEB_DEFAULT_PAGE);
201     }
202
203     /**
204      * Retrieves a String object describing the command line and
205      * properties options for this Server.
206      *
207      * @return the command line and properties options help for this Server
208      */

209     public String JavaDoc getHelpString() {
210         return BundleHandler.getString(serverBundleHandle, "webserver.help");
211     }
212
213     /**
214      * Retrieves this server's product name. <p>
215      *
216      * Typically, this will be something like: "HSQLDB xxx server".
217      *
218      * @return the product name of this server
219      *
220      * @jmx.managed-attribute
221      * access="read-only"
222      * description="Of Server"
223      */

224     public String JavaDoc getProductName() {
225         return "HSQLDB web server";
226     }
227
228     /**
229      * Retrieves a string respresentaion of the network protocol
230      * this server offers, typically one of 'HTTP', HTTPS', 'HSQL' or 'HSQLS'.
231      *
232      * @return string respresentation of this server's protocol
233      *
234      * @jmx.managed-attribute
235      * access="read-only"
236      * description="Used to handle connections"
237      */

238     public String JavaDoc getProtocol() {
239         return isTls() ? "HTTPS"
240                        : "HTTP";
241     }
242
243     /**
244      * Retrieves the root context (directory) from which web content
245      * is served. This property is relevant only when the server
246      * protocol is HTTP(S). Although unlikely, it may be that in the future
247      * other contexts, such as jar urls may be supported, so that pages can
248      * be served from the contents of a jar or from the JVM class path.
249      *
250      * @return the root context (directory) from which web content is served
251      *
252      * @jmx.managed-attribute
253      * access="read-write"
254      * description="Context (directory)"
255      */

256     public String JavaDoc getWebRoot() {
257         return serverProperties.getProperty(ServerConstants.SC_KEY_WEB_ROOT);
258     }
259 }
260
Popular Tags