View Javadoc
1   package org.apache.maven.resolver.examples.util;
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.io.PrintStream;
23  import java.text.DecimalFormat;
24  import java.text.DecimalFormatSymbols;
25  import java.util.Locale;
26  import java.util.Map;
27  import java.util.concurrent.ConcurrentHashMap;
28  
29  import org.eclipse.aether.transfer.AbstractTransferListener;
30  import org.eclipse.aether.transfer.MetadataNotFoundException;
31  import org.eclipse.aether.transfer.TransferEvent;
32  import org.eclipse.aether.transfer.TransferResource;
33  
34  import static java.util.Objects.requireNonNull;
35  
36  /**
37   * A simplistic transfer listener that logs uploads/downloads to the console.
38   */
39  public class ConsoleTransferListener
40      extends AbstractTransferListener
41  {
42  
43      private final PrintStream out;
44  
45      private final Map<TransferResource, Long> downloads = new ConcurrentHashMap<>();
46  
47      private int lastLength;
48  
49      public ConsoleTransferListener()
50      {
51          this( null );
52      }
53  
54      public ConsoleTransferListener( PrintStream out )
55      {
56          this.out = ( out != null ) ? out : System.out;
57      }
58  
59      @Override
60      public void transferInitiated( TransferEvent event )
61      {
62          requireNonNull( event, "event cannot be null" );
63          String message = event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploading" : "Downloading";
64  
65          out.println( message + ": " + event.getResource().getRepositoryUrl() + event.getResource().getResourceName() );
66      }
67  
68      @Override
69      public void transferProgressed( TransferEvent event )
70      {
71          requireNonNull( event, "event cannot be null" );
72          TransferResource resource = event.getResource();
73          downloads.put( resource, event.getTransferredBytes() );
74  
75          StringBuilder buffer = new StringBuilder( 64 );
76  
77          for ( Map.Entry<TransferResource, Long> entry : downloads.entrySet() )
78          {
79              long total = entry.getKey().getContentLength();
80              long complete = entry.getValue();
81  
82              buffer.append( getStatus( complete, total ) ).append( "  " );
83          }
84  
85          int pad = lastLength - buffer.length();
86          lastLength = buffer.length();
87          pad( buffer, pad );
88          buffer.append( '\r' );
89  
90          out.print( buffer );
91      }
92  
93      private String getStatus( long complete, long total )
94      {
95          if ( total >= 1024 )
96          {
97              return toKB( complete ) + "/" + toKB( total ) + " KB ";
98          }
99          else if ( total >= 0 )
100         {
101             return complete + "/" + total + " B ";
102         }
103         else if ( complete >= 1024 )
104         {
105             return toKB( complete ) + " KB ";
106         }
107         else
108         {
109             return complete + " B ";
110         }
111     }
112 
113     private void pad( StringBuilder buffer, int spaces )
114     {
115         String block = "                                        ";
116         while ( spaces > 0 )
117         {
118             int n = Math.min( spaces, block.length() );
119             buffer.append( block, 0, n );
120             spaces -= n;
121         }
122     }
123 
124     @Override
125     public void transferSucceeded( TransferEvent event )
126     {
127         requireNonNull( event, "event cannot be null" );
128         transferCompleted( event );
129 
130         TransferResource resource = event.getResource();
131         long contentLength = event.getTransferredBytes();
132         if ( contentLength >= 0 )
133         {
134             String type = ( event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded" );
135             String len = contentLength >= 1024 ? toKB( contentLength ) + " KB" : contentLength + " B";
136 
137             String throughput = "";
138             long duration = System.currentTimeMillis() - resource.getTransferStartTime();
139             if ( duration > 0 )
140             {
141                 long bytes = contentLength - resource.getResumeOffset();
142                 DecimalFormat format = new DecimalFormat( "0.0", new DecimalFormatSymbols( Locale.ENGLISH ) );
143                 double kbPerSec = ( bytes / 1024.0 ) / ( duration / 1000.0 );
144                 throughput = " at " + format.format( kbPerSec ) + " KB/sec";
145             }
146 
147             out.println( type + ": " + resource.getRepositoryUrl() + resource.getResourceName() + " (" + len
148                 + throughput + ")" );
149         }
150     }
151 
152     @Override
153     public void transferFailed( TransferEvent event )
154     {
155         requireNonNull( event, "event cannot be null" );
156         transferCompleted( event );
157 
158         if ( !( event.getException() instanceof MetadataNotFoundException ) )
159         {
160             event.getException().printStackTrace( out );
161         }
162     }
163 
164     private void transferCompleted( TransferEvent event )
165     {
166         requireNonNull( event, "event cannot be null" );
167         downloads.remove( event.getResource() );
168 
169         StringBuilder buffer = new StringBuilder( 64 );
170         pad( buffer, lastLength );
171         buffer.append( '\r' );
172         out.print( buffer );
173     }
174 
175     public void transferCorrupted( TransferEvent event )
176     {
177         requireNonNull( event, "event cannot be null" );
178         event.getException().printStackTrace( out );
179     }
180 
181     @SuppressWarnings( "checkstyle:magicnumber" )
182     protected long toKB( long bytes )
183     {
184         return ( bytes + 1023 ) / 1024;
185     }
186 
187 }