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.Locale;
23  
24  import org.apache.maven.cli.jansi.MessageUtils;
25  import org.eclipse.aether.transfer.AbstractTransferListener;
26  import org.eclipse.aether.transfer.TransferCancelledException;
27  import org.eclipse.aether.transfer.TransferEvent;
28  import org.eclipse.aether.transfer.TransferResource;
29  
30  /**
31   * AbstractMavenTransferListener
32   */
33  public abstract class AbstractMavenTransferListener extends AbstractTransferListener {
34  
35      private static final String ESC = "\u001B";
36      private static final String ANSI_DARK_SET = ESC + "[90m";
37      private static final String ANSI_DARK_RESET = ESC + "[0m";
38  
39      protected PrintStream out;
40  
41      protected AbstractMavenTransferListener(PrintStream out) {
42          this.out = out;
43      }
44  
45      @Override
46      public void transferInitiated(TransferEvent event) {
47          String darkOn = MessageUtils.isColorEnabled() ? ANSI_DARK_SET : "";
48          String darkOff = MessageUtils.isColorEnabled() ? ANSI_DARK_RESET : "";
49  
50          String action = event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploading" : "Downloading";
51          String direction = event.getRequestType() == TransferEvent.RequestType.PUT ? "to" : "from";
52  
53          TransferResource resource = event.getResource();
54          StringBuilder message = new StringBuilder();
55          message.append(darkOn).append(action).append(' ').append(direction).append(' ');
56          message.append(darkOff).append(resource.getRepositoryId());
57          message.append(darkOn).append(": ").append(resource.getRepositoryUrl());
58          message.append(darkOff).append(resource.getResourceName());
59  
60          out.println(message.toString());
61      }
62  
63      @Override
64      public void transferCorrupted(TransferEvent event) throws TransferCancelledException {
65          TransferResource resource = event.getResource();
66          // TODO This needs to be colorized
67          out.println("[WARNING] " + event.getException().getMessage() + " from " + resource.getRepositoryId() + " for "
68                  + resource.getRepositoryUrl() + resource.getResourceName());
69      }
70  
71      @Override
72      public void transferSucceeded(TransferEvent event) {
73          String darkOn = MessageUtils.isColorEnabled() ? ANSI_DARK_SET : "";
74          String darkOff = MessageUtils.isColorEnabled() ? ANSI_DARK_RESET : "";
75  
76          String action = (event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded");
77          String direction = event.getRequestType() == TransferEvent.RequestType.PUT ? "to" : "from";
78  
79          TransferResource resource = event.getResource();
80          long contentLength = event.getTransferredBytes();
81          FileSizeFormat format = new FileSizeFormat(Locale.ENGLISH);
82  
83          StringBuilder message = new StringBuilder();
84          message.append(action).append(darkOn).append(' ').append(direction).append(' ');
85          message.append(darkOff).append(resource.getRepositoryId());
86          message.append(darkOn).append(": ").append(resource.getRepositoryUrl());
87          message.append(darkOff).append(resource.getResourceName());
88          message.append(darkOn).append(" (");
89          format.format(message, contentLength);
90  
91          long duration = System.currentTimeMillis() - resource.getTransferStartTime();
92          if (duration > 0L) {
93              double bytesPerSecond = contentLength / (duration / 1000.0);
94              message.append(" at ");
95              format.format(message, (long) bytesPerSecond);
96              message.append("/s");
97          }
98  
99          message.append(')').append(darkOff);
100         out.println(message.toString());
101     }
102 }