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