File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\tools\Console.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.tools;
8
9 //## AWT ##
10 import java.awt.Button;
11 import java.awt.Dimension;
12 import java.awt.Font;
13 import java.awt.Frame;
14 import java.awt.GraphicsEnvironment;
15 import java.awt.GridBagConstraints;
16 import java.awt.GridBagLayout;
17 import java.awt.Image;
18 import java.awt.Insets;
19 import java.awt.Label;
20 import java.awt.MenuItem;
21 import java.awt.Panel;
22 import java.awt.PopupMenu;
23 import java.awt.SystemColor;
24 import java.awt.TextField;
25 import java.awt.Toolkit;
26 import java.awt.event.ActionEvent;
27 import java.awt.event.ActionListener;
28 import java.awt.event.MouseEvent;
29 import java.awt.event.MouseListener;
30 import java.awt.event.WindowEvent;
31 import java.awt.event.WindowListener;
32 import java.io.IOException;
33 import java.sql.Connection;
34 import java.sql.SQLException;
35 import org.h2.server.ShutdownHandler;
36 import org.h2.util.JdbcUtils;
37 import org.h2.util.Tool;
38 import org.h2.util.Utils;
39
40 /**
41 * Starts the H2 Console (web-) server, as well as the TCP and PG server.
42 * @h2.resource
43 *
44 * @author Thomas Mueller, Ridvan Agar
45 */
46 public class Console extends Tool implements
47 //## AWT ##
48 ActionListener, MouseListener, WindowListener,
49 //*/
50 ShutdownHandler {
51
52 //## AWT ##
53 private Frame frame;
54 private boolean trayIconUsed;
55 private Font font;
56 private Button startBrowser;
57 private TextField urlText;
58 private Object tray;
59 private Object trayIcon;
60 //*/
61 private Server web, tcp, pg;
62 private boolean isWindows;
63 private long lastOpen;
64
65 /**
66 * When running without options, -tcp, -web, -browser and -pg are started.
67 * <br />
68 * Options are case sensitive. Supported options are:
69 * <table>
70 * <tr><td>[-help] or [-?]</td>
71 * <td>Print the list of options</td></tr>
72 * <tr><td>[-url]</td>
73 * <td>Start a browser and connect to this URL</td></tr>
74 * <tr><td>[-driver]</td>
75 * <td>Used together with -url: the driver</td></tr>
76 * <tr><td>[-user]</td>
77 * <td>Used together with -url: the user name</td></tr>
78 * <tr><td>[-password]</td>
79 * <td>Used together with -url: the password</td></tr>
80 * <tr><td>[-web]</td>
81 * <td>Start the web server with the H2 Console</td></tr>
82 * <tr><td>[-tool]</td>
83 * <td>Start the icon or window that allows to start a browser</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>[-pg]</td>
89 * <td>Start the PG server</td></tr>
90 * </table>
91 * For each Server, additional options are available;
92 * for details, see the Server tool.<br />
93 * If a service can not be started, the program
94 * terminates with an exit code of 1.
95 * @h2.resource
96 *
97 * @param args the command line arguments
98 */
99 public static void main(String... args) throws SQLException {
100 new Console().runTool(args);
101 }
102
103 /**
104 * This tool starts the H2 Console (web-) server, as well as the TCP and PG
105 * server. For JDK 1.6, a system tray icon is created, for platforms that
106 * support it. Otherwise, a small window opens.
107 *
108 * @param args the command line arguments
109 */
110 public void runTool(String... args) throws SQLException {
111 isWindows = Utils.getProperty("os.name", "").startsWith("Windows");
112 boolean tcpStart = false, pgStart = false, webStart = false, toolStart = false;
113 boolean browserStart = false;
114 boolean startDefaultServers = true;
115 boolean printStatus = args != null && args.length > 0;
116 String driver = null, url = null, user = null, password = null;
117 boolean tcpShutdown = false, tcpShutdownForce = false;
118 String tcpPassword = "";
119 String tcpShutdownServer = "";
120 for (int i = 0; args != null && i < args.length; i++) {
121 String arg = args[i];
122 if (arg == null) {
123 continue;
124 } else if ("-?".equals(arg) || "-help".equals(arg)) {
125 showUsage();
126 return;
127 } else if ("-url".equals(arg)) {
128 startDefaultServers = false;
129 url = args[++i];
130 } else if ("-driver".equals(arg)) {
131 driver = args[++i];
132 } else if ("-user".equals(arg)) {
133 user = args[++i];
134 } else if ("-password".equals(arg)) {
135 password = args[++i];
136 } else if (arg.startsWith("-web")) {
137 if ("-web".equals(arg)) {
138 startDefaultServers = false;
139 webStart = true;
140 } else if ("-webAllowOthers".equals(arg)) {
141 // no parameters
142 } else if ("-webDaemon".equals(arg)) {
143 // no parameters
144 } else if ("-webSSL".equals(arg)) {
145 // no parameters
146 } else if ("-webPort".equals(arg)) {
147 i++;
148 } else {
149 showUsageAndThrowUnsupportedOption(arg);
150 }
151 } else if ("-tool".equals(arg)) {
152 startDefaultServers = false;
153 webStart = true;
154 toolStart = true;
155 } else if ("-browser".equals(arg)) {
156 startDefaultServers = false;
157 webStart = true;
158 browserStart = true;
159 } else if (arg.startsWith("-tcp")) {
160 if ("-tcp".equals(arg)) {
161 startDefaultServers = false;
162 tcpStart = true;
163 } else if ("-tcpAllowOthers".equals(arg)) {
164 // no parameters
165 } else if ("-tcpDaemon".equals(arg)) {
166 // no parameters
167 } else if ("-tcpSSL".equals(arg)) {
168 // no parameters
169 } else if ("-tcpPort".equals(arg)) {
170 i++;
171 } else if ("-tcpPassword".equals(arg)) {
172 tcpPassword = args[++i];
173 } else if ("-tcpShutdown".equals(arg)) {
174 startDefaultServers = false;
175 tcpShutdown = true;
176 tcpShutdownServer = args[++i];
177 } else if ("-tcpShutdownForce".equals(arg)) {
178 tcpShutdownForce = true;
179 } else {
180 showUsageAndThrowUnsupportedOption(arg);
181 }
182 } else if (arg.startsWith("-pg")) {
183 if ("-pg".equals(arg)) {
184 startDefaultServers = false;
185 pgStart = true;
186 } else if ("-pgAllowOthers".equals(arg)) {
187 // no parameters
188 } else if ("-pgDaemon".equals(arg)) {
189 // no parameters
190 } else if ("-pgPort".equals(arg)) {
191 i++;
192 } else {
193 showUsageAndThrowUnsupportedOption(arg);
194 }
195 } else if ("-properties".equals(arg)) {
196 i++;
197 } else if ("-trace".equals(arg)) {
198 // no parameters
199 } else if ("-ifExists".equals(arg)) {
200 // no parameters
201 } else if ("-baseDir".equals(arg)) {
202 i++;
203 } else {
204 showUsageAndThrowUnsupportedOption(arg);
205 }
206 }
207 if (startDefaultServers) {
208 webStart = true;
209 toolStart = true;
210 browserStart = true;
211 tcpStart = true;
212 pgStart = true;
213 }
214 if (tcpShutdown) {
215 out.println("Shutting down TCP Server at " + tcpShutdownServer);
216 Server.shutdownTcpServer(tcpShutdownServer, tcpPassword, tcpShutdownForce, false);
217 }
218 SQLException startException = null;
219 boolean webRunning = false;
220
221 if (url != null) {
222 Connection conn = JdbcUtils.getConnection(driver, url, user, password);
223 Server.startWebServer(conn);
224 }
225
226 if (webStart) {
227 try {
228 web = Server.createWebServer(args);
229 web.setShutdownHandler(this);
230 web.start();
231 if (printStatus) {
232 out.println(web.getStatus());
233 }
234 webRunning = true;
235 } catch (SQLException e) {
236 printProblem(e, web);
237 startException = e;
238 }
239 }
240
241 //## AWT ##
242 if (toolStart && webRunning && !GraphicsEnvironment.isHeadless()) {
243 loadFont();
244 try {
245 if (!createTrayIcon()) {
246 showWindow();
247 }
248 } catch (Exception e) {
249 e.printStackTrace();
250 }
251 }
252 //*/
253
254 // start browser in any case (even if the server is already running)
255 // because some people don't look at the output,
256 // but are wondering why nothing happens
257 if (browserStart && web != null) {
258 openBrowser(web.getURL());
259 }
260
261 if (tcpStart) {
262 try {
263 tcp = Server.createTcpServer(args);
264 tcp.start();
265 if (printStatus) {
266 out.println(tcp.getStatus());
267 }
268 tcp.setShutdownHandler(this);
269 } catch (SQLException e) {
270 printProblem(e, tcp);
271 if (startException == null) {
272 startException = e;
273 }
274 }
275 }
276 if (pgStart) {
277 try {
278 pg = Server.createPgServer(args);
279 pg.start();
280 if (printStatus) {
281 out.println(pg.getStatus());
282 }
283 } catch (SQLException e) {
284 printProblem(e, pg);
285 if (startException == null) {
286 startException = e;
287 }
288 }
289 }
290 if (startException != null) {
291 shutdown();
292 throw startException;
293 }
294 }
295
296 private void printProblem(Exception e, Server server) {
297 if (server == null) {
298 e.printStackTrace();
299 } else {
300 out.println(server.getStatus());
301 out.println("Root cause: " + e.getMessage());
302 }
303 }
304
305 private static Image loadImage(String name) {
306 try {
307 byte[] imageData = Utils.getResource(name);
308 if (imageData == null) {
309 return null;
310 }
311 return Toolkit.getDefaultToolkit().createImage(imageData);
312 } catch (IOException e) {
313 e.printStackTrace();
314 return null;
315 }
316 }
317
318 /**
319 * INTERNAL.
320 * Stop all servers that were started using the console.
321 */
322 public void shutdown() {
323 if (web != null && web.isRunning(false)) {
324 web.stop();
325 web = null;
326 }
327 if (tcp != null && tcp.isRunning(false)) {
328 tcp.stop();
329 tcp = null;
330 }
331 if (pg != null && pg.isRunning(false)) {
332 pg.stop();
333 pg = null;
334 }
335 //## AWT ##
336 if (frame != null) {
337 frame.dispose();
338 frame = null;
339 }
340 if (trayIconUsed) {
341 try {
342 // tray.remove(trayIcon);
343 Utils.callMethod(tray, "remove", trayIcon);
344 } catch (Exception e) {
345 // ignore
346 } finally {
347 trayIcon = null;
348 tray = null;
349 trayIconUsed = false;
350 }
351 System.gc();
352 }
353 //*/
354 // System.exit(0);
355 }
356
357 //## AWT ##
358 private void loadFont() {
359 if (isWindows) {
360 font = new Font("Dialog", Font.PLAIN, 11);
361 } else {
362 font = new Font("Dialog", Font.PLAIN, 12);
363 }
364 }
365
366 private boolean createTrayIcon() {
367 try {
368 // SystemTray.isSupported();
369 boolean supported = (Boolean) Utils.callStaticMethod("java.awt.SystemTray.isSupported");
370 if (!supported) {
371 return false;
372 }
373 PopupMenu menuConsole = new PopupMenu();
374 MenuItem itemConsole = new MenuItem("H2 Console");
375 itemConsole.setActionCommand("console");
376 itemConsole.addActionListener(this);
377 itemConsole.setFont(font);
378 menuConsole.add(itemConsole);
379 MenuItem itemStatus = new MenuItem("Status");
380 itemStatus.setActionCommand("status");
381 itemStatus.addActionListener(this);
382 itemStatus.setFont(font);
383 menuConsole.add(itemStatus);
384 MenuItem itemExit = new MenuItem("Exit");
385 itemExit.setFont(font);
386 itemExit.setActionCommand("exit");
387 itemExit.addActionListener(this);
388 menuConsole.add(itemExit);
389
390 // tray = SystemTray.getSystemTray();
391 tray = Utils.callStaticMethod("java.awt.SystemTray.getSystemTray");
392
393 // Dimension d = tray.getTrayIconSize();
394 Dimension d = (Dimension) Utils.callMethod(tray, "getTrayIconSize");
395 String iconFile;
396 if (d.width >= 24 && d.height >= 24) {
397 iconFile = "/org/h2/res/h2-24.png";
398 } else if (d.width >= 22 && d.height >= 22) {
399 // for Mac OS X 10.8.1 with retina display:
400 // the reported resolution is 22 x 22, but the image
401 // is scaled and the real resolution is 44 x 44
402 iconFile = "/org/h2/res/h2-64-t.png";
403 // iconFile = "/org/h2/res/h2-22-t.png";
404 } else {
405 iconFile = "/org/h2/res/h2.png";
406 }
407 Image icon = loadImage(iconFile);
408
409 // trayIcon = new TrayIcon(image, "H2 Database Engine", menuConsole);
410 trayIcon = Utils.newInstance("java.awt.TrayIcon", icon, "H2 Database Engine", menuConsole);
411
412 // trayIcon.addMouseListener(this);
413 Utils.callMethod(trayIcon, "addMouseListener", this);
414
415 // tray.add(trayIcon);
416 Utils.callMethod(tray, "add", trayIcon);
417
418 this.trayIconUsed = true;
419
420 return true;
421 } catch (Exception e) {
422 return false;
423 }
424 }
425
426 private void showWindow() {
427 if (frame != null) {
428 return;
429 }
430 frame = new Frame("H2 Console");
431 frame.addWindowListener(this);
432 Image image = loadImage("/org/h2/res/h2.png");
433 if (image != null) {
434 frame.setIconImage(image);
435 }
436 frame.setResizable(false);
437 frame.setBackground(SystemColor.control);
438
439 GridBagLayout layout = new GridBagLayout();
440 frame.setLayout(layout);
441
442 // the main panel keeps everything together
443 Panel mainPanel = new Panel(layout);
444
445 GridBagConstraints constraintsPanel = new GridBagConstraints();
446 constraintsPanel.gridx = 0;
447 constraintsPanel.weightx = 1.0D;
448 constraintsPanel.weighty = 1.0D;
449 constraintsPanel.fill = GridBagConstraints.BOTH;
450 constraintsPanel.insets = new Insets(0, 10, 0, 10);
451 constraintsPanel.gridy = 0;
452
453 GridBagConstraints constraintsButton = new GridBagConstraints();
454 constraintsButton.gridx = 0;
455 constraintsButton.gridwidth = 2;
456 constraintsButton.insets = new Insets(10, 0, 0, 0);
457 constraintsButton.gridy = 1;
458 constraintsButton.anchor = GridBagConstraints.EAST;
459
460 GridBagConstraints constraintsTextField = new GridBagConstraints();
461 constraintsTextField.fill = GridBagConstraints.HORIZONTAL;
462 constraintsTextField.gridy = 0;
463 constraintsTextField.weightx = 1.0;
464 constraintsTextField.insets = new Insets(0, 5, 0, 0);
465 constraintsTextField.gridx = 1;
466
467 GridBagConstraints constraintsLabel = new GridBagConstraints();
468 constraintsLabel.gridx = 0;
469 constraintsLabel.gridy = 0;
470
471 Label label = new Label("H2 Console URL:", Label.LEFT);
472 label.setFont(font);
473 mainPanel.add(label, constraintsLabel);
474
475 urlText = new TextField();
476 urlText.setEditable(false);
477 urlText.setFont(font);
478 urlText.setText(web.getURL());
479 if (isWindows) {
480 urlText.setFocusable(false);
481 }
482 mainPanel.add(urlText, constraintsTextField);
483
484 startBrowser = new Button("Start Browser");
485 startBrowser.setFocusable(false);
486 startBrowser.setActionCommand("console");
487 startBrowser.addActionListener(this);
488 startBrowser.setFont(font);
489 mainPanel.add(startBrowser, constraintsButton);
490 frame.add(mainPanel, constraintsPanel);
491
492 int width = 300, height = 120;
493 frame.setSize(width, height);
494 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
495 frame.setLocation((screenSize.width - width) / 2, (screenSize.height - height) / 2);
496 try {
497 frame.setVisible(true);
498 } catch (Throwable t) {
499 // ignore
500 // some systems don't support this method, for example IKVM
501 // however it still works
502 }
503 try {
504 // ensure this window is in front of the browser
505 frame.setAlwaysOnTop(true);
506 frame.setAlwaysOnTop(false);
507 } catch (Throwable t) {
508 // ignore
509 }
510 }
511
512 private void startBrowser() {
513 if (web != null) {
514 String url = web.getURL();
515 if (urlText != null) {
516 urlText.setText(url);
517 }
518 long now = System.currentTimeMillis();
519 if (lastOpen == 0 || lastOpen + 100 < now) {
520 lastOpen = now;
521 openBrowser(url);
522 }
523 }
524 }
525 //*/
526
527 private void openBrowser(String url) {
528 try {
529 Server.openBrowser(url);
530 } catch (Exception e) {
531 out.println(e.getMessage());
532 }
533 }
534
535 /**
536 * INTERNAL
537 */
538 //## AWT ##
539 public void actionPerformed(ActionEvent e) {
540 String command = e.getActionCommand();
541 if ("exit".equals(command)) {
542 shutdown();
543 } else if ("console".equals(command)) {
544 startBrowser();
545 } else if ("status".equals(command)) {
546 showWindow();
547 } else if (startBrowser == e.getSource()) {
548 // for some reason, IKVM ignores setActionCommand
549 startBrowser();
550 }
551 }
552 //*/
553
554 /**
555 * INTERNAL
556 */
557 //## AWT ##
558 public void mouseClicked(MouseEvent e) {
559 if (e.getButton() == MouseEvent.BUTTON1) {
560 startBrowser();
561 }
562 }
563 //*/
564
565 /**
566 * INTERNAL
567 */
568 //## AWT ##
569 public void mouseEntered(MouseEvent e) {
570 // nothing to do
571 }
572 //*/
573
574 /**
575 * INTERNAL
576 */
577 //## AWT ##
578 public void mouseExited(MouseEvent e) {
579 // nothing to do
580 }
581 //*/
582
583 /**
584 * INTERNAL
585 */
586 //## AWT ##
587 public void mousePressed(MouseEvent e) {
588 // nothing to do
589 }
590 //*/
591
592 /**
593 * INTERNAL
594 */
595 //## AWT ##
596 public void mouseReleased(MouseEvent e) {
597 // nothing to do
598 }
599 //*/
600
601 /**
602 * INTERNAL
603 */
604 //## AWT ##
605 public void windowClosing(WindowEvent e) {
606 if (trayIconUsed) {
607 frame.dispose();
608 frame = null;
609 } else {
610 shutdown();
611 }
612 }
613 //*/
614
615 /**
616 * INTERNAL
617 */
618 //## AWT ##
619 public void windowActivated(WindowEvent e) {
620 // nothing to do
621 }
622 //*/
623
624 /**
625 * INTERNAL
626 */
627 //## AWT ##
628 public void windowClosed(WindowEvent e) {
629 // nothing to do
630 }
631 //*/
632
633 /**
634 * INTERNAL
635 */
636 //## AWT ##
637 public void windowDeactivated(WindowEvent e) {
638 // nothing to do
639 }
640 //*/
641
642 /**
643 * INTERNAL
644 */
645 //## AWT ##
646 public void windowDeiconified(WindowEvent e) {
647 // nothing to do
648 }
649 //*/
650
651 /**
652 * INTERNAL
653 */
654 //## AWT ##
655 public void windowIconified(WindowEvent e) {
656 // nothing to do
657 }
658 //*/
659
660 /**
661 * INTERNAL
662 */
663 //## AWT ##
664 public void windowOpened(WindowEvent e) {
665 // nothing to do
666 }
667 //*/
668
669 }
670