From 23e8d41651c2bebdf238ec2858dfc658c425acec Mon Sep 17 00:00:00 2001 From: Noah Levitt Date: Mon, 31 Mar 2014 13:40:29 -0700 Subject: [PATCH] avoid NPE; root cause was assumption that LoadingCache.asMap().get() would call CacheLoader.load() but it doesnt, see https://code.google.com/p/guava-libraries/wiki/MapMakerMigration --- .../modules/forms/FormLoginProcessor.java | 49 +++++++++++-------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/modules/src/main/java/org/archive/modules/forms/FormLoginProcessor.java b/modules/src/main/java/org/archive/modules/forms/FormLoginProcessor.java index f2292f77..d6aa637e 100644 --- a/modules/src/main/java/org/archive/modules/forms/FormLoginProcessor.java +++ b/modules/src/main/java/org/archive/modules/forms/FormLoginProcessor.java @@ -21,14 +21,11 @@ package org.archive.modules.forms; import static org.archive.modules.CoreAttributeConstants.A_WARC_RESPONSE_HEADERS; -import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.ExecutionException; import java.util.concurrent.atomic.AtomicLong; import java.util.logging.Level; import java.util.logging.Logger; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.CacheLoader; -import com.google.common.cache.LoadingCache; import org.apache.commons.httpclient.URIException; import org.apache.commons.lang.StringUtils; import org.archive.checkpointing.Checkpointable; @@ -45,6 +42,10 @@ import org.json.JSONException; import org.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.cache.LoadingCache; + /** * A step, post-ExtractorHTMLForms, where a followup CrawlURI to * attempt a form submission may be synthesized. @@ -119,24 +120,24 @@ public class FormLoginProcessor extends Processor implements Checkpointable { Logger.getLogger(FormLoginProcessor.class.getName()); // formProvince (String) -> count - ConcurrentMap eligibleFormsSeenCount = + protected LoadingCache eligibleFormsSeenCount = CacheBuilder.newBuilder() .build( new CacheLoader() { public AtomicLong load(String arg0) { return new AtomicLong(0L); } - }).asMap(); + }); // formProvince (String) -> count - ConcurrentMap eligibleFormsAttemptsCount = + protected LoadingCache eligibleFormsAttemptsCount = CacheBuilder.newBuilder() .build( new CacheLoader() { public AtomicLong load(String arg0) { return new AtomicLong(0L); } - }).asMap(); + }); /** * SURT prefix against which configured username/password is @@ -219,13 +220,17 @@ public class FormLoginProcessor extends Processor implements Checkpointable { for( Object formObject : curi.getDataList(ExtractorHTMLForms.A_HTML_FORM_OBJECTS)) { HTMLForm form = (HTMLForm) formObject; if(form.seemsLoginForm()) { - eligibleFormsSeenCount.get(formProvince).incrementAndGet(); - if(eligibleFormsAttemptsCount.get(formProvince).get()<1) { - eligibleFormsAttemptsCount.get(formProvince).incrementAndGet(); - createFormSubmissionAttempt(curi,form,formProvince); - } else { - // note decline-to-submit: in volume, may be signal of failed first login - curi.getAnnotations().add("nosubmit:"+submitStatusFor(formProvince)); + try { + eligibleFormsSeenCount.get(formProvince).incrementAndGet(); + if(eligibleFormsAttemptsCount.get(formProvince).get()<1) { + eligibleFormsAttemptsCount.get(formProvince).incrementAndGet(); + createFormSubmissionAttempt(curi,form,formProvince); + } else { + // note decline-to-submit: in volume, may be signal of failed first login + curi.getAnnotations().add("nosubmit:"+submitStatusFor(formProvince)); + } + } catch (ExecutionException e) { + throw new RuntimeException(e); // can't happen? } return; } @@ -278,9 +283,13 @@ public class FormLoginProcessor extends Processor implements Checkpointable { } protected String submitStatusFor(String formProvince) { - return eligibleFormsAttemptsCount.get(formProvince).get() - +","+eligibleFormsSeenCount.get(formProvince).get() - +","+formProvince; + try { + return eligibleFormsAttemptsCount.get(formProvince).get() + +","+eligibleFormsSeenCount.get(formProvince).get() + +","+formProvince; + } catch (ExecutionException e) { + throw new RuntimeException(e); + } } @Override @@ -295,10 +304,10 @@ public class FormLoginProcessor extends Processor implements Checkpointable { protected void fromCheckpointJson(JSONObject json) throws JSONException { super.fromCheckpointJson(json); JSONUtils.putAllAtomicLongs( - eligibleFormsAttemptsCount, + eligibleFormsAttemptsCount.asMap(), json.getJSONObject("eligibleFormsAttemptsCount")); JSONUtils.putAllAtomicLongs( - eligibleFormsSeenCount, + eligibleFormsSeenCount.asMap(), json.getJSONObject("eligibleFormsSeenCount")); } }