Return-Path: X-Original-To: apmail-camel-commits-archive@www.apache.org Delivered-To: apmail-camel-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 8238D171F5 for ; Fri, 17 Oct 2014 08:39:55 +0000 (UTC) Received: (qmail 71563 invoked by uid 500); 17 Oct 2014 08:39:55 -0000 Delivered-To: apmail-camel-commits-archive@camel.apache.org Received: (qmail 71418 invoked by uid 500); 17 Oct 2014 08:39:55 -0000 Mailing-List: contact commits-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list commits@camel.apache.org Received: (qmail 71271 invoked by uid 99); 17 Oct 2014 08:39:55 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 17 Oct 2014 08:39:55 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 19731A09C29; Fri, 17 Oct 2014 08:39:55 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: cmoulliard@apache.org To: commits@camel.apache.org Date: Fri, 17 Oct 2014 08:39:57 -0000 Message-Id: <3269f7e94474461a87eeddd2a6869f05@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [04/50] git commit: CAMEL-7798 Exchange formatter configured on Log Component may lead to incoherent results. 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 Authored: Wed Sep 10 13:43:33 2014 +0100 Committer: Raul Kripalani 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 @@ + + + + + + + + + + + + + + + +