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.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          super( logger );
46  
47          downloads = new LinkedHashMap();
48      }
49  
50      public ConsoleDownloadMonitor()
51      {
52          downloads = new LinkedHashMap();
53      }
54  
55      public void transferInitiated( TransferEvent transferEvent )
56      {
57          String message = transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "Uploading" : "Downloading";
58  
59          String url = transferEvent.getWagon().getRepository().getUrl();
60  
61          out.println( message + ": " + url + "/" + transferEvent.getResource().getName() );
62  
63      }
64  
65      public void transferStarted( TransferEvent transferEvent )
66      {
67          // This space left intentionally blank
68      }
69  
70      public synchronized void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
71      {
72          Resource resource = transferEvent.getResource();
73          if ( !downloads.containsKey( resource ) )
74          {
75              downloads.put( resource, new Long( length ) );
76          }
77          else
78          {
79              Long complete = (Long) downloads.get( resource );
80              complete = new Long( complete.longValue() + length );
81              downloads.put( resource, complete );
82          }
83  
84          StringBuffer buf = new StringBuffer();
85          for ( Iterator i = downloads.entrySet().iterator(); i.hasNext(); )
86          {
87              Map.Entry entry = (Map.Entry) i.next();
88              Long complete = (Long) entry.getValue();
89              String status =
90                  getDownloadStatusForResource( complete.longValue(), ( (Resource) entry.getKey() ).getContentLength() );
91              buf.append( status );
92              if ( i.hasNext() )
93              {
94                  buf.append( " " );
95              }
96          }
97          
98          if ( buf.length() > maxLength )
99          {
100             maxLength = buf.length();
101         }
102         
103         out.print( buf.toString() + "\r" );
104     }
105 
106     String getDownloadStatusForResource( long progress, long total )
107     {
108         if ( total >= 1024 )
109         {
110             return ( progress / 1024 ) + "/" + ( total == WagonConstants.UNKNOWN_LENGTH ? "?" : ( total / 1024 ) + "K" );
111         }
112         else
113         {
114             return progress + "/" + ( total == WagonConstants.UNKNOWN_LENGTH ? "?" : total + "b" );
115         }
116     }
117 
118     public synchronized void transferCompleted( TransferEvent transferEvent )
119     {
120         StringBuffer line = new StringBuffer( createCompletionLine( transferEvent ) );
121         
122         while ( line.length() < maxLength )
123         {
124             line.append( " " );
125         }
126         maxLength = 0;
127         
128         out.println( line );
129         downloads.remove( transferEvent.getResource() );
130     }
131 }