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