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