You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ra...@apache.org on 2014/09/10 14:56:17 UTC

git commit: CAMEL-7798 Exchange formatter configured on Log Component may lead to incoherent results.

Repository: camel
Updated Branches:
  refs/heads/master 19b2aa315 -> 3c99fa97d


CAMEL-7798 Exchange formatter configured on Log Component may lead to incoherent results.


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

Branch: refs/heads/master
Commit: 3c99fa97dd66e8aae30febe840a382cc079a88db
Parents: 19b2aa3
Author: Raul Kripalani <ra...@apache.org>
Authored: Wed Sep 10 13:43:33 2014 +0100
Committer: Raul Kripalani <ra...@apache.org>
Committed: Wed Sep 10 13:55:46 2014 +0100

----------------------------------------------------------------------
 .../camel/component/log/LogComponent.java       | 24 +++++----
 .../log/CustomExchangeFormatterTest.java        | 51 ++++++++++++++++++++
 .../component/log/TestExchangeFormatter.java    | 42 ++++++++++++++++
 .../log/custom-exchange-formatter-context.xml   | 36 ++++++++++++++
 4 files changed, 140 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/3c99fa97/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java b/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java
index 7bb716f..a46f705 100644
--- a/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java
+++ b/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java
@@ -67,19 +67,17 @@ public class LogComponent extends UriEndpointComponent {
         } else {
             endpoint.setProvidedLogger(providedLogger);
         }
-       
-        // first, try to use the user-specified formatter (or the one picked up from the Registry and transferred to
-        // the property by a previous endpoint initialisation); if null, try to pick it up from the Registry now
-        ExchangeFormatter localFormatter = exchangeFormatter;
-        if (localFormatter == null) {
-            localFormatter = getCamelContext().getRegistry().lookupByNameAndType("logFormatter", ExchangeFormatter.class);
-            if (localFormatter != null) {
-                exchangeFormatter = localFormatter;
-                setProperties(exchangeFormatter, parameters);
-            }
-        }
-        // if no formatter is available in the Registry, create a local one of the default type, for a single use
-        if (localFormatter == null) {
+
+        // first, try to pick up the ExchangeFormatter from the registry
+        ExchangeFormatter localFormatter = getCamelContext().getRegistry().lookupByNameAndType("logFormatter", ExchangeFormatter.class);
+        if (localFormatter != null) {
+            setProperties(localFormatter, parameters);
+        } else if (localFormatter == null && exchangeFormatter != null) {
+            // do not set properties, the exchangeFormatter is explicitly set, thefore the 
+            // user would have set its properties explicitly too
+            localFormatter = exchangeFormatter;
+        } else {
+            // if no formatter is available in the Registry, create a local one of the default type, for a single use
             localFormatter = new DefaultExchangeFormatter();
             setProperties(localFormatter, parameters);
         }

http://git-wip-us.apache.org/repos/asf/camel/blob/3c99fa97/components/camel-spring/src/test/java/org/apache/camel/component/log/CustomExchangeFormatterTest.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/java/org/apache/camel/component/log/CustomExchangeFormatterTest.java b/components/camel-spring/src/test/java/org/apache/camel/component/log/CustomExchangeFormatterTest.java
new file mode 100644
index 0000000..bb6baf3
--- /dev/null
+++ b/components/camel-spring/src/test/java/org/apache/camel/component/log/CustomExchangeFormatterTest.java
@@ -0,0 +1,51 @@
+/**
+ * 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.log;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.spring.SpringTestSupport;
+import org.springframework.context.support.AbstractXmlApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+
+/**
+ * @version 
+ */
+public class CustomExchangeFormatterTest extends SpringTestSupport {
+
+    public void testExchangeFormattersConfiguredProperly() throws Exception {
+        TestExchangeFormatter aaa = null;
+        TestExchangeFormatter bbb = null;
+        for (Endpoint ep : context.getEndpoints()) {
+            if (!(ep instanceof LogEndpoint)) {
+                continue;
+            }
+            LogEndpoint log = (LogEndpoint) ep;
+            aaa = "aaa".equals(log.getLoggerName()) ? (TestExchangeFormatter) log.getLocalFormatter() : aaa;
+            bbb = "bbb".equals(log.getLoggerName()) ? (TestExchangeFormatter) log.getLocalFormatter() : bbb;
+        }
+        
+        assertNotNull(aaa);
+        assertNotNull(bbb);
+        assertNotSame(aaa, bbb);
+        assertEquals("aaa", aaa.getTestProperty());
+        assertEquals("bbb", bbb.getTestProperty());
+    }
+
+    protected AbstractXmlApplicationContext createApplicationContext() {
+        return new ClassPathXmlApplicationContext("org/apache/camel/component/log/custom-exchange-formatter-context.xml");
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/camel/blob/3c99fa97/components/camel-spring/src/test/java/org/apache/camel/component/log/TestExchangeFormatter.java
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/java/org/apache/camel/component/log/TestExchangeFormatter.java b/components/camel-spring/src/test/java/org/apache/camel/component/log/TestExchangeFormatter.java
new file mode 100644
index 0000000..7b0bc33
--- /dev/null
+++ b/components/camel-spring/src/test/java/org/apache/camel/component/log/TestExchangeFormatter.java
@@ -0,0 +1,42 @@
+/**
+ * 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.log;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.spi.ExchangeFormatter;
+
+/**
+ * A test exchange formatter. 
+ */
+public class TestExchangeFormatter implements ExchangeFormatter {
+
+    private String testProperty;
+    
+    @Override
+    public String format(Exchange exchange) {
+        return exchange.toString();
+    }
+
+    public String getTestProperty() {
+        return testProperty;
+    }
+
+    public void setTestProperty(String testProperty) {
+        this.testProperty = testProperty;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/3c99fa97/components/camel-spring/src/test/resources/org/apache/camel/component/log/custom-exchange-formatter-context.xml
----------------------------------------------------------------------
diff --git a/components/camel-spring/src/test/resources/org/apache/camel/component/log/custom-exchange-formatter-context.xml b/components/camel-spring/src/test/resources/org/apache/camel/component/log/custom-exchange-formatter-context.xml
new file mode 100644
index 0000000..4abdcd0
--- /dev/null
+++ b/components/camel-spring/src/test/resources/org/apache/camel/component/log/custom-exchange-formatter-context.xml
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+    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.
+-->
+<beans xmlns="http://www.springframework.org/schema/beans"
+       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xsi:schemaLocation="
+       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
+       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
+    ">
+
+    <bean id="logFormatter" class="org.apache.camel.component.log.TestExchangeFormatter" scope="prototype" />
+
+    <camelContext xmlns="http://camel.apache.org/schema/spring">
+        <route>
+            <from uri="direct:start"/>
+            <to uri="log:aaa?testProperty=aaa" id="log.aaa" />
+            <to uri="log:bbb?testProperty=bbb" id="log.bbb" />
+            <to uri="mock:result"/>
+        </route>
+    </camelContext>
+
+</beans>