예제 #1
0
파일: SrampWagon.java 프로젝트: rnc/s-ramp
  /**
   * Gets the hash data from the s-ramp repository and stores it in the {@link InputData} for use by
   * Maven.
   *
   * @param gavInfo
   * @param inputData
   * @throws TransferFailedException
   * @throws ResourceDoesNotExistException
   * @throws AuthorizationException
   */
  private void doGetHash(MavenGavInfo gavInfo, InputData inputData)
      throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException {
    String artyPath = gavInfo.getFullName();
    String hashPropName;
    if (gavInfo.getType().endsWith(".md5")) { // $NON-NLS-1$
      hashPropName = "maven.hash.md5"; // $NON-NLS-1$
      artyPath = artyPath.substring(0, artyPath.length() - 4);
    } else {
      hashPropName = "maven.hash.sha1"; // $NON-NLS-1$
      artyPath = artyPath.substring(0, artyPath.length() - 5);
    }
    // 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());
    try {
      SrampArchiveEntry entry = this.archive.getEntry(artyPath);
      if (entry == null) {
        throw new ResourceDoesNotExistException(
            Messages.i18n.format("MISSING_RESOURCE_HASH", gavInfo.getName())); // $NON-NLS-1$
      }
      BaseArtifactType metaData = entry.getMetaData();

      String hashValue = SrampModelUtils.getCustomProperty(metaData, hashPropName);
      if (hashValue == null) {
        throw new ResourceDoesNotExistException(
            Messages.i18n.format("MISSING_RESOURCE_HASH", gavInfo.getName())); // $NON-NLS-1$
      }
      inputData.setInputStream(IOUtils.toInputStream(hashValue));
    } finally {
      Thread.currentThread().setContextClassLoader(oldCtxCL);
    }
  }
예제 #2
0
파일: SrampWagon.java 프로젝트: rnc/s-ramp
  /**
   * Updates an artifact by storing its hash value as an S-RAMP property.
   *
   * @param gavInfo
   * @param resourceInputStream
   * @throws TransferFailedException
   */
  private void doPutHash(MavenGavInfo gavInfo, InputStream resourceInputStream)
      throws TransferFailedException {
    logger.info(Messages.i18n.format("STORING_HASH_AS_PROP", gavInfo.getName())); // $NON-NLS-1$
    try {
      String artyPath = gavInfo.getFullName();
      String hashPropName;
      if (gavInfo.getType().endsWith(".md5")) { // $NON-NLS-1$
        hashPropName = "maven.hash.md5"; // $NON-NLS-1$
        artyPath = artyPath.substring(0, artyPath.length() - 4);
      } else {
        hashPropName = "maven.hash.sha1"; // $NON-NLS-1$
        artyPath = artyPath.substring(0, artyPath.length() - 5);
      }
      String hashValue = IOUtils.toString(resourceInputStream);

      // 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());
      try {
        SrampArchiveEntry entry = this.archive.getEntry(artyPath);
        // Re-fetch the artifact meta-data in case it changed on the server since we uploaded it.
        BaseArtifactType metaData = client.getArtifactMetaData(entry.getMetaData().getUuid());
        SrampModelUtils.setCustomProperty(metaData, hashPropName, hashValue);
        this.archive.updateEntry(entry, null);

        // The meta-data has been updated in the local/temp archive - now send it to the remote repo
        client.updateArtifactMetaData(metaData);
      } catch (Throwable t) {
        throw new TransferFailedException(t.getMessage(), t);
      } finally {
        Thread.currentThread().setContextClassLoader(oldCtxCL);
      }
    } catch (Exception e) {
      throw new TransferFailedException(
          Messages.i18n.format("FAILED_TO_STORE_HASH", gavInfo.getName()), e); // $NON-NLS-1$
    }
  }
예제 #3
0
파일: SrampWagon.java 프로젝트: rnc/s-ramp
  /**
   * * Gets the artifact content from the s-ramp repository and stores it in the {@link InputData}
   * object for use by Maven.
   *
   * @param gavInfo
   * @param inputData
   * @throws TransferFailedException
   * @throws ResourceDoesNotExistException
   * @throws AuthorizationException
   */
  private void doGetArtifact(MavenGavInfo gavInfo, InputData inputData)
      throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException {
    // RESTEasy uses the current thread's context classloader to load its logger class.  This
    // fails in Maven because the context classloader is the wagon plugin's classloader, which
    // doesn't know about any of the RESTEasy JARs.  So here we're temporarily setting the
    // context classloader to the s-ramp wagon extension's classloader, which should have access
    // to all the right stuff.
    ClassLoader oldCtxCL = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(SrampWagon.class.getClassLoader());
    try {
      // Query the artifact meta data using GAV info
      BaseArtifactType artifact = findExistingArtifact(client, gavInfo);
      if (artifact == null)
        throw new ResourceDoesNotExistException(
            Messages.i18n.format("ARTIFACT_NOT_FOUND", gavInfo.getName())); // $NON-NLS-1$
      this.archive.addEntry(gavInfo.getFullName(), artifact, null);
      ArtifactType type = ArtifactType.valueOf(artifact);

      // Get the artifact content as an input stream
      InputStream artifactContent = client.getArtifactContent(type, artifact.getUuid());
      inputData.setInputStream(artifactContent);
    } catch (ResourceDoesNotExistException e) {
      throw e;
    } catch (SrampClientException e) {
      if (e.getCause() instanceof HttpHostConnectException) {
        this.debug(Messages.i18n.format("SRAMP_CONNECTION_FAILED", e.getMessage())); // $NON-NLS-1$
      } else {
        this.error(e.getMessage(), e);
      }
      throw new ResourceDoesNotExistException(
          Messages.i18n.format(
              "FAILED_TO_GET_RESOURCE_FROM_SRAMP", gavInfo.getName())); // $NON-NLS-1$
    } catch (Throwable t) {
      this.error(t.getMessage(), t);
    } finally {
      Thread.currentThread().setContextClassLoader(oldCtxCL);
    }
  }
예제 #4
0
파일: SrampWagon.java 프로젝트: rnc/s-ramp
  /**
   * 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);
    }
  }