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);
}
}
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 + ")");
}
}
Aggregations