Debian Codegraph @

src/main/org/h2/tools/Server.java

raw
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.net.URI;
9 import java.sql.Connection;
10 import java.sql.SQLException;
11
12 import org.h2.api.ErrorCode;
13 import org.h2.engine.SysProperties;
14 import org.h2.message.DbException;
15 import org.h2.server.Service;
16 import org.h2.server.ShutdownHandler;
17 import org.h2.server.TcpServer;
18 import org.h2.server.pg.PgServer;
19 import org.h2.server.web.WebServer;
20 import org.h2.util.StringUtils;
21 import org.h2.util.Tool;
22 import org.h2.util.Utils;
23
24 /**
25 * Starts the H2 Console (web-) server, TCP, and PG server.
26 */
27 public class Server extends Tool implements Runnable, ShutdownHandler {
28
29 private final Service service;
30 private Server web, tcp, pg;
31 private ShutdownHandler shutdownHandler;
32 private boolean fromCommandLine;
33 private boolean started;
34
35 public Server() {
36 // nothing to do
37 this.service = null;
38 }
39
40 /**
41 * Create a new server for the given service.
42 *
43 * @param service the service
44 * @param args the command line arguments
45 * @throws SQLException on failure
46 */
47 public Server(Service service, String... args) throws SQLException {
48 verifyArgs(args);
49 this.service = service;
50 try {
51 service.init(args);
52 } catch (Exception e) {
53 throw DbException.toSQLException(e);
54 }
55 }
56
57 /**
58 * When running without options, -tcp, -web, -browser and -pg are started.
59 *
60 * Options are case sensitive.
61 * <table>
62 * <caption>Supported options</caption>
63 * <tr><td>[-help] or [-?]</td>
64 * <td>Print the list of options</td></tr>
65 * <tr><td>[-web]</td>
66 * <td>Start the web server with the H2 Console</td></tr>
67 * <tr><td>[-webAllowOthers]</td>
68 * <td>Allow other computers to connect - see below</td></tr>
69 * <tr><td>[-webExternalNames &lt;names&gt;]</td>
70 * <td>The comma-separated list of external names and IP addresses of this server,
71 * used together with -webAllowOthers</td></tr>
72 * <tr><td>[-webDaemon]</td>
73 * <td>Use a daemon thread</td></tr>
74 * <tr><td>[-webPort &lt;port&gt;]</td>
75 * <td>The port (default: 8082)</td></tr>
76 * <tr><td>[-webSSL]</td>
77 * <td>Use encrypted (HTTPS) connections</td></tr>
78 * <tr><td>[-webAdminPassword]</td>
79 * <td>Hash of password of DB Console administrator, can be generated with
80 * {@linkplain WebServer#encodeAdminPassword(String)}. Can be passed only to
81 * the {@link #runTool(String...)} method, this method rejects it. It is
82 * also possible to store this setting in configuration file of H2
83 * Console.</td></tr>
84 * <tr><td>[-browser]</td>
85 * <td>Start a browser connecting to the web server</td></tr>
86 * <tr><td>[-tcp]</td>
87 * <td>Start the TCP server</td></tr>
88 * <tr><td>[-tcpAllowOthers]</td>
89 * <td>Allow other computers to connect - see below</td></tr>
90 * <tr><td>[-tcpDaemon]</td>
91 * <td>Use a daemon thread</td></tr>
92 * <tr><td>[-tcpPort &lt;port&gt;]</td>
93 * <td>The port (default: 9092)</td></tr>
94 * <tr><td>[-tcpSSL]</td>
95 * <td>Use encrypted (SSL) connections</td></tr>
96 * <tr><td>[-tcpPassword &lt;pwd&gt;]</td>
97 * <td>The password for shutting down a TCP server</td></tr>
98 * <tr><td>[-tcpShutdown "&lt;url&gt;"]</td>
99 * <td>Stop the TCP server; example: tcp://localhost</td></tr>
100 * <tr><td>[-tcpShutdownForce]</td>
101 * <td>Do not wait until all connections are closed</td></tr>
102 * <tr><td>[-pg]</td>
103 * <td>Start the PG server</td></tr>
104 * <tr><td>[-pgAllowOthers]</td>
105 * <td>Allow other computers to connect - see below</td></tr>
106 * <tr><td>[-pgDaemon]</td>
107 * <td>Use a daemon thread</td></tr>
108 * <tr><td>[-pgPort &lt;port&gt;]</td>
109 * <td>The port (default: 5435)</td></tr>
110 * <tr><td>[-properties "&lt;dir&gt;"]</td>
111 * <td>Server properties (default: ~, disable: null)</td></tr>
112 * <tr><td>[-baseDir &lt;dir&gt;]</td>
113 * <td>The base directory for H2 databases (all servers)</td></tr>
114 * <tr><td>[-ifExists]</td>
115 * <td>Only existing databases may be opened (all servers)</td></tr>
116 * <tr><td>[-ifNotExists]</td>
117 * <td>Databases are created when accessed</td></tr>
118 * <tr><td>[-trace]</td>
119 * <td>Print additional trace information (all servers)</td></tr>
120 * <tr><td>[-key &lt;from&gt; &lt;to&gt;]</td>
121 * <td>Allows to map a database name to another (all servers)</td></tr>
122 * </table>
123 * The options -xAllowOthers are potentially risky.
124 *
125 * For details, see Advanced Topics / Protection against Remote Access.
126 *
127 * @param args the command line arguments
128 * @throws SQLException on failure
129 */
130 public static void main(String... args) throws SQLException {
131 Server server = new Server();
132 server.fromCommandLine = true;
133 server.runTool(args);
134 }
135
136 private void verifyArgs(String... args) throws SQLException {
137 for (int i = 0; args != null && i < args.length; i++) {
138 String arg = args[i];
139 if (arg == null) {
140 } else if ("-?".equals(arg) || "-help".equals(arg)) {
141 // ok
142 } else if (arg.startsWith("-web")) {
143 if ("-web".equals(arg)) {
144 // ok
145 } else if ("-webAllowOthers".equals(arg)) {
146 // no parameters
147 } else if ("-webExternalNames".equals(arg)) {
148 i++;
149 } else if ("-webDaemon".equals(arg)) {
150 // no parameters
151 } else if ("-webSSL".equals(arg)) {
152 // no parameters
153 } else if ("-webPort".equals(arg)) {
154 i++;
155 } else if ("-webAdminPassword".equals(arg)) {
156 if (fromCommandLine) {
157 throwUnsupportedOption(arg);
158 }
159 i++;
160 } else {
161 throwUnsupportedOption(arg);
162 }
163 } else if ("-browser".equals(arg)) {
164 // ok
165 } else if (arg.startsWith("-tcp")) {
166 if ("-tcp".equals(arg)) {
167 // ok
168 } else if ("-tcpAllowOthers".equals(arg)) {
169 // no parameters
170 } else if ("-tcpDaemon".equals(arg)) {
171 // no parameters
172 } else if ("-tcpSSL".equals(arg)) {
173 // no parameters
174 } else if ("-tcpPort".equals(arg)) {
175 i++;
176 } else if ("-tcpPassword".equals(arg)) {
177 i++;
178 } else if ("-tcpShutdown".equals(arg)) {
179 i++;
180 } else if ("-tcpShutdownForce".equals(arg)) {
181 // ok
182 } else {
183 throwUnsupportedOption(arg);
184 }
185 } else if (arg.startsWith("-pg")) {
186 if ("-pg".equals(arg)) {
187 // ok
188 } else if ("-pgAllowOthers".equals(arg)) {
189 // no parameters
190 } else if ("-pgDaemon".equals(arg)) {
191 // no parameters
192 } else if ("-pgPort".equals(arg)) {
193 i++;
194 } else {
195 throwUnsupportedOption(arg);
196 }
197 } else if (arg.startsWith("-ftp")) {
198 if ("-ftpPort".equals(arg)) {
199 i++;
200 } else if ("-ftpDir".equals(arg)) {
201 i++;
202 } else if ("-ftpRead".equals(arg)) {
203 i++;
204 } else if ("-ftpWrite".equals(arg)) {
205 i++;
206 } else if ("-ftpWritePassword".equals(arg)) {
207 i++;
208 } else if ("-ftpTask".equals(arg)) {
209 // no parameters
210 } else {
211 throwUnsupportedOption(arg);
212 }
213 } else if ("-properties".equals(arg)) {
214 i++;
215 } else if ("-trace".equals(arg)) {
216 // no parameters
217 } else if ("-ifExists".equals(arg)) {
218 // no parameters
219 } else if ("-ifNotExists".equals(arg)) {
220 // no parameters
221 } else if ("-baseDir".equals(arg)) {
222 i++;
223 } else if ("-key".equals(arg)) {
224 i += 2;
225 } else if ("-tool".equals(arg)) {
226 // no parameters
227 } else {
228 throwUnsupportedOption(arg);
229 }
230 }
231 }
232
233 @Override
234 public void runTool(String... args) throws SQLException {
235 boolean tcpStart = false, pgStart = false, webStart = false;
236 boolean browserStart = false;
237 boolean tcpShutdown = false, tcpShutdownForce = false;
238 String tcpPassword = "";
239 String tcpShutdownServer = "";
240 boolean startDefaultServers = true;
241 for (int i = 0; args != null && i < args.length; i++) {
242 String arg = args[i];
243 if (arg == null) {
244 } else if ("-?".equals(arg) || "-help".equals(arg)) {
245 showUsage();
246 return;
247 } else if (arg.startsWith("-web")) {
248 if ("-web".equals(arg)) {
249 startDefaultServers = false;
250 webStart = true;
251 } else if ("-webAllowOthers".equals(arg)) {
252 // no parameters
253 } else if ("-webExternalNames".equals(arg)) {
254 i++;
255 } else if ("-webDaemon".equals(arg)) {
256 // no parameters
257 } else if ("-webSSL".equals(arg)) {
258 // no parameters
259 } else if ("-webPort".equals(arg)) {
260 i++;
261 } else if ("-webAdminPassword".equals(arg)) {
262 if (fromCommandLine) {
263 throwUnsupportedOption(arg);
264 }
265 i++;
266 } else {
267 showUsageAndThrowUnsupportedOption(arg);
268 }
269 } else if ("-browser".equals(arg)) {
270 startDefaultServers = false;
271 browserStart = true;
272 } else if (arg.startsWith("-tcp")) {
273 if ("-tcp".equals(arg)) {
274 startDefaultServers = false;
275 tcpStart = true;
276 } else if ("-tcpAllowOthers".equals(arg)) {
277 // no parameters
278 } else if ("-tcpDaemon".equals(arg)) {
279 // no parameters
280 } else if ("-tcpSSL".equals(arg)) {
281 // no parameters
282 } else if ("-tcpPort".equals(arg)) {
283 i++;
284 } else if ("-tcpPassword".equals(arg)) {
285 tcpPassword = args[++i];
286 } else if ("-tcpShutdown".equals(arg)) {
287 startDefaultServers = false;
288 tcpShutdown = true;
289 tcpShutdownServer = args[++i];
290 } else if ("-tcpShutdownForce".equals(arg)) {
291 tcpShutdownForce = true;
292 } else {
293 showUsageAndThrowUnsupportedOption(arg);
294 }
295 } else if (arg.startsWith("-pg")) {
296 if ("-pg".equals(arg)) {
297 startDefaultServers = false;
298 pgStart = true;
299 } else if ("-pgAllowOthers".equals(arg)) {
300 // no parameters
301 } else if ("-pgDaemon".equals(arg)) {
302 // no parameters
303 } else if ("-pgPort".equals(arg)) {
304 i++;
305 } else {
306 showUsageAndThrowUnsupportedOption(arg);
307 }
308 } else if ("-properties".equals(arg)) {
309 i++;
310 } else if ("-trace".equals(arg)) {
311 // no parameters
312 } else if ("-ifExists".equals(arg)) {
313 // no parameters
314 } else if ("-ifNotExists".equals(arg)) {
315 // no parameters
316 } else if ("-baseDir".equals(arg)) {
317 i++;
318 } else if ("-key".equals(arg)) {
319 i += 2;
320 } else {
321 showUsageAndThrowUnsupportedOption(arg);
322 }
323 }
324 verifyArgs(args);
325 if (startDefaultServers) {
326 tcpStart = true;
327 pgStart = true;
328 webStart = true;
329 browserStart = true;
330 }
331 // TODO server: maybe use one single properties file?
332 if (tcpShutdown) {
333 out.println("Shutting down TCP Server at " + tcpShutdownServer);
334 shutdownTcpServer(tcpShutdownServer, tcpPassword,
335 tcpShutdownForce, false);
336 }
337 try {
338 if (tcpStart) {
339 tcp = createTcpServer(args);
340 tcp.start();
341 out.println(tcp.getStatus());
342 tcp.setShutdownHandler(this);
343 }
344 if (pgStart) {
345 pg = createPgServer(args);
346 pg.start();
347 out.println(pg.getStatus());
348 }
349 if (webStart) {
350 web = createWebServer(args);
351 web.setShutdownHandler(this);
352 SQLException result = null;
353 try {
354 web.start();
355 } catch (Exception e) {
356 result = DbException.toSQLException(e);
357 }
358 out.println(web.getStatus());
359 // start browser in any case (even if the server is already
360 // running) because some people don't look at the output, but
361 // are wondering why nothing happens
362 if (browserStart) {
363 try {
364 openBrowser(web.getURL());
365 } catch (Exception e) {
366 out.println(e.getMessage());
367 }
368 }
369 if (result != null) {
370 throw result;
371 }
372 } else if (browserStart) {
373 out.println("The browser can only start if a web server is started (-web)");
374 }
375 } catch (SQLException e) {
376 stopAll();
377 throw e;
378 }
379 }
380
381 /**
382 * Shutdown one or all TCP server. If force is set to false, the server will
383 * not allow new connections, but not kill existing connections, instead it
384 * will stop if the last connection is closed. If force is set to true,
385 * existing connections are killed. After calling the method with
386 * force=false, it is not possible to call it again with force=true because
387 * new connections are not allowed. Example:
388 *
389 * <pre>
390 * Server.shutdownTcpServer(&quot;tcp://localhost:9094&quot;,
391 * password, true, false);
392 * </pre>
393 *
394 * @param url example: tcp://localhost:9094
395 * @param password the password to use ("" for no password)
396 * @param force the shutdown (don't wait)
397 * @param all whether all TCP servers that are running in the JVM should be
398 * stopped
399 * @throws SQLException on failure
400 */
401 public static void shutdownTcpServer(String url, String password,
402 boolean force, boolean all) throws SQLException {
403 TcpServer.shutdown(url, password, force, all);
404 }
405
406 /**
407 * Get the status of this server.
408 *
409 * @return the status
410 */
411 public String getStatus() {
412 StringBuilder buff = new StringBuilder();
413 if (!started) {
414 buff.append("Not started");
415 } else if (isRunning(false)) {
416 buff.append(service.getType()).
417 append(" server running at ").
418 append(service.getURL()).
419 append(" (");
420 if (service.getAllowOthers()) {
421 buff.append("others can connect");
422 } else {
423 buff.append("only local connections");
424 }
425 buff.append(')');
426 } else {
427 buff.append("The ").
428 append(service.getType()).
429 append(" server could not be started. " +
430 "Possible cause: another server is already running at ").
431 append(service.getURL());
432 }
433 return buff.toString();
434 }
435
436 /**
437 * Create a new web server, but does not start it yet. Example:
438 *
439 * <pre>
440 * Server server = Server.createWebServer("-trace").start();
441 * </pre>
442 * Supported options are:
443 * -webPort, -webSSL, -webAllowOthers, -webDaemon,
444 * -trace, -ifExists, -ifNotExists, -baseDir, -properties.
445 * See the main method for details.
446 *
447 * @param args the argument list
448 * @return the server
449 * @throws SQLException on failure
450 */
451 public static Server createWebServer(String... args) throws SQLException {
452 return createWebServer(args, null, false);
453 }
454
455 /**
456 * Create a new web server, but does not start it yet.
457 *
458 * @param args
459 * the argument list
460 * @param key
461 * key, or null
462 * @param allowSecureCreation
463 * whether creation of databases using the key should be allowed
464 * @return the server
465 */
466 static Server createWebServer(String[] args, String key, boolean allowSecureCreation) throws SQLException {
467 WebServer service = new WebServer();
468 service.setKey(key);
469 service.setAllowSecureCreation(allowSecureCreation);
470 Server server = new Server(service, args);
471 service.setShutdownHandler(server);
472 return server;
473 }
474
475 /**
476 * Create a new TCP server, but does not start it yet. Example:
477 *
478 * <pre>
479 * Server server = Server.createTcpServer(
480 * "-tcpPort", "9123", "-tcpAllowOthers").start();
481 * </pre>
482 * Supported options are:
483 * -tcpPort, -tcpSSL, -tcpPassword, -tcpAllowOthers, -tcpDaemon,
484 * -trace, -ifExists, -ifNotExists, -baseDir, -key.
485 * See the main method for details.
486 * <p>
487 * If no port is specified, the default port is used if possible,
488 * and if this port is already used, a random port is used.
489 * Use getPort() or getURL() after starting to retrieve the port.
490 * </p>
491 *
492 * @param args the argument list
493 * @return the server
494 * @throws SQLException on failure
495 */
496 public static Server createTcpServer(String... args) throws SQLException {
497 TcpServer service = new TcpServer();
498 Server server = new Server(service, args);
499 service.setShutdownHandler(server);
500 return server;
501 }
502
503 /**
504 * Create a new PG server, but does not start it yet.
505 * Example:
506 * <pre>
507 * Server server =
508 * Server.createPgServer("-pgAllowOthers").start();
509 * </pre>
510 * Supported options are:
511 * -pgPort, -pgAllowOthers, -pgDaemon,
512 * -trace, -ifExists, -ifNotExists, -baseDir, -key.
513 * See the main method for details.
514 * <p>
515 * If no port is specified, the default port is used if possible,
516 * and if this port is already used, a random port is used.
517 * Use getPort() or getURL() after starting to retrieve the port.
518 * </p>
519 *
520 * @param args the argument list
521 * @return the server
522 * @throws SQLException on failure
523 */
524 public static Server createPgServer(String... args) throws SQLException {
525 return new Server(new PgServer(), args);
526 }
527
528 /**
529 * Tries to start the server.
530 * @return the server if successful
531 * @throws SQLException if the server could not be started
532 */
533 public Server start() throws SQLException {
534 try {
535 started = true;
536 service.start();
537 String url = service.getURL();
538 int idx = url.indexOf('?');
539 if (idx >= 0) {
540 url = url.substring(0, idx);
541 }
542 String name = service.getName() + " (" + url + ')';
543 Thread t = new Thread(this, name);
544 t.setDaemon(service.isDaemon());
545 t.start();
546 for (int i = 1; i < 64; i += i) {
547 wait(i);
548 if (isRunning(false)) {
549 return this;
550 }
551 }
552 if (isRunning(true)) {
553 return this;
554 }
555 throw DbException.get(ErrorCode.EXCEPTION_OPENING_PORT_2,
556 name, "timeout; " +
557 "please check your network configuration, specially the file /etc/hosts");
558 } catch (DbException e) {
559 throw DbException.toSQLException(e);
560 }
561 }
562
563 private static void wait(int i) {
564 try {
565 // sleep at most 4096 ms
566 long sleep = (long) i * (long) i;
567 Thread.sleep(sleep);
568 } catch (InterruptedException e) {
569 // ignore
570 }
571 }
572
573 private void stopAll() {
574 Server s = web;
575 if (s != null && s.isRunning(false)) {
576 s.stop();
577 web = null;
578 }
579 s = tcp;
580 if (s != null && s.isRunning(false)) {
581 s.stop();
582 tcp = null;
583 }
584 s = pg;
585 if (s != null && s.isRunning(false)) {
586 s.stop();
587 pg = null;
588 }
589 }
590
591 /**
592 * Checks if the server is running.
593 *
594 * @param traceError if errors should be written
595 * @return if the server is running
596 */
597 public boolean isRunning(boolean traceError) {
598 return service.isRunning(traceError);
599 }
600
601 /**
602 * Stops the server.
603 */
604 public void stop() {
605 started = false;
606 if (service != null) {
607 service.stop();
608 }
609 }
610
611 /**
612 * Gets the URL of this server.
613 *
614 * @return the url
615 */
616 public String getURL() {
617 return service.getURL();
618 }
619
620 /**
621 * Gets the port this server is listening on.
622 *
623 * @return the port
624 */
625 public int getPort() {
626 return service.getPort();
627 }
628
629 /**
630 * INTERNAL
631 */
632 @Override
633 public void run() {
634 try {
635 service.listen();
636 } catch (Exception e) {
637 DbException.traceThrowable(e);
638 }
639 }
640
641 /**
642 * INTERNAL
643 * @param shutdownHandler to set
644 */
645 public void setShutdownHandler(ShutdownHandler shutdownHandler) {
646 this.shutdownHandler = shutdownHandler;
647 }
648
649 /**
650 * INTERNAL
651 */
652 @Override
653 public void shutdown() {
654 if (shutdownHandler != null) {
655 shutdownHandler.shutdown();
656 } else {
657 stopAll();
658 }
659 }
660
661 /**
662 * Get the service attached to this server.
663 *
664 * @return the service
665 */
666 public Service getService() {
667 return service;
668 }
669
670 /**
671 * Open a new browser tab or window with the given URL.
672 *
673 * @param url the URL to open
674 * @throws Exception on failure
675 */
676 public static void openBrowser(String url) throws Exception {
677 try {
678 String osName = StringUtils.toLowerEnglish(
679 Utils.getProperty("os.name", "linux"));
680 Runtime rt = Runtime.getRuntime();
681 String browser = Utils.getProperty(SysProperties.H2_BROWSER, null);
682 if (browser == null) {
683 // under Linux, this will point to the default system browser
684 try {
685 browser = System.getenv("BROWSER");
686 } catch (SecurityException se) {
687 // ignore
688 }
689 }
690 if (browser != null) {
691 if (browser.startsWith("call:")) {
692 browser = browser.substring("call:".length());
693 Utils.callStaticMethod(browser, url);
694 } else if (browser.contains("%url")) {
695 String[] args = StringUtils.arraySplit(browser, ',', false);
696 for (int i = 0; i < args.length; i++) {
697 args[i] = StringUtils.replaceAll(args[i], "%url", url);
698 }
699 rt.exec(args);
700 } else if (osName.contains("windows")) {
701 rt.exec(new String[] { "cmd.exe", "/C", browser, url });
702 } else {
703 rt.exec(new String[] { browser, url });
704 }
705 return;
706 }
707 try {
708 Class<?> desktopClass = Class.forName("java.awt.Desktop");
709 // Desktop.isDesktopSupported()
710 Boolean supported = (Boolean) desktopClass.
711 getMethod("isDesktopSupported").
712 invoke(null, new Object[0]);
713 URI uri = new URI(url);
714 if (supported) {
715 // Desktop.getDesktop();
716 Object desktop = desktopClass.getMethod("getDesktop").
717 invoke(null);
718 // desktop.browse(uri);
719 desktopClass.getMethod("browse", URI.class).
720 invoke(desktop, uri);
721 return;
722 }
723 } catch (Exception e) {
724 // ignore
725 }
726 if (osName.contains("windows")) {
727 rt.exec(new String[] { "rundll32", "url.dll,FileProtocolHandler", url });
728 } else if (osName.contains("mac") || osName.contains("darwin")) {
729 // Mac OS: to open a page with Safari, use "open -a Safari"
730 Runtime.getRuntime().exec(new String[] { "open", url });
731 } else {
732 String[] browsers = { "xdg-open", "chromium", "google-chrome",
733 "firefox", "mozilla-firefox", "mozilla", "konqueror",
734 "netscape", "opera", "midori" };
735 boolean ok = false;
736 for (String b : browsers) {
737 try {
738 rt.exec(new String[] { b, url });
739 ok = true;
740 break;
741 } catch (Exception e) {
742 // ignore and try the next
743 }
744 }
745 if (!ok) {
746 // No success in detection.
747 throw new Exception(
748 "Browser detection failed, and java property 'h2.browser' " +
749 "and environment variable BROWSER are not set to a browser executable.");
750 }
751 }
752 } catch (Exception e) {
753 throw new Exception(
754 "Failed to start a browser to open the URL " +
755 url + ": " + e.getMessage());
756 }
757 }
758
759 /**
760 * Start a web server and a browser that uses the given connection. The
761 * current transaction is preserved. This is specially useful to manually
762 * inspect the database when debugging. This method return as soon as the
763 * user has disconnected.
764 *
765 * @param conn the database connection (the database must be open)
766 * @throws SQLException on failure
767 */
768 public static void startWebServer(Connection conn) throws SQLException {
769 startWebServer(conn, false);
770 }
771
772 /**
773 * Start a web server and a browser that uses the given connection. The
774 * current transaction is preserved. This is specially useful to manually
775 * inspect the database when debugging. This method return as soon as the
776 * user has disconnected.
777 *
778 * @param conn the database connection (the database must be open)
779 * @param ignoreProperties if {@code true} properties from
780 * {@code .h2.server.properties} will be ignored
781 * @throws SQLException on failure
782 */
783 public static void startWebServer(Connection conn, boolean ignoreProperties) throws SQLException {
784 WebServer webServer = new WebServer();
785 String[] args;
786 if (ignoreProperties) {
787 args = new String[] { "-webPort", "0", "-properties", "null"};
788 } else {
789 args = new String[] { "-webPort", "0" };
790 }
791 Server web = new Server(webServer, args);
792 web.start();
793 Server server = new Server();
794 server.web = web;
795 webServer.setShutdownHandler(server);
796 String url = webServer.addSession(conn);
797 try {
798 Server.openBrowser(url);
799 while (!webServer.isStopped()) {
800 Thread.sleep(1000);
801 }
802 } catch (Exception e) {
803 // ignore
804 }
805 }
806
807 }