ConsoleProgressTransferListener
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import jline.ConsoleReader;
import jline.Terminal;
import org.apache.commons.lang.StringUtils;
import org.apache.maven.wagon.Wagon;
import org.apache.maven.wagon.events.TransferEvent;
import org.apache.maven.wagon.events.TransferListener;
public class ConsoleProgressTransferListener implements TransferListener
{
public void startUpdateTimer(long interval) {
if (consoleRefreshTimer != null) {
// Cancel previous
consoleRefreshTimer.cancel();
}
consoleRefreshTimer = new Timer("Console update timer");
TimerTask updateConsole = new TimerTask() {
@Override
public void run() {
updateProgress();
}
};
consoleRefreshTimer.schedule(updateConsole, 0, interval);
}
public void stopUpdateTimer() {
consoleRefreshTimer.cancel();
}
public Map<String, Long> getDonloadTimes() {
return donloadTimes;
}
/**
* <b>Logging in this class is discouraged</b>
*/
public ConsoleProgressTransferListener()
{
try {
terminal = Terminal.setupTerminal();
// Enable echo to allow Ctrl-C
terminal.enableEcho();
reader = new ConsoleReader();
terminal.beforeReadLine(reader, "", (char) 0);
}
catch (Exception e) {
e.printStackTrace();
terminal = null;
}
}
private class TransferProgress
{
public TransferProgress(String repoName)
{
this.repoName = repoName;
this.completed = 0;
this.startTime = 0;
this.contentLength = 0;
}
protected String repoName;
// max@2GB
protected int contentLength;
protected int completed;
protected long startTime;
}
public TransferListener addRepo(String repoName) {
transfers.put(repoName, new TransferProgress(repoName));
return this;
}
public void debug(String arg0) {
// TODO Auto-generated method stub
}
public void transferCompleted(TransferEvent event) {
TransferProgress transferProgress = getTransfer(event);
donloadTimes.put(transferProgress.repoName + ": " + event.getResource().getName(), System.currentTimeMillis()
- transferProgress.startTime);
// Reset transferProgress state
transferProgress = new TransferProgress(transferProgress.repoName);
}
public void transferError(TransferEvent arg0) {
}
public void transferInitiated(TransferEvent arg0) {
}
public void transferProgress(TransferEvent event, byte[] currentBuffer, int completed) {
TransferProgress transferProgress = getTransfer(event);
transferProgress.completed += completed;
}
public void transferStarted(TransferEvent event) {
TransferProgress transferProgress = getTransfer(event);
long contentLength = event.getResource().getContentLength();
transferProgress.completed = 0;
transferProgress.contentLength = (int) contentLength;
transferProgress.startTime = System.currentTimeMillis();
}
public void updateProgress() {
StringBuilder line = new StringBuilder();
for (TransferProgress transfer : transfers.values()) {
int percentageDone = 0;
int progress = 0;
String progressBar;
if (transfer.contentLength != 0) {
percentageDone = (transfer.completed * 100) / transfer.contentLength;
progress = (transfer.completed * DIVISION) / transfer.contentLength;
if (transfer.completed < transfer.contentLength) {
// Add progressbar only when something is happening
progressBar = StringUtils.repeat("=", progress) + StringUtils.repeat(" ", DIVISION - progress);
line.append(transfer.repoName).append(": ");
String percent = String.format("%03d/%s {", percentageDone, 100);
line.append(percent);
line.append(progressBar);
line.append("} ");
}
}
}
// int w = reader.getTermwidth();
try {
if (line.length() < previousLineLength) {
// If some previous update line has been longer then clear the rest
line.append(StringUtils.repeat(" ", previousLineLength - line.length()));
}
previousLineLength = line.length();
reader.getCursorBuffer().clearBuffer();
reader.getCursorBuffer().write(line.toString());
reader.setCursorPosition(previousLineLength);
reader.redrawLine();
}
catch (IOException e) {
e.printStackTrace();
}
}
private TransferProgress getTransfer(TransferEvent event) {
Wagon wagon = event.getWagon();
String repoId = wagon.getRepository().getId();
return transfers.get(repoId);
}
private Terminal terminal;
private ConsoleReader reader;
// Previous line length used
private int previousLineLength;
private static final int DIVISION = 20;
private Map<String, Long> donloadTimes = new HashMap<String, Long>();
private Map<String, TransferProgress> transfers = new HashMap<String, TransferProgress>();
private Timer consoleRefreshTimer;
}
Like this:
Be the first to like this page.