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
1717import java .io .InputStream ;
1818import java .io .Reader ;
1919import java .net .URL ;
20+ import java .net .URLConnection ;
2021import java .time .Duration ;
2122import java .util .HashMap ;
2223import java .util .Map ;
4849 */
4950public 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