From 9c40f569b817e0886d95ad8b1ac235d8644873da Mon Sep 17 00:00:00 2001 From: olegb Date: Mon, 19 Feb 2018 16:19:17 +0200 Subject: Changed logging context API Issue-ID: SDC-772 Change-Id: I9dce63f3a1bb7df067cf06a96158afa7d799319e Signed-off-by: olegb --- .../openecomp/sdc/logging/api/LoggingContext.java | 48 ++++++++++------------ .../sdc/logging/spi/LoggingContextService.java | 48 ++++++++-------------- .../sdc/logging/api/LoggingContextTest.java | 37 ++++------------- 3 files changed, 49 insertions(+), 84 deletions(-) (limited to 'openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src') diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggingContext.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggingContext.java index f827fec50f..879d9cf098 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggingContext.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/api/LoggingContext.java @@ -1,5 +1,5 @@ /* - * Copyright © 2016-2017 European Support Limited + * Copyright © 2016-2018 European Support Limited * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,37 +22,38 @@ import java.util.Objects; import java.util.concurrent.Callable; /** - * Factory to hide a concrete, framework-specific implementation of diagnostic context. - *

The service used by this factory must implement {@link LoggingContextService}. If no - * implementation has been configured or could be instantiated, a no-op context service will be - * used, and no context will be stored or propagated. No errors will be generated, so that the application can - * still work (albeit without proper logging).

+ *

Factory to hide a concrete, framework-specific implementation of diagnostic context.

* - * @author evitaliy - * @since 07/01/2018. + *

The service used by this factory must implement {@link LoggingContextService}. If no implementation has been + * configured or could be instantiated, a no-op context service will be used, and no context will be + * stored or propagated. No errors will be generated, so that the application can still work (albeit without proper + * logging).

* + * @author evitaliy * @see ServiceBinder * @see LoggingContextService + * @since 07 Jan 2018 */ public class LoggingContext { - private static final LoggingContextService SERVICE = ServiceBinder.getContextServiceBinding().orElse( + private static final LoggingContextService SERVICE = + ServiceBinder.getContextServiceBinding().orElse( new NoOpLoggingContextService()); private LoggingContext() { // prevent instantiation } - public static void put(String key, String value) { - SERVICE.put(key, value); + public static void putRequestId(String requestId) { + SERVICE.putRequestId(requestId); } - public static String get(String key) { - return SERVICE.get(key); + public static void putServiceName(String serviceName) { + SERVICE.putServiceName(serviceName); } - public static void remove(String key) { - SERVICE.remove(key); + public static void putPartnerName(String partnerName) { + SERVICE.putPartnerName(partnerName); } public static void clear() { @@ -69,24 +70,19 @@ public class LoggingContext { private static class NoOpLoggingContextService implements LoggingContextService { - private static final String KEY_CANNOT_BE_NULL = "Key cannot be null"; - @Override - public void put(String key, String value) { - Objects.requireNonNull(key, KEY_CANNOT_BE_NULL); - // no-op + public void putRequestId(String requestId) { + Objects.requireNonNull(requestId, "Request ID cannot be null"); } @Override - public String get(String key) { - Objects.requireNonNull(key, KEY_CANNOT_BE_NULL); - return null; + public void putServiceName(String serviceName) { + Objects.requireNonNull(serviceName, "Service name cannot be null"); } @Override - public void remove(String key) { - Objects.requireNonNull(key, KEY_CANNOT_BE_NULL); - // no-op + public void putPartnerName(String partnerName) { + Objects.requireNonNull(partnerName, "Partner name cannot be null"); } @Override diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/spi/LoggingContextService.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/spi/LoggingContextService.java index 7aed0fc7dc..07b93c1468 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/spi/LoggingContextService.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/main/java/org/openecomp/sdc/logging/spi/LoggingContextService.java @@ -1,12 +1,12 @@ /* - * Copyright © 2016-2017 European Support Limited + * Copyright © 2016-2018 European Support Limited * * Licensed 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. @@ -19,37 +19,25 @@ package org.openecomp.sdc.logging.spi; import java.util.concurrent.Callable; /** - * Should be used to implement a framework-specific mechanism of managing a per-thread diagnostic context - * (for instance MDC), and propagating it to child threads if needed. - * Context propagation should be used when creating a child thread directly, or submitting tasks for potentially - * postponed execution via an - * Executor (including any of - * the - * executor services - * and ForkJoinPool). + * Should be used to implement a framework-specific mechanism of managing a per-thread diagnostic context (for instance + * MDC), and propagating it to child threads if needed. Context + * propagation should be used when creating a child thread directly, or submitting tasks for potentially postponed + * execution via an Executor + * (including any of the + * executor services and + * ForkJoinPool). * * @author evitaliy - * @since 07/01/2018. + * @since 07 Jan 2018 */ public interface LoggingContextService { - /** - * Allows to store a key-value pair on thread context - */ - void put(String key, String value); + void putRequestId(String requestId); - /** - * Returns the value associated with a key stored on thread context - * - * @return value or null if the key does not exits - */ - String get(String key); + void putServiceName(String serviceName); - /** - * Removes a particular key from thread context - */ - void remove(String key); + void putPartnerName(String partnerName); /** * Clear logging thread context @@ -57,14 +45,14 @@ public interface LoggingContextService { void clear(); /** - * Copies logging context of current thread onto a {@link Runnable}, so that the context is available - * when this {@link Runnable} runs in another thread. + * Copies logging context of current thread onto a {@link Runnable}, so that the context is available when this + * {@link Runnable} runs in another thread. */ Runnable copyToRunnable(Runnable runnable); /** - * Copies logging context of current thread onto a {@link Callable}, so that the context is available - * when this {@link Callable} runs in another thread + * Copies logging context of current thread onto a {@link Callable}, so that the context is available when this + * {@link Callable} runs in another thread */ Callable copyToCallable(Callable callable); } diff --git a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggingContextTest.java b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggingContextTest.java index 79252cde0b..bfc53a3146 100644 --- a/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggingContextTest.java +++ b/openecomp-be/lib/openecomp-sdc-logging-lib/openecomp-sdc-logging-api/src/test/java/org/openecomp/sdc/logging/api/LoggingContextTest.java @@ -16,14 +16,12 @@ package org.openecomp.sdc.logging.api; -import org.testng.annotations.Test; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertTrue; import java.lang.reflect.Field; import java.util.concurrent.Callable; - -import static org.testng.Assert.assertEquals; -import static org.testng.Assert.assertNull; -import static org.testng.Assert.assertTrue; +import org.testng.annotations.Test; /** * @author EVITALIY @@ -40,36 +38,19 @@ public class LoggingContextTest { "org.openecomp.sdc.logging.api.LoggingContext$NoOpLoggingContextService"); } - @Test - public void putDoesNotHaveEffectWhenNoBinding() { - final String key = "Key"; - LoggingContext.put(key, "Dummy"); - assertNull(LoggingContext.get(key)); - } - @Test(expectedExceptions = NullPointerException.class) - public void throwNpeWhenPutWithKeyNull() { - LoggingContext.put(null, "value"); - } - - @Test - public void getAlwaysReturnsNull() { - assertNull(LoggingContext.get("GetKey")); + public void throwNpeWhenPartnerNameIsNull() { + LoggingContext.putPartnerName(null); } @Test(expectedExceptions = NullPointerException.class) - public void throwNpeWhenGetWithKeyNull() { - LoggingContext.get(null); - } - - @Test - public void removeDoesNotFail() { - LoggingContext.remove("RemoveKey"); + public void throwNpeWhenServiceNameIsNull() { + LoggingContext.putServiceName(null); } @Test(expectedExceptions = NullPointerException.class) - public void throwNpWhenRemoveWithKeyNull() { - LoggingContext.remove(null); + public void throwNpeWhenRequestIdIsNull() { + LoggingContext.putRequestId(null); } @Test -- cgit 1.2.3-korg