/** @see org.overlord.sramp.shell.api.shell.ShellCommand#execute() */
  @Override
  public boolean execute() throws Exception {
    String filePathArg =
        this.requiredArgument(
            0, Messages.i18n.format("Upload.InvalidArgMsg.LocalFile")); // $NON-NLS-1$
    String artifactTypeArg = this.optionalArgument(1);

    QName clientVarName = new QName("s-ramp", "client"); // $NON-NLS-1$ //$NON-NLS-2$
    SrampAtomApiClient client = (SrampAtomApiClient) getContext().getVariable(clientVarName);
    if (client == null) {
      print(Messages.i18n.format("MissingSRAMPConnection")); // $NON-NLS-1$
      return false;
    }
    InputStream content = null;
    ZipToSrampArchive expander = null;
    SrampArchive archive = null;
    try {
      File file = new File(filePathArg);
      ArtifactType artifactType = null;
      if (artifactTypeArg != null) {
        artifactType = ArtifactType.valueOf(artifactTypeArg);
        if (artifactType.isExtendedType()) {
          artifactType = ArtifactType.ExtendedDocument(artifactType.getExtendedType());
        }
      } else {
        artifactType = determineArtifactType(file);
      }
      content = FileUtils.openInputStream(file);
      BaseArtifactType artifact = client.uploadArtifact(artifactType, content, file.getName());
      IOUtils.closeQuietly(content);

      // Now also add "expanded" content to the s-ramp repository
      expander = ZipToSrampArchiveRegistry.createExpander(artifactType, file);
      if (expander != null) {
        expander.setContextParam(DefaultMetaDataFactory.PARENT_UUID, artifact.getUuid());
        archive = expander.createSrampArchive();
        client.uploadBatch(archive);
      }

      // Put the artifact in the session as the active artifact
      QName artifactVarName = new QName("s-ramp", "artifact"); // $NON-NLS-1$ //$NON-NLS-2$
      getContext().setVariable(artifactVarName, artifact);
      print(Messages.i18n.format("Upload.Success")); // $NON-NLS-1$
      PrintArtifactMetaDataVisitor visitor = new PrintArtifactMetaDataVisitor();
      ArtifactVisitorHelper.visitArtifact(visitor, artifact);
    } catch (Exception e) {
      print(Messages.i18n.format("Upload.Failure")); // $NON-NLS-1$
      print("\t" + e.getMessage()); // $NON-NLS-1$
      IOUtils.closeQuietly(content);
      return false;
    }
    return true;
  }
  /*
   * (non-Javadoc)
   *
   * @see
   * org.jboss.arquillian.container.sramp.SrampService#deployArchive(java.
   * lang.String, java.lang.String, java.io.InputStream)
   */
  public BaseArtifactType deployArchive(
      String archiveId, String archiveName, String artifactTypeArg, InputStream content) {

    assert content != null;

    ZipToSrampArchive expander = null;
    SrampArchive archive = null;
    BaseArtifactType artifact = null;
    File tempResourceFile = null;
    try {
      // internal integrity check
      artifactCounter = client.query("/s-ramp").getTotalResults();

      // First, stash the content in a temp file - we may need it multiple
      // times.
      tempResourceFile = stashResourceContent(content);
      content = FileUtils.openInputStream(tempResourceFile);

      ArtifactType artifactType = ArtifactType.valueOf(artifactTypeArg);
      if (artifactType.isExtendedType()) {
        artifactType = ArtifactType.ExtendedDocument(artifactType.getExtendedType());
      }

      artifact = client.uploadArtifact(artifactType, content, archiveName);
      IOUtils.closeQuietly(content);

      // for all uploaded files add custom property
      SrampModelUtils.setCustomProperty(artifact, "arquillian-archive-id", archiveId);
      client.updateArtifactMetaData(artifact);

      content = FileUtils.openInputStream(tempResourceFile);

      // Now also add "expanded" content to the s-ramp repository
      expander = ZipToSrampArchiveRegistry.createExpander(artifactType, content);

      if (expander != null) {
        expander.setContextParam(DefaultMetaDataFactory.PARENT_UUID, artifact.getUuid());
        archive = expander.createSrampArchive();
        client.uploadBatch(archive);
      }
    } catch (Exception e) {
      log.error("Upload failure:", e);
      IOUtils.closeQuietly(content);
    } finally {
      SrampArchive.closeQuietly(archive);
      ZipToSrampArchive.closeQuietly(expander);
      FileUtils.deleteQuietly(tempResourceFile);
    }

    return artifact;
  }
  /**
   * Uploads a single deployment to S-RAMP.
   *
   * @param deploymentType
   * @param fileName
   * @param client
   * @param tempFile
   * @param responseParams
   * @param version
   * @throws Exception
   */
  private void uploadSingleDeployment(
      String deploymentType,
      String fileName,
      File tempFile,
      Map<String, String> responseParams,
      String version)
      throws Exception {
    ArtifactType at = ArtifactType.valueOf(deploymentType);
    if (at.isExtendedType()) {
      at = ArtifactType.ExtendedDocument(at.getExtendedType());
    }
    String uuid = null;
    // First, upload the deployment
    InputStream contentStream = null;
    try {
      contentStream = FileUtils.openInputStream(tempFile);
      BaseArtifactType artifact = at.newArtifactInstance();
      artifact.setName(fileName);
      artifact.setVersion(version);
      artifact = clientAccessor.getClient().uploadArtifact(artifact, contentStream);
      responseParams.put("model", at.getArtifactType().getModel()); // $NON-NLS-1$
      responseParams.put("type", at.getArtifactType().getType()); // $NON-NLS-1$
      responseParams.put("uuid", artifact.getUuid()); // $NON-NLS-1$
      uuid = artifact.getUuid();
    } finally {
      IOUtils.closeQuietly(contentStream);
    }

    // Try to expand the artifact (works if an expander is available for the given artifact type).
    ZipToSrampArchive j2sramp = null;
    SrampArchive archive = null;
    try {
      j2sramp = ZipToSrampArchiveRegistry.createExpander(at, tempFile);
      if (j2sramp != null) {
        j2sramp.setContextParam(DefaultMetaDataFactory.PARENT_UUID, uuid);
        archive = j2sramp.createSrampArchive();
        clientAccessor.getClient().uploadBatch(archive);
      }
    } finally {
      SrampArchive.closeQuietly(archive);
      ZipToSrampArchive.closeQuietly(j2sramp);
    }
  }
