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.util.Map;
24  import java.util.concurrent.ConcurrentHashMap;
25  
26  import org.sonatype.aether.transfer.TransferCancelledException;
27  import org.sonatype.aether.transfer.TransferEvent;
28  import org.sonatype.aether.transfer.TransferResource;
29  
30  /**
31   * Console download progress meter.
32   * 
33   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
34   */
35  public class ConsoleMavenTransferListener
36      extends AbstractMavenTransferListener
37  {
38  
39      private Map<TransferResource, Long> downloads = new ConcurrentHashMap<TransferResource, Long>();
40  
41      private int lastLength;
42  
43      public ConsoleMavenTransferListener( PrintStream out )
44      {
45          super( out );
46      }
47  
48      @Override
49      public void transferProgressed( TransferEvent event )
50          throws TransferCancelledException
51      {
52          TransferResource resource = event.getResource();
53          downloads.put( resource, Long.valueOf( event.getTransferredBytes() ) );
54  
55          StringBuilder buffer = new StringBuilder( 64 );
56  
57          for ( Map.Entry<TransferResource, Long> entry : downloads.entrySet() )
58          {
59              long total = entry.getKey().getContentLength();
60              Long complete = entry.getValue();
61              // NOTE: This null check guards against http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6312056
62              if ( complete != null )
63              {
64                  buffer.append( getStatus( complete.longValue(), total ) ).append( "  " );
65              }
66          }
67  
68          int pad = lastLength - buffer.length();
69          lastLength = buffer.length();
70          pad( buffer, pad );
71          buffer.append( '\r' );
72  
73          out.print( buffer );
74      }
75  
76      private String getStatus( long complete, long total )
77      {
78          if ( total >= 1024 )
79          {
80              return toKB( complete ) + "/" + toKB( total ) + " KB ";
81          }
82          else if ( total >= 0 )
83          {
84              return complete + "/" + total + " B ";
85          }
86          else if ( complete >= 1024 )
87          {
88              return toKB( complete ) + " KB ";
89          }
90          else
91          {
92              return complete + " B ";
93          }
94      }
95  
96      private void pad( StringBuilder buffer, int spaces )
97      {
98          String block = "                                        ";
99          while ( spaces > 0 )
100         {
101             int n = Math.min( spaces, block.length() );
102             buffer.append( block, 0, n );
103             spaces -= n;
104         }
105     }
106 
107     @Override
108     public void transferSucceeded( TransferEvent event )
109     {
110         transferCompleted( event );
111 
112         super.transferSucceeded( event );
113     }
114 
115     @Override
116     public void transferFailed( TransferEvent event )
117     {
118         transferCompleted( event );
119 
120         super.transferFailed( event );
121     }
122 
123     private void transferCompleted( TransferEvent event )
124     {
125         downloads.remove( event.getResource() );
126 
127         StringBuilder buffer = new StringBuilder( 64 );
128         pad( buffer, lastLength );
129         buffer.append( '\r' );
130         out.print( buffer );
131     }
132 
133 }