001: /* Copyright (c) 2001-2005, The HSQL Development Group
002: * All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * Redistributions of source code must retain the above copyright notice, this
008: * list of conditions and the following disclaimer.
009: *
010: * Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * Neither the name of the HSQL Development Group nor the names of its
015: * contributors may be used to endorse or promote products derived from this
016: * software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
020: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
021: * ARE DISCLAIMED. IN NO EVENT SHALL HSQL DEVELOPMENT GROUP, HSQLDB.ORG,
022: * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
026: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package org.hsqldb;
032:
033: import org.hsqldb.lib.FileUtil;
034: import org.hsqldb.persist.HsqlProperties;
035: import org.hsqldb.resources.BundleHandler;
036:
037: // fredt@users 20020215 - patch 1.7.0 by fredt
038: // method rorganised to use new HsqlServerProperties class
039: // unsaved@users 20021113 - patch 1.7.2 - SSL support
040: // boucherb@users 20030510 - patch 1.7.2 - SSL support moved to factory interface
041: // boucherb@users 20030510 - patch 1.7.2 - moved all common code to Server
042: // boucherb@users 20030510 - patch 1.7.2 - general lint removal
043:
044: /**
045: * The HSQLDB HTTP protocol network database server. <p>
046: *
047: * WebServer has two distinct functions:<p>
048: *
049: * The primary function is to allow client/server access to HSQLDB databases
050: * via the HTTP protocol. This protocol is less efficient than the HSQL
051: * protocol used by the Server class and should be used only in situations
052: * where sandboxes or firewalls between the client and the server do not
053: * allow the use of the HSQL protocol. One example is client/server access by
054: * an applet running in browsers on remote hosts and accessing the database
055: * engine on the HTTP server from which the applet originated. From version
056: * 1.7.2, HTTP database connections are persistent and support transactions.
057: * Similar to HSQL connections, they should be explicitly closed to free the
058: * server resources. <p>
059: *
060: * The secondary function of WebServer is to act as a simple general purpose
061: * HTTP server. It is aimed to support the minimum requirements set out by
062: * the HTTP/1.0 standard. The HEAD and GET methods can be used to query and
063: * retreive static files from the HTTP server.<p>
064: *
065: * Both the database server and HTTP server functions of WebServer can be
066: * configured with the webserver.properties file. It contains entries for the
067: * database server similar to those for the HSQL protocol Server class. In
068: * addition, a list mapping different file endings to their mime types may be
069: * included in this file. (fredt@users) <p>
070: *
071: * From the command line, the options are as follows: <p>
072: * <pre>
073: * +----------------+-------------+----------+------------------------------+
074: * | OPTION | TYPE | DEFAULT | DESCRIPTION |
075: * +----------------+-------------+----------+------------------------------|
076: * | -? | -- | -- | prints this message |
077: * | -address | name|number | any | server inet address |
078: * | -port | number | 80 | port at which server listens |
079: * | -database.i | [type]spec | 0=test | path of database i |
080: * | -dbname.i | alias | -- | url alias for database i |
081: * | -silent | true|false | true | false => display all queries |
082: * | -trace | true|false | false | display JDBC trace messages |
083: * | -no_system_exit| true|false | false | do not issue System.exit() |
084: * +----------------+-------------+----------+------------------------------+
085: * </pre>
086: *
087: * Example of the webserver.properties file:
088: *
089: * <pre>
090: * server.port=80
091: * server.database.0=test
092: * server.dbname.0=...
093: * ...
094: * server.database.n=...
095: * server.dbname.n=...
096: * server.silent=true
097: *
098: * .htm=text/html
099: * .html=text/html
100: * .txt=text/plain
101: * .gif=image/gif
102: * .class=application/octet-stream
103: * .jpg=image/jpeg
104: * .jgep=image/jpeg
105: * .zip=application/x-zip-compressed
106: * </pre>
107: *
108: * <ul>
109: * <li>For server.root, use '/' as the separator, even for DOS/Windows.
110: * <li>File extensions for mime types must be lowercase and start with '.'
111: * </ul>
112: *
113: * Replaces original Hypersonic class of the same name.
114: *
115: * @author fredt@users
116: * @author boucherb@users
117: * @version 1.7.2
118: * @since 1.7.2
119: */
120: public class WebServer extends Server {
121:
122: /**
123: * Handle to resource bundle providing i18n for things like
124: * HTTP error pages.
125: */
126: static int webBundleHandle = BundleHandler.getBundleHandle(
127: "webserver", null);
128:
129: public WebServer() {
130: super (ServerConstants.SC_PROTOCOL_HTTP);
131: }
132:
133: /**
134: * Starts a new WebServer.
135: *
136: * @param args the "command line" parameters with which to start
137: * the WebServer. "-?" will cause the command line arguments
138: * help to be printed to the standard output
139: */
140: public static void main(String[] args) {
141:
142: String propsPath = FileUtil
143: .canonicalOrAbsolutePath("webserver");
144: HsqlProperties fileProps = ServerConfiguration
145: .getPropertiesFromFile(propsPath);
146: HsqlProperties props = fileProps == null ? new HsqlProperties()
147: : fileProps;
148: HsqlProperties stringProps = null;
149: try {
150: stringProps = HsqlProperties.argArrayToProps(args,
151: ServerConstants.SC_KEY_PREFIX);
152: } catch (ArrayIndexOutOfBoundsException aioob) {
153: // I'd like to exit with 0 here, but it's possible that user
154: // has called main() programmatically and does not want us to
155: // exit.
156: printHelp("webserver.help");
157: return;
158: }
159:
160: if (stringProps != null) {
161: if (stringProps.getErrorKeys().length != 0) {
162: printHelp("webserver.help");
163:
164: return;
165: }
166:
167: props.addProperties(stringProps);
168: }
169:
170: ServerConfiguration.translateDefaultDatabaseProperty(props);
171:
172: // Standard behaviour when started from the command line
173: // is to halt the VM when the server shuts down. This may, of
174: // course, be overridden by whatever, if any, security policy
175: // is in place.
176: ServerConfiguration.translateDefaultNoSystemExitProperty(props);
177:
178: // finished setting up properties;
179: Server server = new WebServer();
180:
181: try {
182: server.setProperties(props);
183: } catch (Exception e) {
184: server.printError("Failed to set properties");
185: server.printStackTrace(e);
186:
187: return;
188: }
189:
190: // now messages go to the channel specified in properties
191: server.print("Startup sequence initiated from main() method");
192:
193: if (fileProps != null) {
194: server.print("Loaded properties from [" + propsPath
195: + ".properties]");
196: } else {
197: server.print("Could not load properties from file");
198: server.print("Using cli/default properties only");
199: }
200:
201: server.start();
202: }
203:
204: /**
205: * Retrieves the name of the web page served when no page is specified.
206: * This attribute is relevant only when server protocol is HTTP(S).
207: *
208: * @return the name of the web page served when no page is specified
209: *
210: * @jmx.managed-attribute
211: * access="read-write"
212: * description="Used when server protocol is HTTP(S)"
213: */
214: public String getDefaultWebPage() {
215: return serverProperties
216: .getProperty(ServerConstants.SC_KEY_WEB_DEFAULT_PAGE);
217: }
218:
219: /**
220: * Retrieves a String object describing the command line and
221: * properties options for this Server.
222: *
223: * @return the command line and properties options help for this Server
224: */
225: public String getHelpString() {
226: return BundleHandler.getString(serverBundleHandle,
227: "webserver.help");
228: }
229:
230: /**
231: * Retrieves this server's product name. <p>
232: *
233: * Typically, this will be something like: "HSQLDB xxx server".
234: *
235: * @return the product name of this server
236: *
237: * @jmx.managed-attribute
238: * access="read-only"
239: * description="Of Server"
240: */
241: public String getProductName() {
242: return "HSQLDB web server";
243: }
244:
245: /**
246: * Retrieves a string respresentaion of the network protocol
247: * this server offers, typically one of 'HTTP', HTTPS', 'HSQL' or 'HSQLS'.
248: *
249: * @return string respresentation of this server's protocol
250: *
251: * @jmx.managed-attribute
252: * access="read-only"
253: * description="Used to handle connections"
254: */
255: public String getProtocol() {
256: return isTls() ? "HTTPS" : "HTTP";
257: }
258:
259: /**
260: * Retrieves the root context (directory) from which web content
261: * is served. This property is relevant only when the server
262: * protocol is HTTP(S). Although unlikely, it may be that in the future
263: * other contexts, such as jar urls may be supported, so that pages can
264: * be served from the contents of a jar or from the JVM class path.
265: *
266: * @return the root context (directory) from which web content is served
267: *
268: * @jmx.managed-attribute
269: * access="read-write"
270: * description="Context (directory)"
271: */
272: public String getWebRoot() {
273: return serverProperties
274: .getProperty(ServerConstants.SC_KEY_WEB_ROOT);
275: }
276: }
|