KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > maven > cli > ConsoleDownloadMonitor


1 package org.apache.maven.cli;
2
3 /*
4  * Copyright 2001-2005 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import org.apache.maven.wagon.WagonConstants;
20 import org.apache.maven.wagon.events.TransferEvent;
21 import org.apache.maven.wagon.events.TransferListener;
22 import org.codehaus.plexus.logging.AbstractLogEnabled;
23
24 /**
25  * Console download progress meter.
26  *
27  * @author <a HREF="mailto:brett@apache.org">Brett Porter</a>
28  * @version $Id: ConsoleDownloadMonitor.java 191492 2005-06-20 15:21:50Z brett $
29  */

30 public class ConsoleDownloadMonitor
31     extends AbstractLogEnabled
32     implements TransferListener
33 {
34     private long complete;
35
36     public void transferInitiated( TransferEvent transferEvent )
37     {
38         String JavaDoc message = transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "Uploading" : "Downloading";
39
40         String JavaDoc url = transferEvent.getWagon().getRepository().getUrl();
41
42         // TODO: can't use getLogger() because this isn't currently instantiated as a component
43
System.out.println( message + ": " + url + "/" + transferEvent.getResource().getName() );
44
45         complete = 0;
46     }
47
48     public void transferStarted( TransferEvent transferEvent )
49     {
50         // This space left intentionally blank
51
}
52
53     public void transferProgress( TransferEvent transferEvent, byte[] buffer, int length )
54     {
55         long total = transferEvent.getResource().getContentLength();
56         complete += length;
57         // TODO [BP]: Sys.out may no longer be appropriate, but will \r work with getLogger()?
58
if ( total >= 1024 )
59         {
60             System.out.print(
61                 ( complete / 1024 ) + "/" + ( total == WagonConstants.UNKNOWN_LENGTH ? "?" : ( total / 1024 ) + "K" ) +
62                     "\r" );
63         }
64         else
65         {
66             System.out.print( complete + "/" + ( total == WagonConstants.UNKNOWN_LENGTH ? "?" : total + "b" ) + "\r" );
67         }
68     }
69
70     public void transferCompleted( TransferEvent transferEvent )
71     {
72         long contentLength = transferEvent.getResource().getContentLength();
73         if ( contentLength != WagonConstants.UNKNOWN_LENGTH )
74         {
75             String JavaDoc type = ( transferEvent.getRequestType() == TransferEvent.REQUEST_PUT ? "uploaded" : "downloaded" );
76             String JavaDoc l = contentLength >= 1024 ? ( contentLength / 1024 ) + "K" : contentLength + "b";
77             System.out.println( l + " " + type );
78         }
79     }
80
81     public void transferError( TransferEvent transferEvent )
82     {
83         // TODO: can't use getLogger() because this isn't currently instantiated as a component
84
transferEvent.getException().printStackTrace();
85     }
86
87     public void debug( String JavaDoc message )
88     {
89         // TODO: can't use getLogger() because this isn't currently instantiated as a component
90
// getLogger().debug( message );
91
}
92 }
93
94
95
96
Popular Tags