Search in sources :

Example 1 with SingleVersion

use of org.commonjava.atlas.maven.ident.version.SingleVersion in project indy by Commonjava.

the class MavenMetadataGenerator method writeVersionMetadata.

private boolean writeVersionMetadata(final List<StoreResource> firstLevelFiles, final ArtifactStore store, final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
    ArtifactPathInfo samplePomInfo = null;
    logger.debug("writeVersionMetadata, firstLevelFiles:{}, store:{}", firstLevelFiles, store.getKey());
    // first level will contain version directories...for each directory, we need to verify the presence of a .pom file before including
    // as a valid version
    final List<SingleVersion> versions = new ArrayList<>();
    nextTopResource: for (final StoreResource topResource : firstLevelFiles) {
        final String topPath = topResource.getPath();
        if (topPath.endsWith("/")) {
            final List<StoreResource> secondLevelListing = fileManager.listRaw(store, topPath);
            for (final StoreResource fileResource : secondLevelListing) {
                if (fileResource.getPath().endsWith(".pom")) {
                    ArtifactPathInfo filePomInfo = ArtifactPathInfo.parse(fileResource.getPath());
                    // check if the pom is valid for the path
                    if (filePomInfo != null) {
                        versions.add(VersionUtils.createSingleVersion(new File(topPath).getName()));
                        if (samplePomInfo == null) {
                            samplePomInfo = filePomInfo;
                        }
                        continue nextTopResource;
                    }
                }
            }
        }
    }
    if (versions.isEmpty()) {
        logger.debug("writeVersionMetadata, versions is empty, store:{}", store.getKey());
        return false;
    }
    logger.debug("writeVersionMetadata, versions: {}, store:{}", versions, store.getKey());
    Collections.sort(versions);
    final Transfer metadataFile = fileManager.getTransfer(store, path);
    OutputStream stream = null;
    try {
        final Document doc = xml.newDocumentBuilder().newDocument();
        final Map<String, String> coordMap = new HashMap<>();
        coordMap.put(ARTIFACT_ID, samplePomInfo == null ? null : samplePomInfo.getArtifactId());
        coordMap.put(GROUP_ID, samplePomInfo == null ? null : samplePomInfo.getGroupId());
        final String lastUpdated = generateUpdateTimestamp(getCurrentTimestamp());
        doc.appendChild(doc.createElementNS(doc.getNamespaceURI(), "metadata"));
        xml.createElement(doc.getDocumentElement(), null, coordMap);
        final Map<String, String> versioningMap = new HashMap<>();
        versioningMap.put(LAST_UPDATED, lastUpdated);
        final SingleVersion latest = versions.get(versions.size() - 1);
        versioningMap.put(LATEST, latest.renderStandard());
        SingleVersion release = null;
        for (int i = versions.size() - 1; i >= 0; i--) {
            final SingleVersion r = versions.get(i);
            if (r.isRelease()) {
                release = r;
                break;
            }
        }
        if (release != null) {
            versioningMap.put(RELEASE, release.renderStandard());
        }
        xml.createElement(doc, "versioning", versioningMap);
        final Element versionsElem = xml.createElement(doc, "versioning/versions", Collections.emptyMap());
        for (final SingleVersion version : versions) {
            final Element vElem = doc.createElement(VERSION);
            vElem.setTextContent(version.renderStandard());
            versionsElem.appendChild(vElem);
        }
        final String xmlStr = xml.toXML(doc, true);
        logger.debug("writeVersionMetadata, xmlStr: {}", xmlStr);
        stream = metadataFile.openOutputStream(TransferOperation.GENERATE, true, eventMetadata);
        stream.write(xmlStr.getBytes(UTF_8));
    } catch (final GalleyMavenXMLException e) {
        throw new IndyWorkflowException("Failed to generate maven metadata file: %s. Reason: %s", e, path, e.getMessage());
    } catch (final IOException e) {
        throw new IndyWorkflowException("Failed to write generated maven metadata file: %s. Reason: %s", e, metadataFile, e.getMessage());
    } finally {
        closeQuietly(stream);
    }
    logger.debug("writeVersionMetadata, DONE, store: {}", store.getKey());
    return true;
}
Also used : HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) GalleyMavenXMLException(org.commonjava.maven.galley.maven.parse.GalleyMavenXMLException) IOException(java.io.IOException) Document(org.w3c.dom.Document) StoreResource(org.commonjava.indy.content.StoreResource) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) ArtifactPathInfo(org.commonjava.atlas.maven.ident.util.ArtifactPathInfo) Transfer(org.commonjava.maven.galley.model.Transfer) List(java.util.List) ArrayList(java.util.ArrayList) SingleVersion(org.commonjava.atlas.maven.ident.version.SingleVersion) File(java.io.File)

