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 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
32
33
34
35
36
37 public abstract class AbstractConsoleDownloadMonitor
38 extends AbstractLogEnabled
39 implements TransferListener
40 {
41 private Logger logger;
42
43 PrintStream out = System.out;
44
45 public AbstractConsoleDownloadMonitor()
46 {
47 }
48
49 public AbstractConsoleDownloadMonitor( Logger logger )
50 {
51 this.logger = logger;
52 }
53
54 public void transferInitiated( TransferEvent transferEvent )
55 {
56 String message = transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "Uploading" : "Downloading";
57
58 String url = transferEvent.getWagon().getRepository().getUrl();
59
60
61 out.println( message + ": " + url + "/" + transferEvent.getResource().getName() );
62 }
63
64
65
66
67 public void transferStarted( TransferEvent transferEvent )
68 {
69
70 }
71
72
73
74
75 public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
76 {
77
78 }
79
80 public void transferCompleted( TransferEvent transferEvent )
81 {
82 String line = createCompletionLine( transferEvent );
83 out.println( line );
84 }
85
86 protected String createCompletionLine( TransferEvent transferEvent )
87 {
88 String line;
89 long contentLength = transferEvent.getResource().getContentLength();
90 if ( contentLength != WagonConstants.UNKNOWN_LENGTH )
91 {
92 StringBuffer buf = new StringBuffer();
93 String type = ( transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "uploaded" : "downloaded" );
94 buf.append( contentLength >= 1024 ? ( contentLength / 1024 ) + "K" : contentLength + "b" );
95 String name = transferEvent.getResource().getName();
96 name = name.substring( name.lastIndexOf( '/' ) + 1, name.length() );
97 buf.append( " " );
98 buf.append( type );
99 buf.append( " (" );
100 buf.append( name );
101 buf.append( ")" );
102 line = buf.toString();
103 }
104 else
105 {
106 line = "";
107 }
108 return line;
109 }
110
111 public void transferError( TransferEvent transferEvent )
112 {
113
114 if ( logger != null )
115 {
116 Exception exception = transferEvent.getException();
117 logger.debug( exception.getMessage(), exception );
118 }
119 }
120
121
122
123
124 public void debug( String message )
125 {
126 if ( logger != null )
127 {
128 logger.debug( message );
129 }
130 }
131
132 }