Search in sources :

Example 1 with TypeMapping

use of org.commonjava.maven.galley.model.TypeMapping in project indy by Commonjava.

the class MavenMetadataGenerator method writeSnapshotMetadata.

private boolean writeSnapshotMetadata(final ArtifactPathInfo info, final List<StoreResource> files, final ArtifactStore store, final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
    // first level will contain files that have the timestamp-buildnumber version suffix...for each, we need to parse this info.
    final Map<SnapshotPart, Set<ArtifactPathInfo>> infosBySnap = new HashMap<SnapshotPart, Set<ArtifactPathInfo>>();
    for (final StoreResource resource : files) {
        final ArtifactPathInfo resInfo = ArtifactPathInfo.parse(resource.getPath());
        if (resInfo != null) {
            final SnapshotPart snap = resInfo.getSnapshotInfo();
            Set<ArtifactPathInfo> infos = infosBySnap.get(snap);
            if (infos == null) {
                infos = new HashSet<ArtifactPathInfo>();
                infosBySnap.put(snap, infos);
            }
            infos.add(resInfo);
        }
    }
    if (infosBySnap.isEmpty()) {
        return false;
    }
    final List<SnapshotPart> snaps = new ArrayList<SnapshotPart>(infosBySnap.keySet());
    Collections.sort(snaps);
    final Transfer metadataFile = fileManager.getTransfer(store, path);
    OutputStream stream = null;
    try {
        final Document doc = xml.newDocumentBuilder().newDocument();
        final Map<String, String> coordMap = new HashMap<String, String>();
        coordMap.put(ARTIFACT_ID, info.getArtifactId());
        coordMap.put(GROUP_ID, info.getGroupId());
        coordMap.put(VERSION, info.getVersion());
        final String lastUpdated = SnapshotUtils.generateUpdateTimestamp(SnapshotUtils.getCurrentTimestamp());
        doc.appendChild(doc.createElementNS(doc.getNamespaceURI(), "metadata"));
        xml.createElement(doc.getDocumentElement(), null, coordMap);
        xml.createElement(doc, "versioning", Collections.<String, String>singletonMap(LAST_UPDATED, lastUpdated));
        SnapshotPart snap = snaps.get(snaps.size() - 1);
        Map<String, String> snapMap = new HashMap<String, String>();
        if (snap.isLocalSnapshot()) {
            snapMap.put(LOCAL_COPY, Boolean.TRUE.toString());
        } else {
            snapMap.put(TIMESTAMP, SnapshotUtils.generateSnapshotTimestamp(snap.getTimestamp()));
            snapMap.put(BUILD_NUMBER, Integer.toString(snap.getBuildNumber()));
        }
        xml.createElement(doc, "versioning/snapshot", snapMap);
        for (int i = 0; i < snaps.size(); i++) {
            snap = snaps.get(i);
            // the last one is the most recent.
            final Set<ArtifactPathInfo> infos = infosBySnap.get(snap);
            for (final ArtifactPathInfo pathInfo : infos) {
                snapMap = new HashMap<String, String>();
                final TypeAndClassifier tc = new SimpleTypeAndClassifier(pathInfo.getType(), pathInfo.getClassifier());
                final TypeMapping mapping = typeMapper.lookup(tc);
                final String classifier = mapping == null ? pathInfo.getClassifier() : mapping.getClassifier();
                if (classifier != null && classifier.length() > 0) {
                    snapMap.put(CLASSIFIER, classifier);
                }
                snapMap.put(EXTENSION, mapping == null ? pathInfo.getType() : mapping.getExtension());
                snapMap.put(VALUE, pathInfo.getVersion());
                snapMap.put(UPDATED, lastUpdated);
                xml.createElement(doc, "versioning/snapshotVersions/snapshotVersion", snapMap);
            }
        }
        final String xmlStr = xml.toXML(doc, true);
        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);
    }
    return true;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) OutputStream(java.io.OutputStream) 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) SnapshotPart(org.commonjava.maven.atlas.ident.version.part.SnapshotPart) SimpleTypeAndClassifier(org.commonjava.maven.atlas.ident.ref.SimpleTypeAndClassifier) TypeAndClassifier(org.commonjava.maven.atlas.ident.ref.TypeAndClassifier) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) ArtifactPathInfo(org.commonjava.maven.atlas.ident.util.ArtifactPathInfo) Transfer(org.commonjava.maven.galley.model.Transfer) SimpleTypeAndClassifier(org.commonjava.maven.atlas.ident.ref.SimpleTypeAndClassifier) TypeMapping(org.commonjava.maven.galley.model.TypeMapping)

Example 2 with TypeMapping

use of org.commonjava.maven.galley.model.TypeMapping in project galley by Commonjava.

the class ArtifactPathUtils method formatArtifactPath.

public static String formatArtifactPath(final ProjectVersionRef src, final TypeMapper mapper) throws TransferException {
    /* @formatter:off */
    if (src instanceof ArtifactRef) {
        final ArtifactRef ref = (ArtifactRef) src;
        final TypeMapping tm = mapper.lookup(ref.getTypeAndClassifier());
        return String.format("%s/%s/%s/%s-%s%s.%s", ref.getGroupId().replace('.', '/'), ref.getArtifactId(), formatVersionDirectoryPart(ref), ref.getArtifactId(), formatVersionFilePart(ref), (tm.getClassifier() == null ? "" : "-" + tm.getClassifier()), tm.getExtension());
    } else {
        return String.format("%s/%s/%s/", src.getGroupId().replace('.', '/'), src.getArtifactId(), formatVersionDirectoryPart(src));
    }
/* @formatter:on */
}
Also used : TypeMapping(org.commonjava.maven.galley.model.TypeMapping)

Aggregations

TypeMapping (org.commonjava.maven.galley.model.TypeMapping)2 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)1 StoreResource (org.commonjava.indy.content.StoreResource)1 SimpleTypeAndClassifier (org.commonjava.maven.atlas.ident.ref.SimpleTypeAndClassifier)1 TypeAndClassifier (org.commonjava.maven.atlas.ident.ref.TypeAndClassifier)1 ArtifactPathInfo (org.commonjava.maven.atlas.ident.util.ArtifactPathInfo)1 SnapshotPart (org.commonjava.maven.atlas.ident.version.part.SnapshotPart)1 GalleyMavenXMLException (org.commonjava.maven.galley.maven.parse.GalleyMavenXMLException)1 Transfer (org.commonjava.maven.galley.model.Transfer)1 Document (org.w3c.dom.Document)1