示例#4
0
  /**
   * Puts the artifact into the s-ramp repository.
   *
   * @param gavInfo
   * @param resourceInputStream
   * @throws TransferFailedException
   */
  private void doPutArtifact(final MavenGavInfo gavInfo, InputStream resourceInputStream)
      throws TransferFailedException {

    // See the comment in {@link SrampWagon#fillInputData(InputData)} about why we're doing this
    // context classloader magic.
    ClassLoader oldCtxCL = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(SrampWagon.class.getClassLoader());
    File tempResourceFile = null;
    ZipToSrampArchive expander = null;
    SrampArchive archive = null;
    BaseArtifactType artifactGrouping = null;
    try {
      // First, stash the content in a temp file - we may need it multiple times.
      tempResourceFile = stashResourceContent(resourceInputStream);
      resourceInputStream = FileUtils.openInputStream(tempResourceFile);

      ArchiveInfo archiveInfo = ZipToSrampArchiveRegistry.inspectArchive(resourceInputStream);
      ArtifactType artifactType = getArtifactType(gavInfo, archiveInfo.type);

      resourceInputStream = FileUtils.openInputStream(tempResourceFile);

      // Is the artifact grouping option enabled?
      if (isPrimaryArtifact(gavInfo)
          && getParamFromRepositoryUrl("artifactGrouping") != null) { // $NON-NLS-1$
        artifactGrouping = ensureArtifactGrouping();
      }

      // Only search for existing artifacts by GAV info here
      BaseArtifactType artifact = findExistingArtifactByGAV(client, gavInfo);
      // If we found an artifact, we should update its content.  If not, we should upload
      // the artifact to the repository.
      if (artifact != null) {
        throw new TransferFailedException(
            Messages.i18n.format(
                "ARTIFACT_UPDATE_NOT_ALLOWED", gavInfo.getFullName())); // $NON-NLS-1$

      } else {
        // Upload the content, then add the maven properties to the artifact
        // as meta-data
        artifact = client.uploadArtifact(artifactType, resourceInputStream, gavInfo.getName());
        SrampModelUtils.setCustomProperty(
            artifact, "maven.groupId", gavInfo.getGroupId()); // $NON-NLS-1$
        SrampModelUtils.setCustomProperty(
            artifact, "maven.artifactId", gavInfo.getArtifactId()); // $NON-NLS-1$
        SrampModelUtils.setCustomProperty(
            artifact, "maven.version", gavInfo.getVersion()); // $NON-NLS-1$
        artifact.setVersion(gavInfo.getVersion());
        if (gavInfo.getClassifier() != null) {
          SrampModelUtils.setCustomProperty(
              artifact, "maven.classifier", gavInfo.getClassifier()); // $NON-NLS-1$
        }
        if (gavInfo.getSnapshotId() != null && !gavInfo.getSnapshotId().equals("")) { // $NON-NLS-1$
          SrampModelUtils.setCustomProperty(
              artifact, "maven.snapshot.id", gavInfo.getSnapshotId()); // $NON-NLS-1$
        }
        SrampModelUtils.setCustomProperty(artifact, "maven.type", gavInfo.getType()); // $NON-NLS-1$
        // Also create a relationship to the artifact grouping, if necessary
        if (artifactGrouping != null) {
          SrampModelUtils.addGenericRelationship(
              artifact, "groupedBy", artifactGrouping.getUuid()); // $NON-NLS-1$
          SrampModelUtils.addGenericRelationship(
              artifactGrouping, "groups", artifact.getUuid()); // $NON-NLS-1$
          client.updateArtifactMetaData(artifactGrouping);
        }

        client.updateArtifactMetaData(artifact);
        this.archive.addEntry(gavInfo.getFullName(), artifact, null);
      }

      // Now also add "expanded" content to the s-ramp repository
      expander = ZipToSrampArchiveRegistry.createExpander(artifactType, tempResourceFile);
      if (expander != null) {
        expander.setContextParam(DefaultMetaDataFactory.PARENT_UUID, artifact.getUuid());
        expander.addMetaDataProvider(
            new MetaDataProvider() {
              @Override
              public void provideMetaData(BaseArtifactType artifact) {
                SrampModelUtils.setCustomProperty(
                    artifact, "maven.parent-groupId", gavInfo.getGroupId()); // $NON-NLS-1$
                SrampModelUtils.setCustomProperty(
                    artifact, "maven.parent-artifactId", gavInfo.getArtifactId()); // $NON-NLS-1$
                SrampModelUtils.setCustomProperty(
                    artifact, "maven.parent-version", gavInfo.getVersion()); // $NON-NLS-1$
                SrampModelUtils.setCustomProperty(
                    artifact, "maven.parent-type", gavInfo.getType()); // $NON-NLS-1$
              }
            });
        archive = expander.createSrampArchive();
        client.uploadBatch(archive);
      }
    } catch (Throwable t) {
      throw new TransferFailedException(t.getMessage(), t);
    } finally {
      Thread.currentThread().setContextClassLoader(oldCtxCL);
      SrampArchive.closeQuietly(archive);
      ZipToSrampArchive.closeQuietly(expander);
      FileUtils.deleteQuietly(tempResourceFile);
    }
  }