Example 2 with SingleVersion

use of org.commonjava.atlas.maven.ident.version.SingleVersion in project indy by Commonjava.

the class MavenMetadataGeneratorTest method generateFileContent_VersionsMetadataWith2Versions.

@Test
public void generateFileContent_VersionsMetadataWith2Versions() throws Exception {
    final StoreResource resource = setupVersionsStructureWith2Versions();
    final ConcreteResource metadataFile = resource.getChild("maven-metadata.xml");
    final Transfer transfer = generator.generateFileContent(stores.getArtifactStore(resource.getStoreKey()), metadataFile.getPath(), new EventMetadata());
    assertThat(transfer, notNullValue());
    final MavenMetadataView metadata = metadataReader.readMetadata(new SimpleProjectVersionRef("org.group", "artifact", "1.0-SNAPSHOT"), Collections.singletonList(transfer), new EventMetadata());
    assertThat(metadata, notNullValue());
    final VersioningView versioning = metadata.getVersioning();
    final List<SingleVersion> versions = versioning.getVersions();
    assertThat(versions, notNullValue());
    assertThat(versions.get(0).renderStandard(), equalTo("1.0"));
    assertThat(versions.get(1).renderStandard(), equalTo("1.1"));
    assertThat(versioning.getReleaseVersion().renderStandard(), equalTo("1.1"));
    assertThat(versioning.getLatestVersion().renderStandard(), equalTo("1.1"));
}
Also used : MavenMetadataView(org.commonjava.maven.galley.maven.model.view.meta.MavenMetadataView) VersioningView(org.commonjava.maven.galley.maven.model.view.meta.VersioningView) StoreResource(org.commonjava.indy.content.StoreResource) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) Transfer(org.commonjava.maven.galley.model.Transfer) SimpleProjectVersionRef(org.commonjava.atlas.maven.ident.ref.SimpleProjectVersionRef) SingleVersion(org.commonjava.atlas.maven.ident.version.SingleVersion) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Test(org.junit.Test)

Example 3 with SingleVersion

use of org.commonjava.atlas.maven.ident.version.SingleVersion in project indy by Commonjava.

the class MavenMetadataGeneratorTest method generateFileContent_SnapshotMetadataWith2Versions.

