Search in sources :

Example 1 with TransferCancelledException

use of org.eclipse.aether.transfer.TransferCancelledException in project bnd by bndtools.

the class AetherRepository method get.

@Override
public File get(String bsn, Version version, Map<String, String> properties, DownloadListener... listeners) throws Exception {
    init();
    // Use the index by preference
    if (indexedRepo != null)
        return indexedRepo.get(ConversionUtils.maybeMavenCoordsToBsn(bsn), version, properties, listeners);
    File file = null;
    boolean getSource = false;
    try {
        // Setup the Aether repo session and request
        DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
        session.setLocalRepositoryManager(repoSystem.newLocalRepositoryManager(session, localRepo));
        if (bsn.endsWith(".source")) {
            String originalBsn = properties.get("bsn");
            if (originalBsn != null) {
                bsn = originalBsn;
                getSource = true;
            }
        }
        String[] coords = ConversionUtils.getGroupAndArtifactForBsn(bsn);
        MavenVersion mvnVersion = new MavenVersion(version);
        String versionStr = null;
        if ("exact".equals(properties.get("strategy")) || getSource) {
            versionStr = properties.get("version");
        } else {
            versionStr = mvnVersion.toString();
        }
        Artifact artifact = null;
        if (getSource) {
            artifact = new DefaultArtifact(coords[0], coords[1], "sources", "jar", versionStr);
        } else {
            artifact = new DefaultArtifact(coords[0], coords[1], "jar", versionStr);
        }
        ArtifactRequest request = new ArtifactRequest();
        request.setArtifact(artifact);
        request.setRepositories(Collections.singletonList(remoteRepo));
        // Log the transfer
        session.setTransferListener(new AbstractTransferListener() {

            @Override
            public void transferStarted(TransferEvent event) throws TransferCancelledException {
                System.err.println(event);
            }

            @Override
            public void transferSucceeded(TransferEvent event) {
                System.err.println(event);
            }

            @Override
            public void transferFailed(TransferEvent event) {
                System.err.println(event);
            }
        });
        try {
            // Resolve the version
            ArtifactResult artifactResult = repoSystem.resolveArtifact(session, request);
            artifact = artifactResult.getArtifact();
            file = artifact.getFile();
        } catch (ArtifactResolutionException ex) {
        // could not download artifact, simply return null
        }
        return file;
    } finally {
        for (DownloadListener dl : listeners) {
            if (file != null)
                dl.success(file);
            else
                dl.failure(null, "Download failed");
        }
    }
}
Also used : AbstractTransferListener(org.eclipse.aether.transfer.AbstractTransferListener) TransferCancelledException(org.eclipse.aether.transfer.TransferCancelledException) TransferEvent(org.eclipse.aether.transfer.TransferEvent) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) ArtifactResult(org.eclipse.aether.resolution.ArtifactResult) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) MavenVersion(aQute.bnd.version.MavenVersion) ArtifactRequest(org.eclipse.aether.resolution.ArtifactRequest) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) File(java.io.File) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact)

Example 2 with TransferCancelledException

use of org.eclipse.aether.transfer.TransferCancelledException in project bnd by bndtools.

the class AetherRepository method put.

