/java/src/uk/ac/lkl/migen/system/DatabaseLauncher.java
Java | 164 lines | 101 code | 18 blank | 45 comment | 8 complexity | c5b2189318e245f6aac16341f035cb96 MD5 | raw file
- package uk.ac.lkl.migen.system;
-
- import java.awt.GridLayout;
- import java.awt.event.*;
- import java.net.SocketException;
- import java.net.UnknownHostException;
- import java.util.Enumeration;
- import java.net.*;
-
- import javax.swing.*;
-
- import uk.ac.lkl.common.util.config.*;
- import uk.ac.lkl.common.util.database.*;
-
- /**
- * The launcher for the DB server.
- *
- * This is to enable launching the JavaDB RDBMS without all the
- * complexity of the MiGen server. This is only useful for
- * data analysis purposes, and not to be used in a MiGen
- * environment: use ServerLauncher for that.
- *
- * @author $Author: sergut $
- * @version $Revision: 8505 $
- * @version $Date: 2011-02-17 13:15:10 +0000 (Thu, 17 Feb 2011) $
- *
- */
- public class DatabaseLauncher {
-
- /**
- * The main frame.
- *
- * Changes title depending on the server's state for convenience.
- */
- private JFrame frame;
-
- /**
- * The quit button.
- *
- * Get enabled/disabled depending on the state of the server.
- */
- private JButton quitButton;
-
- /**
- * A button to show the command to connect to the DB.
- *
- * Convenient for researchers. Can be configured to be shown/hidden.
- */
- private JButton connectMessageButton;
-
- /**
- * The name of the file (i.e. directory) where the DB is.
- *
- * Is selected at startup if there is more than one.
- */
- private static String databaseFilename = null;
-
- public DatabaseLauncher() throws ConfigurationException {
- String serverName = "localhost";
- int serverPort = MiGenConfiguration.getServerPort();
- System.out.println("Launching MiGen server on " + serverName + ":" + serverPort);
- try {
- createServerFrame();
- DatabaseServer databaseServer = new DatabaseServer();
- databaseServer.start();
- setServerFrameAsReady();
- } catch (DatabaseException e) {
- errorShutdown(e, "Unspecified error", ExitStatus.UNKNOWN_ERROR);
- }
- }
-
- private void createServerFrame() {
- frame = new JFrame("Launching...");
- frame.setLayout(new GridLayout(1,2));
-
- quitButton = new JButton("Quit server now");
- quitButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- shutdown(ExitStatus.NO_ERROR);
- }});
- quitButton.setEnabled(false);
- quitButton.setToolTipText("Quit the server");
- frame.add(quitButton);
-
- if (MiGenConfiguration.isShowingConnectCommandButton()) {
- connectMessageButton = new JButton("How to connect?");
- connectMessageButton.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- printUsefulInfo();
- }});
- connectMessageButton.setEnabled(false);
- connectMessageButton.setToolTipText("Print useful stuff on the console");
- frame.add(connectMessageButton);
- }
-
- frame.addWindowListener(new WindowAdapter() {
- @Override
- public void windowClosing(WindowEvent e) {
- shutdown(ExitStatus.NO_ERROR);
- }});
- frame.pack();
- frame.setVisible(true);
- }
-
- private void setServerFrameAsReady() {
- printUsefulInfo();
- frame.setTitle("MiGen Server");
- quitButton.setEnabled(true);
- if (connectMessageButton != null)
- connectMessageButton.setEnabled(true);
- }
-
- // Convenience
- private void printUsefulInfo() {
- // 1. Print IP addresses of all interfaces of this machine
- try {
- InetAddress myAddress = Inet4Address.getLocalHost();
- System.out.println("\nCurrent host: " + myAddress.getCanonicalHostName() + ":" + myAddress.getHostAddress());
- System.out.println("All IP addresses of " + myAddress.getHostName() + ":");
- for (Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); interfaces.hasMoreElements();) {
- NetworkInterface networkInterface = interfaces.nextElement();
- for (Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); addresses.hasMoreElements();) {
- InetAddress address = addresses.nextElement();
- if (address instanceof Inet4Address)
- System.out.println(" * IP4: " + address.getHostAddress());
- else
- System.out.println(" * IP6: " + address.getHostAddress());
- }
- }
- } catch (UnknownHostException e) {
- e.printStackTrace();
- } catch (SocketException e) {
- e.printStackTrace();
- }
- System.out.println("Waiting for HTTP requests on port " + MiGenConfiguration.getServerPort());
- // 2. Print command to access DB
- System.out.println("To fix errors in DB by hand:\nconnect 'jdbc:derby://localhost:1527/" + databaseFilename + "';");
- }
-
- /**
- * This method exits the server cleanly (e.g. freeing the ports, etc).
- *
- * All exit paths should call this method instead of System.exit() to
- * ensure that everything is in order before closing the application.
- *
- * @param exitStatus the exit status to be returned to the operating system.
- */
- public static void shutdown(ExitStatus exitStatus) {
- System.out.println("MiGen server halted.");
- System.exit(exitStatus.toInt());
- }
-
- // convenience for clarity
- private static void errorShutdown(Exception e, String msg, ExitStatus exitStatus) {
- if (e != null)
- e.printStackTrace();
- JOptionPane.showMessageDialog(null, msg, "Server Error", JOptionPane.ERROR_MESSAGE);
- shutdown(exitStatus);
- }
-
- public static void main(String[] args) throws Exception {
- new DatabaseLauncher();
- }
- }