You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2015/09/28 17:05:54 UTC

camel git commit: Simplify ScrHelper

Repository: camel
Updated Branches:
  refs/heads/master 9f83799c0 -> 1b5546697


Simplify ScrHelper


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/1b554669
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/1b554669
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/1b554669

Branch: refs/heads/master
Commit: 1b554669752f8d3cfec979db22eb4f8ef1eb2698
Parents: 9f83799
Author: Jyrki Ruuskanen <yu...@kotikone.fi>
Authored: Mon Sep 28 17:56:12 2015 +0300
Committer: Jyrki Ruuskanen <yu...@kotikone.fi>
Committed: Mon Sep 28 17:56:12 2015 +0300

----------------------------------------------------------------------
 .../java/org/apache/camel/scr/ScrHelper.java    | 104 +++++--------------
 .../org/apache/camel/scr/ScrHelperTest.java     |   1 +
 .../resources/componentDefinitionExample.xml    |  10 ++
 3 files changed, 37 insertions(+), 78 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/1b554669/components/camel-scr/src/main/java/org/apache/camel/scr/ScrHelper.java
----------------------------------------------------------------------
diff --git a/components/camel-scr/src/main/java/org/apache/camel/scr/ScrHelper.java b/components/camel-scr/src/main/java/org/apache/camel/scr/ScrHelper.java
index 2918965..13ba761 100644
--- a/components/camel-scr/src/main/java/org/apache/camel/scr/ScrHelper.java
+++ b/components/camel-scr/src/main/java/org/apache/camel/scr/ScrHelper.java
@@ -16,29 +16,19 @@
  */
 package org.apache.camel.scr;
 
-import java.io.File;
+import java.io.FileReader;
 import java.util.HashMap;
-import java.util.Iterator;
 import java.util.Map;
