Author: sseifert
Date: Fri May  5 09:44:55 2017
New Revision: 1793995

URL: http://svn.apache.org/viewvc?rev=1793995&view=rev
Log:
SLING-6832 osgi-mock: Support parsing SCR metadata when multiple definition XML 
files exists with the same name

Modified:
    
sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java

Modified: 
sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java
URL: 
http://svn.apache.org/viewvc/sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java?rev=1793995&r1=1793994&r2=1793995&view=diff
==============================================================================
--- 
sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java
 (original)
+++ 
sling/branches/testing/mocks/osgi-mock-1.x/src/main/java/org/apache/sling/testing/mock/osgi/OsgiMetadataUtil.java
 Fri May  5 09:44:55 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,38 +175,47 @@ final class OsgiMetadataUtil {
     }
     
     private static void parseMetadataDocuments(Map<String,Document> cacheMap, 
String resourcePath, XPathExpression xpathExpression) {
-        InputStream fileStream = null;
         try {
-            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);
+            Enumeration<URL> resourceUrls = 
OsgiMetadataUtil.class.getClassLoader().getResources(resourcePath);
+            while (resourceUrls.hasMoreElements()) {
+                URL resourceUrl = resourceUrls.nextElement();
+                InputStream fileStream = null;
+                try {
+                    fileStream = resourceUrl.openStream();
+                    parseMetadataDocument(cacheMap, resourcePath, fileStream, 
xpathExpression);
+                }
+                finally {
+                    if (fileStream != null) {
+                        try {
+                            fileStream.close();
+                        }
+                        catch (IOException e) {
+                            // ignore
                         }
                     }
-                }                            
+                }
             }
         }
-        catch (Throwable ex) {
+        catch (Exception ex) {
             log.warn("Error reading SCR metadata XML document from " + 
resourcePath, ex);
         }
-        finally {
-            if (fileStream != null) {
-                try {
-                    fileStream.close();
-                }
-                catch (IOException e) {
-                    // ignore
+    }
+    
+    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++) {


Reply via email to