View Javadoc
1   package org.apache.maven.cli.transfer;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.text.DecimalFormat;
23  import java.text.DecimalFormatSymbols;
24  import java.util.Locale;
25  
26  import org.eclipse.aether.transfer.AbstractTransferListener;
27  import org.eclipse.aether.transfer.TransferCancelledException;
28  import org.eclipse.aether.transfer.TransferEvent;
29  import org.eclipse.aether.transfer.TransferResource;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  public class Slf4jMavenTransferListener
34      extends AbstractTransferListener
35  {
36  
37      protected final Logger out;
38  
39      public Slf4jMavenTransferListener()
40      {
41          this.out = LoggerFactory.getLogger( Slf4jMavenTransferListener.class );
42      }
43  
44      // TODO should we deprecate?
45      public Slf4jMavenTransferListener( Logger out )
46      {
47          this.out = out;
48      }
49  
50      @Override
51      public void transferInitiated( TransferEvent event )
52      {
53          String message = event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploading" : "Downloading";
54  
55          out.info( message + ": " + event.getResource().getRepositoryUrl() + event.getResource().getResourceName() );
56      }
57  
58      @Override
59      public void transferCorrupted( TransferEvent event )
60          throws TransferCancelledException
61      {
62          TransferResource resource = event.getResource();
63  
64          out.warn( event.getException().getMessage() + " for " + resource.getRepositoryUrl()
65              + resource.getResourceName() );
66      }
67  
68      @Override
69      public void transferSucceeded( TransferEvent event )
70      {
71          TransferResource resource = event.getResource();
72          long contentLength = event.getTransferredBytes();
73          if ( contentLength >= 0 )
74          {
75              String type = ( event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded" );
76              String len = contentLength >= 1024 ? toKB( contentLength ) + " KB" : contentLength + " B";
77  
78              String throughput = "";
79              long duration = System.currentTimeMillis() - resource.getTransferStartTime();
80              if ( duration > 0 )
81              {
82                  DecimalFormat format = new DecimalFormat( "0.0", new DecimalFormatSymbols( Locale.ENGLISH ) );
83                  double kbPerSec = ( contentLength / 1024.0 ) / ( duration / 1000.0 );
84                  throughput = " at " + format.format( kbPerSec ) + " KB/sec";
85              }
86  
87              out.info( type + ": " + resource.getRepositoryUrl() + resource.getResourceName() + " (" + len
88                  + throughput + ")" );
89          }
90      }
91  
92      protected long toKB( long bytes )
93      {
94          return ( bytes + 1023 ) / 1024;
95      }
96  
97  }