camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [2/2] git commit: CAMEL-6356: stax component supports reference lookup of ContentHandler. Thanks to Romain Manni-Bucau for patch.
Date Sat, 25 May 2013 08:44:05 GMT
CAMEL-6356: stax component supports reference lookup of ContentHandler. Thanks to Romain Manni-Bucau
for patch.


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

Branch: refs/heads/camel-2.11.x
Commit: 64b1bc0f19c9e3748c9abcaf19188bd042048a21
Parents: e60090c
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Sat May 25 10:40:35 2013 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Sat May 25 10:43:37 2013 +0200

----------------------------------------------------------------------
 .../apache/camel/component/stax/StAXComponent.java |   13 ++-
 .../apache/camel/component/stax/StAXProcessor.java |   14 +++-
 .../camel/component/stax/StAXComponentRefTest.java |   73 +++++++++++++++
 3 files changed, 96 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/64b1bc0f/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXComponent.java
b/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXComponent.java
index ebdc4e2..94bfb2e 100644
--- a/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXComponent.java
+++ b/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXComponent.java
@@ -23,15 +23,22 @@ import org.xml.sax.ContentHandler;
 import org.apache.camel.Endpoint;
 import org.apache.camel.impl.DefaultComponent;
 import org.apache.camel.impl.ProcessorEndpoint;
+import org.apache.camel.util.EndpointHelper;
 
 public class StAXComponent extends DefaultComponent {
 
     @Override
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object>
parameters) throws Exception {
-        Class<ContentHandler> clazz = getCamelContext().getClassResolver().resolveMandatoryClass(remaining,
ContentHandler.class);
-
-        StAXProcessor processor = new StAXProcessor(clazz);
+        StAXProcessor processor;
+        if (EndpointHelper.isReferenceParameter(remaining)) {
+            ContentHandler handler = EndpointHelper.resolveReferenceParameter(getCamelContext(),
remaining.substring(1), ContentHandler.class, true);
+            processor = new StAXProcessor(handler);
+        } else {
+            Class clazz = getCamelContext().getClassResolver().resolveMandatoryClass(remaining,
ContentHandler.class);
+            processor = new StAXProcessor(clazz);
+        }
         setProperties(processor, parameters);
         return new ProcessorEndpoint(uri, this, processor);
     }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/64b1bc0f/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXProcessor.java
----------------------------------------------------------------------
diff --git a/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXProcessor.java
b/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXProcessor.java
index ce7a657..c8c7fcb 100644
--- a/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXProcessor.java
+++ b/components/camel-stax/src/main/java/org/apache/camel/component/stax/StAXProcessor.java
@@ -35,9 +35,16 @@ import org.apache.camel.util.ExchangeHelper;
  */
 public class StAXProcessor implements Processor {
     private final Class<ContentHandler> contentHandlerClass;
+    private final ContentHandler contentHandler;
 
     public StAXProcessor(Class<ContentHandler> contentHandlerClass) {
         this.contentHandlerClass = contentHandlerClass;
+        this.contentHandler = null;
+    }
+
+    public StAXProcessor(ContentHandler contentHandler) {
+        this.contentHandler = contentHandler;
+        this.contentHandlerClass = null;
     }
 
     @Override
@@ -46,7 +53,12 @@ public class StAXProcessor implements Processor {
         XMLStreamReader stream = exchange.getIn().getMandatoryBody(XMLStreamReader.class);
         XMLReader reader = new StaxStreamXMLReader(stream);
 
-        ContentHandler handler = contentHandlerClass.newInstance();
+        ContentHandler handler;
+        if (contentHandlerClass != null) {
+            handler = contentHandlerClass.newInstance();
+        } else {
+            handler = contentHandler;
+        }
         reader.setContentHandler(handler);
         reader.parse(is);
 

http://git-wip-us.apache.org/repos/asf/camel/blob/64b1bc0f/components/camel-stax/src/test/java/org/apache/camel/component/stax/StAXComponentRefTest.java
----------------------------------------------------------------------
diff --git a/components/camel-stax/src/test/java/org/apache/camel/component/stax/StAXComponentRefTest.java
b/components/camel-stax/src/test/java/org/apache/camel/component/stax/StAXComponentRefTest.java
new file mode 100644
index 0000000..3fdbc03
--- /dev/null
+++ b/components/camel-stax/src/test/java/org/apache/camel/component/stax/StAXComponentRefTest.java
@@ -0,0 +1,73 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.stax;
+
+import org.apache.camel.EndpointInject;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.stax.model.RecordsUtil;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.test.junit4.CamelTestSupport;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class StAXComponentRefTest extends CamelTestSupport {
+
+    @EndpointInject(uri = "mock:records")
+    private MockEndpoint recordsEndpoint;
+
+    @BeforeClass
+    public static void initRouteExample() {
+        RecordsUtil.createXMLFile();
+    }
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi = super.createRegistry();
+        jndi.bind("myHandler", new CountingHandler());
+        return jndi;
+    }
+
+    @Override
+    public RouteBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("file:target/in")
+                    .routeId("stax-parser")
+                    .to("stax:#myHandler")
+                    .process(new Processor() {
+                        @Override
+                        public void process(Exchange exchange) throws Exception {
+                            assertEquals(11, exchange.getIn().getBody(CountingHandler.class).getNumber());
+                        }
+                    })
+                    .to("mock:records");
+            }
+        };
+    }
+
+    @Test
+    public void testStax() throws Exception {
+        recordsEndpoint.expectedMessageCount(1);
+        recordsEndpoint.message(0).body().isInstanceOf(CountingHandler.class);
+
+        recordsEndpoint.assertIsSatisfied();
+    }
+}


Mime
View raw message