From 39109e5078620483cb2e4589e7e822f5ddc3b38e Mon Sep 17 00:00:00 2001 From: James Netherton Date: Thu, 9 Jan 2020 08:16:13 +0000 Subject: [PATCH] CAMEL-14377: Add configuration option to enable / disable BraintreeLogHandler --- .../src/main/docs/braintree-component.adoc | 6 +- .../braintree/BraintreeComponent.java | 18 ++++ .../braintree/BraintreeConfiguration.java | 22 ++++- .../braintree/BraintreeComponentTest.java | 69 +++++++++++++ .../braintree/BraintreeConfigurationTest.java | 62 ++++++++++++ .../dsl/BraintreeEndpointBuilderFactory.java | 96 +++++++++++++++++++ .../ROOT/pages/braintree-component.adoc | 6 +- 7 files changed, 274 insertions(+), 5 deletions(-) create mode 100644 components/camel-braintree/src/test/java/org/apache/camel/component/braintree/BraintreeComponentTest.java create mode 100644 components/camel-braintree/src/test/java/org/apache/camel/component/braintree/BraintreeConfigurationTest.java diff --git a/components/camel-braintree/src/main/docs/braintree-component.adoc b/components/camel-braintree/src/main/docs/braintree-component.adoc index ec05bc9476203..c447ef83549fc 100644 --- a/components/camel-braintree/src/main/docs/braintree-component.adoc +++ b/components/camel-braintree/src/main/docs/braintree-component.adoc @@ -40,7 +40,7 @@ for this component: // component options: START -The Braintree component supports 4 options, which are listed below. +The Braintree component supports 5 options, which are listed below. @@ -48,6 +48,7 @@ The Braintree component supports 4 options, which are listed below. |=== | Name | Description | Default | Type | *configuration* (common) | To use the shared configuration | | BraintreeConfiguration +| *logHandlerEnabled* (logging) | Sets whether to enable the BraintreeLogHandler. It may be desirable to set this to 'false' where an existing JUL - SLF4J logger bridge is on the classpath. | true | boolean | *basicPropertyBinding* (advanced) | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean | *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean @@ -78,7 +79,7 @@ with the following path and query parameters: |=== -=== Query Parameters (32 parameters): +=== Query Parameters (33 parameters): [width="100%",cols="2,5,^1,2",options="header"] @@ -100,6 +101,7 @@ with the following path and query parameters: | *httpReadTimeout* (advanced) | Set read timeout for http calls. | | Integer | *synchronous* (advanced) | Sets whether synchronous processing should be strictly used, or Camel is allowed to use asynchronous processing (if supported). | false | boolean | *httpLogLevel* (logging) | Set logging level for http calls, see java.util.logging.Level | | String +| *logHandlerEnabled* (logging) | Sets whether to enable the BraintreeLogHandler. It may be desirable to set this to 'false' where an existing JUL - SLF4J logger bridge is on the classpath. This option can also be configured globally on the BraintreeComponent. | true | boolean | *backoffErrorThreshold* (scheduler) | The number of subsequent error polls (failed due some error) that should happen before the backoffMultipler should kick-in. | | int | *backoffIdleThreshold* (scheduler) | The number of subsequent idle polls that should happen before the backoffMultipler should kick-in. | | int | *backoffMultiplier* (scheduler) | To let the scheduled polling consumer backoff if there has been a number of subsequent idles/errors in a row. The multiplier is then the number of polls that will be skipped before the next actual attempt is happening again. When this option is in use then backoffIdleThreshold and/or backoffErrorThreshold must also be configured. | | int diff --git a/components/camel-braintree/src/main/java/org/apache/camel/component/braintree/BraintreeComponent.java b/components/camel-braintree/src/main/java/org/apache/camel/component/braintree/BraintreeComponent.java index 395e6bd0ed729..e4219e19224b7 100644 --- a/components/camel-braintree/src/main/java/org/apache/camel/component/braintree/BraintreeComponent.java +++ b/components/camel-braintree/src/main/java/org/apache/camel/component/braintree/BraintreeComponent.java @@ -24,6 +24,7 @@ import org.apache.camel.Endpoint; import org.apache.camel.component.braintree.internal.BraintreeApiCollection; import org.apache.camel.component.braintree.internal.BraintreeApiName; +import org.apache.camel.spi.Metadata; import org.apache.camel.spi.annotations.Component; import org.apache.camel.support.component.AbstractApiComponent; @@ -32,6 +33,10 @@ */ @Component("braintree") public class BraintreeComponent extends AbstractApiComponent { + + @Metadata(label = "advanced,logging", defaultValue = "true") + private boolean logHandlerEnabled = true; + private final Map gateways; public BraintreeComponent() { @@ -53,6 +58,7 @@ protected BraintreeApiName getApiName(String apiNameStr) throws IllegalArgumentE protected Endpoint createEndpoint(String uri, String methodName, BraintreeApiName apiName, BraintreeConfiguration endpointConfiguration) { endpointConfiguration.setApiName(apiName); endpointConfiguration.setMethodName(methodName); + endpointConfiguration.setLogHandlerEnabled(logHandlerEnabled); return new BraintreeEndpoint(uri, this, apiName, methodName, endpointConfiguration); } @@ -69,6 +75,18 @@ public BraintreeConfiguration getConfiguration() { return super.getConfiguration(); } + /** + * Sets whether to enable the BraintreeLogHandler. It may be desirable to set this to + * 'false' where an existing JUL - SLF4J logger bridge is on the classpath. + */ + public void setLogHandlerEnabled(boolean logHandlerEnabled) { + this.logHandlerEnabled = logHandlerEnabled; + } + + public boolean isLogHandlerEnabled() { + return logHandlerEnabled; + } + public synchronized BraintreeGateway getGateway(BraintreeConfiguration configuration) { BraintreeGateway gateway; if (configuration.getAccessToken() != null) { diff --git a/components/camel-braintree/src/main/java/org/apache/camel/component/braintree/BraintreeConfiguration.java b/components/camel-braintree/src/main/java/org/apache/camel/component/braintree/BraintreeConfiguration.java index b2cc8a0c2d500..b5a4c2ae5b9d5 100644 --- a/components/camel-braintree/src/main/java/org/apache/camel/component/braintree/BraintreeConfiguration.java +++ b/components/camel-braintree/src/main/java/org/apache/camel/component/braintree/BraintreeConfiguration.java @@ -78,6 +78,10 @@ public class BraintreeConfiguration { @Metadata(label = "advanced,logging") private String httpLogName; + @UriParam(defaultValue = "true") + @Metadata(label = "advanced,logging") + private boolean logHandlerEnabled = true; + @UriParam @Metadata(label = "advanced") private Integer httpReadTimeout; @@ -215,6 +219,20 @@ public Integer getHttpReadTimeout() { return httpReadTimeout; } + /** + * Sets whether to enable the BraintreeLogHandler. It may be desirable to set this to + * 'false' where an existing JUL - SLF4J logger bridge is on the classpath. + * + * This option can also be configured globally on the BraintreeComponent. + */ + public void setLogHandlerEnabled(boolean logHandlerEnabled) { + this.logHandlerEnabled = logHandlerEnabled; + } + + public boolean isLogHandlerEnabled() { + return logHandlerEnabled; + } + /** * Set read timeout for http calls. */ @@ -280,7 +298,9 @@ synchronized BraintreeGateway newBraintreeGateway() { logger.removeHandler(handler); } - logger.addHandler(new BraintreeLogHandler()); + if (isLogHandlerEnabled()) { + logger.addHandler(new BraintreeLogHandler()); + } if (httpLogLevel != null) { logger.setLevel(httpLogLevel); diff --git a/components/camel-braintree/src/test/java/org/apache/camel/component/braintree/BraintreeComponentTest.java b/components/camel-braintree/src/test/java/org/apache/camel/component/braintree/BraintreeComponentTest.java new file mode 100644 index 0000000000000..dee3d64001bfe --- /dev/null +++ b/components/camel-braintree/src/test/java/org/apache/camel/component/braintree/BraintreeComponentTest.java @@ -0,0 +1,69 @@ +/* + * 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.braintree; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import com.braintreegateway.BraintreeGateway; +import org.apache.camel.component.braintree.internal.BraintreeApiName; +import org.apache.camel.component.braintree.internal.BraintreeLogHandler; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class BraintreeComponentTest { + + @Test + public void testLoggerConfiguration() { + BraintreeConfiguration configuration = createBraintreeConfiguration(); + configuration.setHttpLogLevel(Level.WARNING); + + BraintreeComponent component = new BraintreeComponent(); + component.createEndpoint("braintree:clientToken", "generate", BraintreeApiName.CLIENTTOKEN, configuration); + + BraintreeGateway braintreeGateway = component.getGateway(configuration); + Logger logger = braintreeGateway.getConfiguration().getLogger(); + assertEquals(Level.WARNING, logger.getLevel()); + assertEquals(1, logger.getHandlers().length); + assertTrue(logger.getHandlers()[0] instanceof BraintreeLogHandler); + } + + @Test + public void testBraintreeLogHandlerDisabled() { + BraintreeConfiguration configuration = createBraintreeConfiguration(); + + BraintreeComponent component = new BraintreeComponent(); + component.setLogHandlerEnabled(false); + component.createEndpoint("", "", BraintreeApiName.CLIENTTOKEN, configuration); + + BraintreeGateway braintreeGateway = component.getGateway(configuration); + Logger logger = braintreeGateway.getConfiguration().getLogger(); + assertEquals(0, logger.getHandlers().length); + } + + private BraintreeConfiguration createBraintreeConfiguration() { + BraintreeConfiguration configuration = new BraintreeConfiguration(); + configuration.setEnvironment("SANDBOX"); + configuration.setMerchantId("dummy-merchant-id"); + configuration.setPublicKey("dummy-public-key"); + configuration.setPrivateKey("dummy-private-key"); + return configuration; + } + +} diff --git a/components/camel-braintree/src/test/java/org/apache/camel/component/braintree/BraintreeConfigurationTest.java b/components/camel-braintree/src/test/java/org/apache/camel/component/braintree/BraintreeConfigurationTest.java new file mode 100644 index 0000000000000..40c19a7f806bc --- /dev/null +++ b/components/camel-braintree/src/test/java/org/apache/camel/component/braintree/BraintreeConfigurationTest.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.braintree; + +import java.util.logging.Level; +import java.util.logging.Logger; + +import com.braintreegateway.BraintreeGateway; +import org.apache.camel.component.braintree.internal.BraintreeLogHandler; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +public class BraintreeConfigurationTest { + + @Test + public void testLoggerConfiguration() { + BraintreeConfiguration configuration = createBraintreeConfiguration(); + configuration.setHttpLogLevel(Level.WARNING); + + BraintreeGateway braintreeGateway = configuration.newBraintreeGateway(); + Logger logger = braintreeGateway.getConfiguration().getLogger(); + assertEquals(Level.WARNING, logger.getLevel()); + assertEquals(1, logger.getHandlers().length); + assertTrue(logger.getHandlers()[0] instanceof BraintreeLogHandler); + } + + @Test + public void testBraintreeLogHandlerDisabled() { + BraintreeConfiguration configuration = createBraintreeConfiguration(); + configuration.setLogHandlerEnabled(false); + + BraintreeGateway braintreeGateway = configuration.newBraintreeGateway(); + Logger logger = braintreeGateway.getConfiguration().getLogger(); + assertEquals(0, logger.getHandlers().length); + } + + private BraintreeConfiguration createBraintreeConfiguration() { + BraintreeConfiguration configuration = new BraintreeConfiguration(); + configuration.setEnvironment("SANDBOX"); + configuration.setMerchantId("dummy-merchant-id"); + configuration.setPublicKey("dummy-public-key"); + configuration.setPrivateKey("dummy-private-key"); + return configuration; + } + +} diff --git a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/BraintreeEndpointBuilderFactory.java b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/BraintreeEndpointBuilderFactory.java index 18eac25003c8a..d8de00310658d 100644 --- a/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/BraintreeEndpointBuilderFactory.java +++ b/core/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/dsl/BraintreeEndpointBuilderFactory.java @@ -800,6 +800,38 @@ default AdvancedBraintreeEndpointConsumerBuilder httpLogLevel( doSetProperty("httpLogLevel", httpLogLevel); return this; } + /** + * Sets whether to enable the BraintreeLogHandler. It may be desirable + * to set this to 'false' where an existing JUL - SLF4J logger bridge is + * on the classpath. This option can also be configured globally on the + * BraintreeComponent. + * + * The option is a: boolean type. + * + * Default: true + * Group: logging + */ + default AdvancedBraintreeEndpointConsumerBuilder logHandlerEnabled( + boolean logHandlerEnabled) { + doSetProperty("logHandlerEnabled", logHandlerEnabled); + return this; + } + /** + * Sets whether to enable the BraintreeLogHandler. It may be desirable + * to set this to 'false' where an existing JUL - SLF4J logger bridge is + * on the classpath. This option can also be configured globally on the + * BraintreeComponent. + * + * The option will be converted to a boolean type. + * + * Default: true + * Group: logging + */ + default AdvancedBraintreeEndpointConsumerBuilder logHandlerEnabled( + String logHandlerEnabled) { + doSetProperty("logHandlerEnabled", logHandlerEnabled); + return this; + } } /** @@ -1073,6 +1105,38 @@ default AdvancedBraintreeEndpointProducerBuilder httpLogLevel( doSetProperty("httpLogLevel", httpLogLevel); return this; } + /** + * Sets whether to enable the BraintreeLogHandler. It may be desirable + * to set this to 'false' where an existing JUL - SLF4J logger bridge is + * on the classpath. This option can also be configured globally on the + * BraintreeComponent. + * + * The option is a: boolean type. + * + * Default: true + * Group: logging + */ + default AdvancedBraintreeEndpointProducerBuilder logHandlerEnabled( + boolean logHandlerEnabled) { + doSetProperty("logHandlerEnabled", logHandlerEnabled); + return this; + } + /** + * Sets whether to enable the BraintreeLogHandler. It may be desirable + * to set this to 'false' where an existing JUL - SLF4J logger bridge is + * on the classpath. This option can also be configured globally on the + * BraintreeComponent. + * + * The option will be converted to a boolean type. + * + * Default: true + * Group: logging + */ + default AdvancedBraintreeEndpointProducerBuilder logHandlerEnabled( + String logHandlerEnabled) { + doSetProperty("logHandlerEnabled", logHandlerEnabled); + return this; + } } /** @@ -1300,6 +1364,38 @@ default AdvancedBraintreeEndpointBuilder httpLogLevel( doSetProperty("httpLogLevel", httpLogLevel); return this; } + /** + * Sets whether to enable the BraintreeLogHandler. It may be desirable + * to set this to 'false' where an existing JUL - SLF4J logger bridge is + * on the classpath. This option can also be configured globally on the + * BraintreeComponent. + * + * The option is a: boolean type. + * + * Default: true + * Group: logging + */ + default AdvancedBraintreeEndpointBuilder logHandlerEnabled( + boolean logHandlerEnabled) { + doSetProperty("logHandlerEnabled", logHandlerEnabled); + return this; + } + /** + * Sets whether to enable the BraintreeLogHandler. It may be desirable + * to set this to 'false' where an existing JUL - SLF4J logger bridge is + * on the classpath. This option can also be configured globally on the + * BraintreeComponent. + * + * The option will be converted to a boolean type. + * + * Default: true + * Group: logging + */ + default AdvancedBraintreeEndpointBuilder logHandlerEnabled( + String logHandlerEnabled) { + doSetProperty("logHandlerEnabled", logHandlerEnabled); + return this; + } } /** * Braintree (camel-braintree) diff --git a/docs/components/modules/ROOT/pages/braintree-component.adoc b/docs/components/modules/ROOT/pages/braintree-component.adoc index ab70ff6df5112..bfeb8676ff6fc 100644 --- a/docs/components/modules/ROOT/pages/braintree-component.adoc +++ b/docs/components/modules/ROOT/pages/braintree-component.adoc @@ -41,7 +41,7 @@ for this component: // component options: START -The Braintree component supports 4 options, which are listed below. +The Braintree component supports 5 options, which are listed below. @@ -49,6 +49,7 @@ The Braintree component supports 4 options, which are listed below. |=== | Name | Description | Default | Type | *configuration* (common) | To use the shared configuration | | BraintreeConfiguration +| *logHandlerEnabled* (logging) | Sets whether to enable the BraintreeLogHandler. It may be desirable to set this to 'false' where an existing JUL - SLF4J logger bridge is on the classpath. | true | boolean | *basicPropertyBinding* (advanced) | Whether the component should use basic property binding (Camel 2.x) or the newer property binding with additional capabilities | false | boolean | *lazyStartProducer* (producer) | Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | *bridgeErrorHandler* (consumer) | Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions occurred while the consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean @@ -79,7 +80,7 @@ with the following path and query parameters: |=== -=== Query Parameters (32 parameters): +=== Query Parameters (33 parameters): [width="100%",cols="2,5,^1,2",options="header"] @@ -101,6 +102,7 @@ with the following path and query parameters: | *httpReadTimeout* (advanced) | Set read timeout for http calls. | | Integer | *synchronous* (advanced) | Sets whether synchronous processing should be strictly used, or Camel is allowed to use asynchronous processing (if supported). | false | boolean | *httpLogLevel* (logging) | Set logging level for http calls, see java.util.logging.Level | | String +| *logHandlerEnabled* (logging) | Sets whether to enable the BraintreeLogHandler. It may be desirable to set this to 'false' where an existing JUL - SLF4J logger bridge is on the classpath. This option can also be configured globally on the BraintreeComponent. | true | boolean | *backoffErrorThreshold* (scheduler) | The number of subsequent error polls (failed due some error) that should happen before the backoffMultipler should kick-in. | | int | *backoffIdleThreshold* (scheduler) | The number of subsequent idle polls that should happen before the backoffMultipler should kick-in. | | int | *backoffMultiplier* (scheduler) | To let the scheduled polling consumer backoff if there has been a number of subsequent idles/errors in a row. The multiplier is then the number of polls that will be skipped before the next actual attempt is happening again. When this option is in use then backoffIdleThreshold and/or backoffErrorThreshold must also be configured. | | int