From 280f8015d06af1f41a3ef12e8300801c7a5e0d54 Mon Sep 17 00:00:00 2001 From: AviZi Date: Fri, 9 Jun 2017 02:39:56 +0300 Subject: [SDC-29] Amdocs OnBoard 1707 initial commit. Change-Id: Ie4d12a3f574008b792899b368a0902a8b46b5370 Signed-off-by: AviZi --- .../openecomp/core/logging/api/BaseFactory.java | 54 +++++ .../org/openecomp/core/logging/api/Logger.java | 103 +++++++++ .../core/logging/api/LoggerCreationService.java | 11 + .../openecomp/core/logging/api/LoggerFactory.java | 232 +++++++++++++++++++++ .../core/logging/api/annotations/Metrics.java | 15 ++ .../api/context/ContextPropagationService.java | 10 + .../core/logging/api/context/TaskFactory.java | 73 +++++++ 7 files changed, 498 insertions(+) create mode 100644 common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/BaseFactory.java create mode 100644 common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/Logger.java create mode 100644 common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/LoggerCreationService.java create mode 100644 common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/LoggerFactory.java create mode 100644 common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/annotations/Metrics.java create mode 100644 common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/context/ContextPropagationService.java create mode 100644 common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/context/TaskFactory.java (limited to 'common/openecomp-logging-lib/openecomp-logging-api/src/main') diff --git a/common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/BaseFactory.java b/common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/BaseFactory.java new file mode 100644 index 0000000000..14943805cc --- /dev/null +++ b/common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/BaseFactory.java @@ -0,0 +1,54 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.core.logging.api; + +import java.util.Iterator; +import java.util.ServiceLoader; + +/** + * Contains common functionality for factories used in the logging framework.

In order to use the + * factory, a particular (e.g. framework-specific) implementation of a service must be configured as + * described in java.util.ServiceLoader).

+ * + * @see java.util.ServiceLoader + */ +public class BaseFactory { + + protected static T locateService(Class clazz) throws Exception { + + T service; + ServiceLoader loader = ServiceLoader.load(clazz); + Iterator iterator = loader.iterator(); + if (iterator.hasNext()) { + + service = iterator.next(); + if (iterator.hasNext()) { + System.err.println(String.format("Warning! Configured more than one implementation of %s", + clazz.getName())); + } + + return service; + } + + throw new IllegalArgumentException( + (String.format("No implementations configured for %s", clazz.getName()))); + } +} diff --git a/common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/Logger.java b/common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/Logger.java new file mode 100644 index 0000000000..0fe3d8f230 --- /dev/null +++ b/common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/Logger.java @@ -0,0 +1,103 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.core.logging.api; + +/** + *

This interface defines logging as specified by OPENECOMP logging requirements.

Formatted + * messages must follow the Factory to hide a concrete, framework-specific implementation of logger creation.

The + * service used by this factory must implement {@link LoggerCreationService}. If no implementation + * has been configured or could not be instantiated, a no-op logger will be used, and no + * events will be logged. This is done to prevent recursion if attempts are being made to log + * exceptions that resulted from logger initialization.

+ * + * @see BaseFactory + * @see LoggerCreationService + */ +@SuppressWarnings("ThrowableInstanceNeverThrown") +public class LoggerFactory extends BaseFactory { + + private static final LoggerCreationService SERVICE; + + static { + + LoggerCreationService service; + + try { + service = locateService(LoggerCreationService.class); + } catch (Throwable throwable) { + new RuntimeException("Failed to instantiate logger factory", throwable).printStackTrace(); + // use the no-op service to prevent recursion in case of an attempt to log an exception as a + // result of a logger initialization error + service = new NoOpLoggerCreationService(); + } + + SERVICE = service; + } + + public static Logger getLogger(String clazzName) { + return SERVICE.getLogger(clazzName); + } + + public static Logger getLogger(Class clazz) { + return SERVICE.getLogger(clazz); + } + + private static class NoOpLoggerCreationService implements LoggerCreationService { + + private static final Logger NO_OP_LOGGER = new Logger() { + + @Override + public String getName() { + return "No-Op Logger"; + } + + @Override + public boolean isMetricsEnabled() { + return false; + } + + @Override + public void metrics(String msg) { + } + + @Override + public void metrics(String msg, Object arg) { + } + + @Override + public void metrics(String msg, Object arg1, Object arg2) { + } + + @Override + public void metrics(String msg, Object... arguments) { + } + + @Override + public void metrics(String msg, Throwable throwable) { + } + + @Override + public boolean isAuditEnabled() { + return false; + } + + @Override + public void audit(String msg) { + } + + @Override + public void audit(String msg, Object arg) { + } + + @Override + public void audit(String msg, Object arg1, Object arg2) { + } + + @Override + public void audit(String msg, Object... arguments) { + } + + @Override + public void audit(String msg, Throwable throwable) { + } + + @Override + public boolean isDebugEnabled() { + return false; + } + + @Override + public void debug(String msg) { + } + + @Override + public void debug(String msg, Object arg) { + } + + @Override + public void debug(String msg, Object arg1, Object arg2) { + } + + @Override + public void debug(String msg, Object... arguments) { + } + + @Override + public void debug(String msg, Throwable throwable) { + } + + @Override + public boolean isInfoEnabled() { + return false; + } + + @Override + public void info(String msg) { + } + + @Override + public void info(String msg, Object arg) { + } + + @Override + public void info(String msg, Object arg1, Object arg2) { + } + + @Override + public void info(String msg, Object... arguments) { + } + + @Override + public void info(String msg, Throwable throwable) { + } + + @Override + public boolean isWarnEnabled() { + return false; + } + + @Override + public void warn(String msg) { + } + + @Override + public void warn(String msg, Object arg) { + } + + @Override + public void warn(String msg, Object... arguments) { + } + + @Override + public void warn(String msg, Object arg1, Object arg2) { + } + + @Override + public void warn(String msg, Throwable throwable) { + } + + @Override + public boolean isErrorEnabled() { + return false; + } + + @Override + public void error(String msg) { + } + + @Override + public void error(String msg, Object arg) { + } + + @Override + public void error(String msg, Object arg1, Object arg2) { + } + + @Override + public void error(String msg, Object... arguments) { + } + + @Override + public void error(String msg, Throwable throwable) { + } + }; + + @Override + public Logger getLogger(String className) { + return NO_OP_LOGGER; + } + + @Override + public Logger getLogger(Class clazz) { + return NO_OP_LOGGER; + } + } +} diff --git a/common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/annotations/Metrics.java b/common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/annotations/Metrics.java new file mode 100644 index 0000000000..1588bcafc0 --- /dev/null +++ b/common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/annotations/Metrics.java @@ -0,0 +1,15 @@ +package org.openecomp.core.logging.api.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Indicates a method whose execution time should be measured and logged as required for OPENECOMP + * metrics. + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface Metrics { +} diff --git a/common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/context/ContextPropagationService.java b/common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/context/ContextPropagationService.java new file mode 100644 index 0000000000..8e9c2dfa28 --- /dev/null +++ b/common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/context/ContextPropagationService.java @@ -0,0 +1,10 @@ +package org.openecomp.core.logging.api.context; + +/** + * Should be used to implement a framework-specific mechanism of propagation of a diagnostic context + * to child threads. + */ +public interface ContextPropagationService { + + Runnable create(Runnable task); +} diff --git a/common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/context/TaskFactory.java b/common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/context/TaskFactory.java new file mode 100644 index 0000000000..b358a77573 --- /dev/null +++ b/common/openecomp-logging-lib/openecomp-logging-api/src/main/java/org/openecomp/core/logging/api/context/TaskFactory.java @@ -0,0 +1,73 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * 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. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + */ + +package org.openecomp.core.logging.api.context; + +import org.openecomp.core.logging.api.BaseFactory; + +/** + *

Should be used to propagate a diagnostic context (for instance MDC) to other threads.

Applicable when + * creating a child thread directly, or submitting tasks for potentially postponed execution via an + * Executor + * (including any of the executor + * services and ForkJoinPool).

+ *

The service used by this factory must implement {@link ContextPropagationService}.

+ * + * @see ContextPropagationService + */ +@SuppressWarnings("ThrowableInstanceNeverThrown") +public class TaskFactory extends BaseFactory { + + private static final ContextPropagationService SERVICE; + private static final RuntimeException ERROR; + + static { + + ContextPropagationService service = null; + RuntimeException error = null; + + try { + service = locateService(ContextPropagationService.class); + } catch (Throwable throwable) { + error = new RuntimeException("Failed to instantiate task factory", throwable); + } + + SERVICE = service; + ERROR = error; + } + + /** + * Modify a task so that a diagnostic context is propagated to the thread when the task runs. Done + * in a logging-framework specific way. + * + * @param task any Runnable that will run in a thread + * @return modified (wrapped) original task that runs the same business logic, but also takes care + of copying the diagnostic context for logging. + */ + public static Runnable create(Runnable task) { + + if (SERVICE == null) { + throw ERROR; + } + + return SERVICE.create(task); + } +} -- cgit 1.2.3-korg