From dfe8113f3f35cd4ee3d0173bc7d1b269ec82b716 Mon Sep 17 00:00:00 2001 From: michaeloffner Date: Fri, 25 Oct 2024 15:09:54 +0200 Subject: [PATCH] LDEV-5118 - cache builder class loading and check if the class can be loaded --- .../java/lucee/runtime/text/xml/XMLUtil.java | 69 +++++++++++-------- loader/build.xml | 2 +- loader/pom.xml | 2 +- 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/core/src/main/java/lucee/runtime/text/xml/XMLUtil.java b/core/src/main/java/lucee/runtime/text/xml/XMLUtil.java index 190cca4349..107014f12f 100755 --- a/core/src/main/java/lucee/runtime/text/xml/XMLUtil.java +++ b/core/src/main/java/lucee/runtime/text/xml/XMLUtil.java @@ -144,6 +144,13 @@ public final class XMLUtil { private static boolean disableXmlFeatureOverride = Caster.toBooleanValue(SystemUtil.getSystemPropOrEnvVar("lucee.xmlfeatures.override.disable", "false"), false); + private static Class dbf; + private static boolean initDbf = false; + + private static URL documentBuilderFactoryResource; + + private static URL saxParserFactoryResource; + public static String unescapeXMLString(String str) { StringBuffer rtn = new StringBuffer(); @@ -424,34 +431,6 @@ else if (obj2 != null) { return factory; } - private static Class dbf; - - private static URL documentBuilderFactoryResource; - - private static URL saxParserFactoryResource; - - private static Class _newDocumentBuilderFactoryClass() { - if (dbf == null) { - Thread.currentThread().setContextClassLoader(EnvClassLoader.getInstance((ConfigPro) ThreadLocalPageContext.getConfig())); - Class clazz = null; - try { - clazz = ClassUtil.loadClass("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"); - } - catch (Exception e) { - try { - clazz = ClassUtil.loadClass("org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"); - } - catch (Exception ee) { - } - } - if (clazz != null) { - dbf = clazz; - LogUtil.log(Log.LEVEL_INFO, "application", "xml", clazz.getName() + " is used as DocumentBuilderFactory"); - } - } - return dbf; - } - public static String getXMLParserConfigurationName() { String value = "org.apache.xerces.parsers.XIncludeAwareParserConfiguration"; System.setProperty("org.apache.xerces.xni.parser.XMLParserConfiguration", value); @@ -483,12 +462,46 @@ private static DocumentBuilderFactory _newDocumentBuilderFactory() { factory = (DocumentBuilderFactory) ClassUtil.loadInstance(clazz); } catch (Exception e) { + LogUtil.log(Log.LEVEL_DEBUG, "application", "xml", ExceptionUtil.getStacktrace(e, true)); } } if (factory == null) factory = DocumentBuilderFactory.newInstance(); return factory; } + private static Class _newDocumentBuilderFactoryClass() { + if (!initDbf) { + synchronized (SystemUtil.createToken("XMLUtil", "newDocumentBuilderFactoryClass")) { + if (!initDbf) { + + Thread.currentThread().setContextClassLoader(EnvClassLoader.getInstance((ConfigPro) ThreadLocalPageContext.getConfig())); + Class clazz = null; + try { + Class c = ClassUtil.loadClass("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"); + ClassUtil.loadInstance(clazz); // make sure we can also load it + clazz = c; + } + catch (Exception e) { + try { + Class c = ClassUtil.loadClass("org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"); + ClassUtil.loadInstance(clazz); // make sure we can also load it + clazz = c; + } + catch (Exception ee) { + } + } + initDbf = true; // we only try once to load it, because this comes from the main classloader that will not change + // his mind + if (clazz != null) { + dbf = clazz; + LogUtil.log(Log.LEVEL_INFO, "application", "xml", clazz.getName() + " is used as DocumentBuilderFactory"); + } + } + } + } + return dbf; + } + private static SAXParserFactory newSAXParserFactory() { if (saxParserFactory == null) { Thread.currentThread().setContextClassLoader(EnvClassLoader.getInstance((ConfigPro) ThreadLocalPageContext.getConfig())); diff --git a/loader/build.xml b/loader/build.xml index e7b0a248a9..dcabbed69c 100644 --- a/loader/build.xml +++ b/loader/build.xml @@ -2,7 +2,7 @@ - + diff --git a/loader/pom.xml b/loader/pom.xml index 1898158314..4c79ceb9fd 100644 --- a/loader/pom.xml +++ b/loader/pom.xml @@ -3,7 +3,7 @@ org.lucee lucee - 6.2.0.133-SNAPSHOT + 6.2.0.134-SNAPSHOT jar Lucee Loader Build