View Javadoc

1   package org.apache.maven.cli;
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  
27  import org.sonatype.aether.transfer.AbstractTransferListener;
28  import org.sonatype.aether.transfer.TransferCancelledException;
29  import org.sonatype.aether.transfer.TransferEvent;
30  import org.sonatype.aether.transfer.TransferResource;
31  
32  public abstract class AbstractMavenTransferListener
33      extends AbstractTransferListener
34  {
35  
36      protected PrintStream out;
37  
38      protected AbstractMavenTransferListener( PrintStream out )
39      {
40          this.out = ( out != null ) ? out : System.out;
41      }
42  
43      @Override
44      public void transferInitiated( TransferEvent event )
45      {
46          String message = event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploading" : "Downloading";
47  
48          out.println( message + ": " + event.getResource().getRepositoryUrl() + event.getResource().getResourceName() );
49      }
50  
51      @Override
52      public void transferCorrupted( TransferEvent event )
53          throws TransferCancelledException
54      {
55          TransferResource resource = event.getResource();
56  
57          out.println( "[WARNING] " + event.getException().getMessage() + " for " + resource.getRepositoryUrl()
58              + resource.getResourceName() );
59      }
60  
61      @Override
62      public void transferSucceeded( TransferEvent event )
63      {
64          TransferResource resource = event.getResource();
65          long contentLength = event.getTransferredBytes();
66          if ( contentLength >= 0 )
67          {
68              String type = ( event.getRequestType() == TransferEvent.RequestType.PUT ? "Uploaded" : "Downloaded" );
69              String len = contentLength >= 1024 ? toKB( contentLength ) + " KB" : contentLength + " B";
70  
71              String throughput = "";
72              long duration = System.currentTimeMillis() - resource.getTransferStartTime();
73              if ( duration > 0 )
74              {
75                  DecimalFormat format = new DecimalFormat( "0.0", new DecimalFormatSymbols( Locale.ENGLISH ) );
76                  double kbPerSec = ( contentLength / 1024.0 ) / ( duration / 1000.0 );
77                  throughput = " at " + format.format( kbPerSec ) + " KB/sec";
78              }
79  
80              out.println( type + ": " + resource.getRepositoryUrl() + resource.getResourceName() + " (" + len
81                  + throughput + ")" );
82          }
83      }
84  
85      protected long toKB( long bytes )
86      {
87          return ( bytes + 1023 ) / 1024;
88      }
89  
90  }