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