You are viewing a plain text version of this content. The canonical link for it is here.
Posted to server-dev@james.apache.org by bt...@apache.org on 2017/08/24 03:46:28 UTC
[03/38] james-project git commit: JAMES-2114 Adding MDC management
utils
JAMES-2114 Adding MDC management utils
Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/47ceea4a
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/47ceea4a
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/47ceea4a
Branch: refs/heads/master
Commit: 47ceea4acf4ed2ef43729bfe69b5ce8a9ee9832a
Parents: 742c402
Author: benwa <bt...@linagora.com>
Authored: Mon Aug 14 10:30:09 2017 +0700
Committer: benwa <bt...@linagora.com>
Committed: Thu Aug 24 10:33:43 2017 +0700
----------------------------------------------------------------------
server/container/util-java8/pom.xml | 4 +
.../java/org/apache/james/util/MDCBuilder.java | 114 +++++++++++++++
.../org/apache/james/util/MDCBuilderTest.java | 137 +++++++++++++++++++
3 files changed, 255 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/james-project/blob/47ceea4a/server/container/util-java8/pom.xml
----------------------------------------------------------------------
diff --git a/server/container/util-java8/pom.xml b/server/container/util-java8/pom.xml
index e127afd..bd19e9f 100644
--- a/server/container/util-java8/pom.xml
+++ b/server/container/util-java8/pom.xml
@@ -71,6 +71,10 @@
<scope>test</scope>
</dependency>
<dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ </dependency>
+ <dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
http://git-wip-us.apache.org/repos/asf/james-project/blob/47ceea4a/server/container/util-java8/src/main/java/org/apache/james/util/MDCBuilder.java
----------------------------------------------------------------------
diff --git a/server/container/util-java8/src/main/java/org/apache/james/util/MDCBuilder.java b/server/container/util-java8/src/main/java/org/apache/james/util/MDCBuilder.java
new file mode 100644
index 0000000..bf2d334
--- /dev/null
+++ b/server/container/util-java8/src/main/java/org/apache/james/util/MDCBuilder.java
@@ -0,0 +1,114 @@
+/****************************************************************
+ * 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.james.util;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.slf4j.MDC;
+
+import com.github.steveash.guavate.Guavate;
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+
+public class MDCBuilder {
+
+ public static final String HOST = "host";
+ public static final String IP = "ip";
+ public static final String PROTOCOL = "protocol";
+ public static final String USER = "user";
+ public static final String ACTION = "action";
+ public static final String SESSION_ID = "sessionId";
+ public static final String CHARSET = "charset";
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(MDCBuilder.class);
+
+ public static class Closeables implements Closeable {
+ private final List<Closeable> closeables;
+
+ public Closeables(List<Closeable> closeables) {
+ Preconditions.checkNotNull(closeables);
+ this.closeables = ImmutableList.copyOf(closeables);
+ }
+
+ @Override
+ public void close() throws IOException {
+ closeables.forEach(this::closeQuietly);
+ }
+
+ private void closeQuietly(Closeable closeable) {
+ try {
+ closeable.close();
+ } catch (IOException e) {
+ LOGGER.warn("Failed to close Closeable", e);
+ }
+ }
+ }
+
+ public static MDCBuilder create() {
+ return new MDCBuilder();
+ }
+
+ private final ImmutableMap.Builder<String, String> contextMap = ImmutableMap.builder();
+ private final ImmutableList.Builder<MDCBuilder> nestedBuilder = ImmutableList.builder();
+
+ private MDCBuilder() {}
+
+ public MDCBuilder addContext(MDCBuilder nested) {
+ this.nestedBuilder.add(nested);
+ return this;
+ }
+
+ public MDCBuilder addContext(String key, Object value) {
+ Preconditions.checkNotNull(key);
+ Optional.ofNullable(value)
+ .ifPresent(nonNullValue -> contextMap.put(key, nonNullValue.toString()));
+ return this;
+ }
+
+ @VisibleForTesting
+ Map<String, String> buildContextMap() {
+ return ImmutableMap.<String, String>builder()
+ .putAll(nestedBuilder.build()
+ .stream()
+ .map(MDCBuilder::buildContextMap)
+ .flatMap(map -> map.entrySet().stream())
+ .collect(Guavate.toImmutableMap(Map.Entry::getKey, Map.Entry::getValue)))
+ .putAll(contextMap.build())
+ .build();
+ }
+
+ public Closeable build() {
+ return new Closeables(
+ buildContextMap()
+ .entrySet()
+ .stream()
+ .map(entry -> MDC.putCloseable(entry.getKey(), entry.getValue()))
+ .collect(Guavate.toImmutableList()));
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/james-project/blob/47ceea4a/server/container/util-java8/src/test/java/org/apache/james/util/MDCBuilderTest.java
----------------------------------------------------------------------
diff --git a/server/container/util-java8/src/test/java/org/apache/james/util/MDCBuilderTest.java b/server/container/util-java8/src/test/java/org/apache/james/util/MDCBuilderTest.java
new file mode 100644
index 0000000..8e4d4f1
--- /dev/null
+++ b/server/container/util-java8/src/test/java/org/apache/james/util/MDCBuilderTest.java
@@ -0,0 +1,137 @@
+/****************************************************************
+ * 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.james.util;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.Closeable;
+import java.io.IOException;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.testcontainers.shaded.com.google.common.collect.ImmutableList;
+
+public class MDCBuilderTest {
+
+ private static final String KEY_1 = "key1";
+ private static final String KEY_2 = "key2";
+ private static final String VALUE_1 = "value1";
+ private static final String VALUE_2 = "value2";
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+
+ @Test
+ public void addContextShouldThrowOnNullKey() {
+ expectedException.expect(NullPointerException.class);
+
+ MDCBuilder.create()
+ .addContext(null, "any");
+ }
+
+ @Test
+ public void buildContextMapShouldReturnEmptyWhenNoContext() {
+ assertThat(MDCBuilder.create().buildContextMap())
+ .isEmpty();
+ }
+
+ @Test
+ public void buildContextMapShouldReturnContext() {
+ assertThat(
+ MDCBuilder.create()
+ .addContext(KEY_1, VALUE_1)
+ .addContext(KEY_2, VALUE_2)
+ .buildContextMap())
+ .containsOnlyKeys(KEY_1, KEY_2)
+ .containsEntry(KEY_1, VALUE_1)
+ .containsEntry(KEY_2, VALUE_2);
+ }
+
+ @Test
+ public void addContextShouldFilterOutNullValues() {
+ assertThat(
+ MDCBuilder.create()
+ .addContext(KEY_1, null)
+ .buildContextMap())
+ .isEmpty();
+ }
+
+ @Test
+ public void addContextShouldAllowRecursiveBuild() {
+ assertThat(
+ MDCBuilder.create()
+ .addContext(KEY_1, VALUE_1)
+ .addContext(MDCBuilder.create()
+ .addContext(KEY_2, VALUE_2))
+ .buildContextMap())
+ .containsOnlyKeys(KEY_1, KEY_2)
+ .containsEntry(KEY_1, VALUE_1)
+ .containsEntry(KEY_2, VALUE_2);
+ }
+
+ @Test
+ public void closeablesConstructorShouldThrowOnNullList() {
+ expectedException.expect(NullPointerException.class);
+
+ new MDCBuilder.Closeables(null);
+ }
+
+ @Test
+ public void closeablesCloseShouldNotThrowWhenEmpty() throws IOException {
+ new MDCBuilder.Closeables(ImmutableList.of())
+ .close();
+ }
+
+ @Test
+ public void closeablesCloseShouldCallAllUnderlyingCloseables() throws IOException {
+ ImmutableList.Builder<String> builder = ImmutableList.builder();
+
+ Closeable closeable1 = () -> builder.add(VALUE_1);
+ Closeable closeable2 = () -> builder.add(VALUE_2);
+
+ new MDCBuilder.Closeables(
+ ImmutableList.of(closeable1, closeable2))
+ .close();
+
+ assertThat(builder.build())
+ .containsExactly(VALUE_1, VALUE_2);
+ }
+
+
+ @Test
+ public void closeablesCloseShouldCallAllUnderlyingCloseablesWhenError() throws IOException {
+ ImmutableList.Builder<String> builder = ImmutableList.builder();
+
+ Closeable closeable1 = () -> builder.add(VALUE_1);
+ Closeable closeable2 = () -> {
+ throw new IOException();
+ };
+ Closeable closeable3 = () -> builder.add(VALUE_2);
+
+ new MDCBuilder.Closeables(
+ ImmutableList.of(closeable1, closeable2, closeable3))
+ .close();
+
+ assertThat(builder.build())
+ .containsExactly(VALUE_1, VALUE_2);
+ }
+
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org