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