View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.resolver.examples.util;
20  
21  import java.io.PrintStream;
22  import java.text.DecimalFormat;
23  import java.text.DecimalFormatSymbols;
24  import java.time.Duration;
25  import java.time.Instant;
26  import java.util.Locale;
27  import java.util.Map;
28  import java.util.concurrent.ConcurrentHashMap;
29  
30  import org.eclipse.aether.transfer.AbstractTransferListener;
31  import org.eclipse.aether.transfer.MetadataNotFoundException;
32  import org.eclipse.aether.transfer.TransferEvent;
33  import org.eclipse.aether.transfer.TransferResource;
34  
35  import static java.util.Objects.requireNonNull;
36  
37  /**
38   * A simplistic transfer listener that logs uploads/downloads to the console.
39   */
40  public class ConsoleTransferListener extends AbstractTransferListener {
41  
42      private final PrintStream out;
43  
44      private final Map<TransferResource, Long> downloads = new ConcurrentHashMap<>();
45  
46      private int lastLength;
47  
48      public ConsoleTransferListener() {
49          this(null);
50      }
51  
52      public ConsoleTransferListener(PrintStream out) {
53          this.out = (out != null) ? out : System.out;
54      }
55  
56      @Override
57      public void transferInitiated(TransferEvent event) {
58          requireNonNull(event, "event cannot be null");
59          String message = event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploading" : "Downloading";
60  
61          out.println(message + ": " + event.getResource().getRepositoryUrl()
62                  + event.getResource().getResourceName());
63      }
64  
65      @Override
66      public void transferProgressed(TransferEvent event) {
67          requireNonNull(event, "event cannot be null");
68          TransferResource resource = event.getResource();
69          downloads.put(resource, event.getTransferredBytes());
70  
71          StringBuilder buffer = new StringBuilder(64);
72  
73          for (Map.Entry<TransferResource, Long> entry : downloads.entrySet()) {
74              long total = entry.getKey().getContentLength();
75              long complete = entry.getValue();
76  
77              buffer.append(getStatus(complete, total)).append("  ");
78          }
79  
80          int pad = lastLength - buffer.length();
81          lastLength = buffer.length();
82          pad(buffer, pad);
83          buffer.append('\r');
84  
85          out.print(buffer);
86      }
87  
88      private String getStatus(long complete, long total) {
89          if (total >= 1024) {
90              return toKB(complete) + "/" + toKB(total) + " KB ";
91          } else if (total >= 0) {
92              return complete + "/" + total + " B ";
93          } else if (complete >= 1024) {
94              return toKB(complete) + " KB ";
95          } else {
96              return complete + " B ";
97          }
98      }
99  
100     private void pad(StringBuilder buffer, int spaces) {
101         String block = "                                        ";
102         while (spaces > 0) {
103             int n = Math.min(spaces, block.length());
104             buffer.append(block, 0, n);
105             spaces -= n;
106         }
107     }
108 
109     @Override
110     public void transferSucceeded(TransferEvent event) {
111         requireNonNull(event, "event cannot be null");
112         transferCompleted(event);
113 
114         TransferResource resource = event.getResource();
115         long contentLength = event.getTransferredBytes();
116         if (contentLength >= 0) {
117             String type = (event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded");
118             String len = contentLength >= 1024 ? toKB(contentLength) + " KB" : contentLength + " B";
119 
120             String throughput = "";
121             Duration duration = Duration.between(resource.getStartTime(), Instant.now());
122             if (duration.toMillis() > 0) {
123                 long bytes = contentLength - resource.getResumeOffset();
124                 DecimalFormat format = new DecimalFormat("0.0", new DecimalFormatSymbols(Locale.ENGLISH));
125                 double kbPerSec = (bytes / 1024.0) / duration.toSeconds();
126                 throughput = " at " + format.format(kbPerSec) + " KB/sec";
127             }
128 
129             out.println(type + ": " + resource.getRepositoryUrl() + resource.getResourceName() + " (" + len + throughput
130                     + ")");
131         }
132     }
133 
134     @Override
135     public void transferFailed(TransferEvent event) {
136         requireNonNull(event, "event cannot be null");
137         transferCompleted(event);
138 
139         if (!(event.getException() instanceof MetadataNotFoundException)) {
140             event.getException().printStackTrace(out);
141         }
142     }
143 
144     private void transferCompleted(TransferEvent event) {
145         requireNonNull(event, "event cannot be null");
146         downloads.remove(event.getResource());
147 
148         StringBuilder buffer = new StringBuilder(64);
149         pad(buffer, lastLength);
150         buffer.append('\r');
151         out.print(buffer);
152     }
153 
154     public void transferCorrupted(TransferEvent event) {
155         requireNonNull(event, "event cannot be null");
156         event.getException().printStackTrace(out);
157     }
158 
159     protected long toKB(long bytes) {
160         return (bytes + 1023) / 1024;
161     }
162 }