1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.cli.transfer;
20
21 import java.io.PrintStream;
22
23 import org.apache.maven.api.services.MessageBuilder;
24 import org.apache.maven.api.services.MessageBuilderFactory;
25 import org.eclipse.aether.transfer.AbstractTransferListener;
26 import org.eclipse.aether.transfer.TransferCancelledException;
27 import org.eclipse.aether.transfer.TransferEvent;
28 import org.eclipse.aether.transfer.TransferResource;
29
30
31
32
33 public abstract class AbstractMavenTransferListener extends AbstractTransferListener {
34 public static final String STYLE = ".transfer:-faint";
35
36 protected final MessageBuilderFactory messageBuilderFactory;
37 protected final PrintStream out;
38
39 protected AbstractMavenTransferListener(MessageBuilderFactory messageBuilderFactory, PrintStream out) {
40 this.messageBuilderFactory = messageBuilderFactory;
41 this.out = out;
42 }
43
44 @Override
45 public void transferInitiated(TransferEvent event) {
46 String action = event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploading" : "Downloading";
47 String direction = event.getRequestType() == TransferEvent.RequestType.PUT ? "to" : "from";
48
49 TransferResource resource = event.getResource();
50 MessageBuilder message = messageBuilderFactory.builder();
51 message.style(STYLE).append(action).append(' ').append(direction).append(' ');
52 message.resetStyle().append(resource.getRepositoryId());
53 message.style(STYLE).append(": ").append(resource.getRepositoryUrl());
54 message.resetStyle().append(resource.getResourceName());
55
56 out.println(message.toString());
57 }
58
59 @Override
60 public void transferCorrupted(TransferEvent event) throws TransferCancelledException {
61 TransferResource resource = event.getResource();
62
63 out.println("[WARNING] " + event.getException().getMessage() + " from " + resource.getRepositoryId() + " for "
64 + resource.getRepositoryUrl() + resource.getResourceName());
65 }
66
67 @Override
68 public void transferSucceeded(TransferEvent event) {
69 String action = (event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded");
70 String direction = event.getRequestType() == TransferEvent.RequestType.PUT ? "to" : "from";
71
72 TransferResource resource = event.getResource();
73 long contentLength = event.getTransferredBytes();
74 FileSizeFormat format = new FileSizeFormat();
75
76 MessageBuilder message = messageBuilderFactory.builder();
77 message.append(action).style(STYLE).append(' ').append(direction).append(' ');
78 message.resetStyle().append(resource.getRepositoryId());
79 message.style(STYLE).append(": ").append(resource.getRepositoryUrl());
80 message.resetStyle().append(resource.getResourceName());
81 message.style(STYLE).append(" (").append(format.format(contentLength));
82
83 long duration = System.currentTimeMillis() - resource.getTransferStartTime();
84 if (duration > 0L) {
85 double bytesPerSecond = contentLength / (duration / 1000.0);
86 message.append(" at ");
87 format.format(message, (long) bytesPerSecond);
88 message.append("/s");
89 }
90
91 message.append(')').resetStyle();
92 out.println(message.toString());
93 }
94 }