@Test
public void generateFileContent_SnapshotMetadataWith2Versions() throws Exception {
    final StoreResource resource = setupSnapshotDirWith2Snapshots();
    final EventMetadata emd = new EventMetadata();
    final Transfer transfer = generator.generateFileContent(stores.getArtifactStore(resource.getStoreKey()), resource.getChild("maven-metadata.xml").getPath(), emd);
    assertThat(transfer, notNullValue());
    final MavenMetadataView metadata = metadataReader.readMetadata(new SimpleProjectVersionRef("org.group", "artifact", "1.0-SNAPSHOT"), Collections.singletonList(transfer), emd);
    assertThat(metadata, notNullValue());
    final VersioningView versioning = metadata.getVersioning();
    final LatestSnapshotView latestSnapshot = versioning.getLatestSnapshot();
    assertThat(latestSnapshot.isLocalCopy(), equalTo(false));
    assertThat(latestSnapshot.getTimestamp(), equalTo(SnapshotUtils.parseSnapshotTimestamp("20140828.225800")));
    assertThat(latestSnapshot.getBuildNumber(), equalTo(1));
    final List<SnapshotArtifactView> snapshots = versioning.getSnapshotArtifacts();
    assertThat(snapshots.size(), equalTo(4));
    for (final SnapshotArtifactView snap : snapshots) {
        final String extension = snap.getExtension();
        assertThat(extension.equals("jar") || extension.equals("pom"), equalTo(true));
        final String version = snap.getVersion();
        System.out.println(version);
        final SingleVersion parsed = VersionUtils.createSingleVersion(version);
        assertThat(parsed.isSnapshot(), equalTo(true));
        assertThat(parsed.isLocalSnapshot(), equalTo(false));
        final SnapshotPart part = parsed.getSnapshotPart();
        final String tstamp = SnapshotUtils.generateSnapshotTimestamp(part.getTimestamp());
        assertThat(tstamp.equals("20140828.225800") || tstamp.equals("20140828.221400"), equalTo(true));
    }
}
Also used : MavenMetadataView(org.commonjava.maven.galley.maven.model.view.meta.MavenMetadataView) VersioningView(org.commonjava.maven.galley.maven.model.view.meta.VersioningView) StoreResource(org.commonjava.indy.content.StoreResource) SnapshotPart(org.commonjava.atlas.maven.ident.version.part.SnapshotPart) Transfer(org.commonjava.maven.galley.model.Transfer) SimpleProjectVersionRef(org.commonjava.atlas.maven.ident.ref.SimpleProjectVersionRef) LatestSnapshotView(org.commonjava.maven.galley.maven.model.view.meta.LatestSnapshotView) SingleVersion(org.commonjava.atlas.maven.ident.version.SingleVersion) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) SnapshotArtifactView(org.commonjava.maven.galley.maven.model.view.meta.SnapshotArtifactView) Test(org.junit.Test)

Example 4 with SingleVersion

use of org.commonjava.atlas.maven.ident.version.SingleVersion in project indy by Commonjava.

the class KojiMavenMetadataProvider method scanArchive.

private ArchiveScan scanArchive(final KojiArchiveInfo archive, final Set<Integer> seenBuilds) throws KojiClientException {
    Logger logger = LoggerFactory.getLogger(getClass());
    ArchiveScan scan = new ArchiveScan();
    if (!archive.getFilename().endsWith(".pom")) {
        logger.debug("Skipping non-POM: {}", archive.getFilename());
        scan.setDisqualified(true);
        return scan;
    }
    if (!isVerSignedAllowed(archive.getVersion())) {
        logger.debug("version filter pattern not matched: {}", archive.getVersion());
        scan.setDisqualified(true);
        return scan;
    }
    SingleVersion singleVersion = null;
    try {
        singleVersion = VersionUtils.createSingleVersion(archive.getVersion());
        scan.setSingleVersion(singleVersion);
    } catch (InvalidVersionSpecificationException ivse) {
        logger.warn("Skipping mal-formatted version: {}, relPath: {}, buildId: {}", archive.getVersion(), archive.getRelPath(), archive.getBuildId());
        scan.setDisqualified(true);
        return scan;
    }
    KojiBuildInfo build = null;
    if (seenBuilds.contains(archive.getBuildId())) {
        logger.debug("Skipping already seen build: {}", archive.getBuildId());
        scan.setDisqualified(true);
        return scan;
    } else {
        build = kojiContentProvider.getBuildInfo(archive.getBuildId(), null);
        seenBuilds.add(archive.getBuildId());
        scan.setBuild(build);
    }
    if (build == null) {
        logger.debug("Cannot retrieve build info: {}. Skipping: {}", archive.getBuildId(), archive.getFilename());
        scan.setDisqualified(true);
        return scan;
    }
    if (build.getBuildState() != KojiBuildState.COMPLETE) {
        logger.debug("Build: {} is not completed. The state is {}. Skipping.", build.getNvr(), build.getBuildState());
        scan.setDisqualified(true);
        return scan;
    }
    if (build.getTaskId() == null) {
        logger.debug("Build: {} is not a real build. It looks like a binary import. Skipping.", build.getNvr());
        // This is not a real build, it's a binary import.
        scan.setDisqualified(true);
        return scan;
    }
    return scan;
}
Also used : InvalidVersionSpecificationException(org.commonjava.atlas.maven.ident.version.InvalidVersionSpecificationException) Logger(org.slf4j.Logger) SingleVersion(org.commonjava.atlas.maven.ident.version.SingleVersion) KojiBuildInfo(com.redhat.red.build.koji.model.xmlrpc.KojiBuildInfo)

