You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@aries.apache.org by rm...@apache.org on 2020/10/30 10:20:50 UTC

[aries-cdi] 01/03: [ARIES-2018] don't create document builder and xpath if no beans.xml is seen

This is an automated email from the ASF dual-hosted git repository.

rmannibucau pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/aries-cdi.git

commit 08ebf6c474f1ec1d99fd8bed38b461c8b7806b0b
Author: Romain Manni-Bucau <rm...@gmail.com>
AuthorDate: Fri Oct 30 09:40:31 2020 +0100

    [ARIES-2018] don't create document builder and xpath if no beans.xml is seen
---
 .../container/internal/container/Discovery.java    | 37 ++++++++++++----------
 1 file changed, 20 insertions(+), 17 deletions(-)

diff --git a/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/Discovery.java b/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/Discovery.java
index 96e68ce..709d46b 100644
--- a/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/Discovery.java
+++ b/cdi-extender/src/main/java/org/apache/aries/cdi/container/internal/container/Discovery.java
@@ -88,21 +88,23 @@ public class Discovery {
 
 	private static final List<Type> BIND_TYPES = Arrays.asList(BindService.class, BindBeanServiceObjects.class, BindServiceReference.class);
 
-	static final DocumentBuilderFactory	dbf	= DocumentBuilderFactory.newInstance();
-	static final XPathFactory			xpf	= XPathFactory.newInstance();
-	static final XPathExpression		trimExpression;
-	static final XPathExpression		excludeExpression;
+	private static final class LazyXml { // when not needed, don't create that
+		static final DocumentBuilderFactory	dbf	= DocumentBuilderFactory.newInstance();
+		static final XPathFactory			xpf	= XPathFactory.newInstance();
+		static final XPathExpression		trimExpression;
+		static final XPathExpression		excludeExpression;
 
-	static {
-		try {
-			dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
-			dbf.setXIncludeAware(false);
-			dbf.setExpandEntityReferences(false);
-			XPath xPath = xpf.newXPath();
-			trimExpression = xPath.compile("boolean(/beans/trim)");
-			excludeExpression = xPath.compile("/beans/scan/exclude");
-		} catch (Throwable t) {
-			throw Exceptions.duck(t);
+		static {
+			try {
+				dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
+				dbf.setXIncludeAware(false);
+				dbf.setExpandEntityReferences(false);
+				XPath xPath = xpf.newXPath();
+				trimExpression = xPath.compile("boolean(/beans/trim)");
+				excludeExpression = xPath.compile("/beans/scan/exclude");
+			} catch (Throwable t) {
+				throw Exceptions.duck(t);
+			}
 		}
 	}
 
@@ -460,7 +462,7 @@ public class Discovery {
 
 	boolean checkTrim(Document document) {
 		try {
-			return Boolean.class.cast(trimExpression.evaluate(document, XPathConstants.BOOLEAN));
+			return Boolean.class.cast(LazyXml.trimExpression.evaluate(document, XPathConstants.BOOLEAN));
 		} catch (XPathExpressionException e) {
 			throw Exceptions.duck(e);
 		}
@@ -470,7 +472,8 @@ public class Discovery {
 		try {
 			List<Exclude> excludes = new ArrayList<>();
 
-			NodeList excludeNodes = NodeList.class.cast(excludeExpression.evaluate(document, XPathConstants.NODESET));
+			NodeList excludeNodes = NodeList.class.cast(
+					LazyXml.excludeExpression.evaluate(document, XPathConstants.NODESET));
 
 			for (int i = 0; i < excludeNodes.getLength(); i++) {
 				Element excludeElement = (Element)excludeNodes.item(i);
@@ -487,7 +490,7 @@ public class Discovery {
 
 	Document readXMLResource(URL resource) {
 		try {
-			DocumentBuilder db = dbf.newDocumentBuilder();
+			DocumentBuilder db = LazyXml.dbf.newDocumentBuilder();
 			try (InputStream is = resource.openStream()) {
 				return db.parse(is);
 			} catch (Throwable t) {