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 org.eclipse.aether.transfer.AbstractTransferListener;
22 import org.eclipse.aether.transfer.TransferCancelledException;
23 import org.eclipse.aether.transfer.TransferEvent;
24 import org.eclipse.aether.transfer.TransferResource;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28
29
30
31 public class Slf4jMavenTransferListener extends AbstractTransferListener {
32
33 protected final Logger out;
34
35 public Slf4jMavenTransferListener() {
36 this.out = LoggerFactory.getLogger(Slf4jMavenTransferListener.class);
37 }
38
39
40 public Slf4jMavenTransferListener(Logger out) {
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 StringBuilder message = new StringBuilder();
51 message.append(action).append(' ').append(direction).append(' ').append(resource.getRepositoryId());
52 message.append(": ");
53 message.append(resource.getRepositoryUrl()).append(resource.getResourceName());
54
55 out.info(message.toString());
56 }
57
58 @Override
59 public void transferCorrupted(TransferEvent event) throws TransferCancelledException {
60 TransferResource resource = event.getResource();
61 out.warn(
62 "{} from {} for {}{}",
63 event.getException().getMessage(),
64 resource.getRepositoryId(),
65 resource.getRepositoryUrl(),
66 resource.getResourceName());
67 }
68
69 @Override
70 public void transferSucceeded(TransferEvent event) {
71 String action = (event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded");
72 String direction = event.getRequestType() == TransferEvent.RequestType.PUT ? "to" : "from";
73
74 TransferResource resource = event.getResource();
75 long contentLength = event.getTransferredBytes();
76 FileSizeFormat format = new FileSizeFormat();
77
78 StringBuilder message = new StringBuilder();
79 message.append(action).append(' ').append(direction).append(' ').append(resource.getRepositoryId());
80 message.append(": ");
81 message.append(resource.getRepositoryUrl())
82 .append(resource.getResourceName())
83 .append(" (");
84 format.format(message, contentLength);
85
86 long duration = System.currentTimeMillis() - resource.getTransferStartTime();
87 if (duration > 0L) {
88 double bytesPerSecond = contentLength / (duration / 1000.0);
89 message.append(" at ");
90 format.format(message, (long) bytesPerSecond);
91 message.append("/s");
92 }
93
94 message.append(')');
95 out.info(message.toString());
96 }
97 }