Package aQute.jpm.lib

Examples of aQute.jpm.lib.Service


    }

    List<String> cmdline = opts._();
    String name = cmdline.remove(0);

    Service s = jpm.getService(name);

    if (opts.remove()) {
      if (!jpm.hasAccess()) {
        error("No write access to remove service %s", name);
        return;
      }
      if (s == null) {
        error("No such service %s to remove", name);
        return;
      }
      s.stop();
      s.remove();
      return;
    }

    if (opts.create() != null) {
      if (s != null) {
        error("Service already exists, cannot be created: %s. Update or remove it first", name);
        return;
      }

      ArtifactData target = jpm.getCandidate(opts.create());
      if (target == null)
        return;

      ServiceData data = null; // TODO target.service;
      updateServiceData(data, opts);

      String result = jpm.createService(data);
      if (result != null)
        error("Create service failed: %s", result);
      return;
    }

    if (s == null) {
      error("No such service: %s", name);
      return;
    }

    ServiceData data = s.getServiceData();
    if (updateServiceData(data, opts) || opts.coordinates() != null || opts.update()) {
      if (!jpm.hasAccess()) {
        error("No write access to update service %s", name);
        return;
      }

      //
      // Check if we have to update the underlying artifact
      // This is triggered by --coordinate, which provides
      // the new coordinate or just --update which reuses the
      // old coordinate without version
      //

      if (opts.coordinates() != null || opts.update()) {
        String coordinates = opts.coordinates();
        if (coordinates == null) {
          error("No coordinate found in old service record");
          return;
        }

        int n = coordinates.indexOf('@');
        if (n > 0)
          coordinates = coordinates.substring(0, n);

        trace("Updating from coordinate: %s", coordinates);
        ArtifactData target = jpm.getCandidate(coordinates);
        if (target == null) {
          error("No candidates found for %s (%s)", coordinates, opts.staged() ? "staged" : "only masters");
          return;
        }

        // data.dependencies.clear();
        // data.dependencies.add(target.file);
        // data.dependencies.addAll(target.dependencies);
        // data.coordinates = coordinates;
      }
      String result = jpm.createService(data);
      if (result != null)
        error("Update service failed: %s", result);
      else if (s.isRunning())
        warning("Changes will not affect the currently running process");
    }
    Data.details(data, out);
  }
View Full Code Here


    return update;
  }

  private void print(ServiceData sd) throws Exception {
    Service s = jpm.getService(sd.name);
    out.printf("%-40s (%s) %s%n", sd.name, s.isRunning() ? "runs   " : "stopped", sd.args);
  }
View Full Code Here

    if (!jpm.hasAccess()) {
      error("No write acces, might require administrator or root privileges (sudo in *nix)");
      return;
    }
    for (String s : options._()) {
      Service service = jpm.getService(s);
      if (service == null)
        error("Non existent service %s", s);
      else {
        if (!service.isRunning()) {
          try {
            ServiceData d = service.getServiceData();
            trace("starting %s as user %s, lock=%s, log=%s", d.name, d.user, d.lock, d.log);
            if (options.clean())
              service.clear();
            String result = service.start();
            if (result != null)
              error("Failed to start: %s", result);
          }
          catch (Exception e) {
            exception(e, "Could not start service %s due to %s", s, e.getMessage());
View Full Code Here

  public interface RestartOptions extends Options {}

  @Description("Restart a service")
  public void _restart(RestartOptions options) throws Exception {
    for (String s : options._()) {
      Service service = jpm.getService(s);
      if (service == null)
        error("Non existent service %s", s);
      else {
        try {
          if (service.isRunning()) {
            service.stop();
          }

          String result = service.start();
          if (result != null)
            error("Failed to start: %s", result);
        }
        catch (Exception e) {
          exception(e, "Could not start service %s due to %s", s, e.getMessage());
View Full Code Here

  public void _trace(traceOptions options) throws Exception {
    List<String> args = options._();
    String s = args.remove(0);
    boolean on = args.isEmpty() || !"off".equalsIgnoreCase(args.remove(0));

    Service service = jpm.getService(s);
    if (service == null)
      error("Non existent service %s", s);
    else {
      try {
        if (!service.isRunning())
          error("First start the service to trace it");
        else {
          String result = service.trace(on);
          if (result != null)
            error("Failed to trace: %s", result);
        }
      }
      catch (Exception e) {
View Full Code Here

  public interface StopOptions extends Options {}

  @Description("Stop a service")
  public void _stop(StopOptions options) throws Exception {
    for (String s : options._()) {
      Service service = jpm.getService(s);
      if (service == null)
        error("Non existent service %s", s);
      else {
        if (service.isRunning()) {
          try {
            String result = service.stop();
            if (result != null)
              error("Failed to stop: %s", result);
          }
          catch (Exception e) {
            exception(e, "Could not stop service %s due to %s", s, e.getMessage());
View Full Code Here

    while (true) {
      for (String s : options._()) {
        String runs = "false";
        String status = "no service";
        try {
          Service service = jpm.getService(s);
          if (service != null) {
            runs = service.isRunning() + "";
            status = service.status();
          }
        }
        catch (Exception e) {
          status = e.getMessage();
          exception(e, "could not fetch status information from service %s, due to %s", s, e.getMessage());
View Full Code Here

    String s = opts._().isEmpty() ? null : opts._().get(0);
    if (s == null) {
      error("No such service %s", s);
      return;
    }
    Service service = jpm.getService(s);
    if (service == null) {
      error("No such service %s", s);
      return;
    }

    ServiceData data = service.getServiceData();
    File logFile = new File(data.log);
    if (!logFile.isFile()) {
      error("Log file %s for service %s is not a file", logFile, s);
      return;
    }

    if (opts.clear()) {
      logFile.delete();
      logFile.createNewFile();
    }

    RandomAccessFile raf = new RandomAccessFile(logFile, "r");
    try {
      long start = Math.max(logFile.length() - 2000, 0);
      while (true) {
        long l = raf.length();
        byte[] buffer = new byte[(int) (l - start)];
        raf.seek(start);
        raf.read(buffer);
        out.write(buffer);
        start = l;
        if (!service.isRunning() || !opts.tail())
          return;

        if (l == raf.length())
          Thread.sleep(100);
      }
View Full Code Here

    }

    int ccount = 0, scount = 0;

    for (String name : toDelete) {
      Service s = null;
      if (jpm.getCommand(name) != null) { // Try command first
        trace("Corresponding command found, removing");
        jpm.deleteCommand(name);
        ccount++;

      } else if ((s = jpm.getService(name)) != null) { // No command
                                // matching, try
                                // service
        trace("Corresponding service found, removing");
        s.remove();
        scount++;

      } else { // No match amongst commands & services
        error("No matching command or service found for: %s", name);
      }
View Full Code Here

        Glob glob = new Glob(pattern);
        for (String name : refs) {
          if (glob.matcher(name).matches()) {
            CommandData data = jpm.getCommand(name);
            if (data == null) {
              Service service = jpm.getService(name);
              if (service != null) {
                data = service.getServiceData();
              }
            }
            if (data != null) {
              datas.add(data);
            }
View Full Code Here

TOP

Related Classes of aQute.jpm.lib.Service

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.