Coverage Report - org.apache.maven.cli.ConsoleDownloadMonitor
 
Classes in this File Line Coverage Branch Coverage Complexity
ConsoleDownloadMonitor
90 %
36/40
83 %
15/18
2
 
 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.util.Iterator;
 23  
 import java.util.LinkedHashMap;
 24  
 import java.util.Map;
 25  
 
 26  
 import org.apache.maven.wagon.WagonConstants;
 27  
 import org.apache.maven.wagon.events.TransferEvent;
 28  
 import org.apache.maven.wagon.resource.Resource;
 29  
 import org.codehaus.plexus.logging.Logger;
 30  
 
 31  
 /**
 32  
  * Console download progress meter. Properly handles multiple downloads simultaneously.
 33  
  * 
 34  
  * @author <a href="mailto:brett@apache.org">Brett Porter</a>
 35  
  * @version $Id: ConsoleDownloadMonitor.java 747773 2009-02-25 13:35:29Z brett $
 36  
  */
 37  
 public class ConsoleDownloadMonitor
 38  
     extends AbstractConsoleDownloadMonitor
 39  
 {
 40  
     private Map/* <Resource,Integer> */downloads;
 41  
     private int maxLength;
 42  
 
 43  
     public ConsoleDownloadMonitor( Logger logger )
 44  
     {
 45  0
         super( logger );
 46  
 
 47  0
         downloads = new LinkedHashMap();
 48  0
     }
 49  
 
 50  
     public ConsoleDownloadMonitor()
 51  8
     {
 52  8
         downloads = new LinkedHashMap();
 53  8
     }
 54  
 
 55  
     public void transferInitiated( TransferEvent transferEvent )
 56  
     {
 57  1
         String message = transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "Uploading" : "Downloading";
 58  
 
 59  1
         String url = transferEvent.getWagon().getRepository().getUrl();
 60  
 
 61  1
         out.println( message + ": " + url + "/" + transferEvent.getResource().getName() );
 62  
 
 63  1
     }
 64  
 
 65  
     public void transferStarted( TransferEvent transferEvent )
 66  
     {
 67  
         // This space left intentionally blank
 68  1
     }
 69  
 
 70  
     public synchronized void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
 71  
     {
 72  5
         Resource resource = transferEvent.getResource();
 73  5
         if ( !downloads.containsKey( resource ) )
 74  
         {
 75  3
             downloads.put( resource, new Long( length ) );
 76  
         }
 77  
         else
 78  
         {
 79  2
             Long complete = (Long) downloads.get( resource );
 80  2
             complete = new Long( complete.longValue() + length );
 81  2
             downloads.put( resource, complete );
 82  
         }
 83  
 
 84  5
         StringBuffer buf = new StringBuffer();
 85  5
         for ( Iterator i = downloads.entrySet().iterator(); i.hasNext(); )
 86  
         {
 87  8
             Map.Entry entry = (Map.Entry) i.next();
 88  8
             Long complete = (Long) entry.getValue();
 89  8
             String status =
 90  
                 getDownloadStatusForResource( complete.longValue(), ( (Resource) entry.getKey() ).getContentLength() );
 91  8
             buf.append( status );
 92  8
             if ( i.hasNext() )
 93  
             {
 94  3
                 buf.append( " " );
 95  
             }
 96  8
         }
 97  
         
 98  5
         if ( buf.length() > maxLength )
 99  
         {
 100  3
             maxLength = buf.length();
 101  
         }
 102  
         
 103  5
         out.print( buf.toString() + "\r" );
 104  5
     }
 105  
 
 106  
     String getDownloadStatusForResource( long progress, long total )
 107  
     {
 108  13
         if ( total >= 1024 )
 109  
         {
 110  10
             return ( progress / 1024 ) + "/" + ( total == WagonConstants.UNKNOWN_LENGTH ? "?" : ( total / 1024 ) + "K" );
 111  
         }
 112  
         else
 113  
         {
 114  3
             return progress + "/" + ( total == WagonConstants.UNKNOWN_LENGTH ? "?" : total + "b" );
 115  
         }
 116  
     }
 117  
 
 118  
     public synchronized void transferCompleted( TransferEvent transferEvent )
 119  
     {
 120  1
         StringBuffer line = new StringBuffer( createCompletionLine( transferEvent ) );
 121  
         
 122  1
         while ( line.length() < maxLength )
 123  
         {
 124  0
             line.append( " " );
 125  
         }
 126  1
         maxLength = 0;
 127  
         
 128  1
         out.println( line );
 129  1
         downloads.remove( transferEvent.getResource() );
 130  1
     }
 131  
 }