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