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 import java.util.Iterator;
23 import java.util.LinkedHashMap;
24 import java.util.Map;
25
26 import org.apache.maven.api.services.MessageBuilderFactory;
27 import org.eclipse.aether.transfer.TransferCancelledException;
28 import org.eclipse.aether.transfer.TransferEvent;
29 import org.eclipse.aether.transfer.TransferResource;
30
31
32
33
34
35
36 public class ConsoleMavenTransferListener extends AbstractMavenTransferListener {
37
38 private final Map<TransferResourceIdentifier, TransferResourceAndSize> transfers = new LinkedHashMap<>();
39 private final FileSizeFormat format = new FileSizeFormat();
40 private final StringBuilder buffer = new StringBuilder(128);
41
42 private final boolean printResourceNames;
43 private int lastLength;
44
45 public ConsoleMavenTransferListener(
46 MessageBuilderFactory messageBuilderFactory, PrintStream out, boolean printResourceNames) {
47 super(messageBuilderFactory, out);
48 this.printResourceNames = printResourceNames;
49 }
50
51 @Override
52 public void transferInitiated(TransferEvent event) {
53 overridePreviousTransfer(event);
54
55 super.transferInitiated(event);
56 }
57
58 @Override
59 public void transferCorrupted(TransferEvent event) throws TransferCancelledException {
60 overridePreviousTransfer(event);
61
62 super.transferCorrupted(event);
63 }
64
65 @Override
66 public void transferProgressed(TransferEvent event) throws TransferCancelledException {
67 TransferResource resource = event.getResource();
68 transfers.put(
69 new TransferResourceIdentifier(resource),
70 new TransferResourceAndSize(resource, event.getTransferredBytes()));
71
72 buffer.append("Progress (").append(transfers.size()).append("): ");
73
74 Iterator<TransferResourceAndSize> entries = transfers.values().iterator();
75 while (entries.hasNext()) {
76 TransferResourceAndSize entry = entries.next();
77 long total = entry.resource.getContentLength();
78 Long complete = entry.transferredBytes;
79
80 String resourceName = entry.resource.getResourceName();
81
82 if (printResourceNames) {
83 int idx = resourceName.lastIndexOf('/');
84
85 if (idx < 0) {
86 buffer.append(resourceName);
87 } else {
88 buffer.append(resourceName, idx + 1, resourceName.length());
89 }
90 buffer.append(" (");
91 }
92
93 format.formatProgress(buffer, complete, total);
94
95 if (printResourceNames) {
96 buffer.append(")");
97 }
98
99 if (entries.hasNext()) {
100 buffer.append(" | ");
101 }
102 }
103
104 int pad = lastLength - buffer.length();
105 lastLength = buffer.length();
106 pad(buffer, pad);
107 buffer.append('\r');
108 out.print(buffer);
109 out.flush();
110 buffer.setLength(0);
111 }
112
113 private void pad(StringBuilder buffer, int spaces) {
114 String block = " ";
115 while (spaces > 0) {
116 int n = Math.min(spaces, block.length());
117 buffer.append(block, 0, n);
118 spaces -= n;
119 }
120 }
121
122 @Override
123 public void transferSucceeded(TransferEvent event) {
124 transfers.remove(new TransferResourceIdentifier(event.getResource()));
125 overridePreviousTransfer(event);
126
127 super.transferSucceeded(event);
128 }
129
130 @Override
131 public void transferFailed(TransferEvent event) {
132 transfers.remove(new TransferResourceIdentifier(event.getResource()));
133 overridePreviousTransfer(event);
134
135 super.transferFailed(event);
136 }
137
138 private void overridePreviousTransfer(TransferEvent event) {
139 if (lastLength > 0) {
140 pad(buffer, lastLength);
141 buffer.append('\r');
142 out.print(buffer);
143 out.flush();
144 lastLength = 0;
145 buffer.setLength(0);
146 }
147 }
148
149 private record TransferResourceAndSize(TransferResource resource, long transferredBytes) {}
150 }