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() + resource.getResourceName() );
65      }
66  
67      @Override
68      public void transferSucceeded( TransferEvent event )
69      {
70          TransferResource resource = event.getResource();
71          long contentLength = event.getTransferredBytes();
72          if ( contentLength >= 0 )
73          {
74              String type = ( event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded" );
75              String len = contentLength >= 1024 ? toKB( contentLength ) + " KB" : contentLength + " B";
76  
77              String throughput = "";
78              long duration = System.currentTimeMillis() - resource.getTransferStartTime();
79              if ( duration > 0 )
80              {
81                  DecimalFormat format = new DecimalFormat( "0.0", new DecimalFormatSymbols( Locale.ENGLISH ) );
82                  double kbPerSec = ( contentLength / 1024.0 ) / ( duration / 1000.0 );
83                  throughput = " at " + format.format( kbPerSec ) + " KB/sec";
84              }
85  
86              out.info( type + ": " + resource.getRepositoryUrl() + resource.getResourceName() + " (" + len
87                  + throughput + ")" );
88          }
89      }
90  
91      protected long toKB( long bytes )
92      {
93          return ( bytes + 1023 ) / 1024;
94      }
95  
96  }