Search in sources :

Example 6 with SingleVersion

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

the class PackageMetadata method merge.

public boolean merge(PackageMetadata source) {
    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
    Iterator maintainer = source.getMaintainers().iterator();
    while (maintainer.hasNext()) {
        UserInfo m = (UserInfo) maintainer.next();
        if (!maintainers.contains(m)) {
            this.addMaintainers(new UserInfo(m.getName(), m.getEmail(), m.getUrl()));
            changed = true;
        }
    }
    // merge keywords list
    Iterator keyword = source.getKeywords().iterator();
    while (keyword.hasNext()) {
        String key = (String) keyword.next();
        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 = null;
        try {
            date = sdf.parse(value);
        } catch (ParseException e) {
            logger.error(String.format("Cannot parse date: %s. Reason: %s", value, e));
        }
        // 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
        Collections.sort(timeList, (o1, o2) -> o1.getValue().compareTo(o2.getValue()));
        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);
        if (!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) Iterator(java.util.Iterator) ParseException(java.text.ParseException) SingleVersion(org.commonjava.maven.atlas.ident.version.SingleVersion) SimpleDateFormat(java.text.SimpleDateFormat) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 7 with SingleVersion

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

the class VersionResolverImpl method resolveLatestSnapshotRefWithLocation.

private ProjectVersionRefLocation resolveLatestSnapshotRefWithLocation(final List<? extends Location> locations, final ProjectVersionRef ref, final VersionSelectionStrategy selectionStrategy, final EventMetadata eventMetadata) throws TransferException {
    final Map<SingleVersion, Location> available = new TreeMap<>();
    for (final Location location : locations) {
        try {
            final MavenMetadataView metadata = metadataReader.getMetadata(ref, Collections.singletonList(location), eventMetadata);
            if (metadata != null) {
                addSnapshotFrom(metadata, location, ref, available);
            }
        } catch (final GalleyMavenException e) {
            debug("Failed to resolve/parse metadata for snapshot version of: %s from: %s.", e, ref, location);
        }
    }
    if (available.isEmpty()) {
        return null;
    }
    final List<SingleVersion> versions = new ArrayList<>(available.keySet());
    Collections.sort(versions);
    final SingleVersion selected = selectionStrategy.select(versions);
    if (selected == null) {
        return null;
    }
    return new ProjectVersionRefLocation(ref.selectVersion(selected), available.get(selected));
}
Also used : MavenMetadataView(org.commonjava.maven.galley.maven.model.view.meta.MavenMetadataView) GalleyMavenException(org.commonjava.maven.galley.maven.GalleyMavenException) ProjectVersionRefLocation(org.commonjava.maven.galley.maven.model.ProjectVersionRefLocation) ArrayList(java.util.ArrayList) SingleVersion(org.commonjava.maven.atlas.ident.version.SingleVersion) TreeMap(java.util.TreeMap) ProjectVersionRefLocation(org.commonjava.maven.galley.maven.model.ProjectVersionRefLocation) Location(org.commonjava.maven.galley.model.Location)

Example 8 with SingleVersion

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

the class VersionResolverImpl method resolveAllMultiRefsWithLocations.

private List<ProjectVersionRefLocation> resolveAllMultiRefsWithLocations(final List<? extends Location> locations, final ProjectVersionRef ref, final VersionSelectionStrategy selectionStrategy, final EventMetadata eventMetadata) throws TransferException {
    final Map<SingleVersion, Location> available = new TreeMap<>();
    for (final Location location : locations) {
        try {
            final MavenMetadataView metadata = metadataReader.getMetadata(ref.asProjectRef(), Collections.singletonList(location), eventMetadata);
            if (metadata != null) {
                final List<String> versions = metadata.resolveValues("/metadata/versioning/versions/version");
                if (versions != null) {
                    for (final String version : versions) {
                        try {
                            final SingleVersion spec = VersionUtils.createSingleVersion(version);
                            if (!available.containsKey(spec)) {
                                available.put(spec, location);
                            }
                        } catch (final InvalidVersionSpecificationException e) {
                            debug("Unparsable version spec found in metadata: '%s' for: %s from: %s.", e, version, ref, location);
                        }
                    }
                }
            }
        } catch (final GalleyMavenException e) {
            debug("Failed to resolve/parse metadata for variable version of: '%s' from: %s.", e, ref, location);
        }
    }
    if (!available.isEmpty()) {
        final List<ProjectVersionRefLocation> result = new ArrayList<>();
        final VersionSpec spec = ref.getVersionSpec();
        final List<SingleVersion> versions = new ArrayList<>(available.keySet());
        Collections.sort(versions);
        while (!versions.isEmpty()) {
            final SingleVersion selected = selectionStrategy.select(versions);
            if (selected != null) {
                versions.remove(selected);
                if (selected.isConcrete() && spec.contains(selected)) {
                    result.add(new ProjectVersionRefLocation(ref.selectVersion(selected), available.get(selected)));
                }
            }
        }
        return result;
    }
    return Collections.emptyList();
}
Also used : GalleyMavenException(org.commonjava.maven.galley.maven.GalleyMavenException) ArrayList(java.util.ArrayList) JoinString(org.commonjava.maven.atlas.ident.util.JoinString) TreeMap(java.util.TreeMap) VersionSpec(org.commonjava.maven.atlas.ident.version.VersionSpec) MavenMetadataView(org.commonjava.maven.galley.maven.model.view.meta.MavenMetadataView) InvalidVersionSpecificationException(org.commonjava.maven.atlas.ident.version.InvalidVersionSpecificationException) ProjectVersionRefLocation(org.commonjava.maven.galley.maven.model.ProjectVersionRefLocation) SingleVersion(org.commonjava.maven.atlas.ident.version.SingleVersion) ProjectVersionRefLocation(org.commonjava.maven.galley.maven.model.ProjectVersionRefLocation) Location(org.commonjava.maven.galley.model.Location)

Aggregations

SingleVersion (org.commonjava.maven.atlas.ident.version.SingleVersion)8 ArrayList (java.util.ArrayList)6 TreeMap (java.util.TreeMap)5 GalleyMavenException (org.commonjava.maven.galley.maven.GalleyMavenException)5 ProjectVersionRefLocation (org.commonjava.maven.galley.maven.model.ProjectVersionRefLocation)5 MavenMetadataView (org.commonjava.maven.galley.maven.model.view.meta.MavenMetadataView)5 Location (org.commonjava.maven.galley.model.Location)5 JoinString (org.commonjava.maven.atlas.ident.util.JoinString)4 InvalidVersionSpecificationException (org.commonjava.maven.atlas.ident.version.InvalidVersionSpecificationException)4 VersionSpec (org.commonjava.maven.atlas.ident.version.VersionSpec)2 Logger (org.slf4j.Logger)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileReader (java.io.FileReader)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 ParseException (java.text.ParseException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1