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