Пример #1
0
  public MedSavantServerEngine(
      String databaseHost, int databasePort, String rootUserName, String password)
      throws RemoteException, SQLException, SessionExpiredException {

    try {
      // get the address of this host.
      thisAddress = (InetAddress.getLocalHost()).toString();
    } catch (Exception e) {
      throw new RemoteException("Can't get inet address.");
    }

    listenOnPort = MedSavantServerUnicastRemoteObject.getListenPort();

    if (!performPreemptiveSystemCheck()) {
      System.out.println("System check FAILED, see errors above");
      System.exit(1);
    }

    System.out.println("Server Information:");
    System.out.println(
        "  SERVER VERSION: "
            + VersionSettings.getVersionString()
            + "\n"
            + "  SERVER ADDRESS: "
            + thisAddress
            + "\n"
            + "  LISTENING ON PORT: "
            + listenOnPort
            + "\n");
    // + "  EXPORTING ON PORT: " + MedSavantServerUnicastRemoteObject.getExportPort());
    try {
      // create the registry and bind the name and object.
      registry = LocateRegistry.createRegistry(listenOnPort);

      // TODO: get these from the user

      ConnectionController.setHost(databaseHost);
      ConnectionController.setPort(databasePort);

      System.out.println();
      System.out.println("Database Information:");

      System.out.println(
          "  DATABASE ADDRESS: " + databaseHost + "\n" + "  DATABASE PORT: " + databasePort);

      System.out.println("  DATABASE USER: "******"  PASSWORD FOR " + rootUserName + ": ");
        System.out.flush();
        char[] pass = System.console().readPassword();
        password = new String(pass);
      } else {
        System.out.print("  PASSWORD: "******"Connecting to database ... ");
      try {
        ConnectionController.connectOnce(databaseHost, databasePort, "", rootUserName, password);
      } catch (SQLException ex) {
        System.out.println("FAILED");
        throw ex;
      }
      System.out.println("OK");

      bindAdapters(registry);

      System.out.println("\nServer initialized, waiting for incoming connections...");

      EmailLogger.logByEmail("Server booted", "The MedSavant Server Engine successfully booted.");
    } catch (RemoteException e) {
      throw e;
    } catch (SessionExpiredException e) {
      throw e;
    }
  }
Пример #2
0
  public static void main(String args[]) {

    System.out.println("== MedSavant Server Engine ==\n");

    try {

      /** Override with commands from the command line */
      Getopt g = new Getopt("MedSavantServerEngine", args, "c:l:h:p:u:e:");
      //
      int c;

      String user = "******";
      String password = null;
      String host = "localhost";
      int port = 5029;

      // print usage
      if (args.length > 0 && args[0].equals("--help")) {
        System.out.println(
            "java -jar -Djava.rmi.server.hostname=<hostname> MedSavantServerEngine.jar { [-c CONFIG_FILE] } or { [-l RMI_PORT] [-h DATABASE_HOST] [-p DATABASE_PORT] [-u DATABASE_ROOT_USER] [-e ADMIN_EMAIL] }");
        System.out.println(
            "\n\tCONFIG_FILE should be a file containing any number of these keys:\n"
                + "\t\tdb-user - the database user\n"
                + "\t\tdb-password - the database password\n"
                + "\t\tdb-host - the database host\n"
                + "\t\tdb-port - the database port\n"
                + "\t\tlisten-on-port - the port on which clients will connect\n"
                + "\t\temail - the email address to send important notifications\n"
                + "\t\ttmp-dir - the directory to use for temporary files\n"
                + "\t\tms-dir - the directory to use to store permanent files\n");
        return;
      }

      while ((c = g.getopt()) != -1) {
        switch (c) {
          case 'c':
            String configFileName = g.getOptarg();
            System.out.println(
                "Loading configuration from "
                    + (new File(configFileName)).getAbsolutePath()
                    + " ...");

            Properties prop = new Properties();
            try {
              prop.load(new FileInputStream(configFileName));
              if (prop.containsKey("db-user")) {
                user = prop.getProperty("db-user");
              }
              if (prop.containsKey("db-password")) {
                password = prop.getProperty("db-password");
              }
              if (prop.containsKey("db-host")) {
                host = prop.getProperty("db-host");
              }
              if (prop.containsKey("db-port")) {
                port = Integer.parseInt(prop.getProperty("db-port"));
              }
              if (prop.containsKey("listen-on-port")) {
                int listenOnPort = Integer.parseInt(prop.getProperty("listen-on-port"));
                MedSavantServerUnicastRemoteObject.setListenPort(listenOnPort);
                // MedSavantServerUnicastRemoteObject.setExportPort(listenOnPort + 1);
              }
              if (prop.containsKey("email")) {
                EmailLogger.setMailRecipient(prop.getProperty("email"));
              }
              if (prop.containsKey("tmp-dir")) {
                DirectorySettings.setTmpDirectory(prop.getProperty("tmp-dir"));
              }
              if (prop.containsKey("ms-dir")) {
                DirectorySettings.setMedSavantDirectory(prop.getProperty("ms-dir"));
              }

            } catch (Exception e) {
              System.out.println("ERROR: Could not load properties file " + configFileName);
            }
            break;
          case 'h':
            System.out.println("Host " + g.getOptarg());
            host = g.getOptarg();
            break;
          case 'p':
            port = Integer.parseInt(g.getOptarg());
            break;
          case 'l':
            int listenOnPort = Integer.parseInt(g.getOptarg());
            MedSavantServerUnicastRemoteObject.setListenPort(listenOnPort);
            // MedSavantServerUnicastRemoteObject.setExportPort(listenOnPort + 1);
            break;
          case 'u':
            user = g.getOptarg();
            break;
          case 'e':
            EmailLogger.setMailRecipient(g.getOptarg());
            break;
          case '?':
            break; // getopt() already printed an error
          default:
            System.out.print("getopt() returned " + c + "\n");
        }
      }

      new MedSavantServerEngine(host, port, user, password);
    } catch (Exception e) {
      e.printStackTrace();
      System.exit(1);
    }
  }