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.cling.transfer;
20  
21  import java.io.PrintWriter;
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   * Console download progress meter.
33   * <p>
34   * This listener is not thread-safe and should be wrapped in the {@link SimplexTransferListener} in a multi-threaded scenario.
35   */
36  public class ConsoleMavenTransferListener extends AbstractMavenTransferListener {
37  
38      private final Map<TransferResourceIdentifier, TransferResourceAndSize> transfers = new LinkedHashMap<>();
39      private final FileSizeFormat format = new FileSizeFormat(); // use in a synchronized fashion
40      private final StringBuilder buffer = new StringBuilder(128); // use in a synchronized fashion
41  
42      private final boolean printResourceNames;
43      private int lastLength;
44  
45      public ConsoleMavenTransferListener(
46              MessageBuilderFactory messageBuilderFactory, PrintWriter 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              // just in case, make sure 0 <= complete <= total
78              long complete = Math.max(0, entry.transferredBytes);
79              long total = Math.max(complete, entry.resource.getContentLength());
80  
81              String resourceName = entry.resource.getResourceName();
82  
83              if (printResourceNames) {
84                  int idx = resourceName.lastIndexOf('/');
85  
86                  if (idx < 0) {
87                      buffer.append(resourceName);
88                  } else {
89                      buffer.append(resourceName, idx + 1, resourceName.length());
90                  }
91                  buffer.append(" (");
92              }
93  
94              format.formatProgress(buffer, complete, total);
95  
96              if (printResourceNames) {
97                  buffer.append(")");
98              }
99  
100             if (entries.hasNext()) {
101                 buffer.append(" | ");
102             }
103         }
104 
105         int pad = lastLength - buffer.length();
106         lastLength = buffer.length();
107         pad(buffer, pad);
108         buffer.append('\r');
109         out.print(buffer);
110         out.flush();
111         buffer.setLength(0);
112     }
113 
114     private void pad(StringBuilder buffer, int spaces) {
115         String block = "                                        ";
116         while (spaces > 0) {
117             int n = Math.min(spaces, block.length());
118             buffer.append(block, 0, n);
119             spaces -= n;
120         }
121     }
122 
123     @Override
124     public void transferSucceeded(TransferEvent event) {
125         transfers.remove(new TransferResourceIdentifier(event.getResource()));
126         overridePreviousTransfer(event);
127 
128         super.transferSucceeded(event);
129     }
130 
131     @Override
132     public void transferFailed(TransferEvent event) {
133         transfers.remove(new TransferResourceIdentifier(event.getResource()));
134         overridePreviousTransfer(event);
135 
136         super.transferFailed(event);
137     }
138 
139     private void overridePreviousTransfer(TransferEvent event) {
140         if (lastLength > 0) {
141             pad(buffer, lastLength);
142             buffer.append('\r');
143             out.print(buffer);
144             out.flush();
145             lastLength = 0;
146             buffer.setLength(0);
147         }
148     }
149 
150     private record TransferResourceAndSize(TransferResource resource, long transferredBytes) {}
151 }