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