Skip to content

Commit 544c11a

Browse files
committed
[nobug] Change synchronization setup to reduce chance of blocking UI during validation
1 parent 8f6074f commit 544c11a

1 file changed

Lines changed: 13 additions & 7 deletions

File tree

  • xml/bundles/org.eclipse.wst.xml.core/src-validation/org/eclipse/wst/xml/core/internal/validation

xml/bundles/org.eclipse.wst.xml.core/src-validation/org/eclipse/wst/xml/core/internal/validation/ValidatorHelper.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2001, 2017 IBM Corporation and others.
2+
* Copyright (c) 2001, 2023 IBM Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License 2.0
55
* which accompanies this distribution, and is available at
@@ -17,6 +17,7 @@
1717
import java.io.InputStream;
1818
import java.io.Reader;
1919
import java.net.URL;
20+
import java.net.URLConnection;
2021
import java.time.Duration;
2122
import java.util.HashMap;
2223
import java.util.Map;
@@ -48,6 +49,7 @@
4849
*/
4950
public class ValidatorHelper
5051
{
52+
private static final int TWO_MINUTES = (int) Duration.ofMinutes(2).toMillis();
5153
public boolean isGrammarEncountered = false;
5254
public boolean isDTDEncountered = false;
5355
public boolean isNamespaceEncountered = false;
@@ -388,10 +390,12 @@ protected static String replace(String string, String oldPattern, String newPatt
388390
return string;
389391
}
390392

391-
private static boolean checkGrammarImpl(String location) {
393+
private static boolean doCheckGrammar(String location) {
392394
try {
393395
URL url = new URL(location);
394-
try (InputStream is = url.openStream()) {
396+
URLConnection connection = url.openConnection();
397+
connection.setConnectTimeout(TWO_MINUTES);
398+
try (InputStream is = connection.getInputStream()) {
395399
return true;
396400
}
397401
} catch (Exception e) {
@@ -400,11 +404,13 @@ private static boolean checkGrammarImpl(String location) {
400404
return false;
401405
}
402406

403-
private synchronized static boolean checkGrammar(String location) {
407+
private static boolean checkGrammar(String location) {
404408
GrammarEncounteredResult result = grammarCheckerCache.get(location);
405-
if (result == null || (System.currentTimeMillis() - result.timestamp) > Duration.ofMinutes(10).toMillis()) {
406-
result = new GrammarEncounteredResult(checkGrammarImpl(location));
407-
grammarCheckerCache.put(location, result);
409+
if (result == null || (System.currentTimeMillis() - result.timestamp) > TWO_MINUTES) {
410+
result = new GrammarEncounteredResult(doCheckGrammar(location));
411+
synchronized (grammarCheckerCache) {
412+
grammarCheckerCache.put(location, result);
413+
}
408414
}
409415
return result.result;
410416
}

0 commit comments

Comments
 (0)