1 4 package net.sf.mybatchfwk.test.websitedownloader; 5 6 import java.io.File ; 7 import java.net.MalformedURLException ; 8 import java.net.URL ; 9 import java.util.Date ; 10 import java.util.List ; 11 import java.util.Vector ; 12 13 import net.sf.mybatchfwk.BatchConfiguration; 14 import net.sf.mybatchfwk.BatchException; 15 import net.sf.mybatchfwk.ExecutionReport; 16 import net.sf.mybatchfwk.IBatch; 17 import net.sf.mybatchfwk.ITask; 18 import net.sf.mybatchfwk.ITaskExecutor; 19 20 import org.apache.commons.logging.Log; 21 import org.apache.commons.logging.LogFactory; 22 23 public class WebSiteDownloader implements IBatch { 24 25 private static final Log log = LogFactory.getLog(WebSiteDownloader.class); 26 27 private URL webSiteUrl; 28 private File downloadFolder; 29 private List <String > links; 30 private BatchConfiguration config; 31 32 public WebSiteDownloader(URL webSiteUrl, File downloadFolder) { 33 this.webSiteUrl = webSiteUrl; 34 this.downloadFolder = downloadFolder; 35 36 this.links = new Vector <String >(); 37 this.links.add("/index.html"); 38 } 39 40 public void init(BatchConfiguration configuration) throws BatchException { 41 this.config = configuration; 43 } 44 45 public void execute(ITaskExecutor executor) throws BatchException { 46 Date d1 = new Date (); 48 do { 49 while ((!links.isEmpty()) && ((System.currentTimeMillis()-d1.getTime()) < 3000)) { 50 try { 51 URL url = new URL (webSiteUrl.getProtocol(), webSiteUrl.getHost(), webSiteUrl.getPort(), links.remove(0)); 52 executor.execute(new DownloadTask(url, downloadFolder)); 53 } catch (MalformedURLException e) { 54 log.error(null, e); 55 } 56 } 57 try { 58 Thread.sleep(1000); 59 } catch (InterruptedException e) { 60 throw new BatchException(e); 61 } 62 } while (((executor.getTaskCount() > 0) || (!links.isEmpty())) && ((System.currentTimeMillis()-d1.getTime()) < 3000)); 63 } 64 65 public void pause() throws BatchException { 66 log.info("###> [pause]"); 67 } 68 69 public void resume() throws BatchException { 70 log.info("###> [resume]"); 71 } 72 73 public void end(ExecutionReport report) throws BatchException { 74 log.info("###> [end]"); 75 System.out.println("#completed: " + report.getNumberOfCompletedTasks()); 76 System.out.println("#failed: " + report.getNumberOfFailedTasks()); 77 System.out.println("#execution time: " + ((report.getEndDate().getTime()-report.getBeginDate().getTime())/1000) + "s"); 78 System.out.println("#downloaded: " + getFileSize(new File ("c:\\test")) + "b"); 79 } 80 81 public void endOfExecution(ITask task, Throwable exception) { 82 if (exception == null) { 83 DownloadTask downloadTask = (DownloadTask) task; 84 String [] relativeLinks = downloadTask.getLinks(); 85 for (int i = 0; i < relativeLinks.length; i++) { 86 String link = relativeLinks[i]; 87 if (!links.contains(link)) { 88 links.add(link); 89 } 90 } 91 } else { 92 log.error(null, exception); 93 } 94 } 95 96 public void manageFatalError(ExecutionReport report, Throwable throwable) { 97 log.info("###> [manageFatalError]"); 98 log.fatal(null, throwable); 99 } 100 101 private static long getFileSize(File folder) { 102 long foldersize = 0; 103 104 File [] filelist = folder.listFiles(); 105 for (int i = 0; i < filelist.length; i++) { 106 if (filelist[i].isDirectory()) { 107 foldersize += getFileSize(filelist[i]); 108 } else { 109 foldersize += filelist[i].length(); 110 } 111 } 112 113 return foldersize; 114 } 115 } 116 | Popular Tags |