Пример #1
0
  /** @see org.apache.maven.wagon.StreamWagon#fillInputData(org.apache.maven.wagon.InputData) */
  @Override
  public void fillInputData(InputData inputData)
      throws TransferFailedException, ResourceDoesNotExistException, AuthorizationException {
    Resource resource = inputData.getResource();
    MavenGavInfo gavInfo = MavenGavInfo.fromResource(resource);
    if (gavInfo.isMavenMetaData() && gavInfo.getVersion() == null) {
      doGenerateArtifactDirMavenMetaData(gavInfo, inputData);
      return;
    }

    if (gavInfo.isMavenMetaData() && gavInfo.getVersion() != null) {
      doGenerateSnapshotMavenMetaData(gavInfo, inputData);
      return;
    }

    debug(Messages.i18n.format("LOOKING_UP_RESOURCE_IN_SRAMP", resource)); // $NON-NLS-1$

    if (gavInfo.isHash()) {
      doGetHash(gavInfo, inputData);
    } else {
      doGetArtifact(gavInfo, inputData);
    }
  }
Пример #2
0
  /**
   * Finds an existing artifact in the s-ramp repository that matches the GAV information.
   *
   * @param client
   * @param gavInfo
   * @return an s-ramp artifact (if found) or null (if not found)
   * @throws SrampClientException
   * @throws SrampAtomException
   * @throws JAXBException
   */
  private BaseArtifactType findExistingArtifactByGAV(
      SrampAtomApiClient client, MavenGavInfo gavInfo)
      throws SrampAtomException, SrampClientException, JAXBException {
    SrampClientQuery clientQuery = null;

    StringBuilder queryBuilder = new StringBuilder();
    queryBuilder.append("/s-ramp"); // $NON-NLS-1$
    List<String> criteria = new ArrayList<String>();
    List<Object> params = new ArrayList<Object>();

    criteria.add("@maven.groupId = ?"); // $NON-NLS-1$
    params.add(gavInfo.getGroupId());
    criteria.add("@maven.artifactId = ?"); // $NON-NLS-1$
    params.add(gavInfo.getArtifactId());
    criteria.add("@maven.version = ?"); // $NON-NLS-1$
    params.add(gavInfo.getVersion());

    if (StringUtils.isNotBlank(gavInfo.getType())) {
      criteria.add("@maven.type = ?"); // $NON-NLS-1$
      params.add(gavInfo.getType());
    }
    if (StringUtils.isNotBlank(gavInfo.getClassifier())) {
      criteria.add("@maven.classifier = ?"); // $NON-NLS-1$
      params.add(gavInfo.getClassifier());
    }
    if (StringUtils.isNotBlank(gavInfo.getSnapshotId())) {
      return null;
    } else {
      criteria.add("xp2:not(@maven.snapshot.id)"); // $NON-NLS-1$
    }

    if (criteria.size() > 0) {
      queryBuilder.append("["); // $NON-NLS-1$
      queryBuilder.append(StringUtils.join(criteria, " and ")); // $NON-NLS-1$
      queryBuilder.append("]"); // $NON-NLS-1$
    }
    clientQuery = client.buildQuery(queryBuilder.toString());
    for (Object param : params) {
      if (param instanceof String) {
        clientQuery.parameter((String) param);
      }
      if (param instanceof Calendar) {
        clientQuery.parameter((Calendar) param);
      }
    }

    QueryResultSet rset = clientQuery.count(100).query();
    if (rset.size() > 0) {
      for (ArtifactSummary summary : rset) {
        String uuid = summary.getUuid();
        ArtifactType artifactType = summary.getType();
        BaseArtifactType arty = client.getArtifactMetaData(artifactType, uuid);
        // If no classifier in the GAV info, only return the artifact that also has no classifier
        // TODO replace this with "not(@maven.classifer)" in the query, then force the result set to
        // return 2 items (expecting only 1)
        if (gavInfo.getClassifier() == null) {
          String artyClassifier =
              SrampModelUtils.getCustomProperty(arty, "maven.classifier"); // $NON-NLS-1$
          if (artyClassifier == null) {
            return arty;
          }
        } else {
          // If classifier was supplied in the GAV info, we'll get the first artifact <shrug>
          return arty;
        }
      }
    }
    return null;
  }
Пример #3
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);
    }
  }