Example 5 with SingleVersion

use of org.commonjava.atlas.maven.ident.version.SingleVersion in project indy by Commonjava.

the class PackageMetadata method merge.

public boolean merge(PackageMetadata source, boolean isForGroup) {
    Logger logger = LoggerFactory.getLogger(getClass());
    boolean changed = false;
    if (source.getName() != null) {
        this.setName(source.getName());
        changed = true;
    }
    if (source.getDescription() != null) {
        this.setDescription(source.getDescription());
        changed = true;
    }
    if (source.getAuthor() != null) {
        this.setAuthor(source.getAuthor());
        changed = true;
    }
    if (source.getRepository() != null) {
        this.setRepository(source.getRepository());
        changed = true;
    }
    if (source.getReadme() != null) {
        this.setReadme(source.getReadme());
        changed = true;
    }
    if (source.getReadmeFilename() != null) {
        this.setReadmeFilename(source.getReadmeFilename());
        changed = true;
    }
    if (source.getHomepage() != null) {
        this.setHomepage(source.getHomepage());
        changed = true;
    }
    if (source.getBugs() != null) {
        this.setBugs(source.getBugs());
        changed = true;
    }
    if (source.getLicense() != null) {
        this.setLicense(source.getLicense());
        changed = true;
    }
    // merge maintainers list
    for (UserInfo m : source.getMaintainers()) {
        if (m.getName() != null && !maintainers.contains(m)) {
            this.addMaintainers(new UserInfo(m.getName(), m.getEmail(), m.getUrl()));
            changed = true;
        }
    }
    // merge keywords list
    for (String key : source.getKeywords()) {
        if (!keywords.contains(key)) {
            this.addKeywords(key);
            changed = true;
        }
    }
    // merge users map
    Map<String, Boolean> sourceUsers = source.getUsers();
    for (final String user : sourceUsers.keySet()) {
        if (users.containsKey(user) && users.get(user).equals(sourceUsers.get(user))) {
            continue;
        }
        users.put(user, sourceUsers.get(user));
        changed = true;
    }
    // merge time map
    SimpleDateFormat sdf = new SimpleDateFormat(TIMEOUT_FORMAT);
    Map<String, Date> clone = new LinkedHashMap<>();
    for (final String key : time.keySet()) {
        String value = time.get(key);
        Date date = null;
        try {
            date = sdf.parse(value);
        } catch (ParseException e) {
            logger.error(String.format("Cannot parse date: %s. Reason: %s", value, e));
        }
        clone.put(key, date);
    }
    Map<String, String> sourceTimes = source.getTime();
    boolean added = false;
    for (final String key : sourceTimes.keySet()) {
        String value = sourceTimes.get(key);
        Date date;
        try {
            date = sdf.parse(value);
        } catch (ParseException e) {
            logger.error(String.format("Cannot parse date: %s. Reason: %s", value, e));
            continue;
        }
        // if source's version update time is more recent(sort as the letter order), will update it into the original map.
        if (clone.containsKey(key) && date.compareTo(clone.get(key)) <= 0) {
            continue;
        }
        clone.put(key, date);
        added = true;
        changed = true;
    }
    // only sorting the map when update occurred
    if (added) {
        // sort as the time value in map
        List<Map.Entry<String, Date>> timeList = new ArrayList<>(clone.entrySet());
        // sort the time as value (update time) asc
        timeList.sort(Map.Entry.comparingByValue());
        Map<String, String> result = new LinkedHashMap<>();
        // make the 'modified' and 'created' value as the first two keys in final map
        if (clone.get(MODIFIED) != null) {
            result.put(MODIFIED, sdf.format(clone.get(MODIFIED)));
        }
        if (clone.get(CREATED) != null) {
            result.put(CREATED, sdf.format(clone.get(CREATED)));
        }
        for (final Map.Entry<String, Date> entry : timeList) {
            if (MODIFIED.equals(entry.getKey()) || CREATED.equals(entry.getKey())) {
                continue;
            }
            result.put(entry.getKey(), sdf.format(entry.getValue()));
        }
        time = result;
    }
    // merge dist-tag object
    DistTag sourceDist = source.getDistTags();
    Map<String, String> sourceDistMap = sourceDist.fetchTagsMap();
    Map<String, String> thisDistMap = distTags.fetchTagsMap();
    if (thisDistMap.isEmpty() && !sourceDistMap.isEmpty()) {
        this.setDistTags(sourceDist);
        changed = true;
    } else {
        for (final String tag : sourceDistMap.keySet()) {
            SingleVersion sourceVersion = VersionUtils.createSingleVersion(sourceDistMap.get(tag));
            SingleVersion thisVersion = VersionUtils.createSingleVersion(thisDistMap.get(tag));
            if (thisDistMap.containsKey(tag) && sourceVersion.compareTo(thisVersion) <= 0) {
                continue;
            }
            distTags.putTag(tag, sourceDistMap.get(tag));
            changed = true;
        }
    }
    // merge versions, keep the first version metadata for a given version that coming across
    Map<String, VersionMetadata> sourceVersions = source.getVersions();
    for (final String v : sourceVersions.keySet()) {
        VersionMetadata value = sourceVersions.get(v);
        // for publish merging, it always accepts the latest updated version meta
        if (!isForGroup || !versions.containsKey(v)) {
            versions.put(v, value);
            changed = true;
        }
    }
    return changed;
}
Also used : ArrayList(java.util.ArrayList) Logger(org.slf4j.Logger) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) ParseException(java.text.ParseException) SingleVersion(org.commonjava.atlas.maven.ident.version.SingleVersion) SimpleDateFormat(java.text.SimpleDateFormat) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

SingleVersion (org.commonjava.atlas.maven.ident.version.SingleVersion)7 ArrayList (java.util.ArrayList)3 StoreResource (org.commonjava.indy.content.StoreResource)3 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)3 Transfer (org.commonjava.maven.galley.model.Transfer)3 Logger (org.slf4j.Logger)3 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 Versioning (org.apache.maven.artifact.repository.metadata.Versioning)2 SimpleProjectVersionRef (org.commonjava.atlas.maven.ident.ref.SimpleProjectVersionRef)2 InvalidVersionSpecificationException (org.commonjava.atlas.maven.ident.version.InvalidVersionSpecificationException)2 MavenMetadataView (org.commonjava.maven.galley.maven.model.view.meta.MavenMetadataView)2 VersioningView (org.commonjava.maven.galley.maven.model.view.meta.VersioningView)2 Test (org.junit.Test)2 KojiArchiveInfo (com.redhat.red.build.koji.model.xmlrpc.KojiArchiveInfo)1 KojiBuildArchiveCollection (com.redhat.red.build.koji.model.xmlrpc.KojiBuildArchiveCollection)1 KojiBuildInfo (com.redhat.red.build.koji.model.xmlrpc.KojiBuildInfo)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 IOException (java.io.IOException)1