-import javax.xml.XMLConstants;
-import javax.xml.namespace.NamespaceContext;
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.xpath.XPath;
-import javax.xml.xpath.XPathConstants;
-import javax.xml.xpath.XPathExpression;
-import javax.xml.xpath.XPathExpressionException;
-import javax.xml.xpath.XPathFactory;
-
-import org.w3c.dom.Document;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLEventReader;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.events.XMLEvent;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Helper class.
+ * Helper class for reading properties from a component description file. Used in unit testing.
  */
 public final class ScrHelper {
 
@@ -54,68 +44,26 @@ public final class ScrHelper {
     public static Map<String, String> getScrProperties(String xmlLocation, String componentName) throws Exception {
         Map<String, String> result = new HashMap<String, String>();
 
-        final Document dom = readXML(new File(xmlLocation));
-        final XPath xPath = XPathFactory.newInstance(XPathFactory.DEFAULT_OBJECT_MODEL_URI, "com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl", null).newXPath();
-        xPath.setNamespaceContext(new ScrNamespaceContext(dom, xPath));
-
-        String propertyListExpression = String.format("/components/scr:component[@name='%s']/property", componentName);
-        XPathExpression propertyList = xPath.compile(propertyListExpression);
-        XPathExpression propertyName = xPath.compile("@name");
-        XPathExpression propertyValue = xPath.compile("@value");
-        NodeList nodes = (NodeList) propertyList.evaluate(dom, XPathConstants.NODESET);
-        for (int i = 0; i < nodes.getLength(); i++) {
-            Node node = nodes.item(i);
-            result.put((String) propertyName.evaluate(node, XPathConstants.STRING), (String) propertyValue.evaluate(node, XPathConstants.STRING));
-        }
-        return result;
-    }
-
-    private static Document readXML(File xml) throws Exception {
-        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
-        builderFactory.setNamespaceAware(true);
-        DocumentBuilder builder = builderFactory.newDocumentBuilder();
-        return builder.parse(xml);
-    }
-
-    private static final class ScrNamespaceContext implements NamespaceContext {
-
-        private final Document dom;
-        private final XPath xPath;
-
-        private ScrNamespaceContext(Document dom, XPath xPath) {
-            this.dom = dom;
-            this.xPath = xPath;
-        }
-
-        @Override
-        public String getNamespaceURI(String prefix) {
-            switch (prefix) {
-            case "scr":
-                try {
-                    XPathExpression scrNamespace = xPath.compile("/*/namespace::*[name()='scr']");
-                    Node node = (Node) scrNamespace.evaluate(dom, XPathConstants.NODE);
-                    return node.getNodeValue();
-                } catch (XPathExpressionException e) {
-                    // ignore
-                    LOG.debug("Error evaluating xpath to obtain namespace prefix. This exception is ignored and using namespace: http://www.osgi.org/xmlns/scr/v1.1.0", e);
-                }
-                return "http://www.osgi.org/xmlns/scr/v1.1.0";
-            default:
-                // noop
+        XMLInputFactory inputFactory = XMLInputFactory.newFactory();
+        inputFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, false);
+        XMLEventReader eventReader = inputFactory.createXMLEventReader(new FileReader(xmlLocation));
+        boolean collect = false;
+        while (eventReader.hasNext()) {
+            XMLEvent event = eventReader.nextEvent();
+            if (event.getEventType() == XMLEvent.START_ELEMENT
+                && event.asStartElement().getName().toString().equals("scr:component")
+                && event.asStartElement().getAttributeByName(QName.valueOf("name")).getValue().equals(componentName)) {
+                collect = true;
+            } else if (collect
+                && event.getEventType() == XMLEvent.START_ELEMENT
+                && event.asStartElement().getName().toString().equals("property")) {
+                result.put(event.asStartElement().getAttributeByName(QName.valueOf("name")).getValue(), event.asStartElement().getAttributeByName(QName.valueOf("value")).getValue());
+            } else if (collect
+                && event.getEventType() == XMLEvent.END_ELEMENT
+                && event.asEndElement().getName().toString().equals("scr:component")) {
+                break;
             }
-            return XMLConstants.NULL_NS_URI;
-        }
-
-        @Override
-        public String getPrefix(String namespaceURI) {
-            return null;
-        }
-
-        @Override
-        public Iterator<String> getPrefixes(String namespaceURI) {
-            return null;
         }
+        return result;
     }
-
-}
-
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/1b554669/components/camel-scr/src/test/java/org/apache/camel/scr/ScrHelperTest.java
----------------------------------------------------------------------
diff --git a/components/camel-scr/src/test/java/org/apache/camel/scr/ScrHelperTest.java b/components/camel-scr/src/test/java/org/apache/camel/scr/ScrHelperTest.java
index e80e208..38ee78b 100644
--- a/components/camel-scr/src/test/java/org/apache/camel/scr/ScrHelperTest.java
+++ b/components/camel-scr/src/test/java/org/apache/camel/scr/ScrHelperTest.java
@@ -49,6 +49,7 @@ public class ScrHelperTest {
     public void scrHelperTest() throws Exception {
         Map<String, String> properties = ScrHelper.getScrProperties("src/test/resources/componentDefinitionExample.xml", "my.example.Component");
         assertEquals("exampleContext", properties.get("camelContextId"));
+        assertEquals("true", properties.get("active"));
         assertTrue(properties.size() == 6);
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/1b554669/components/camel-scr/src/test/resources/componentDefinitionExample.xml
----------------------------------------------------------------------
diff --git a/components/camel-scr/src/test/resources/componentDefinitionExample.xml b/components/camel-scr/src/test/resources/componentDefinitionExample.xml
index 162ca9c..d2030c3 100644
--- a/components/camel-scr/src/test/resources/componentDefinitionExample.xml
+++ b/components/camel-scr/src/test/resources/componentDefinitionExample.xml
@@ -10,4 +10,14 @@
         <property name="service.pid" value="my.example.Component"/>
         <reference name="camelComponent" interface="org.apache.camel.spi.ComponentResolver" cardinality="1..n" policy="dynamic" bind="gotCamelComponent" unbind="lostCamelComponent" policy-option="greedy"/>
     </scr:component>
+    <scr:component immediate="true" name="my.example.Component2">
+        <implementation class="my.example.Component2"/>
+        <property name="camelContextId" value="exampleContext2"/>
+        <property name="unit.camelContextId" value="exampleContextForUnitTest2"/>
+        <property name="camelRouteId" value="{{camelContextId}}/timer-log"/>
+        <property name="active" value="false"/>
+        <property name="master" value="master:{{camelContextId}}"/>
+        <property name="service.pid" value="my.example.Component"/>
+        <reference name="camelComponent" interface="org.apache.camel.spi.ComponentResolver" cardinality="1..n" policy="dynamic" bind="gotCamelComponent" unbind="lostCamelComponent" policy-option="greedy"/>
+    </scr:component>
 </components>