1 | |
package org.apache.maven.cli; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
public class ConsoleDownloadMonitor |
38 | |
extends AbstractConsoleDownloadMonitor |
39 | |
{ |
40 | |
private Mapdownloads; |
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 | |
|
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 | |
} |