added synchroniztion for ArrayLists: agents and domains

This commit is contained in:
Bill Tantzen
2014-12-11 12:39:33 -06:00
parent 845532b29d
commit c70f99895c

View File

@@ -15,6 +15,7 @@ import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.Collections;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import org.dspace.core.ConfigurationManager; import org.dspace.core.ConfigurationManager;
@@ -42,10 +43,10 @@ public class SpiderDetector {
private static IPTable table = null; private static IPTable table = null;
/** Collection of regular expressions to match known spiders' agents. */ /** Collection of regular expressions to match known spiders' agents. */
private static List<Pattern> agents = new ArrayList<Pattern>(); private static List<Pattern> agents = Collections.synchronizedList(new ArrayList<Pattern>());
/** Collection of regular expressions to match known spiders' domain names. */ /** Collection of regular expressions to match known spiders' domain names. */
private static List<Pattern> domains = new ArrayList<Pattern>(); private static List<Pattern> domains = Collections.synchronizedList(new ArrayList<Pattern>());
/** /**
* Utility method which reads lines from a file & returns them in a Set. * Utility method which reads lines from a file & returns them in a Set.
@@ -199,13 +200,15 @@ public class SpiderDetector {
{ {
// See if any agent patterns match // See if any agent patterns match
if (null != agent) if (null != agent)
{ {
if (agents.isEmpty()) synchronized(agents)
loadPatterns("agents", agents); {
if (agents.isEmpty())
loadPatterns("agents", agents);
}
for (Pattern candidate : agents) for (Pattern candidate : agents)
{ {
// prevent matcher() invocation from a null Pattern object // prevent matcher() invocation from a null Pattern object
if (null != candidate && candidate.matcher(agent).find()) if (null != candidate && candidate.matcher(agent).find())
{ {
return true; return true;
@@ -230,15 +233,16 @@ public class SpiderDetector {
// No. See if any DNS names match // No. See if any DNS names match
if (null != hostname) if (null != hostname)
{ {
synchronized(domains) {
if (domains.isEmpty()) if (domains.isEmpty())
{ {
loadPatterns("domains", domains); loadPatterns("domains", domains);
}
} }
for (Pattern candidate : domains) for (Pattern candidate : domains)
{ {
// prevent matcher() invocation from a null Pattern object // prevent matcher() invocation from a null Pattern object
if (null != candidate && candidate.matcher(hostname).find()) if (null != candidate && candidate.matcher(hostname).find())
{ {
return true; return true;
} }