aboutsummaryrefslogtreecommitdiffstats
path: root/common-logging/src/main/java/org/openecomp/policy/common/logging/nsa
diff options
context:
space:
mode:
Diffstat (limited to 'common-logging/src/main/java/org/openecomp/policy/common/logging/nsa')
-rw-r--r--common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/LoggingContext.java61
-rw-r--r--common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/LoggingContextFactory.java55
-rw-r--r--common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/SharedLoggingContext.java37
-rw-r--r--common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/impl/SharedContext.java58
-rw-r--r--common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/impl/Slf4jLoggingContext.java71
-rw-r--r--common-logging/src/main/java/org/openecomp/policy/common/logging/nsa/package-info.java27
6 files changed, 309 insertions, 0 deletions
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<String,String> ();
+ }
+
+ @Override
+ public void put ( String key, String value )
+ {
+ super.put ( key, value );
+ fMap.put ( key, value );
+ }
+
+ @Override
+ public void transferTo ( SharedLoggingContext lc )
+ {
+ for ( Entry<String,String> e : fMap.entrySet () )
+ {
+ lc.put ( e.getKey(), e.getValue() );
+ }
+ }
+
+ private final HashMap<String,String> 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;
+