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