@Override
public PutResult put(InputStream stream, PutOptions options) throws Exception {
    init();
    DigestInputStream digestStream = new DigestInputStream(stream, MessageDigest.getInstance("SHA-1"));
    final File tmpFile = IO.createTempFile(cacheDir, "put", ".bnd");
    try {
        IO.copy(digestStream, tmpFile);
        byte[] digest = digestStream.getMessageDigest().digest();
        if (options.digest != null && !Arrays.equals(options.digest, digest))
            throw new IOException("Retrieved artifact digest doesn't match specified digest");
        // Get basic info about the bundle we're deploying
        Jar jar = new Jar(tmpFile);
        Artifact artifact = ConversionUtils.fromBundleJar(jar);
        artifact = artifact.setFile(tmpFile);
        // Setup the Aether repo session and create the deployment request
        DefaultRepositorySystemSession session = MavenRepositorySystemUtils.newSession();
        session.setLocalRepositoryManager(repoSystem.newLocalRepositoryManager(session, localRepo));
        final DeployRequest request = new DeployRequest();
        request.addArtifact(artifact);
        request.setRepository(remoteRepo);
        // Capture the result including remote resource URI
        final ResultHolder resultHolder = new ResultHolder();
        session.setTransferListener(new AbstractTransferListener() {

            @Override
            public void transferSucceeded(TransferEvent event) {
                TransferResource resource = event.getResource();
                if (event.getRequestType() == RequestType.PUT && tmpFile.equals(resource.getFile())) {
                    PutResult result = new PutResult();
                    result.artifact = URI.create(resource.getRepositoryUrl() + resource.getResourceName());
                    resultHolder.result = result;
                    System.out.println("UPLOADED to: " + URI.create(resource.getRepositoryUrl() + resource.getResourceName()));
                }
            }

            @Override
            public void transferFailed(TransferEvent event) {
                if (event.getRequestType() == RequestType.PUT && tmpFile.equals(event.getResource().getFile()))
                    resultHolder.error = event.getException();
            }

            @Override
            public void transferCorrupted(TransferEvent event) throws TransferCancelledException {
                if (event.getRequestType() == RequestType.PUT && tmpFile.equals(event.getResource().getFile()))
                    resultHolder.error = event.getException();
            }
        });
        // Do the deploy and report results
        repoSystem.deploy(session, request);
        if (resultHolder.result != null) {
            if (indexedRepo != null)
                indexedRepo.reset();
            return resultHolder.result;
        } else if (resultHolder.error != null) {
            throw new Exception("Error during artifact upload", resultHolder.error);
        } else {
            throw new Exception("Artifact was not uploaded");
        }
    } finally {
        if (tmpFile != null && tmpFile.isFile())
            IO.delete(tmpFile);
    }
}
Also used : DigestInputStream(java.security.DigestInputStream) DeployRequest(org.eclipse.aether.deployment.DeployRequest) AbstractTransferListener(org.eclipse.aether.transfer.AbstractTransferListener) TransferCancelledException(org.eclipse.aether.transfer.TransferCancelledException) TransferEvent(org.eclipse.aether.transfer.TransferEvent) IOException(java.io.IOException) Artifact(org.eclipse.aether.artifact.Artifact) DefaultArtifact(org.eclipse.aether.artifact.DefaultArtifact) URISyntaxException(java.net.URISyntaxException) TransferCancelledException(org.eclipse.aether.transfer.TransferCancelledException) ArtifactResolutionException(org.eclipse.aether.resolution.ArtifactResolutionException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) DefaultRepositorySystemSession(org.eclipse.aether.DefaultRepositorySystemSession) TransferResource(org.eclipse.aether.transfer.TransferResource) Jar(aQute.bnd.osgi.Jar) File(java.io.File)

Aggregations

File (java.io.File)2 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)2 Artifact (org.eclipse.aether.artifact.Artifact)2 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)2 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)2 AbstractTransferListener (org.eclipse.aether.transfer.AbstractTransferListener)2 TransferCancelledException (org.eclipse.aether.transfer.TransferCancelledException)2 TransferEvent (org.eclipse.aether.transfer.TransferEvent)2 Jar (aQute.bnd.osgi.Jar)1 MavenVersion (aQute.bnd.version.MavenVersion)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 UnknownHostException (java.net.UnknownHostException)1 DigestInputStream (java.security.DigestInputStream)1 DeployRequest (org.eclipse.aether.deployment.DeployRequest)1 ArtifactRequest (org.eclipse.aether.resolution.ArtifactRequest)1 ArtifactResult (org.eclipse.aether.resolution.ArtifactResult)1 TransferResource (org.eclipse.aether.transfer.TransferResource)1