View Javadoc

1   package org.apache.maven.util;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   import org.apache.maven.wagon.events.TransferEvent;
6   import org.apache.maven.wagon.events.TransferListener;
7   
8   /* ====================================================================
9    *   Licensed to the Apache Software Foundation (ASF) under one or more
10   *   contributor license agreements.  See the NOTICE file distributed with
11   *   this work for additional information regarding copyright ownership.
12   *   The ASF licenses this file to You under the Apache License, Version 2.0
13   *   (the "License"); you may not use this file except in compliance with
14   *   the License.  You may obtain a copy of the License at
15   *
16   *       http://www.apache.org/licenses/LICENSE-2.0
17   *
18   *   Unless required by applicable law or agreed to in writing, software
19   *   distributed under the License is distributed on an "AS IS" BASIS,
20   *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21   *   See the License for the specific language governing permissions and
22   *   limitations under the License.
23   * ====================================================================
24   */
25  
26  /**
27   * Bootstrap download progress meter.
28   *
29   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
30   * @version $Id: BootstrapDownloadMeter.java 517014 2007-03-11 21:15:50Z ltheussl $
31   */
32  public class BootstrapDownloadMeter
33      implements TransferListener
34  {
35      /** LOGGER for debug output */
36      private static final Log LOGGER = LogFactory.getLog( BootstrapDownloadMeter.class );
37  
38      private int shownSoFar = 0;
39  
40      private final int numHashes;
41  
42      private final char hashChar;
43  
44      private long complete = 0;
45  
46      public BootstrapDownloadMeter()
47      {
48          this( 20, '.' );
49      }
50  
51      public BootstrapDownloadMeter( int numHashes, char hashChar )
52      {
53          this.numHashes = numHashes;
54          this.hashChar = hashChar;
55      }
56  
57      public void transferInitiated( TransferEvent transferEvent )
58      {
59          // this space left intentionally blank
60      }
61  
62      public void transferStarted( TransferEvent transferEvent )
63      {
64          shownSoFar = 0;
65          complete = 0;
66      }
67  
68      public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
69      {
70          long total = transferEvent.getResource().getContentLength();
71  
72          complete += length;
73  
74          if ( total > 0 )
75          {
76              int numToShow = (int) ( ( complete * numHashes ) / total );
77              for ( int i = shownSoFar + 1; i <= numToShow; i++ )
78              {
79                  System.out.print( hashChar );
80              }
81              shownSoFar = numToShow;
82          }
83      }
84  
85      public void transferCompleted( TransferEvent transferEvent )
86      {
87          long total = transferEvent.getResource().getContentLength();
88          System.out.println( " (" + ( total / 1024 ) + "K)" );
89      }
90  
91      public void transferError( TransferEvent transferEvent )
92      {
93          LOGGER.error( transferEvent.getException().getMessage() );
94      }
95  
96      public void debug( String message )
97      {
98          LOGGER.debug( message );
99      }
100 }