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.cling.transfer;
20  
21  import java.time.Duration;
22  import java.time.Instant;
23  
24  import org.apache.maven.api.MonotonicClock;
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  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  /**
33   * Slf4jMavenTransferListener
34   */
35  public class Slf4jMavenTransferListener extends AbstractTransferListener {
36  
37      protected final Logger out;
38  
39      public Slf4jMavenTransferListener() {
40          this.out = LoggerFactory.getLogger(Slf4jMavenTransferListener.class);
41      }
42  
43      // TODO should we deprecate?
44      public Slf4jMavenTransferListener(Logger out) {
45          this.out = out;
46      }
47  
48      @Override
49      public void transferInitiated(TransferEvent event) {
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(action).append(' ').append(direction).append(' ').append(resource.getRepositoryId());
56          message.append(": ");
57          message.append(resource.getRepositoryUrl()).append(resource.getResourceName());
58  
59          out.info(message.toString());
60      }
61  
62      @Override
63      public void transferCorrupted(TransferEvent event) throws TransferCancelledException {
64          TransferResource resource = event.getResource();
65          out.warn(
66                  "{} from {} for {}{}",
67                  event.getException().getMessage(),
68                  resource.getRepositoryId(),
69                  resource.getRepositoryUrl(),
70                  resource.getResourceName());
71      }
72  
73      @Override
74      public void transferSucceeded(TransferEvent event) {
75          String action = (event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded");
76          String direction = event.getRequestType() == TransferEvent.RequestType.PUT ? "to" : "from";
77  
78          TransferResource resource = event.getResource();
79          long contentLength = event.getTransferredBytes();
80          FileSizeFormat format = new FileSizeFormat();
81  
82          StringBuilder message = new StringBuilder();
83          message.append(action).append(' ').append(direction).append(' ').append(resource.getRepositoryId());
84          message.append(": ");
85          message.append(resource.getRepositoryUrl())
86                  .append(resource.getResourceName())
87                  .append(" (");
88          format.format(message, contentLength);
89  
90          Duration duration =
91                  Duration.between(Instant.ofEpochMilli(resource.getTransferStartTime()), MonotonicClock.now());
92          if ((duration.getSeconds() | duration.getNano()) > 0) { // duration.isPositive()
93              long bytesPerSecond = Math.round(contentLength / (double) duration.toSeconds());
94              message.append(" at ");
95              format.format(message, bytesPerSecond);
96              message.append("/s");
97          }
98  
99          message.append(')');
100         out.info(message.toString());
101     }
102 }