Author: sseifert Date: Fri May 5 09:43:35 2017 New Revision: 1793994 URL: http://svn.apache.org/viewvc?rev=1793994&view=rev Log: SLING-6832 osgi-mock: Support parsing SCR metadata when multiple definition XML files exists with the same name
Modified: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java Modified: sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java URL: http://svn.apache.org/viewvc/sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java?rev=1793994&r1=1793993&r2=1793994&view=diff ============================================================================== --- sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java (original) +++ sling/trunk/testing/mocks/osgi-mock/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java Fri May 5 09:43:35 2017 @@ -20,7 +20,9 @@ package org.apache.sling.testing.mock.os import java.io.IOException; import java.io.InputStream; +import java.net.URL; import java.util.ArrayList; +import java.util.Enumeration; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -173,26 +175,35 @@ final class OsgiMetadataUtil { } private static void parseMetadataDocuments(Map<String,Document> cacheMap, String resourcePath, XPathExpression xpathExpression) { - try (InputStream fileStream = OsgiMetadataUtil.class.getClassLoader().getResourceAsStream(resourcePath)) { - if (fileStream != null) { - Document metadata = toXmlDocument(fileStream, resourcePath); - NodeList nodes = (NodeList)xpathExpression.evaluate(metadata, XPathConstants.NODESET); - if (nodes != null) { - for (int i = 0; i < nodes.getLength(); i++) { - Node node = nodes.item(i); - String implementationClass = getImplementationClassName(node); - if (implementationClass != null) { - cacheMap.put(implementationClass, metadata); - } - } - } + try { + Enumeration<URL> resourceUrls = OsgiMetadataUtil.class.getClassLoader().getResources(resourcePath); + while (resourceUrls.hasMoreElements()) { + URL resourceUrl = resourceUrls.nextElement(); + try (InputStream fileStream = resourceUrl.openStream()) { + parseMetadataDocument(cacheMap, resourcePath, fileStream, xpathExpression); + } } } - catch (Throwable ex) { + catch (Exception ex) { log.warn("Error reading SCR metadata XML document from " + resourcePath, ex); } } + private static void parseMetadataDocument(Map<String,Document> cacheMap, String resourcePath, + InputStream fileStream, XPathExpression xpathExpression) throws XPathExpressionException { + Document metadata = toXmlDocument(fileStream, resourcePath); + NodeList nodes = (NodeList)xpathExpression.evaluate(metadata, XPathConstants.NODESET); + if (nodes != null) { + for (int i = 0; i < nodes.getLength(); i++) { + Node node = nodes.item(i); + String implementationClass = getImplementationClassName(node); + if (implementationClass != null) { + cacheMap.put(implementationClass, metadata); + } + } + } + } + private static String getImplementationClassName(Node componentNode) { NodeList childNodes = componentNode.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) {