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.apache.maven.api.services.MessageBuilderFactory;
28  import org.eclipse.aether.transfer.TransferCancelledException;
29  import org.eclipse.aether.transfer.TransferEvent;
30  import org.eclipse.aether.transfer.TransferResource;
31  
32  /**
33   * Console download progress meter.
34   *
35   */
36  public class ConsoleMavenTransferListener extends AbstractMavenTransferListener {
37  
38      private Map<TransferResource, Long> transfers = new LinkedHashMap<>();
39      private FileSizeFormat format = new FileSizeFormat(Locale.ENGLISH); // use in a synchronized fashion
40      private StringBuilder buffer = new StringBuilder(128); // use in a synchronized fashion
41  
42      private 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(resource, event.getTransferredBytes());
69  
70          buffer.append("Progress (").append(transfers.size()).append("): ");
71  
72          Iterator<Map.Entry<TransferResource, Long>> entries =
73                  transfers.entrySet().iterator();
74          while (entries.hasNext()) {
75              Map.Entry<TransferResource, Long> entry = entries.next();
76              long total = entry.getKey().getContentLength();
77              Long complete = entry.getValue();
78  
79              String resourceName = entry.getKey().getResourceName();
80  
81              if (printResourceNames) {
82                  int idx = resourceName.lastIndexOf('/');
83  
84                  if (idx < 0) {
85                      buffer.append(resourceName);
86                  } else {
87                      buffer.append(resourceName, idx + 1, resourceName.length());
88                  }
89                  buffer.append(" (");
90              }
91  
92              format.formatProgress(buffer, complete, total);
93  
94              if (printResourceNames) {
95                  buffer.append(")");
96              }
97  
98              if (entries.hasNext()) {
99                  buffer.append(" | ");
100             }
101         }
102 
103         int pad = lastLength - buffer.length();
104         lastLength = buffer.length();
105         pad(buffer, pad);
106         buffer.append('\r');
107         out.print(buffer);
108         out.flush();
109         buffer.setLength(0);
110     }
111 
112     private void pad(StringBuilder buffer, int spaces) {
113         String block = "                                        ";
114         while (spaces > 0) {
115             int n = Math.min(spaces, block.length());
116             buffer.append(block, 0, n);
117             spaces -= n;
118         }
119     }
120 
121     @Override
122     public void transferSucceeded(TransferEvent event) {
123         transfers.remove(event.getResource());
124         overridePreviousTransfer(event);
125 
126         super.transferSucceeded(event);
127     }
128 
129     @Override
130     public void transferFailed(TransferEvent event) {
131         transfers.remove(event.getResource());
132         overridePreviousTransfer(event);
133 
134         super.transferFailed(event);
135     }
136 
137     private void overridePreviousTransfer(TransferEvent event) {
138         if (lastLength > 0) {
139             pad(buffer, lastLength);
140             buffer.append('\r');
141             out.print(buffer);
142             out.flush();
143             lastLength = 0;
144             buffer.setLength(0);
145         }
146     }
147 }