예제 #1
0
  /**
   * 'handler' can be of any type that implements 'exportedInterface', but only methods declared by
   * the interface (and its superinterfaces) will be invocable.
   */
  public <T> InAppServer(
      String name,
      String portFilename,
      InetAddress inetAddress,
      Class<T> exportedInterface,
      T handler) {
    this.fullName = name + "Server";
    this.exportedInterface = exportedInterface;
    this.handler = handler;

    // In the absence of authentication, we shouldn't risk starting a server as root.
    if (System.getProperty("user.name").equals("root")) {
      Log.warn(
          "InAppServer: refusing to start unauthenticated server \"" + fullName + "\" as root!");
      return;
    }

    try {
      File portFile = FileUtilities.fileFromString(portFilename);
      secretFile = new File(portFile.getPath() + ".secret");
      Thread serverThread = new Thread(new ConnectionAccepter(portFile, inetAddress), fullName);
      // If there are no other threads left, the InApp server shouldn't keep us alive.
      serverThread.setDaemon(true);
      serverThread.start();
    } catch (Throwable th) {
      Log.warn("InAppServer: couldn't start \"" + fullName + "\".", th);
    }
    writeNewSecret();
  }
 /**
  * Reads all the lines from the named file into a string array. Throws a RuntimeException on
  * failure.
  */
 public static String[] readLinesFromFile(String filename) {
   ArrayList<String> result = new ArrayList<String>();
   LineNumberReader in = null;
   try {
     File file = FileUtilities.fileFromString(filename);
     in = new LineNumberReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
     String line;
     while ((line = in.readLine()) != null) {
       result.add(line);
     }
     return result.toArray(new String[result.size()]);
   } catch (IOException ex) {
     throw new RuntimeException(ex);
   } finally {
     FileUtilities.close(in);
   }
 }