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

Conflicts:
        
camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java


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

Branch: refs/remotes/origin/camel-2.13.x
Commit: d91dfb39b51b4b76aa610770de6eb731ba78f5d4
Parents: 59e467d
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 14:41:24 2014 +0100

----------------------------------------------------------------------
 .../camel/component/log/LogComponent.java       | 23 ++++----
 .../log/CustomExchangeFormatterTest.java        | 62 ++++++++++++++++++++
 .../component/log/TestExchangeFormatter.java    | 42 +++++++++++++
 .../log/custom-exchange-formatter-context.xml   | 36 ++++++++++++
 4 files changed, 151 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/d91dfb39/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 1e9e18b..4fc838a 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
@@ -70,6 +70,7 @@ public class LogComponent extends UriEndpointComponent {
         } else {
             camelLogger = new CamelLogger(providedLogger, level, 
endpoint.getMarker());
         }
+
         Processor logger;
         if (endpoint.getGroupSize() != null) {
             logger = new ThroughputLogger(camelLogger, 
endpoint.getGroupSize());
@@ -78,18 +79,16 @@ public class LogComponent extends UriEndpointComponent {
             Long groupDelay = endpoint.getGroupDelay();
             logger = new ThroughputLogger(camelLogger, this.getCamelContext(), 
endpoint.getGroupInterval(), groupDelay, groupActiveOnly);
         } else {
-            // 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/d91dfb39/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..4d6a15a
--- /dev/null
+++ 
b/components/camel-spring/src/test/java/org/apache/camel/component/log/CustomExchangeFormatterTest.java
@@ -0,0 +1,62 @@
+/**
+ * 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 java.lang.reflect.Field;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.processor.CamelLogProcessor;
+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 {
+        // the exchangeFormatter field is not a property, therefore we have to 
inspect it via reflection
+        Field f = CamelLogProcessor.class.getDeclaredField("formatter");
+        f.setAccessible(true);
+        
+        TestExchangeFormatter aaa = null;
+        TestExchangeFormatter bbb = null;
+        for (Endpoint ep : context.getEndpoints()) {
+            if (!(ep instanceof LogEndpoint)) {
+                continue;
+            }
+            
+            LogEndpoint log = (LogEndpoint) ep;
+            
+            aaa = log.getEndpointUri().contains("aaa") ? 
+                    (TestExchangeFormatter) f.get(((CamelLogProcessor) 
log.getLogger())) : aaa;
+            bbb = log.getEndpointUri().contains("bbb") ? 
+                    (TestExchangeFormatter) f.get(((CamelLogProcessor) 
log.getLogger())) : 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/d91dfb39/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/d91dfb39/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>

Reply via email to