1 23 package org.archive.crawler.selftest; 24 25 import java.io.File ; 26 import java.io.FileNotFoundException ; 27 import java.io.IOException ; 28 import java.util.logging.Logger ; 29 30 import javax.management.Attribute ; 31 import javax.management.AttributeNotFoundException ; 32 import javax.management.InvalidAttributeValueException ; 33 import javax.management.MBeanException ; 34 import javax.management.ReflectionException ; 35 36 import org.archive.crawler.admin.CrawlJob.MBeanCrawlController; 37 import org.archive.crawler.datamodel.Checkpoint; 38 import org.archive.crawler.datamodel.CrawlOrder; 39 import org.archive.crawler.datamodel.CrawlURI; 40 import org.archive.crawler.event.CrawlStatusListener; 41 import org.archive.crawler.event.CrawlURIDispositionListener; 42 import org.archive.crawler.framework.Checkpointer; 43 import org.archive.crawler.framework.CrawlController; 44 import org.archive.crawler.framework.exceptions.InitializationException; 45 import org.archive.crawler.settings.XMLSettingsHandler; 46 import org.archive.crawler.util.CheckpointUtils; 47 48 49 54 public class CheckpointSelfTest extends SelfTestCase 55 implements CrawlStatusListener, CrawlURIDispositionListener { 56 private final Logger LOG = Logger.getLogger(this.getClass().getName()); 57 private boolean crawlEnded = false; 58 59 public CheckpointSelfTest() { 60 } 62 63 public CheckpointSelfTest(String testName) { 64 super(testName); 65 } 67 68 79 public void testCheckpointRecover() 80 throws InitializationException, IOException , 81 InvalidAttributeValueException , AttributeNotFoundException , 82 MBeanException , ReflectionException , ClassNotFoundException , 83 InterruptedException { 84 File f = getFile(getCrawlJobDir(), "checkpoints"); 86 File cpdir = getFile(f, Checkpointer.formatCheckpointName("", 1)); 88 getFile(cpdir, Checkpoint.VALIDITY_STAMP_FILENAME); 90 File order = getFile(cpdir, "order.xml"); 92 XMLSettingsHandler handler = 93 new XMLSettingsHandler(order); 94 handler.initialize(); 95 handler.getOrder().setAttribute( 97 new Attribute (CrawlOrder.ATTR_RECOVER_PATH, cpdir.toString())); 98 Checkpoint cp = 99 CrawlController.getCheckpointRecover(handler.getOrder()); 100 if (cp == null) { 101 throw new NullPointerException ("Failed read of checkpoint object"); 102 } 103 CrawlController c = (MBeanCrawlController)CheckpointUtils. 104 readObjectFromFile(MBeanCrawlController.class, cpdir); 105 c.initialize(handler); 106 c.addCrawlStatusListener(this); 107 c.addCrawlURIDispositionListener(this); 108 c.requestCrawlStart(); 109 LOG.info("Recover from selftest crawl started using " + 110 order.toString() + "."); 111 while(!this.crawlEnded) { 113 LOG.info("Waiting on recovered crawl to finish"); 114 Thread.sleep(1000); 115 } 116 } 117 118 private File getFile(final File parent, final String name) 119 throws IOException { 120 File f = new File (parent, name); 121 if (!f.exists()) { 122 throw new FileNotFoundException (f.getAbsolutePath()); 123 } 124 if (!f.canRead()) { 125 throw new IOException ("Can't read " + f.getAbsolutePath()); 126 } 127 return f; 128 } 129 130 public void crawlCheckpoint(File checkpointDir) throws Exception { 131 133 } 134 135 public void crawlEnded(String sExitMessage) { 136 this.crawlEnded = true; 137 } 138 139 public void crawlEnding(String sExitMessage) { 140 142 } 143 144 public void crawlPaused(String statusMessage) { 145 147 } 148 149 public void crawlPausing(String statusMessage) { 150 152 } 153 154 public void crawlResuming(String statusMessage) { 155 157 } 158 159 public void crawlStarted(String message) { 160 162 } 163 164 public void crawledURIDisregard(CrawlURI curi) { 165 167 } 168 169 public void crawledURIFailure(CrawlURI curi) { 170 172 } 173 174 public void crawledURINeedRetry(CrawlURI curi) { 175 177 } 178 179 public void crawledURISuccessful(CrawlURI curi) { 180 LOG.info(curi.toString()); 181 } 182 } | Popular Tags |