All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xapi.shell.impl.ShellCommandDefault Maven / Gradle / Ivy

Go to download

Everything needed to run a comprehensive dev environment. Just type X_ and pick a service from autocomplete; new dev modules will be added as they are built. The only dev service not included in the uber jar is xapi-dev-maven, as it includes all runtime dependencies of maven, adding ~4 seconds to build time, and 6 megabytes to the final output jar size (without xapi-dev-maven, it's ~1MB).

The newest version!
package xapi.shell.impl;

import java.io.File;
import java.io.IOException;

import xapi.annotation.inject.InstanceDefault;
import xapi.collect.X_Collect;
import xapi.collect.api.Fifo;
import xapi.file.X_File;
import xapi.log.X_Log;
import xapi.process.X_Process;
import xapi.shell.api.ArgumentProcessor;
import xapi.shell.api.ShellCommand;
import xapi.shell.api.ShellSession;
import xapi.util.X_Debug;
import xapi.util.api.ErrorHandler;
import xapi.util.api.SuccessHandler;

@InstanceDefault(implFor = ShellCommand.class)
public class ShellCommandDefault implements ShellCommand {

  final Fifo commands;
  private String owner="User";
  private String directory;

  public ShellCommandDefault() {
    this.commands = X_Collect.newFifo();
  }

  public ShellCommandDefault(String owner, String... cmds) {// for the public
    this();
    this.owner = owner;
    for (String cmd : cmds) {
      commands.give(cmd);
    }
  }

  @Override
  public String owner() {
    return owner;
  }

  @Override
  public String directory() {
    if (directory == null) {
      File dir = X_File.createTempDir("exeFor"+owner, true);
      if (dir == null) {
        directory = ".";
      } else {
        if (dir.exists() || dir.mkdirs()) {
          directory = dir.getPath();
        } else {
          directory = ".";
        }
      }
    }
    return directory;
  }

  @Override
  public Fifo commands() {
    return commands;
  }

  public ShellCommand directory(String directory) {
    this.directory = directory;
    return this;
  }

  @Override
  public ShellCommand owner(String owner) {
    if (owner != null)
      return this;
    this.owner = owner;
    return this;
  }

  @Override
  public ShellCommand commands(String... cmds) {
    for (String cmd : cmds) {
      commands.give(cmd);
    }
    return this;
  }

  
  @Override
  @SuppressWarnings({ "unchecked", "rawtypes" })
  public ShellSession run(final SuccessHandler callback, final ArgumentProcessor processor) {
    final ErrorHandler err = callback instanceof ErrorHandler ? (ErrorHandler) callback
        : X_Debug.defaultHandler();
    
    final ShellSessionDefault result = new ShellSessionDefault(this, processor, callback, err);
    X_Process.newThread(result).start();
    return result;
  }

  protected Process doRun(ArgumentProcessor processor)
      throws IOException {
    if (processor == null)
      processor = ArgumentProcessor.NO_OP;
    ProcessBuilder pb = new ProcessBuilder(
        (processor == null ? ArgumentProcessor.NO_OP : processor)
            .convert(commands.forEach()));
    File dir = new File(directory()).getCanonicalFile();
    if (dir.exists()) {
      pb.directory(dir);
    } else {
      X_Log.error(getClass(), "Process run directory " + dir.getCanonicalPath()
          + " does not exist " + "for ");
    }
    return pb.start();
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy