提交 20037a4a authored 作者: Thomas Mueller's avatar Thomas Mueller

The API of the tools changed a bit.

上级 6ab81676
...@@ -70,9 +70,7 @@ public class Backup extends Tool { ...@@ -70,9 +70,7 @@ public class Backup extends Tool {
showUsage(); showUsage();
return; return;
} else { } else {
out.println("Unsupported option: " + arg); throwUnsupportedOption(arg);
showUsage();
return;
} }
} }
process(zipFileName, dir, db, quiet); process(zipFileName, dir, db, quiet);
......
...@@ -80,14 +80,12 @@ public class ChangeFileEncryption extends Tool { ...@@ -80,14 +80,12 @@ public class ChangeFileEncryption extends Tool {
showUsage(); showUsage();
return; return;
} else { } else {
out.println("Unsupported option: " + arg); throwUnsupportedOption(arg);
showUsage();
return;
} }
} }
if ((encryptPassword == null && decryptPassword == null) || cipher == null) { if ((encryptPassword == null && decryptPassword == null) || cipher == null) {
showUsage(); showUsage();
return; throw new SQLException("Encryption or decryption password not set, or cipher not set");
} }
process(dir, db, cipher, decryptPassword, encryptPassword, quiet); process(dir, db, cipher, decryptPassword, encryptPassword, quiet);
} }
......
...@@ -30,9 +30,9 @@ import java.awt.event.MouseListener; ...@@ -30,9 +30,9 @@ import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter; import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent; import java.awt.event.WindowEvent;
import org.h2.util.Resources; import org.h2.util.Resources;
import org.h2.util.Tool;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream;
//## AWT end ## //## AWT end ##
import java.sql.SQLException; import java.sql.SQLException;
...@@ -41,20 +41,17 @@ import org.h2.server.ShutdownHandler; ...@@ -41,20 +41,17 @@ import org.h2.server.ShutdownHandler;
import org.h2.util.StartBrowser; import org.h2.util.StartBrowser;
/** /**
* This tool starts the H2 Console (web-) server, as well as the TCP and PG * Starts the H2 Console (web-) server, as well as the TCP and PG server.
* server. For JDK 1.6, a system tray icon is created, for platforms that * @h2.resource
* support it. Otherwise, a small window opens.
* *
* @author Thomas Mueller, Ridvan Agar * @author Thomas Mueller, Ridvan Agar
*/ */
public class Console implements public class Console extends Tool implements
//## AWT begin ## //## AWT begin ##
ActionListener, MouseListener, ActionListener, MouseListener,
//## AWT end ## //## AWT end ##
ShutdownHandler { ShutdownHandler {
private static final int EXIT_ERROR = 1;
//## AWT begin ## //## AWT begin ##
Frame frame; Frame frame;
private Font font; private Font font;
...@@ -65,50 +62,40 @@ ShutdownHandler { ...@@ -65,50 +62,40 @@ ShutdownHandler {
private boolean isWindows; private boolean isWindows;
/** /**
* The command line interface for this tool. * When running without options, -tcp, -web, -browser and -pg are started.<br />
* The command line options are the same as in the Server tool, * Options are case sensitive. Supported options are:
* but this tool will always start the TCP, TCP and PG server. * <table>
* Options are case sensitive. * <tr><td>[-help] or [-?]</td>
* * <td>Print the list of options</td></tr>
* The command line interface for this tool. The options must be split into * <tr><td>[-web]</td>
* strings like this: "-baseDir", "/temp/data",... By default, -tcp, -web, * <td>Start the web server with the H2 Console</td></tr>
* -browser and -pg are started. If there is a problem starting a service, * <tr><td>[-tool]</td>
* the program terminates with an exit code of 1. Options are case * <td>Start the icon or window that allows to start a browser</td></tr>
* sensitive. The following options are supported: * <tr><td>[-browser]</td>
* <ul> * <td>Start a browser and open a page to connect to the web server</td></tr>
* <li>-help or -? (print the list of options) </li> * <tr><td>[-tcp]</td>
* <li>-web (start the Web Server and H2 Console) </li> * <td>Start the TCP server</td></tr>
* <li>-tool (start the icon or window that allows to start a browser)</li> * <tr><td>[-pg]</td>
* <li>-browser (start a browser and open a page to connect to the * <td>Start the PG server</td></tr>
* Web Server) </li> * </table>
* <li>-tcp (start the TCP Server) </li> * For each Server, additional options are available; for details, see the Server tool.<br />
* <li>-pg (start the PG Server) </li> * If a service can not be started, the program terminates with an exit code of 1.
* </ul> * @h2.resource
* For each Server, additional options are available.
* Those options are the same as in the Server tool.
* *
* @param args the command line arguments * @param args the command line arguments
*/ */
public static void main(String[] args) { public static void main(String[] args) throws SQLException {
int exitCode = new Console().run(args, System.out); new Console().run(args);
if (exitCode != 0) {
System.exit(exitCode);
}
}
private void showUsage(PrintStream out) {
out.println("Starts H2 Console");
out.println("By default, -web, -tool, -browser, -tcp, and -pg are started. Options are case sensitive.");
out.println("java "+getClass().getName());
out.println("-web Start the Web Server and H2 Console");
out.println("-tool Start the icon or window that allows to start a browser (includes -web)");
out.println("-browser Start a browser to connect to the H2 Console (includes -web)");
out.println("-tcp Start the TCP Server");
out.println("-pg Start the PG Server");
out.println("See also http://h2database.com/javadoc/" + getClass().getName().replace('.', '/') + ".html");
} }
private int run(String[] args, PrintStream out) { /**
* This tool starts the H2 Console (web-) server, as well as the TCP and PG
* server. For JDK 1.6, a system tray icon is created, for platforms that
* support it. Otherwise, a small window opens.
*
* @param args the command line arguments
*/
public void run(String[] args) throws SQLException {
isWindows = SysProperties.getStringSetting("os.name", "").startsWith("Windows"); isWindows = SysProperties.getStringSetting("os.name", "").startsWith("Windows");
boolean tcpStart = false, pgStart = false, webStart = false, toolStart = false; boolean tcpStart = false, pgStart = false, webStart = false, toolStart = false;
boolean browserStart = false; boolean browserStart = false;
...@@ -119,8 +106,8 @@ ShutdownHandler { ...@@ -119,8 +106,8 @@ ShutdownHandler {
if (arg == null) { if (arg == null) {
continue; continue;
} else if ("-?".equals(arg) || "-help".equals(arg)) { } else if ("-?".equals(arg) || "-help".equals(arg)) {
showUsage(out); showUsage();
return EXIT_ERROR; return;
} else if ("-web".equals(arg)) { } else if ("-web".equals(arg)) {
startDefaultServers = false; startDefaultServers = false;
webStart = true; webStart = true;
...@@ -147,7 +134,6 @@ ShutdownHandler { ...@@ -147,7 +134,6 @@ ShutdownHandler {
tcpStart = true; tcpStart = true;
pgStart = true; pgStart = true;
} }
int exitCode = 0;
if (webStart) { if (webStart) {
try { try {
web = Server.createWebServer(args); web = Server.createWebServer(args);
...@@ -199,17 +185,16 @@ ShutdownHandler { ...@@ -199,17 +185,16 @@ ShutdownHandler {
StartBrowser.openURL(web.getURL()); StartBrowser.openURL(web.getURL());
} }
if (!web.isRunning(true)) { if (!web.isRunning(true)) {
exitCode = EXIT_ERROR; throw new SQLException("The web server is not running");
} }
return exitCode;
} }
private void printProblem(SQLException e, Server server) { private void printProblem(SQLException e, Server server) {
if (server == null) { if (server == null) {
e.printStackTrace(); e.printStackTrace();
} else { } else {
System.out.println(server.getStatus()); out.println(server.getStatus());
System.out.println("Root cause: " + e.getMessage()); out.println("Root cause: " + e.getMessage());
} }
} }
......
...@@ -92,9 +92,7 @@ public class ConvertTraceFile extends Tool { ...@@ -92,9 +92,7 @@ public class ConvertTraceFile extends Tool {
showUsage(); showUsage();
return; return;
} else { } else {
out.println("Unsupported option: " + arg); throwUnsupportedOption(arg);
showUsage();
return;
} }
} }
try { try {
......
...@@ -69,14 +69,12 @@ public class CreateCluster extends Tool { ...@@ -69,14 +69,12 @@ public class CreateCluster extends Tool {
showUsage(); showUsage();
return; return;
} else { } else {
out.println("Unsupported option: " + arg); throwUnsupportedOption(arg);
showUsage();
return;
} }
} }
if (urlSource == null || urlTarget == null || user == null || serverList == null) { if (urlSource == null || urlTarget == null || serverList == null) {
showUsage(); showUsage();
return; throw new SQLException("Source URL, target URL, or server list not set");
} }
process(urlSource, urlTarget, user, password, serverList); process(urlSource, urlTarget, user, password, serverList);
} }
......
...@@ -59,9 +59,7 @@ public class DeleteDbFiles extends Tool { ...@@ -59,9 +59,7 @@ public class DeleteDbFiles extends Tool {
showUsage(); showUsage();
return; return;
} else { } else {
out.println("Unsupported option: " + arg); throwUnsupportedOption(arg);
showUsage();
return;
} }
} }
process(dir, db, quiet); process(dir, db, quiet);
......
...@@ -106,6 +106,7 @@ public class Recover extends Tool implements DataHandler { ...@@ -106,6 +106,7 @@ public class Recover extends Tool implements DataHandler {
* a hardware problem. * a hardware problem.
* *
* @param args the command line arguments * @param args the command line arguments
* @return the exit code
*/ */
public void run(String[] args) throws SQLException { public void run(String[] args) throws SQLException {
String dir = "."; String dir = ".";
...@@ -124,9 +125,7 @@ public class Recover extends Tool implements DataHandler { ...@@ -124,9 +125,7 @@ public class Recover extends Tool implements DataHandler {
showUsage(); showUsage();
return; return;
} else { } else {
out.println("Unsupported option: " + arg); throwUnsupportedOption(arg);
showUsage();
return;
} }
} }
if (!SysProperties.PAGE_STORE && remove) { if (!SysProperties.PAGE_STORE && remove) {
......
...@@ -66,9 +66,7 @@ public class Restore extends Tool { ...@@ -66,9 +66,7 @@ public class Restore extends Tool {
showUsage(); showUsage();
return; return;
} else { } else {
out.println("Unsupported option: " + arg); throwUnsupportedOption(arg);
showUsage();
return;
} }
} }
process(zipFileName, dir, db); process(zipFileName, dir, db);
......
...@@ -127,14 +127,12 @@ public class RunScript extends Tool { ...@@ -127,14 +127,12 @@ public class RunScript extends Tool {
showUsage(); showUsage();
return; return;
} else { } else {
out.println("Unsupported option: " + arg); throwUnsupportedOption(arg);
showUsage();
return;
} }
} }
if (url == null || user == null || password == null || script == null) { if (url == null) {
showUsage(); showUsage();
return; throw new SQLException("URL not set");
} }
long time = System.currentTimeMillis(); long time = System.currentTimeMillis();
if (options != null) { if (options != null) {
......
...@@ -89,14 +89,12 @@ public class Script extends Tool { ...@@ -89,14 +89,12 @@ public class Script extends Tool {
showUsage(); showUsage();
return; return;
} else { } else {
out.println("Unsupported option: " + arg); throwUnsupportedOption(arg);
showUsage();
return;
} }
} }
if (url == null) { if (url == null) {
showUsage(); showUsage();
return; throw new SQLException("URL not set");
} }
if (options1 != null) { if (options1 != null) {
processScript(url, user, password, file, options1, options2); processScript(url, user, password, file, options1, options2);
......
...@@ -28,83 +28,71 @@ import org.h2.util.FileUtils; ...@@ -28,83 +28,71 @@ import org.h2.util.FileUtils;
import org.h2.util.JdbcDriverUtils; import org.h2.util.JdbcDriverUtils;
import org.h2.util.JdbcUtils; import org.h2.util.JdbcUtils;
import org.h2.util.SortedProperties; import org.h2.util.SortedProperties;
import org.h2.util.Tool;
/** /**
* Interactive command line tool to access a database using JDBC. * Interactive command line tool to access a database using JDBC.
* @h2.resource
*/ */
public class Shell { public class Shell extends Tool {
/**
* The system output stream.
*/
PrintStream out = System.out;
private PrintStream err = System.err; private PrintStream err = System.err;
private InputStream in = System.in; private InputStream in = System.in;
private BufferedReader reader;
private Connection conn; private Connection conn;
private Statement stat; private Statement stat;
private boolean listMode; private boolean listMode;
private int maxColumnSize = 100; private int maxColumnSize = 100;
// Windows: '\u00b3'; // Windows: '\u00b3';
private char boxVertical = '|'; private char boxVertical = '|';
private BufferedReader reader;
/** /**
* The command line interface for this tool. The options must be split into * Options are case sensitive. Supported options are:
* strings like this: "-user", "sa",... Options are case sensitive. The * <table>
* following options are supported: * <tr><td>[-help] or [-?]</td>
* <ul> * <td>Print the list of options</td></tr>
* <li>-help or -? (print the list of options) </li> * <tr><td>[-url &lt;url&gt;]</td>
* <li>-url jdbc:h2:... (database URL) </li> * <td>The database URL (jdbc:h2:...)</td></tr>
* <li>-user username </li> * <tr><td>[-user &lt;user&gt;]</td>
* <li>-password password </li> * <td>The user name</td></tr>
* <li>-driver driver the JDBC driver class name (not required for most * <tr><td>[-password &lt;pwd&gt;]</td>
* databases) </li> * <td>The password</td></tr>
* </ul> * <tr><td>[-driver &lt;class&gt;]</td>
* <td>The JDBC driver class to use (not required in most cases)</td></tr>
* </table>
* @h2.resource
* *
* @param args the command line arguments * @param args the command line arguments
* @throws SQLException
*/ */
public static void main(String[] args) throws SQLException { public static void main(String[] args) throws SQLException {
new Shell().run(args); new Shell().run(args);
} }
private void showUsage() { /**
println("An interactive command line database tool."); * Sets the standard error stream.
println("java "+getClass().getName() + "\n" + *
" [-url <url>] The database URL\n" + * @param out the new standard error stream
" [-user <user>] The user name\n" + */
" [-password <pwd>] The password\n" + public void setErr(PrintStream err) {
" [-driver <class>] The JDBC driver class to use (not required in most cases)"); this.err = err;
println("See also http://h2database.com/javadoc/" + getClass().getName().replace('.', '/') + ".html");
} }
/** /**
* Redirects the input and output. By default, System.in, out and err are * Redirects the standard input. By default, System.in is used.
* used.
* *
* @param in the input stream to use * @param in the input stream to use
* @param out the output stream to use
* @param err the output error stream to use
*/ */
public void setStreams(InputStream in, PrintStream out, PrintStream err) { public void setIn(InputStream in) {
this.in = in; this.in = in;
this.out = out;
this.err = err;
} }
/** /**
* Redirects the input and output. By default, System.in, out and err are * Redirects the standard input. By default, System.in is used.
* used.
* *
* @param reader the input stream reader to use * @param reader the input stream reader to use
* @param out the output stream to use
* @param err the output error stream to use
*/ */
public void setStreams(BufferedReader reader, PrintStream out, PrintStream err) { public void setInReader(BufferedReader reader) {
this.reader = reader; this.reader = reader;
this.out = out;
this.err = err;
} }
/** /**
...@@ -117,19 +105,18 @@ public class Shell { ...@@ -117,19 +105,18 @@ public class Shell {
String user = ""; String user = "";
String password = ""; String password = "";
for (int i = 0; args != null && i < args.length; i++) { for (int i = 0; args != null && i < args.length; i++) {
if (args[i].equals("-url")) { String arg = args[i];
if (arg.equals("-url")) {
url = args[++i]; url = args[++i];
} else if (args[i].equals("-user")) { } else if (arg.equals("-user")) {
user = args[++i]; user = args[++i];
} else if (args[i].equals("-password")) { } else if (arg.equals("-password")) {
password = args[++i]; password = args[++i];
} else if (args[i].equals("-driver")) { } else if (arg.equals("-driver")) {
String driver = args[++i]; String driver = args[++i];
ClassUtils.loadUserClass(driver); ClassUtils.loadUserClass(driver);
} else { } else {
println("Unsupported option: " + args[i]); throwUnsupportedOption(arg);
showUsage();
return;
} }
} }
if (url != null) { if (url != null) {
......
...@@ -57,7 +57,9 @@ public class TestShell extends TestBase { ...@@ -57,7 +57,9 @@ public class TestShell extends TestBase {
public void run() { public void run() {
try { try {
Shell shell = new Shell(); Shell shell = new Shell();
shell.setStreams(toolIn, toolOut, toolOut); shell.setIn(toolIn);
shell.setOut(toolOut);
shell.setErr(toolOut);
shell.run(new String[0]); shell.run(new String[0]);
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
......
...@@ -162,8 +162,14 @@ public class TestTools extends TestBase { ...@@ -162,8 +162,14 @@ public class TestTools extends TestBase {
ByteArrayOutputStream buff = new ByteArrayOutputStream(); ByteArrayOutputStream buff = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(buff); PrintStream ps = new PrintStream(buff);
server = new Server(); server = new Server();
int gotCode = server.run(args, ps); server.setOut(ps);
assertEquals(exitCode, gotCode); int result = 0;
try {
server.run(args);
} catch (SQLException e) {
result = 1;
}
assertEquals(exitCode, result);
ps.flush(); ps.flush();
String s = new String(buff.toByteArray()); String s = new String(buff.toByteArray());
return s; return s;
......
...@@ -9,8 +9,10 @@ package org.h2.dev.util; ...@@ -9,8 +9,10 @@ package org.h2.dev.util;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import org.h2.message.Message;
import org.h2.util.Tool; import org.h2.util.Tool;
...@@ -24,7 +26,7 @@ public class FileViewer extends Tool { ...@@ -24,7 +26,7 @@ public class FileViewer extends Tool {
* *
* @param args the command line arguments * @param args the command line arguments
*/ */
public static void main(String[] args) throws IOException { public static void main(String[] args) throws SQLException {
new FileViewer().run(args); new FileViewer().run(args);
} }
...@@ -42,7 +44,7 @@ public class FileViewer extends Tool { ...@@ -42,7 +44,7 @@ public class FileViewer extends Tool {
// getClass().getName().replace('.', '/') + ".html"); // getClass().getName().replace('.', '/') + ".html");
} }
public void run(String[] args) { public void run(String[] args) throws SQLException {
String file = null; String file = null;
String find = null; String find = null;
boolean head = false, tail = false; boolean head = false, tail = false;
...@@ -69,9 +71,7 @@ public class FileViewer extends Tool { ...@@ -69,9 +71,7 @@ public class FileViewer extends Tool {
showUsage(); showUsage();
return; return;
} else { } else {
out.println("Unsupported option: " + arg); throwUnsupportedOption(arg);
showUsage();
return;
} }
} }
if (file == null) { if (file == null) {
...@@ -84,7 +84,7 @@ public class FileViewer extends Tool { ...@@ -84,7 +84,7 @@ public class FileViewer extends Tool {
try { try {
process(file, find, head, tail, start, lines, quiet); process(file, find, head, tail, start, lines, quiet);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); throw Message.convert(e);
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论