camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject camel git commit: Simplify ScrHelper
Date Mon, 28 Sep 2015 15:06:46 GMT
Repository: camel
Updated Branches:
  refs/heads/camel-2.15.x bf6d1b7c4 -> 1f499f626


Simplify ScrHelper


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

Branch: refs/heads/camel-2.15.x
Commit: 1f499f626291e47ab64c8fc5ab4c02e5784b8bfe
Parents: bf6d1b7
Author: Jyrki Ruuskanen <yuruki@kotikone.fi>
Authored: Mon Sep 28 17:23:47 2015 +0300
Committer: Jyrki Ruuskanen <yuruki@kotikone.fi>
Committed: Mon Sep 28 17:23:47 2015 +0300

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


http://git-wip-us.apache.org/repos/asf/camel/blob/1f499f62/components/camel-scr/src/main/java/org/apache/camel/scr/internal/ScrHelper.java
----------------------------------------------------------------------
diff --git a/components/camel-scr/src/main/java/org/apache/camel/scr/internal/ScrHelper.java
b/components/camel-scr/src/main/java/org/apache/camel/scr/internal/ScrHelper.java
index 3d5842b..59f2745 100644
--- a/components/camel-scr/src/main/java/org/apache/camel/scr/internal/ScrHelper.java
+++ b/components/camel-scr/src/main/java/org/apache/camel/scr/internal/ScrHelper.java
@@ -16,29 +16,19 @@
  */
 package org.apache.camel.scr.internal;
 
-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/1f499f62/components/camel-scr/src/test/java/org/apache/camel/scr/internal/ScrHelperTest.java
----------------------------------------------------------------------
diff --git a/components/camel-scr/src/test/java/org/apache/camel/scr/internal/ScrHelperTest.java
b/components/camel-scr/src/test/java/org/apache/camel/scr/internal/ScrHelperTest.java
index d97cf71..9d301e5 100644
--- a/components/camel-scr/src/test/java/org/apache/camel/scr/internal/ScrHelperTest.java
+++ b/components/camel-scr/src/test/java/org/apache/camel/scr/internal/ScrHelperTest.java
@@ -50,6 +50,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/1f499f62/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>


Mime
View raw message