Ejemplo n.º 1
0
  private ApplicationProcessDescriptor getDescriptor(
      RunnerProcess process, ServiceContext restfulRequestContext) throws RunnerException {
    final ApplicationStatus status =
        process.getError() == null
            ? (process.isCancelled()
                ? ApplicationStatus.CANCELLED
                : (process.isStopped()
                    ? ApplicationStatus.STOPPED
                    : (process.isStarted() ? ApplicationStatus.RUNNING : ApplicationStatus.NEW)))
            : ApplicationStatus.FAILED;
    final List<Link> links = new LinkedList<>();
    final UriBuilder servicePathBuilder = restfulRequestContext.getServiceUriBuilder();
    final DtoFactory dtoFactory = DtoFactory.getInstance();
    links.add(
        dtoFactory
            .createDto(Link.class)
            .withRel(Constants.LINK_REL_GET_STATUS)
            .withHref(
                servicePathBuilder
                    .clone()
                    .path(getClass(), "getStatus")
                    .build(process.getRunner(), process.getId())
                    .toString())
            .withMethod("GET")
            .withProduces(MediaType.APPLICATION_JSON));
    links.add(
        dtoFactory
            .createDto(Link.class)
            .withRel(Constants.LINK_REL_VIEW_LOG)
            .withHref(
                servicePathBuilder
                    .clone()
                    .path(getClass(), "getLogs")
                    .build(process.getRunner(), process.getId())
                    .toString())
            .withMethod("GET"));
    switch (status) {
      case NEW:
      case RUNNING:
        links.add(
            dtoFactory
                .createDto(Link.class)
                .withRel(Constants.LINK_REL_STOP)
                .withHref(
                    servicePathBuilder
                        .clone()
                        .path(getClass(), "stop")
                        .build(process.getRunner(), process.getId())
                        .toString())
                .withMethod("POST")
                .withProduces(MediaType.APPLICATION_JSON));
        break;
    }
    final RunnerConfiguration configuration = process.getConfiguration();
    final RunRequest request = configuration.getRequest();
    final java.io.File recipeFile = configuration.getRecipeFile();
    if (recipeFile != null) {
      links.add(
          dtoFactory
              .createDto(Link.class)
              .withRel(Constants.LINK_REL_RUNNER_RECIPE)
              .withHref(
                  servicePathBuilder
                      .clone()
                      .path(getClass(), "getRecipeFile")
                      .build(process.getRunner(), process.getId())
                      .toString())
              .withMethod("GET")
              .withProduces(MediaType.TEXT_PLAIN));
    }
    final List<Link> additionalLinks = new LinkedList<>();
    PortMapping portMapping = null;
    switch (status) {
      case NEW:
      case RUNNING:
        for (Link link : configuration.getLinks()) {
          additionalLinks.add(dtoFactory.clone(link));
        }
        final Map<String, String> ports = configuration.getPortMapping();
        if (!ports.isEmpty()) {
          portMapping =
              dtoFactory
                  .createDto(PortMapping.class)
                  .withHost(configuration.getHost())
                  .withPorts(new HashMap<>(ports));
        }
        break;
      default:
        for (Link link : configuration.getLinks()) {
          if ("web url".equals(link.getRel()) || "shell url".equals(link.getRel())) {
            // Hide web and shell links if application is not running.
            continue;
          }
          additionalLinks.add(dtoFactory.clone(link));
        }
        break;
    }

    links.addAll(additionalLinks);
    return dtoFactory
        .createDto(ApplicationProcessDescriptor.class)
        .withProcessId(process.getId())
        .withStatus(status)
        .withStartTime(process.getStartTime())
        .withStopTime(process.getStopTime())
        .withLinks(links)
        .withWorkspace(request.getWorkspace())
        .withProject(request.getProject())
        .withUserId(request.getUserId())
        .withDebugHost(configuration.getDebugHost())
        .withDebugPort(configuration.getDebugPort())
        .withPortMapping(portMapping);
  }