Search in sources :

Example 56 with Transfer

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

the class ArchetypeCatalogGenerator method generateGroupFileContent.

@Override
public Transfer generateGroupFileContent(final Group group, final List<ArtifactStore> members, final String path, final EventMetadata eventMetadata) throws IndyWorkflowException {
    if (!canProcess(path)) {
        return null;
    }
    final Transfer target = fileManager.getTransfer(group, path);
    if (!target.exists()) {
        String toMergePath = path;
        if (!path.endsWith(ArchetypeCatalogMerger.CATALOG_NAME)) {
            toMergePath = normalize(normalize(parentPath(toMergePath)), ArchetypeCatalogMerger.CATALOG_NAME);
        }
        final List<Transfer> sources = fileManager.retrieveAllRaw(members, toMergePath, new EventMetadata());
        final byte[] merged = merger.merge(sources, group, toMergePath);
        if (merged != null) {
            OutputStream fos = null;
            try {
                fos = target.openOutputStream(TransferOperation.GENERATE, true, eventMetadata);
                fos.write(merged);
            } catch (final IOException e) {
                throw new IndyWorkflowException("Failed to write merged archetype catalog to: {}.\nError: {}", e, target, e.getMessage());
            } finally {
                closeQuietly(fos);
            }
        //                helper.writeChecksumsAndMergeInfo( merged, sources, group, toMergePath );
        }
    }
    if (target.exists()) {
        return target;
    }
    return null;
}
Also used : OutputStream(java.io.OutputStream) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Transfer(org.commonjava.maven.galley.model.Transfer) IOException(java.io.IOException) EventMetadata(org.commonjava.maven.galley.event.EventMetadata)

Example 57 with Transfer

use of org.commonjava.maven.galley.model.Transfer 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;
    // 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<SingleVersion>();
    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()) {
        return false;
    }
    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<String, String>();
        coordMap.put(ARTIFACT_ID, samplePomInfo.getArtifactId());
        coordMap.put(GROUP_ID, samplePomInfo.getGroupId());
        final String lastUpdated = SnapshotUtils.generateUpdateTimestamp(SnapshotUtils.getCurrentTimestamp());
        doc.appendChild(doc.createElementNS(doc.getNamespaceURI(), "metadata"));
        xml.createElement(doc.getDocumentElement(), null, coordMap);
        final Map<String, String> versioningMap = new HashMap<String, String>();
        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.<String, String>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);
        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 : HashMap(java.util.HashMap) 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.maven.atlas.ident.util.ArtifactPathInfo) Transfer(org.commonjava.maven.galley.model.Transfer) ArrayList(java.util.ArrayList) List(java.util.List) SingleVersion(org.commonjava.maven.atlas.ident.version.SingleVersion) File(java.io.File)

Example 58 with Transfer

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

the class MavenMetadataMergerTest method mergeTwoFilesWithInterleavedVersions.

@Test
public void mergeTwoFilesWithInterleavedVersions() throws Exception {
    String path = "org/foo/bar/maven-metadata.xml";
    HostedRepository h1 = new HostedRepository("test-hosted-1");
    HostedRepository h2 = new HostedRepository("test-hosted-2");
    Transfer t1 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h1), path));
    initTestData(t1, VERSION_META + "simple-skip.xml");
    Transfer t2 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h2), path));
    initTestData(t2, VERSION_META + "simple-2.xml");
    Group g = new Group("test-group", h1.getKey(), h2.getKey());
    List<Transfer> sources = Arrays.asList(t1, t2);
    byte[] output = new MavenMetadataMerger(Collections.emptyList()).merge(sources, g, path);
    Metadata merged = new MetadataXpp3Reader().read(new ByteArrayInputStream(output));
    assertThat(merged.getGroupId(), equalTo("org.foo"));
    assertThat(merged.getArtifactId(), equalTo("bar"));
    Versioning versioning = merged.getVersioning();
    assertThat(versioning, notNullValue());
    List<String> versions = versioning.getVersions();
    assertThat(versions, notNullValue());
    assertThat(versions.size(), equalTo(3));
    assertThat(versioning.getRelease(), equalTo("1.2"));
    assertThat(versioning.getLatest(), equalTo("1.2"));
    int idx = 0;
    assertThat(versions.get(idx), equalTo("1.0"));
    idx++;
    assertThat(versions.get(idx), equalTo("1.1"));
    idx++;
    assertThat(versions.get(idx), equalTo("1.2"));
}
Also used : Group(org.commonjava.indy.model.core.Group) MetadataXpp3Reader(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) Versioning(org.apache.maven.artifact.repository.metadata.Versioning) ByteArrayInputStream(java.io.ByteArrayInputStream) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) HostedRepository(org.commonjava.indy.model.core.HostedRepository) Test(org.junit.Test)

Example 59 with Transfer

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

the class MavenMetadataMergerTest method mergeTwoSimpleVersionMetadataFiles.

