Skip to content

Commit

Permalink
[nobug] Change synchronization setup to reduce chance of blocking UI …
Browse files Browse the repository at this point in the history
…during validation
  • Loading branch information
nitind committed Aug 21, 2023
1 parent 8f6074f commit 544c11a
Showing 1 changed file with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2001, 2017 IBM Corporation and others.
* Copyright (c) 2001, 2023 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
Expand All @@ -17,6 +17,7 @@
import java.io.InputStream;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -48,6 +49,7 @@
*/
public class ValidatorHelper
{
private static final int TWO_MINUTES = (int) Duration.ofMinutes(2).toMillis();
public boolean isGrammarEncountered = false;
public boolean isDTDEncountered = false;
public boolean isNamespaceEncountered = false;
Expand Down Expand Up @@ -388,10 +390,12 @@ protected static String replace(String string, String oldPattern, String newPatt
return string;
}

private static boolean checkGrammarImpl(String location) {
private static boolean doCheckGrammar(String location) {
try {
URL url = new URL(location);
try (InputStream is = url.openStream()) {
URLConnection connection = url.openConnection();
connection.setConnectTimeout(TWO_MINUTES);
try (InputStream is = connection.getInputStream()) {
return true;
}
} catch (Exception e) {
Expand All @@ -400,11 +404,13 @@ private static boolean checkGrammarImpl(String location) {
return false;
}

private synchronized static boolean checkGrammar(String location) {
private static boolean checkGrammar(String location) {
GrammarEncounteredResult result = grammarCheckerCache.get(location);
if (result == null || (System.currentTimeMillis() - result.timestamp) > Duration.ofMinutes(10).toMillis()) {
result = new GrammarEncounteredResult(checkGrammarImpl(location));
grammarCheckerCache.put(location, result);
if (result == null || (System.currentTimeMillis() - result.timestamp) > TWO_MINUTES) {
result = new GrammarEncounteredResult(doCheckGrammar(location));
synchronized (grammarCheckerCache) {
grammarCheckerCache.put(location, result);
}
}
return result.result;
}
Expand Down

0 comments on commit 544c11a

Please sign in to comment.