Skip to content

Commit

Permalink
LDEV-5118 - cache builder class loading and check if the class can be…
Browse files Browse the repository at this point in the history
… loaded
  • Loading branch information
michaeloffner committed Oct 25, 2024
1 parent eb2c46d commit dfe8113
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
69 changes: 41 additions & 28 deletions core/src/main/java/lucee/runtime/text/xml/XMLUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<DocumentBuilderFactory> dbf;
private static boolean initDbf = false;

private static URL documentBuilderFactoryResource;

private static URL saxParserFactoryResource;

public static String unescapeXMLString(String str) {

StringBuffer rtn = new StringBuffer();
Expand Down Expand Up @@ -424,34 +431,6 @@ else if (obj2 != null) {
return factory;
}

private static Class<DocumentBuilderFactory> dbf;

private static URL documentBuilderFactoryResource;

private static URL saxParserFactoryResource;

private static Class<DocumentBuilderFactory> _newDocumentBuilderFactoryClass() {
if (dbf == null) {
Thread.currentThread().setContextClassLoader(EnvClassLoader.getInstance((ConfigPro) ThreadLocalPageContext.getConfig()));
Class<DocumentBuilderFactory> 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);
Expand Down Expand Up @@ -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<DocumentBuilderFactory> _newDocumentBuilderFactoryClass() {
if (!initDbf) {
synchronized (SystemUtil.createToken("XMLUtil", "newDocumentBuilderFactoryClass")) {
if (!initDbf) {

Thread.currentThread().setContextClassLoader(EnvClassLoader.getInstance((ConfigPro) ThreadLocalPageContext.getConfig()));
Class<DocumentBuilderFactory> 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()));
Expand Down
2 changes: 1 addition & 1 deletion loader/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project default="core" basedir="." name="Lucee"
xmlns:resolver="antlib:org.apache.maven.resolver.ant">

<property name="version" value="6.2.0.133-SNAPSHOT"/>
<property name="version" value="6.2.0.134-SNAPSHOT"/>

<taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml">
<classpath>
Expand Down
2 changes: 1 addition & 1 deletion loader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.lucee</groupId>
<artifactId>lucee</artifactId>
<version>6.2.0.133-SNAPSHOT</version>
<version>6.2.0.134-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down

0 comments on commit dfe8113

Please sign in to comment.