public void handleMessage(final Connection connection, final InputStream dataStream)
     throws IOException {
   final int cmd = readUnsignedByte(dataStream);
   if (cmd != Protocol.AUTH) {
     log.warnf(
         "Received unrecognized greeting code 0x%02x from %s",
         Integer.valueOf(cmd), connection.getPeerAddress());
     connection.close();
     return;
   }
   final int version = StreamUtils.readUnsignedByte(dataStream);
   if (version < 1) {
     log.warnf("Received connection with invalid version from %s", connection.getPeerAddress());
     connection.close();
     return;
   }
   final byte[] authCode = new byte[16];
   StreamUtils.readFully(dataStream, authCode);
   final ManagedProcess process = processController.getServerByAuthCode(authCode);
   if (process == null) {
     log.warnf(
         "Received connection with unknown credentials from %s", connection.getPeerAddress());
     StreamUtils.safeClose(connection);
     return;
   }
   log.tracef("Received authentic connection from %s", connection.getPeerAddress());
   connection.setMessageHandler(
       new ConnectedMessageHandler(processController, process.isInitial()));
   processController.addManagedConnection(connection);
   dataStream.close();
 }
  private boolean lookupServerInModel(String host, String server) throws Exception {
    final ModelNode operation = new ModelNode();
    operation.get(OP).set(READ_RESOURCE_OPERATION);
    operation.get(OP_ADDR).set(getHostControllerServerAddress(host, server));

    final ModelControllerClient client = ModelControllerClient.Factory.create("localhost", HC_PORT);
    try {
      final ModelNode result = client.execute(operation);
      if (result.get(OUTCOME).asString().equals(SUCCESS)) {
        final ModelNode model = result.require(RESULT);
        if (model.hasDefined(NAME) && model.get(NAME).asString().equals(server)) {
          return true;
        }
      }
    } catch (IOException e) {
    } finally {
      StreamUtils.safeClose(client);
    }
    return false;
  }
 protected void printHelp(CommandContext ctx) {
   InputStream helpInput =
       SecurityActions.getClassLoader(CommandHandlerWithHelp.class).getResourceAsStream(filename);
   if (helpInput != null) {
     BufferedReader reader = new BufferedReader(new InputStreamReader(helpInput));
     try {
       String helpLine = reader.readLine();
       while (helpLine != null) {
         ctx.printLine(helpLine);
         helpLine = reader.readLine();
       }
     } catch (java.io.IOException e) {
       ctx.printLine("Failed to read help/help.txt: " + e.getLocalizedMessage());
     } finally {
       StreamUtils.safeClose(reader);
     }
   } else {
     ctx.printLine("Failed to locate command description " + filename);
   }
   return;
 }
    List<String> listProcesses() {
      final Process p;
      try {
        p = Runtime.getRuntime().exec(getJpsCommand());
      } catch (IOException e) {
        throw new RuntimeException(e);
      }

      List<String> processes = new ArrayList<String>();
      BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
      try {
        String line;
        while ((line = input.readLine()) != null) {
          if (line.contains("jboss-modules.jar")) {
            processes.add(line);
          }
        }
      } catch (IOException e) {
        throw new RuntimeException(e);
      } finally {
        StreamUtils.safeClose(input);
      }
      return processes;
    }
      public void handleMessage(final Connection connection, final InputStream dataStream)
          throws IOException {
        try {

          final int cmd = StreamUtils.readUnsignedByte(dataStream);
          switch (cmd) {
            case Protocol.SEND_STDIN:
              {
                // HostController only
                if (isHostController) {
                  final String processName = readUTFZBytes(dataStream);
                  log.tracef("Received send_stdin for process %s", processName);
                  processController.sendStdin(processName, dataStream);
                } else {
                  log.tracef("Ignoring send_stdin message from untrusted source");
                }
                dataStream.close();
                break;
              }
            case Protocol.ADD_PROCESS:
              {
                if (isHostController) {
                  final String processName = readUTFZBytes(dataStream);
                  final byte[] authKey = new byte[16];
                  readFully(dataStream, authKey);
                  final int commandCount = readInt(dataStream);
                  final String[] command = new String[commandCount];
                  for (int i = 0; i < commandCount; i++) {
                    command[i] = readUTFZBytes(dataStream);
                  }
                  final int envCount = readInt(dataStream);
                  final Map<String, String> env = new HashMap<String, String>();
                  for (int i = 0; i < envCount; i++) {
                    env.put(readUTFZBytes(dataStream), readUTFZBytes(dataStream));
                  }
                  final String workingDirectory = readUTFZBytes(dataStream);
                  log.tracef("Received add_process for process %s", processName);
                  processController.addProcess(
                      processName, Arrays.asList(command), env, workingDirectory, false);
                } else {
                  log.tracef("Ignoring add_process message from untrusted source");
                }
                dataStream.close();
                break;
              }
            case Protocol.START_PROCESS:
              {
                if (isHostController) {
                  final String processName = readUTFZBytes(dataStream);
                  processController.startProcess(processName);
                  log.tracef("Received start_process for process %s", processName);
                } else {
                  log.tracef("Ignoring start_process message from untrusted source");
                }
                dataStream.close();
                break;
              }
            case Protocol.STOP_PROCESS:
              {
                if (isHostController) {
                  final String processName = readUTFZBytes(dataStream);
                  // HostController only
                  processController.stopProcess(processName);
                } else {
                  log.tracef("Ignoring stop_process message from untrusted source");
                }
                dataStream.close();
                break;
              }
            case Protocol.REMOVE_PROCESS:
              {
                if (isHostController) {
                  final String processName = readUTFZBytes(dataStream);
                  processController.removeProcess(processName);
                } else {
                  log.tracef("Ignoring remove_process message from untrusted source");
                }
                dataStream.close();
                break;
              }
            case Protocol.REQUEST_PROCESS_INVENTORY:
              {
                if (isHostController) {
                  processController.sendInventory();
                } else {
                  log.tracef("Ignoring request_process_inventory message from untrusted source");
                }
                dataStream.close();
                break;
              }
            case Protocol.RECONNECT_PROCESS:
              {
                if (isHostController) {
                  final String processName = readUTFZBytes(dataStream);
                  final String hostName = readUTFZBytes(dataStream);
                  final int port = readInt(dataStream);
                  processController.sendReconnectProcess(processName, hostName, port);
                } else {
                  log.tracef("Ignoring reconnect_process message from untrusted source");
                }
                dataStream.close();
                break;
              }
            case Protocol.SHUTDOWN:
              {
                if (isHostController) {
                  new Thread(
                          new Runnable() {
                            public void run() {
                              processController.shutdown();
                              System.exit(0);
                            }
                          })
                      .start();
                } else {
                  log.tracef("Ignoring shutdown message from untrusted source");
                }
                break;
              }
            default:
              {
                log.warnf("Received unknown message with code 0x%02x", Integer.valueOf(cmd));
                // unknown
                dataStream.close();
              }
          }
        } finally {
          safeClose(dataStream);
        }
      }