PageRenderTime 52ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/java/src/uk/ac/lkl/migen/system/DatabaseLauncher.java

http://migen.googlecode.com/
Java | 164 lines | 101 code | 18 blank | 45 comment | 8 complexity | c5b2189318e245f6aac16341f035cb96 MD5 | raw file
  1. package uk.ac.lkl.migen.system;
  2. import java.awt.GridLayout;
  3. import java.awt.event.*;
  4. import java.net.SocketException;
  5. import java.net.UnknownHostException;
  6. import java.util.Enumeration;
  7. import java.net.*;
  8. import javax.swing.*;
  9. import uk.ac.lkl.common.util.config.*;
  10. import uk.ac.lkl.common.util.database.*;
  11. /**
  12. * The launcher for the DB server.
  13. *
  14. * This is to enable launching the JavaDB RDBMS without all the
  15. * complexity of the MiGen server. This is only useful for
  16. * data analysis purposes, and not to be used in a MiGen
  17. * environment: use ServerLauncher for that.
  18. *
  19. * @author $Author: sergut $
  20. * @version $Revision: 8505 $
  21. * @version $Date: 2011-02-17 13:15:10 +0000 (Thu, 17 Feb 2011) $
  22. *
  23. */
  24. public class DatabaseLauncher {
  25. /**
  26. * The main frame.
  27. *
  28. * Changes title depending on the server's state for convenience.
  29. */
  30. private JFrame frame;
  31. /**
  32. * The quit button.
  33. *
  34. * Get enabled/disabled depending on the state of the server.
  35. */
  36. private JButton quitButton;
  37. /**
  38. * A button to show the command to connect to the DB.
  39. *
  40. * Convenient for researchers. Can be configured to be shown/hidden.
  41. */
  42. private JButton connectMessageButton;
  43. /**
  44. * The name of the file (i.e. directory) where the DB is.
  45. *
  46. * Is selected at startup if there is more than one.
  47. */
  48. private static String databaseFilename = null;
  49. public DatabaseLauncher() throws ConfigurationException {
  50. String serverName = "localhost";
  51. int serverPort = MiGenConfiguration.getServerPort();
  52. System.out.println("Launching MiGen server on " + serverName + ":" + serverPort);
  53. try {
  54. createServerFrame();
  55. DatabaseServer databaseServer = new DatabaseServer();
  56. databaseServer.start();
  57. setServerFrameAsReady();
  58. } catch (DatabaseException e) {
  59. errorShutdown(e, "Unspecified error", ExitStatus.UNKNOWN_ERROR);
  60. }
  61. }
  62. private void createServerFrame() {
  63. frame = new JFrame("Launching...");
  64. frame.setLayout(new GridLayout(1,2));
  65. quitButton = new JButton("Quit server now");
  66. quitButton.addActionListener(new ActionListener() {
  67. public void actionPerformed(ActionEvent e) {
  68. shutdown(ExitStatus.NO_ERROR);
  69. }});
  70. quitButton.setEnabled(false);
  71. quitButton.setToolTipText("Quit the server");
  72. frame.add(quitButton);
  73. if (MiGenConfiguration.isShowingConnectCommandButton()) {
  74. connectMessageButton = new JButton("How to connect?");
  75. connectMessageButton.addActionListener(new ActionListener() {
  76. public void actionPerformed(ActionEvent e) {
  77. printUsefulInfo();
  78. }});
  79. connectMessageButton.setEnabled(false);
  80. connectMessageButton.setToolTipText("Print useful stuff on the console");
  81. frame.add(connectMessageButton);
  82. }
  83. frame.addWindowListener(new WindowAdapter() {
  84. @Override
  85. public void windowClosing(WindowEvent e) {
  86. shutdown(ExitStatus.NO_ERROR);
  87. }});
  88. frame.pack();
  89. frame.setVisible(true);
  90. }
  91. private void setServerFrameAsReady() {
  92. printUsefulInfo();
  93. frame.setTitle("MiGen Server");
  94. quitButton.setEnabled(true);
  95. if (connectMessageButton != null)
  96. connectMessageButton.setEnabled(true);
  97. }
  98. // Convenience
  99. private void printUsefulInfo() {
  100. // 1. Print IP addresses of all interfaces of this machine
  101. try {
  102. InetAddress myAddress = Inet4Address.getLocalHost();
  103. System.out.println("\nCurrent host: " + myAddress.getCanonicalHostName() + ":" + myAddress.getHostAddress());
  104. System.out.println("All IP addresses of " + myAddress.getHostName() + ":");
  105. for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements();) {
  106. NetworkInterface networkInterface = interfaces.nextElement();
  107. for (Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); addresses.hasMoreElements();) {
  108. InetAddress address = addresses.nextElement();
  109. if (address instanceof Inet4Address)
  110. System.out.println(" * IP4: " + address.getHostAddress());
  111. else
  112. System.out.println(" * IP6: " + address.getHostAddress());
  113. }
  114. }
  115. } catch (UnknownHostException e) {
  116. e.printStackTrace();
  117. } catch (SocketException e) {
  118. e.printStackTrace();
  119. }
  120. System.out.println("Waiting for HTTP requests on port " + MiGenConfiguration.getServerPort());
  121. // 2. Print command to access DB
  122. System.out.println("To fix errors in DB by hand:\nconnect 'jdbc:derby://localhost:1527/" + databaseFilename + "';");
  123. }
  124. /**
  125. * This method exits the server cleanly (e.g. freeing the ports, etc).
  126. *
  127. * All exit paths should call this method instead of System.exit() to
  128. * ensure that everything is in order before closing the application.
  129. *
  130. * @param exitStatus the exit status to be returned to the operating system.
  131. */
  132. public static void shutdown(ExitStatus exitStatus) {
  133. System.out.println("MiGen server halted.");
  134. System.exit(exitStatus.toInt());
  135. }
  136. // convenience for clarity
  137. private static void errorShutdown(Exception e, String msg, ExitStatus exitStatus) {
  138. if (e != null)
  139. e.printStackTrace();
  140. JOptionPane.showMessageDialog(null, msg, "Server Error", JOptionPane.ERROR_MESSAGE);
  141. shutdown(exitStatus);
  142. }
  143. public static void main(String[] args) throws Exception {
  144. new DatabaseLauncher();
  145. }
  146. }