CommandContext readCommandContext()

in nailgun-server/src/main/java/com/facebook/nailgun/NGCommunicator.java [129:178]


  CommandContext readCommandContext() throws IOException {
    // client info - command line arguments and environment
    List<String> remoteArgs = new ArrayList();
    Properties remoteEnv = new Properties();
    String cwd = null; // working directory
    String command = null; // alias or class name
    // read everything from the client up to and including the command
    while (command == null) {
      int bytesToRead = in.readInt();
      byte chunkType = in.readByte();

      byte[] b = new byte[bytesToRead];
      in.readFully(b);
      String line = new String(b, "UTF-8");

      switch (chunkType) {
        case NGConstants.CHUNKTYPE_ARGUMENT:
          //	command line argument
          remoteArgs.add(line);
          break;

        case NGConstants.CHUNKTYPE_ENVIRONMENT:
          //	parse environment into property
          int equalsIndex = line.indexOf('=');
          if (equalsIndex > 0) {
            remoteEnv.setProperty(line.substring(0, equalsIndex), line.substring(equalsIndex + 1));
          }
          break;

        case NGConstants.CHUNKTYPE_COMMAND:
          // 	command (alias or classname)
          command = line;
          break;

        case NGConstants.CHUNKTYPE_WORKINGDIRECTORY:
          //	client working directory
          cwd = line;
          break;

        default: // freakout?
      }
    }

    // Command and environment is read. Move other communication with client, which is heartbeats
    // and
    // stdin, to background thread
    startBackgroundReceive();

    return new CommandContext(command, cwd, remoteEnv, remoteArgs);
  }