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.util.Locale;
22  
23  import org.eclipse.aether.transfer.AbstractTransferListener;
24  import org.eclipse.aether.transfer.TransferCancelledException;
25  import org.eclipse.aether.transfer.TransferEvent;
26  import org.eclipse.aether.transfer.TransferResource;
27  import org.slf4j.Logger;
28  import org.slf4j.LoggerFactory;
29  
30  /**
31   * Slf4jMavenTransferListener
32   */
33  public class Slf4jMavenTransferListener extends AbstractTransferListener {
34  
35      protected final Logger out;
36  
37      public Slf4jMavenTransferListener() {
38          this.out = LoggerFactory.getLogger(Slf4jMavenTransferListener.class);
39      }
40  
41      // TODO should we deprecate?
42      public Slf4jMavenTransferListener(Logger out) {
43          this.out = out;
44      }
45  
46      @Override
47      public void transferInitiated(TransferEvent event) {
48          String action = event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploading" : "Downloading";
49          String direction = event.getRequestType() == TransferEvent.RequestType.PUT ? "to" : "from";
50  
51          TransferResource resource = event.getResource();
52          StringBuilder message = new StringBuilder();
53          message.append(action).append(' ').append(direction).append(' ').append(resource.getRepositoryId());
54          message.append(": ");
55          message.append(resource.getRepositoryUrl()).append(resource.getResourceName());
56  
57          out.info(message.toString());
58      }
59  
60      @Override
61      public void transferCorrupted(TransferEvent event) throws TransferCancelledException {
62          TransferResource resource = event.getResource();
63          out.warn(
64                  "{} from {} for {}{}",
65                  event.getException().getMessage(),
66                  resource.getRepositoryId(),
67                  resource.getRepositoryUrl(),
68                  resource.getResourceName());
69      }
70  
71      @Override
72      public void transferSucceeded(TransferEvent event) {
73          String action = (event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded");
74          String direction = event.getRequestType() == TransferEvent.RequestType.PUT ? "to" : "from";
75  
76          TransferResource resource = event.getResource();
77          long contentLength = event.getTransferredBytes();
78          FileSizeFormat format = new FileSizeFormat(Locale.ENGLISH);
79  
80          StringBuilder message = new StringBuilder();
81          message.append(action).append(' ').append(direction).append(' ').append(resource.getRepositoryId());
82          message.append(": ");
83          message.append(resource.getRepositoryUrl())
84                  .append(resource.getResourceName())
85                  .append(" (");
86          format.format(message, contentLength);
87  
88          long duration = System.currentTimeMillis() - resource.getTransferStartTime();
89          if (duration > 0L) {
90              double bytesPerSecond = contentLength / (duration / 1000.0);
91              message.append(" at ");
92              format.format(message, (long) bytesPerSecond);
93              message.append("/s");
94          }
95  
96          message.append(')');
97          out.info(message.toString());
98      }
99  }