From a974aa0cfb827476104c140096de676711d2b673 Mon Sep 17 00:00:00 2001 From: Pamela Dragosh Date: Tue, 14 Feb 2017 19:31:53 -0500 Subject: Initial OpenECOMP policy/common commit Change-Id: I61cd29d6d8bf8702c1a66915895b519bf3484afa Signed-off-by: Pamela Dragosh --- .../policy/common/logging/nsa/LoggingContext.java | 61 +++++++++++++++++++ .../common/logging/nsa/LoggingContextFactory.java | 55 +++++++++++++++++ .../common/logging/nsa/SharedLoggingContext.java | 37 +++++++++++ .../common/logging/nsa/impl/SharedContext.java | 58 ++++++++++++++++++ .../logging/nsa/impl/Slf4jLoggingContext.java | 71 ++++++++++++++++++++++ .../policy/common/logging/nsa/package-info.java | 27 ++++++++ 6 files changed, 309 insertions(+) create mode 100644 common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/LoggingContext.java create mode 100644 common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/LoggingContextFactory.java create mode 100644 common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/SharedLoggingContext.java create mode 100644 common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/impl/SharedContext.java create mode 100644 common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/impl/Slf4jLoggingContext.java create mode 100644 common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/package-info.java (limited to 'common-logging/src/main/java/org/openecomp/policy/common/logging/nsa') diff --git a/common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/LoggingContext.java b/common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/LoggingContext.java new file mode 100644 index 00000000..56f6686a --- /dev/null +++ b/common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/LoggingContext.java @@ -0,0 +1,61 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP-Logging + * ================================================================================ + * 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.policy.common.logging.nsa; + +/** + * An interface for providing data into the underlying logging context. Systems should use + * this interface rather than log system specific MDC solutions in order to reduce dependencies. + * + * A LoggingContext is specific to the calling thread. + * + */ +public interface LoggingContext +{ + /** + * Put a key/value pair into the logging context, replacing an entry with the same key. + * @param key + * @param value + */ + void put ( String key, String value ); + + /** + * Put a key/value pair into the logging context, replacing an entry with the same key. + * @param key + * @param value + */ + void put ( String key, long value ); + + /** + * Get a string value, returning the default value if the value is missing. + * @param key + * @param defaultValue + * @return a string value + */ + String get ( String key, String defaultValue ); + + /** + * Get a long value, returning the default value if the value is missing or not a long. + * @param key + * @param defaultValue + * @return a long value + */ + long get ( String key, long defaultValue ); +} diff --git a/common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/LoggingContextFactory.java b/common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/LoggingContextFactory.java new file mode 100644 index 00000000..b8fce10c --- /dev/null +++ b/common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/LoggingContextFactory.java @@ -0,0 +1,55 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP-Logging + * ================================================================================ + * 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.policy.common.logging.nsa; + + +import org.openecomp.policy.common.logging.nsa.impl.SharedContext; +import org.openecomp.policy.common.logging.nsa.impl.Slf4jLoggingContext; + +/** + * A factory for setting up a LoggingContext + * + */ +public class LoggingContextFactory +{ + public static class Builder + { + public Builder withBaseContext ( LoggingContext lc ) + { + fBase = lc; + return this; + } + + public Builder forSharing () + { + fShared = true; + return this; + } + + public LoggingContext build () + { + return fShared ? new SharedContext ( fBase ) : new Slf4jLoggingContext ( fBase ); + } + + private LoggingContext fBase = null; + private boolean fShared = false; + } +} diff --git a/common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/SharedLoggingContext.java b/common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/SharedLoggingContext.java new file mode 100644 index 00000000..2ecbcb0a --- /dev/null +++ b/common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/SharedLoggingContext.java @@ -0,0 +1,37 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP-Logging + * ================================================================================ + * 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.policy.common.logging.nsa; + +/** + * A logging context must be thread-specific. Contexts that implement SharedLoggingContext + * are expected to be shared across threads, and they have to be able to populate another + * logging context with their data. + * + */ +public interface SharedLoggingContext extends LoggingContext +{ + /** + * Copy this context's data to the given context. This must work across threads so that + * a base context can be shared in another thread. + * @param lc + */ + void transferTo ( SharedLoggingContext lc ); +} diff --git a/common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/impl/SharedContext.java b/common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/impl/SharedContext.java new file mode 100644 index 00000000..96ffbf64 --- /dev/null +++ b/common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/impl/SharedContext.java @@ -0,0 +1,58 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP-Logging + * ================================================================================ + * 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.policy.common.logging.nsa.impl; + +import java.util.HashMap; +import java.util.Map.Entry; + +import org.openecomp.policy.common.logging.nsa.LoggingContext; +import org.openecomp.policy.common.logging.nsa.SharedLoggingContext; + +/** + * A shared logging context for SLF4J + * + */ +public class SharedContext extends Slf4jLoggingContext implements SharedLoggingContext +{ + public SharedContext ( LoggingContext base ) + { + super ( base ); + fMap = new HashMap (); + } + + @Override + public void put ( String key, String value ) + { + super.put ( key, value ); + fMap.put ( key, value ); + } + + @Override + public void transferTo ( SharedLoggingContext lc ) + { + for ( Entry e : fMap.entrySet () ) + { + lc.put ( e.getKey(), e.getValue() ); + } + } + + private final HashMap fMap; +} diff --git a/common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/impl/Slf4jLoggingContext.java b/common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/impl/Slf4jLoggingContext.java new file mode 100644 index 00000000..de31af98 --- /dev/null +++ b/common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/impl/Slf4jLoggingContext.java @@ -0,0 +1,71 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP-Logging + * ================================================================================ + * 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.policy.common.logging.nsa.impl; + +import org.slf4j.MDC; + +import org.openecomp.policy.common.logging.nsa.LoggingContext; + +/** + * A logging context for SLF4J + * + */ +public class Slf4jLoggingContext implements LoggingContext +{ + public Slf4jLoggingContext ( LoggingContext base ) + { + } + + @Override + public void put ( String key, String value ) + { + MDC.put ( key, value ); + } + + public void put ( String key, long value ) + { + put ( key, "" + value ); + } + + + public String get ( String key, String defaultValue ) + { + String result = MDC.get ( key ); + if ( result == null ) + { + result = defaultValue; + } + return result; + } + + public long get ( String key, long defaultValue ) + { + final String str = get ( key, "" + defaultValue ); + try + { + return Long.parseLong ( str ); + } + catch ( NumberFormatException x ) + { + return defaultValue; + } + } +} diff --git a/common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/package-info.java b/common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/package-info.java new file mode 100644 index 00000000..e1ef710b --- /dev/null +++ b/common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/package-info.java @@ -0,0 +1,27 @@ +/*- + * ============LICENSE_START======================================================= + * ECOMP-Logging + * ================================================================================ + * 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========================================================= + */ + +/** + * This package provides a logging context infrastructure and a corresponding + * implementation based on the SLF4J/Log4j "MDC" (Mapped Diagnostic Context) feature. + * + */ +package org.openecomp.policy.common.logging.nsa; + -- cgit 1.2.3-korg