Пример #4
0
  /**
   * Generates the maven-metadata.xml file dynamically for a given
   * groupId/artifactId/snapshot-version. This will list all of the snapshot versions available.
   *
   * @param gavInfo
   * @param inputData
   * @throws ResourceDoesNotExistException
   */
  private void doGenerateSnapshotMavenMetaData(MavenGavInfo gavInfo, InputData inputData)
      throws ResourceDoesNotExistException {
    // 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 {
      String artyPath = gavInfo.getFullName();
      if (gavInfo.isHash()) {
        artyPath = artyPath.substring(0, artyPath.lastIndexOf('.'));
      }
      SrampArchiveEntry entry = this.archive.getEntry(artyPath);
      if (entry == null) {
        QueryResultSet resultSet =
            client
                .buildQuery(
                    "/s-ramp[@maven.groupId = ? and @maven.artifactId = ? and @maven.version = ?]") //$NON-NLS-1$
                .parameter(gavInfo.getGroupId())
                .parameter(gavInfo.getArtifactId())
                .parameter(gavInfo.getVersion())
                .propertyName("maven.classifier")
                .propertyName("maven.type") // $NON-NLS-1$ //$NON-NLS-2$
                .count(500)
                .orderBy("createdTimestamp")
                .ascending()
                .query(); //$NON-NLS-1$
        if (resultSet.size() == 0) {
          throw new Exception(Messages.i18n.format("NO_ARTIFACTS_FOUND")); // $NON-NLS-1$
        }

        SimpleDateFormat timestampFormat = new SimpleDateFormat("yyyyMMdd.HHmmss"); // $NON-NLS-1$
        SimpleDateFormat updatedFormat = new SimpleDateFormat("yyyyMMddHHmmss"); // $NON-NLS-1$

        StringBuilder snapshotVersions = new StringBuilder();
        snapshotVersions.append("    <snapshotVersions>\n"); // $NON-NLS-1$
        Set<String> processed = new HashSet<String>();
        Date latestDate = null;
        for (ArtifactSummary artifactSummary : resultSet) {
          String extension = artifactSummary.getCustomPropertyValue("maven.type"); // $NON-NLS-1$
          String classifier =
              artifactSummary.getCustomPropertyValue("maven.classifier"); // $NON-NLS-1$
          String value = gavInfo.getVersion();
          Date updatedDate = artifactSummary.getLastModifiedTimestamp();
          String updated = updatedFormat.format(updatedDate);
          String pkey = classifier + "::" + extension; // $NON-NLS-1$
          if (processed.add(pkey)) {
            snapshotVersions.append("      <snapshotVersion>\n"); // $NON-NLS-1$
            if (classifier != null)
              snapshotVersions
                  .append("        <classifier>")
                  .append(classifier)
                  .append("</classifier>\n"); // $NON-NLS-1$ //$NON-NLS-2$
            snapshotVersions
                .append("        <extension>")
                .append(extension)
                .append("</extension>\n"); // $NON-NLS-1$ //$NON-NLS-2$
            snapshotVersions
                .append("        <value>")
                .append(value)
                .append("</value>\n"); // $NON-NLS-1$ //$NON-NLS-2$
            snapshotVersions
                .append("        <updated>")
                .append(updated)
                .append("</updated>\n"); // $NON-NLS-1$ //$NON-NLS-2$
            snapshotVersions.append("      </snapshotVersion>\n"); // $NON-NLS-1$
            if (latestDate == null || latestDate.before(updatedDate)) {
              latestDate = updatedDate;
            }
          }
        }
        snapshotVersions.append("    </snapshotVersions>\n"); // $NON-NLS-1$

        String groupId = gavInfo.getGroupId();
        String artifactId = gavInfo.getArtifactId();
        String version = gavInfo.getVersion();
        String lastUpdated = updatedFormat.format(latestDate);

        StringBuilder mavenMetadata = new StringBuilder();
        mavenMetadata.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); // $NON-NLS-1$
        mavenMetadata.append("<metadata>\n"); // $NON-NLS-1$
        mavenMetadata
            .append("  <groupId>")
            .append(groupId)
            .append("</groupId>\n"); // $NON-NLS-1$ //$NON-NLS-2$
        mavenMetadata
            .append("  <artifactId>")
            .append(artifactId)
            .append("</artifactId>\n"); // $NON-NLS-1$ //$NON-NLS-2$
        mavenMetadata
            .append("  <version>")
            .append(version)
            .append("</version>\n"); // $NON-NLS-1$ //$NON-NLS-2$
        mavenMetadata.append("  <versioning>\n"); // $NON-NLS-1$
        mavenMetadata.append("    <snapshot>\n"); // $NON-NLS-1$
        mavenMetadata
            .append("      <timestamp>")
            .append(timestampFormat.format(latestDate))
            .append("</timestamp>\n"); // $NON-NLS-1$ //$NON-NLS-2$
        mavenMetadata.append("      <buildNumber>1</buildNumber>\n"); // $NON-NLS-1$
        mavenMetadata.append("    </snapshot>\n"); // $NON-NLS-1$
        mavenMetadata
            .append("    <lastUpdated>")
            .append(lastUpdated)
            .append("</lastUpdated>\n"); // $NON-NLS-1$ //$NON-NLS-2$
        mavenMetadata.append(snapshotVersions.toString());
        mavenMetadata.append("  </versioning>\n"); // $NON-NLS-1$
        mavenMetadata.append("</metadata>\n"); // $NON-NLS-1$

        BaseArtifactType artifact =
            ArtifactType.ExtendedDocument("MavenMetaData").newArtifactInstance(); // $NON-NLS-1$
        this.archive.addEntry(artyPath, artifact, IOUtils.toInputStream(mavenMetadata.toString()));

        entry = this.archive.getEntry(artyPath);
      }

      if (!gavInfo.isHash()) {
        inputData.setInputStream(this.archive.getInputStream(entry));
      } else {
        String hash = generateHash(this.archive.getInputStream(entry), gavInfo.getHashAlgorithm());
        inputData.setInputStream(IOUtils.toInputStream(hash));
      }
    } catch (Exception e) {
      throw new ResourceDoesNotExistException(
          Messages.i18n.format("FAILED_TO_GENERATE_METADATA"), e); // $NON-NLS-1$
    } finally {
      Thread.currentThread().setContextClassLoader(oldCtxCL);
    }
  }