@Test
public void mergeTwoSimpleVersionMetadataFiles() throws Exception {
    String path = "org/foo/bar/maven-metadata.xml";
    HostedRepository h1 = new HostedRepository("test-hosted-1");
    HostedRepository h2 = new HostedRepository("test-hosted-2");
    Transfer t1 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h1), path));
    initTestData(t1, VERSION_META + "simple-1.xml");
    Transfer t2 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h2), path));
    initTestData(t2, VERSION_META + "simple-2.xml");
    Group g = new Group("test-group", h1.getKey(), h2.getKey());
    List<Transfer> sources = Arrays.asList(t1, t2);
    byte[] output = new MavenMetadataMerger(Collections.emptyList()).merge(sources, g, path);
    Metadata merged = new MetadataXpp3Reader().read(new ByteArrayInputStream(output));
    assertThat(merged.getGroupId(), equalTo("org.foo"));
    assertThat(merged.getArtifactId(), equalTo("bar"));
    Versioning versioning = merged.getVersioning();
    assertThat(versioning, notNullValue());
    List<String> versions = versioning.getVersions();
    assertThat(versions, notNullValue());
    assertThat(versions.size(), equalTo(2));
    assertThat(versioning.getRelease(), equalTo("1.1"));
    assertThat(versioning.getLatest(), equalTo("1.1"));
    int idx = 0;
    assertThat(versions.get(idx), equalTo("1.0"));
    idx++;
    assertThat(versions.get(idx), equalTo("1.1"));
}
Also used : Group(org.commonjava.indy.model.core.Group) MetadataXpp3Reader(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) Versioning(org.apache.maven.artifact.repository.metadata.Versioning) ByteArrayInputStream(java.io.ByteArrayInputStream) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) HostedRepository(org.commonjava.indy.model.core.HostedRepository) Test(org.junit.Test)

Example 60 with Transfer

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

the class MavenMetadataMergerTest method mergeOneTransferWithProviderError.

@Test
public void mergeOneTransferWithProviderError() throws Exception {
    String path = "org/foo/bar/maven-metadata.xml";
    HostedRepository h1 = new HostedRepository("test-hosted-1");
    Transfer t1 = cacheProvider.getTransfer(new ConcreteResource(LocationUtils.toLocation(h1), path));
    initTestData(t1, VERSION_META + "simple-1.xml");
    Group g = new Group("test-group", h1.getKey());
    List<Transfer> sources = Arrays.asList(t1);
    TestMavenMetadataProvider testProvider = new TestMavenMetadataProvider(new IndyWorkflowException("Failed to get provider content"));
    byte[] output = new MavenMetadataMerger(Collections.singletonList(testProvider)).merge(sources, g, path);
    Metadata merged = new MetadataXpp3Reader().read(new ByteArrayInputStream(output));
    assertThat(merged.getGroupId(), equalTo("org.foo"));
    assertThat(merged.getArtifactId(), equalTo("bar"));
    Versioning versioning = merged.getVersioning();
    assertThat(versioning, notNullValue());
    List<String> versions = versioning.getVersions();
    assertThat(versions, notNullValue());
    assertThat(versions.size(), equalTo(1));
    assertThat(versioning.getRelease(), equalTo("1.0"));
    assertThat(versioning.getLatest(), equalTo("1.0"));
    int idx = 0;
    assertThat(versions.get(idx), equalTo("1.0"));
}
Also used : Group(org.commonjava.indy.model.core.Group) MetadataXpp3Reader(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader) Metadata(org.apache.maven.artifact.repository.metadata.Metadata) Versioning(org.apache.maven.artifact.repository.metadata.Versioning) ByteArrayInputStream(java.io.ByteArrayInputStream) IndyWorkflowException(org.commonjava.indy.IndyWorkflowException) Transfer(org.commonjava.maven.galley.model.Transfer) ConcreteResource(org.commonjava.maven.galley.model.ConcreteResource) HostedRepository(org.commonjava.indy.model.core.HostedRepository) Test(org.junit.Test)

Aggregations

Transfer (org.commonjava.maven.galley.model.Transfer)154 ConcreteResource (org.commonjava.maven.galley.model.ConcreteResource)63 Test (org.junit.Test)62 IOException (java.io.IOException)39 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)38 IndyWorkflowException (org.commonjava.indy.IndyWorkflowException)37 InputStream (java.io.InputStream)27 StoreKey (org.commonjava.indy.model.core.StoreKey)27 Logger (org.slf4j.Logger)26 SimpleLocation (org.commonjava.maven.galley.model.SimpleLocation)22 ByteArrayInputStream (java.io.ByteArrayInputStream)21 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)21 HostedRepository (org.commonjava.indy.model.core.HostedRepository)21 Group (org.commonjava.indy.model.core.Group)20 ArrayList (java.util.ArrayList)19 IndyDataException (org.commonjava.indy.data.IndyDataException)19 OutputStream (java.io.OutputStream)15 HashSet (java.util.HashSet)15 Location (org.commonjava.maven.galley.model.Location)14 TransferException (org.commonjava.maven.galley.TransferException)12