Search in sources :

Example 6 with TransferResource

use of org.eclipse.aether.transfer.TransferResource 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)

Example 7 with TransferResource

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

the class ConsoleTransferListener method transferSucceeded.

@Override
public void transferSucceeded(TransferEvent event) {
    transferCompleted(event);
    TransferResource resource = event.getResource();
    long contentLength = event.getTransferredBytes();
    if (contentLength >= 0) {
        String type = (event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded");
        String len = contentLength >= 1024 ? toKB(contentLength) + " KB" : contentLength + " B";
        String throughput = "";
        long duration = System.currentTimeMillis() - resource.getTransferStartTime();
        if (duration > 0) {
            long bytes = contentLength - resource.getResumeOffset();
            DecimalFormat format = new DecimalFormat("0.0", new DecimalFormatSymbols(Locale.ENGLISH));
            double kbPerSec = (bytes / 1024.0) / (duration / 1000.0);
            throughput = " at " + format.format(kbPerSec) + " KB/sec";
        }
        out.println(type + ": " + resource.getRepositoryUrl() + resource.getResourceName() + " (" + len + throughput + ")");
    }
}
Also used : DecimalFormatSymbols(java.text.DecimalFormatSymbols) TransferResource(org.eclipse.aether.transfer.TransferResource) DecimalFormat(java.text.DecimalFormat)

Aggregations

TransferResource (org.eclipse.aether.transfer.TransferResource)7 File (java.io.File)3 RemoteException (java.rmi.RemoteException)2 Jar (aQute.bnd.osgi.Jar)1 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 UnknownHostException (java.net.UnknownHostException)1 DigestInputStream (java.security.DigestInputStream)1 DecimalFormat (java.text.DecimalFormat)1 DecimalFormatSymbols (java.text.DecimalFormatSymbols)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 DefaultRepositorySystemSession (org.eclipse.aether.DefaultRepositorySystemSession)1 Artifact (org.eclipse.aether.artifact.Artifact)1 DefaultArtifact (org.eclipse.aether.artifact.DefaultArtifact)1 DeployRequest (org.eclipse.aether.deployment.DeployRequest)1 ArtifactResolutionException (org.eclipse.aether.resolution.ArtifactResolutionException)1 AbstractTransferListener (org.eclipse.aether.transfer.AbstractTransferListener)1 TransferCancelledException (org.eclipse.aether.transfer.TransferCancelledException)1 TransferEvent (org.eclipse.aether.transfer.TransferEvent)1