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.util;
032:
033: import java.sql.Connection;
034: import java.sql.DriverManager;
035: import java.sql.SQLException;
036: import java.sql.Statement;
037:
038: import org.hsqldb.persist.HsqlProperties;
039:
040: /*
041: * Shutdown class for Server, used by hsqldbserver to shutdown the server
042: *
043: * Send any questions to fchoong@user
044: */
045: public class ShutdownServer {
046:
047: public static void main(String[] arg) {
048:
049: boolean webserver;
050: String driver = "org.hsqldb.jdbcDriver";
051: String url;
052: String user;
053: String password;
054: int port;
055: String defaulturl;
056: String shutdownarg;
057:
058: if (arg.length > 0) {
059: String p = arg[0];
060:
061: if ((p != null) && p.startsWith("-?")) {
062: printHelp();
063:
064: return;
065: }
066: }
067:
068: HsqlProperties props = HsqlProperties.argArrayToProps(arg,
069: "server");
070:
071: webserver = props.isPropertyTrue("server.webserver", false);
072: defaulturl = webserver ? "jdbc:hsqldb:http://localhost"
073: : "jdbc:hsqldb:hsql://localhost";
074:
075: int defaultport = webserver ? 80 : 9001;
076:
077: port = props.getIntegerProperty("server.port", defaultport);
078: url = props.getProperty("server.url", defaulturl + ":" + port);
079: user = props.getProperty("server.user", "sa");
080: password = props.getProperty("server.password", "");
081: shutdownarg = props.getProperty("server.shutdownarg", "");
082:
083: try {
084: Class.forName(driver); // Load the driver
085:
086: Connection connection = DriverManager.getConnection(url,
087: user, password);
088: Statement statement = connection.createStatement();
089:
090: // can use SHUTDOWN COMPACT or SHUTDOWN IMMEDIATELY
091: statement.execute("SHUTDOWN " + shutdownarg);
092: } catch (ClassNotFoundException e) {
093: System.err.println(e); // Driver not found
094: } catch (SQLException e) {
095: System.err.println(e); // error connection to database
096: }
097: }
098:
099: static void printHelp() {
100:
101: System.out
102: .print("Usage: java ShutdownServer [-options]\n"
103: + "where options include:\n"
104: + " -port <nr> port where the server is listening\n"
105: + " -user <name> username of admin user\n"
106: + " -password <value> password of admin user\n"
107: + " -webserver <true/false> whether it's a web server\n"
108: + " -url <value> server url (overrides -webserver and -port options)\n"
109: + " -shutdownarg <value> IMMEDIATELY or COMPACT are allowed\n"
110: + "The command line arguments override the default values.");
111: }
112: }
|