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          String direction = event.getRequestType() == TransferEvent.RequestType.PUT ? "to" : "from";
54  
55          TransferResource resource = event.getResource();
56          StringBuilder message = new StringBuilder();
57          message.append( action ).append( ' ' ).append( direction ).append( ' ' ).append( resource.getRepositoryId() );
58          message.append( ": " );
59          message.append( resource.getRepositoryUrl() ).append( resource.getResourceName() );
60  
61          out.info( message.toString() );
62      }
63  
64      @Override
65      public void transferCorrupted( TransferEvent event )
66          throws TransferCancelledException
67      {
68          TransferResource resource = event.getResource();
69          out.warn( event.getException().getMessage() + " from " + resource.getRepositoryId() + " for "
70              + resource.getRepositoryUrl() + resource.getResourceName() );
71      }
72  
73      @Override
74      public void transferSucceeded( TransferEvent event )
75      {
76          String action = ( event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded" );
77          String direction = event.getRequestType() == TransferEvent.RequestType.PUT ? "to" : "from";
78  
79          TransferResource resource = event.getResource();
80          long contentLength = event.getTransferredBytes();
81          FileSizeFormat format = new FileSizeFormat( Locale.ENGLISH );
82  
83          StringBuilder message = new StringBuilder();
84          message.append( action ).append( ' ' ).append( direction ).append( ' ' ).append( resource.getRepositoryId() );
85          message.append( ": " );
86          message.append( resource.getRepositoryUrl() ).append( resource.getResourceName() );
87          message.append( " (" ).append( format.format( contentLength ) );
88  
89          long duration = System.currentTimeMillis() - resource.getTransferStartTime();
90          if ( duration > 0L )
91          {
92              double bytesPerSecond = contentLength / ( duration / 1000.0 );
93              message.append( " at " ).append( format.format( (long) bytesPerSecond ) ).append( "/s" );
94          }
95  
96          message.append( ')' );
97          out.info( message.toString() );
98      }
99  
100 }