Coverage Report - org.apache.maven.cli.AbstractConsoleDownloadMonitor
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractConsoleDownloadMonitor
71 %
27/38
42 %
5/12
1,333
 
 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 org.apache.maven.wagon.WagonConstants;
 23  
 import org.apache.maven.wagon.events.TransferEvent;
 24  
 import org.apache.maven.wagon.events.TransferListener;
 25  
 import org.codehaus.plexus.logging.AbstractLogEnabled;
 26  
 import org.codehaus.plexus.logging.Logger;
 27  
 
 28  
 import java.io.PrintStream;
 29  
 
 30  
 /**
 31  
  * Abstract console download progress meter.
 32  
  * 
 33  
  * @author <a href="mailto:carlos@apache.org">Carlos Sanchez</a>
 34  
  * @version $Id: AbstractConsoleDownloadMonitor.java 747773 2009-02-25 13:35:29Z brett $
 35  
  * @since 2.0.5
 36  
  */
 37  
 public abstract class AbstractConsoleDownloadMonitor
 38  
     extends AbstractLogEnabled
 39  
     implements TransferListener
 40  
 {
 41  
     private Logger logger;
 42  
 
 43  14
     PrintStream out = System.out;
 44  
 
 45  
     public AbstractConsoleDownloadMonitor()
 46  14
     {
 47  14
     }
 48  
 
 49  
     public AbstractConsoleDownloadMonitor( Logger logger )
 50  0
     {
 51  0
         this.logger = logger;
 52  0
     }
 53  
 
 54  
     public void transferInitiated( TransferEvent transferEvent )
 55  
     {
 56  0
         String message = transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "Uploading" : "Downloading";
 57  
 
 58  0
         String url = transferEvent.getWagon().getRepository().getUrl();
 59  
 
 60  
         // TODO: can't use getLogger() because this isn't currently instantiated as a component
 61  0
         out.println( message + ": " + url + "/" + transferEvent.getResource().getName() );
 62  0
     }
 63  
 
 64  
     /**
 65  
      * Do nothing
 66  
      */
 67  
     public void transferStarted( TransferEvent transferEvent )
 68  
     {
 69  
         // This space left intentionally blank
 70  1
     }
 71  
 
 72  
     /**
 73  
      * Do nothing
 74  
      */
 75  
     public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
 76  
     {
 77  
         // This space left intentionally blank
 78  1
     }
 79  
 
 80  
     public void transferCompleted( TransferEvent transferEvent )
 81  
     {
 82  1
         String line = createCompletionLine( transferEvent );
 83  1
         out.println( line );
 84  1
     }
 85  
 
 86  
     protected String createCompletionLine( TransferEvent transferEvent )
 87  
     {
 88  
         String line;
 89  2
         long contentLength = transferEvent.getResource().getContentLength();
 90  2
         if ( contentLength != WagonConstants.UNKNOWN_LENGTH )
 91  
         {
 92  2
             StringBuffer buf = new StringBuffer();
 93  2
             String type = ( transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "uploaded" : "downloaded" );
 94  2
             buf.append( contentLength >= 1024 ? ( contentLength / 1024 ) + "K" : contentLength + "b" );
 95  2
             String name = transferEvent.getResource().getName();
 96  2
             name = name.substring( name.lastIndexOf( '/' ) + 1, name.length() );
 97  2
             buf.append( " " );
 98  2
             buf.append( type );
 99  2
             buf.append( "  (" );
 100  2
             buf.append( name );
 101  2
             buf.append( ")" );
 102  2
             line = buf.toString();
 103  2
         }
 104  
         else
 105  
         {
 106  0
             line = "";
 107  
         }
 108  2
         return line;
 109  
     }
 110  
 
 111  
     public void transferError( TransferEvent transferEvent )
 112  
     {
 113  
         // these errors should already be handled elsewhere by Maven since they all result in an exception from Wagon
 114  2
         if ( logger != null )
 115  
         {
 116  0
             Exception exception = transferEvent.getException();
 117  0
             logger.debug( exception.getMessage(), exception );
 118  
         }
 119  2
     }
 120  
 
 121  
     /**
 122  
      * Do nothing
 123  
      */
 124  
     public void debug( String message )
 125  
     {
 126  2
         if ( logger != null )
 127  
         {
 128  0
             logger.debug( message );
 129  
         }
 130  2
     }
 131  
 
 132  
 }