| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.tools; |
| 7 |
|
| 8 |
import java.sql.Connection; |
| 9 |
import java.sql.SQLException; |
| 10 |
import org.h2.server.ShutdownHandler; |
| 11 |
import org.h2.util.JdbcUtils; |
| 12 |
import org.h2.util.MathUtils; |
| 13 |
import org.h2.util.StringUtils; |
| 14 |
import org.h2.util.Tool; |
| 15 |
import org.h2.util.Utils; |
| 16 |
|
| 17 |
/** |
| 18 |
* Starts the H2 Console (web-) server, as well as the TCP and PG server. |
| 19 |
* |
| 20 |
* @author Thomas Mueller, Ridvan Agar |
| 21 |
*/ |
| 22 |
public class Console extends Tool implements ShutdownHandler { |
| 23 |
|
| 24 |
Server web; |
| 25 |
|
| 26 |
private Server tcp, pg; |
| 27 |
|
| 28 |
boolean isWindows; |
| 29 |
|
| 30 |
/** |
| 31 |
* When running without options, -tcp, -web, -browser and -pg are started. |
| 32 |
* |
| 33 |
* Options are case sensitive. |
| 34 |
* <table> |
| 35 |
* <caption>Supported options</caption> |
| 36 |
* <tr><td>[-help] or [-?]</td> |
| 37 |
* <td>Print the list of options</td></tr> |
| 38 |
* <tr><td>[-url]</td> |
| 39 |
* <td>Start a browser and connect to this URL</td></tr> |
| 40 |
* <tr><td>[-driver]</td> |
| 41 |
* <td>Used together with -url: the driver</td></tr> |
| 42 |
* <tr><td>[-user]</td> |
| 43 |
* <td>Used together with -url: the user name</td></tr> |
| 44 |
* <tr><td>[-password]</td> |
| 45 |
* <td>Used together with -url: the password</td></tr> |
| 46 |
* <tr><td>[-web]</td> |
| 47 |
* <td>Start the web server with the H2 Console</td></tr> |
| 48 |
* <tr><td>[-tool]</td> |
| 49 |
* <td>Start the icon or window that allows to start a browser</td></tr> |
| 50 |
* <tr><td>[-browser]</td> |
| 51 |
* <td>Start a browser connecting to the web server</td></tr> |
| 52 |
* <tr><td>[-tcp]</td> |
| 53 |
* <td>Start the TCP server</td></tr> |
| 54 |
* <tr><td>[-pg]</td> |
| 55 |
* <td>Start the PG server</td></tr> |
| 56 |
* </table> |
| 57 |
* For each Server, additional options are available; |
| 58 |
* for details, see the Server tool. |
| 59 |
* If a service can not be started, the program |
| 60 |
* terminates with an exit code of 1. |
| 61 |
* |
| 62 |
* @param args the command line arguments |
| 63 |
* @throws SQLException on failure |
| 64 |
*/ |
| 65 |
public static void main(String... args) throws SQLException { |
| 66 |
Console console; |
| 67 |
try { |
| 68 |
console = (Console) Utils.newInstance("org.h2.tools.GUIConsole"); |
| 69 |
} catch (Exception | NoClassDefFoundError e) { |
| 70 |
console = new Console(); |
| 71 |
} |
| 72 |
console.runTool(args); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* This tool starts the H2 Console (web-) server, as well as the TCP and PG |
| 77 |
* server. A system tray icon is created, for platforms that |
| 78 |
* support it. Otherwise, a small window opens. |
| 79 |
* |
| 80 |
* @param args the command line arguments |
| 81 |
*/ |
| 82 |
@Override |
| 83 |
public void runTool(String... args) throws SQLException { |
| 84 |
isWindows = Utils.getProperty("os.name", "").startsWith("Windows"); |
| 85 |
boolean tcpStart = false, pgStart = false, webStart = false, toolStart = false; |
| 86 |
boolean browserStart = false; |
| 87 |
boolean startDefaultServers = true; |
| 88 |
boolean printStatus = args != null && args.length > 0; |
| 89 |
String driver = null, url = null, user = null, password = null; |
| 90 |
boolean tcpShutdown = false, tcpShutdownForce = false; |
| 91 |
String tcpPassword = ""; |
| 92 |
String tcpShutdownServer = ""; |
| 93 |
boolean ifExists = false, webAllowOthers = false; |
| 94 |
for (int i = 0; args != null && i < args.length; i++) { |
| 95 |
String arg = args[i]; |
| 96 |
if (arg == null) { |
| 97 |
} else if ("-?".equals(arg) || "-help".equals(arg)) { |
| 98 |
showUsage(); |
| 99 |
return; |
| 100 |
} else if ("-url".equals(arg)) { |
| 101 |
startDefaultServers = false; |
| 102 |
url = args[++i]; |
| 103 |
} else if ("-driver".equals(arg)) { |
| 104 |
driver = args[++i]; |
| 105 |
} else if ("-user".equals(arg)) { |
| 106 |
user = args[++i]; |
| 107 |
} else if ("-password".equals(arg)) { |
| 108 |
password = args[++i]; |
| 109 |
} else if (arg.startsWith("-web")) { |
| 110 |
if ("-web".equals(arg)) { |
| 111 |
startDefaultServers = false; |
| 112 |
webStart = true; |
| 113 |
} else if ("-webAllowOthers".equals(arg)) { |
| 114 |
// no parameters |
| 115 |
webAllowOthers = true; |
| 116 |
} else if ("-webExternalNames".equals(arg)) { |
| 117 |
i++; |
| 118 |
} else if ("-webDaemon".equals(arg)) { |
| 119 |
// no parameters |
| 120 |
} else if ("-webSSL".equals(arg)) { |
| 121 |
// no parameters |
| 122 |
} else if ("-webPort".equals(arg)) { |
| 123 |
i++; |
| 124 |
} else { |
| 125 |
showUsageAndThrowUnsupportedOption(arg); |
| 126 |
} |
| 127 |
} else if ("-tool".equals(arg)) { |
| 128 |
startDefaultServers = false; |
| 129 |
webStart = true; |
| 130 |
toolStart = true; |
| 131 |
} else if ("-browser".equals(arg)) { |
| 132 |
startDefaultServers = false; |
| 133 |
webStart = true; |
| 134 |
browserStart = true; |
| 135 |
} else if (arg.startsWith("-tcp")) { |
| 136 |
if ("-tcp".equals(arg)) { |
| 137 |
startDefaultServers = false; |
| 138 |
tcpStart = true; |
| 139 |
} else if ("-tcpAllowOthers".equals(arg)) { |
| 140 |
// no parameters |
| 141 |
} else if ("-tcpDaemon".equals(arg)) { |
| 142 |
// no parameters |
| 143 |
} else if ("-tcpSSL".equals(arg)) { |
| 144 |
// no parameters |
| 145 |
} else if ("-tcpPort".equals(arg)) { |
| 146 |
i++; |
| 147 |
} else if ("-tcpPassword".equals(arg)) { |
| 148 |
tcpPassword = args[++i]; |
| 149 |
} else if ("-tcpShutdown".equals(arg)) { |
| 150 |
startDefaultServers = false; |
| 151 |
tcpShutdown = true; |
| 152 |
tcpShutdownServer = args[++i]; |
| 153 |
} else if ("-tcpShutdownForce".equals(arg)) { |
| 154 |
tcpShutdownForce = true; |
| 155 |
} else { |
| 156 |
showUsageAndThrowUnsupportedOption(arg); |
| 157 |
} |
| 158 |
} else if (arg.startsWith("-pg")) { |
| 159 |
if ("-pg".equals(arg)) { |
| 160 |
startDefaultServers = false; |
| 161 |
pgStart = true; |
| 162 |
} else if ("-pgAllowOthers".equals(arg)) { |
| 163 |
// no parameters |
| 164 |
} else if ("-pgDaemon".equals(arg)) { |
| 165 |
// no parameters |
| 166 |
} else if ("-pgPort".equals(arg)) { |
| 167 |
i++; |
| 168 |
} else { |
| 169 |
showUsageAndThrowUnsupportedOption(arg); |
| 170 |
} |
| 171 |
} else if ("-properties".equals(arg)) { |
| 172 |
i++; |
| 173 |
} else if ("-trace".equals(arg)) { |
| 174 |
// no parameters |
| 175 |
} else if ("-ifExists".equals(arg)) { |
| 176 |
// no parameters |
| 177 |
ifExists = true; |
| 178 |
} else if ("-baseDir".equals(arg)) { |
| 179 |
i++; |
| 180 |
} else { |
| 181 |
showUsageAndThrowUnsupportedOption(arg); |
| 182 |
} |
| 183 |
} |
| 184 |
if (startDefaultServers) { |
| 185 |
webStart = true; |
| 186 |
toolStart = true; |
| 187 |
browserStart = true; |
| 188 |
tcpStart = true; |
| 189 |
pgStart = true; |
| 190 |
} |
| 191 |
if (tcpShutdown) { |
| 192 |
out.println("Shutting down TCP Server at " + tcpShutdownServer); |
| 193 |
Server.shutdownTcpServer(tcpShutdownServer, |
| 194 |
tcpPassword, tcpShutdownForce, false); |
| 195 |
} |
| 196 |
SQLException startException = null; |
| 197 |
boolean webRunning = false; |
| 198 |
|
| 199 |
if (url != null) { |
| 200 |
Connection conn = JdbcUtils.getConnection(driver, url, user, password); |
| 201 |
Server.startWebServer(conn); |
| 202 |
} |
| 203 |
|
| 204 |
if (webStart) { |
| 205 |
try { |
| 206 |
String webKey = webAllowOthers ? null |
| 207 |
: StringUtils.convertBytesToHex(MathUtils.secureRandomBytes(32)); |
| 208 |
web = Server.createWebServer(args, webKey, !ifExists); |
| 209 |
web.setShutdownHandler(this); |
| 210 |
web.start(); |
| 211 |
if (printStatus) { |
| 212 |
out.println(web.getStatus()); |
| 213 |
} |
| 214 |
webRunning = true; |
| 215 |
} catch (SQLException e) { |
| 216 |
printProblem(e, web); |
| 217 |
startException = e; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
if (toolStart && webRunning){ |
| 222 |
show(); |
| 223 |
} |
| 224 |
|
| 225 |
// start browser in any case (even if the server is already running) |
| 226 |
// because some people don't look at the output, |
| 227 |
// but are wondering why nothing happens |
| 228 |
if (browserStart && web != null) { |
| 229 |
openBrowser(web.getURL()); |
| 230 |
} |
| 231 |
|
| 232 |
if (tcpStart) { |
| 233 |
try { |
| 234 |
tcp = Server.createTcpServer(args); |
| 235 |
tcp.start(); |
| 236 |
if (printStatus) { |
| 237 |
out.println(tcp.getStatus()); |
| 238 |
} |
| 239 |
tcp.setShutdownHandler(this); |
| 240 |
} catch (SQLException e) { |
| 241 |
printProblem(e, tcp); |
| 242 |
if (startException == null) { |
| 243 |
startException = e; |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
if (pgStart) { |
| 248 |
try { |
| 249 |
pg = Server.createPgServer(args); |
| 250 |
pg.start(); |
| 251 |
if (printStatus) { |
| 252 |
out.println(pg.getStatus()); |
| 253 |
} |
| 254 |
} catch (SQLException e) { |
| 255 |
printProblem(e, pg); |
| 256 |
if (startException == null) { |
| 257 |
startException = e; |
| 258 |
} |
| 259 |
} |
| 260 |
} |
| 261 |
if (startException != null) { |
| 262 |
shutdown(); |
| 263 |
throw startException; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Overridden by GUIConsole to show a window |
| 269 |
*/ |
| 270 |
void show() { |
| 271 |
} |
| 272 |
|
| 273 |
private void printProblem(Exception e, Server server) { |
| 274 |
if (server == null) { |
| 275 |
e.printStackTrace(); |
| 276 |
} else { |
| 277 |
out.println(server.getStatus()); |
| 278 |
out.println("Root cause: " + e.getMessage()); |
| 279 |
} |
| 280 |
} |
| 281 |
|
| 282 |
/** |
| 283 |
* INTERNAL. |
| 284 |
* Stop all servers that were started using the console. |
| 285 |
*/ |
| 286 |
@Override |
| 287 |
public void shutdown() { |
| 288 |
if (web != null && web.isRunning(false)) { |
| 289 |
web.stop(); |
| 290 |
web = null; |
| 291 |
} |
| 292 |
if (tcp != null && tcp.isRunning(false)) { |
| 293 |
tcp.stop(); |
| 294 |
tcp = null; |
| 295 |
} |
| 296 |
if (pg != null && pg.isRunning(false)) { |
| 297 |
pg.stop(); |
| 298 |
pg = null; |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
/** |
| 303 |
* Open a new browser tab or window with the given URL. |
| 304 |
* |
| 305 |
* @param url the URL to open |
| 306 |
*/ |
| 307 |
void openBrowser(String url) { |
| 308 |
try { |
| 309 |
Server.openBrowser(url); |
| 310 |
} catch (Exception e) { |
| 311 |
out.println(e.getMessage()); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
} |