package net.sourceforge.javautil.classloader.util;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import net.sourceforge.javautil.classloader.resolver.ClassPackageContext;
import net.sourceforge.javautil.classloader.resolver.ClassPackageResolverNetworkNode;
import net.sourceforge.javautil.classloader.resolver.IClassPackageReference;
import net.sourceforge.javautil.classloader.resolver.ClassPackageContext.DownloadAuthorization;
import net.sourceforge.javautil.common.io.console.StandardConsole;
/**
* Simple console based authorizer.
*
* @author elponderador
* @author $Author$
* @version $Id$
*/
public class SimpleDownloadAuthorizer implements IClassPackageDownloadAuthorizer {
protected final int maximumAutoAttempts;
protected final Map<ClassPackageResolverNetworkNode, Integer> attempts = new HashMap<ClassPackageResolverNetworkNode, Integer>();
protected final StandardConsole console = new StandardConsole();
public SimpleDownloadAuthorizer(int maximumAutoAttempts) {
this.maximumAutoAttempts = maximumAutoAttempts;
}
@Override public boolean authorize(ClassPackageResolverNetworkNode... requiredPackages) {
for (ClassPackageResolverNetworkNode dl : requiredPackages) {
console.getPrintStream().println("Package: " + dl.getReference() + " from " + dl.getRemote().getRepositoryURL());
}
console.getPrintStream().println("Press any ENTER to authorize download of above packages, otherwise CTRL+C to abort");
try {
console.getReader().read();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
@Override public void downloadStarted(ClassPackageResolverNetworkNode download) {
System.out.println("Downloading " + download.getReference() + " from " + download.getRemote().getRepositoryURL());
}
@Override public boolean downloadError(ClassPackageResolverNetworkNode download, Throwable error) {
DownloadAuthorization da = ClassPackageContext.getDownloadAuthorization();
if (da == DownloadAuthorization.AUTOMATIC) {
Integer dat = attempts.get(download);
if (dat == null) dat = 0;
attempts.put(download, dat = dat + 1);
if (dat >= this.maximumAutoAttempts) return false;
} else {
console.getPrintStream().println("Download failed " + download.getReference() + " from " + download.getRemote().getRepositoryURL());
console.getPrintStream().println("Error: " + error.getMessage());
console.getPrintStream().println("Press any ENTER to try again, otherwise CTRL+C to abort");
try {
console.getReader().read();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
return true;
}
@Override public void downloadFinished(ClassPackageResolverNetworkNode download) {
System.out.println("Download finished: " + download.getReference() + " from " + download.getRemote().getRepositoryURL());
}
}