Skip to content

Commit 722f624

Browse files
authored
Add deprecations for v2 simplification (#510)
* Deprecate Clearable to be removed in next major release. * Deprecate complicated things and introduce new ContextObserver mechanism. * Switch to new minor version due to relatively big change in this PR. * Move ContextSnapshot to api package. * Move Context to api package and improve documentation. * Move ContextManager to api package and improve documentation. * Simplify Wrapper base class. * Simplify Wrapper-related classes and methods. * Move ContextManagers class to core package. * Move ContextTimer service interface to api package. * Remove observer registration methods that were never released. * Add test for new wrapper method. * Move Wrapper base class to core package * Deprecate ContextSnapshotSupplier * Move DelegatingFuture to core.delegation package * Move DelegatingExecutorService to core.delegation package * Move WrapperWithContext to core.delegation package * Move CallMappingExecutorService to core.delegation package * Move ContextAwareExecutorService to core.concurrent package * Move AbstractThreadLocalContext to core.threadlocal package * Move ContextAwareCompletableFuture to core.concurrent package * Move BiConsumerWithContext to core.function package * Move BiFunctionWithContext to core.function package * Move BinaryOperatorWithContext to core.function package * Move BiPredicateWithContext to core.function package * Move BooleanSupplierWithContext to core.function package * Move ConsumerWithContext to core.function package * Move FunctionWithContext to core.function package * Move PredicateWithContext to core.function package * Move RunnableWithContext to core.function package * Move SupplierWithContext to core.function package * Move UnaryOperatorWithContext to core.function package * Clean up left over stuff from old ContextManagers class. * Rename Timers logger. * Add unit test for concurrent observer registration. * Add unit test new context observer mechanism. --------- Signed-off-by: Sjoerd Talsma <[email protected]>
1 parent bbace8f commit 722f624

File tree

98 files changed

+5710
-1276
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+5710
-1276
lines changed

context-propagation-bom/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2016-2022 Talsma ICT
4+
Copyright 2016-2024 Talsma ICT
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -22,7 +22,7 @@
2222
<parent>
2323
<groupId>nl.talsmasoftware.context</groupId>
2424
<artifactId>context-propagation-root</artifactId>
25-
<version>1.0.12-SNAPSHOT</version>
25+
<version>1.1.0-SNAPSHOT</version>
2626
</parent>
2727

2828
<artifactId>context-propagation-bom</artifactId>

context-propagation-java8/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!--
33
4-
Copyright 2016-2022 Talsma ICT
4+
Copyright 2016-2024 Talsma ICT
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -24,7 +24,7 @@
2424
<parent>
2525
<groupId>nl.talsmasoftware.context</groupId>
2626
<artifactId>context-propagation-root</artifactId>
27-
<version>1.0.12-SNAPSHOT</version>
27+
<version>1.1.0-SNAPSHOT</version>
2828
</parent>
2929

3030
<!-- Artifact identification -->

context-propagation-java8/src/main/java/nl/talsmasoftware/context/core/concurrent/ContextAwareCompletableFuture.java

Lines changed: 801 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2016-2024 Talsma ICT
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package nl.talsmasoftware.context.core.concurrent;
17+
18+
import nl.talsmasoftware.context.ContextManagers;
19+
import nl.talsmasoftware.context.ContextSnapshot;
20+
21+
import java.util.function.Consumer;
22+
import java.util.function.Supplier;
23+
24+
import static java.util.Objects.requireNonNull;
25+
26+
/**
27+
* Snapshot holder that is used internally to temporarily 'hold' a context snapshot
28+
* to be propagated from one {@code CompletionStage} to another.
29+
*
30+
* @author Sjoerd Talsma
31+
*/
32+
final class ContextSnapshotHolder implements Consumer<ContextSnapshot>, Supplier<ContextSnapshot> {
33+
private volatile ContextSnapshot snapshot;
34+
35+
/**
36+
* Create a new snapshot holder initially containing either the provided snapshot,
37+
* or takes a new snapshot as initial value.
38+
*
39+
* @param snapshot The snapshot to hold initially.
40+
* Optional, if {@code null} the holder will initialize with a new snapshot.
41+
*/
42+
ContextSnapshotHolder(ContextSnapshot snapshot) {
43+
this.snapshot = (snapshot == null ? ContextManagers.createContextSnapshot() : snapshot);
44+
}
45+
46+
/**
47+
* Accept a new snapshot (i.e. after a single stage is completed) to be propagated
48+
* into another completion stage.
49+
*
50+
* @param snapshot The snapshot to hold (required, must not be {@code null})
51+
*/
52+
@Override
53+
public void accept(ContextSnapshot snapshot) {
54+
this.snapshot = requireNonNull(snapshot, "Context snapshot is <null>.");
55+
}
56+
57+
/**
58+
* Returns the current snapshot, normally to activate when beginning a new completion stage.
59+
*
60+
* @return The held snapshot (should never be {@code null})
61+
*/
62+
@Override
63+
public ContextSnapshot get() {
64+
return snapshot;
65+
}
66+
67+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2016-2024 Talsma ICT
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package nl.talsmasoftware.context.core.function;
17+
18+
import nl.talsmasoftware.context.Context;
19+
import nl.talsmasoftware.context.ContextManagers;
20+
import nl.talsmasoftware.context.ContextSnapshot;
21+
22+
import java.util.function.BiConsumer;
23+
import java.util.function.Consumer;
24+
import java.util.function.Supplier;
25+
import java.util.logging.Level;
26+
import java.util.logging.Logger;
27+
28+
import static java.util.Objects.requireNonNull;
29+
30+
/**
31+
* A wrapper for {@link BiConsumer} that {@link ContextSnapshot#reactivate() reactivates a context snapshot} before
32+
* calling a delegate.
33+
*
34+
* @author Sjoerd Talsma
35+
*/
36+
public class BiConsumerWithContext<T, U> extends WrapperWithContextAndConsumer<BiConsumer<T, U>> implements BiConsumer<T, U> {
37+
private static final Logger LOGGER = Logger.getLogger(BiConsumerWithContext.class.getName());
38+
39+
public BiConsumerWithContext(ContextSnapshot snapshot, BiConsumer<T, U> delegate) {
40+
this(snapshot, delegate, null);
41+
}
42+
43+
public BiConsumerWithContext(ContextSnapshot snapshot, BiConsumer<T, U> delegate, Consumer<ContextSnapshot> consumer) {
44+
super(snapshot, delegate, consumer);
45+
}
46+
47+
protected BiConsumerWithContext(Supplier<ContextSnapshot> supplier, BiConsumer<T, U> delegate, Consumer<ContextSnapshot> consumer) {
48+
super(supplier, delegate, consumer);
49+
}
50+
51+
@Override
52+
public void accept(T t, U u) {
53+
try (Context<Void> context = snapshot().reactivate()) {
54+
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
55+
LOGGER.log(Level.FINEST, "Delegating accept method with {0} to {1}.", new Object[]{context, delegate()});
56+
delegate().accept(t, u);
57+
} finally {
58+
if (contextSnapshotConsumer != null) {
59+
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
60+
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
61+
contextSnapshotConsumer.accept(resultSnapshot);
62+
}
63+
}
64+
}
65+
}
66+
67+
@Override
68+
public BiConsumer<T, U> andThen(BiConsumer<? super T, ? super U> after) {
69+
requireNonNull(after, "Cannot post-process with after bi-consumer <null>.");
70+
return (l, r) -> {
71+
try (Context<Void> context = snapshot().reactivate()) {
72+
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
73+
LOGGER.log(Level.FINEST, "Delegating andThen method with {0} to {1}.", new Object[]{context, delegate()});
74+
delegate().accept(l, r);
75+
after.accept(l, r);
76+
} finally {
77+
if (contextSnapshotConsumer != null) {
78+
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
79+
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
80+
contextSnapshotConsumer.accept(resultSnapshot);
81+
}
82+
}
83+
}
84+
};
85+
}
86+
87+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2016-2024 Talsma ICT
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package nl.talsmasoftware.context.core.function;
17+
18+
import nl.talsmasoftware.context.Context;
19+
import nl.talsmasoftware.context.ContextManagers;
20+
import nl.talsmasoftware.context.ContextSnapshot;
21+
22+
import java.util.function.BiFunction;
23+
import java.util.function.Consumer;
24+
import java.util.function.Function;
25+
import java.util.function.Supplier;
26+
import java.util.logging.Level;
27+
import java.util.logging.Logger;
28+
29+
import static java.util.Objects.requireNonNull;
30+
31+
/**
32+
* A wrapper for {@link BiFunction} that {@link ContextSnapshot#reactivate() reactivates a context snapshot} before
33+
* calling a delegate.
34+
*
35+
* @author Sjoerd Talsma
36+
*/
37+
public class BiFunctionWithContext<IN1, IN2, OUT> extends WrapperWithContextAndConsumer<BiFunction<IN1, IN2, OUT>> implements BiFunction<IN1, IN2, OUT> {
38+
private static final Logger LOGGER = Logger.getLogger(BiFunctionWithContext.class.getName());
39+
40+
public BiFunctionWithContext(ContextSnapshot snapshot, BiFunction<IN1, IN2, OUT> delegate) {
41+
this(snapshot, delegate, null);
42+
}
43+
44+
public BiFunctionWithContext(ContextSnapshot snapshot, BiFunction<IN1, IN2, OUT> delegate, Consumer<ContextSnapshot> consumer) {
45+
super(snapshot, delegate, consumer);
46+
}
47+
48+
protected BiFunctionWithContext(Supplier<ContextSnapshot> supplier, BiFunction<IN1, IN2, OUT> delegate, Consumer<ContextSnapshot> consumer) {
49+
super(supplier, delegate, consumer);
50+
}
51+
52+
@Override
53+
public OUT apply(IN1 in1, IN2 in2) {
54+
try (Context<Void> context = snapshot().reactivate()) {
55+
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
56+
LOGGER.log(Level.FINEST, "Delegating apply method with {0} to {1}.", new Object[]{context, delegate()});
57+
return delegate().apply(in1, in2);
58+
} finally {
59+
if (contextSnapshotConsumer != null) {
60+
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
61+
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
62+
contextSnapshotConsumer.accept(resultSnapshot);
63+
}
64+
}
65+
}
66+
}
67+
68+
@Override
69+
public <V> BiFunction<IN1, IN2, V> andThen(Function<? super OUT, ? extends V> after) {
70+
requireNonNull(after, "Cannot post-process bi-function with after function <null>.");
71+
return (IN1 in1, IN2 in2) -> {
72+
try (Context<Void> context = snapshot().reactivate()) {
73+
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
74+
LOGGER.log(Level.FINEST, "Delegating andThen method with {0} to {1}.", new Object[]{context, delegate()});
75+
return after.apply(delegate().apply(in1, in2));
76+
} finally {
77+
if (contextSnapshotConsumer != null) {
78+
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
79+
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
80+
contextSnapshotConsumer.accept(resultSnapshot);
81+
}
82+
}
83+
}
84+
};
85+
}
86+
87+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2016-2024 Talsma ICT
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package nl.talsmasoftware.context.core.function;
17+
18+
import nl.talsmasoftware.context.Context;
19+
import nl.talsmasoftware.context.ContextManagers;
20+
import nl.talsmasoftware.context.ContextSnapshot;
21+
22+
import java.util.function.BiPredicate;
23+
import java.util.function.Consumer;
24+
import java.util.function.Supplier;
25+
import java.util.logging.Level;
26+
import java.util.logging.Logger;
27+
28+
import static java.util.Objects.requireNonNull;
29+
30+
/**
31+
* A wrapper for {@link BiPredicate} that {@link ContextSnapshot#reactivate() reactivates a context snapshot} before
32+
* calling a delegate.
33+
*
34+
* @author Sjoerd Talsma
35+
*/
36+
public class BiPredicateWithContext<IN1, IN2> extends WrapperWithContextAndConsumer<BiPredicate<IN1, IN2>> implements BiPredicate<IN1, IN2> {
37+
private static final Logger LOGGER = Logger.getLogger(BiPredicateWithContext.class.getName());
38+
39+
public BiPredicateWithContext(ContextSnapshot snapshot, BiPredicate<IN1, IN2> delegate) {
40+
this(snapshot, delegate, null);
41+
}
42+
43+
public BiPredicateWithContext(ContextSnapshot snapshot, BiPredicate<IN1, IN2> delegate, Consumer<ContextSnapshot> consumer) {
44+
super(snapshot, delegate, consumer);
45+
}
46+
47+
protected BiPredicateWithContext(Supplier<ContextSnapshot> supplier, BiPredicate<IN1, IN2> delegate, Consumer<ContextSnapshot> consumer) {
48+
super(supplier, delegate, consumer);
49+
}
50+
51+
@Override
52+
public boolean test(IN1 in1, IN2 in2) {
53+
try (Context<Void> context = snapshot().reactivate()) {
54+
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
55+
LOGGER.log(Level.FINEST, "Delegating test method with {0} to {1}.", new Object[]{context, delegate()});
56+
return delegate().test(in1, in2);
57+
} finally {
58+
if (contextSnapshotConsumer != null) {
59+
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
60+
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
61+
contextSnapshotConsumer.accept(resultSnapshot);
62+
}
63+
}
64+
}
65+
}
66+
67+
@Override
68+
public BiPredicate<IN1, IN2> and(BiPredicate<? super IN1, ? super IN2> other) {
69+
requireNonNull(other, "Cannot combine bi-predicate with 'and' <null>.");
70+
return (IN1 in1, IN2 in2) -> {
71+
try (Context<Void> context = snapshot().reactivate()) {
72+
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
73+
LOGGER.log(Level.FINEST, "Delegating 'and' method with {0} to {1}.", new Object[]{context, delegate()});
74+
return delegate().test(in1, in2) && other.test(in1, in2);
75+
} finally {
76+
if (contextSnapshotConsumer != null) {
77+
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
78+
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
79+
contextSnapshotConsumer.accept(resultSnapshot);
80+
}
81+
}
82+
}
83+
};
84+
}
85+
86+
@Override
87+
public BiPredicate<IN1, IN2> or(BiPredicate<? super IN1, ? super IN2> other) {
88+
requireNonNull(other, "Cannot combine bi-predicate with 'or' <null>.");
89+
return (IN1 in1, IN2 in2) -> {
90+
try (Context<Void> context = snapshot().reactivate()) {
91+
try { // inner 'try' is needed: https://github.com/talsma-ict/context-propagation/pull/56#discussion_r201590623
92+
LOGGER.log(Level.FINEST, "Delegating 'or' method with {0} to {1}.", new Object[]{context, delegate()});
93+
return delegate().test(in1, in2) || other.test(in1, in2);
94+
} finally {
95+
if (contextSnapshotConsumer != null) {
96+
ContextSnapshot resultSnapshot = ContextManagers.createContextSnapshot();
97+
LOGGER.log(Level.FINEST, "Captured context snapshot after delegation: {0}", resultSnapshot);
98+
contextSnapshotConsumer.accept(resultSnapshot);
99+
}
100+
}
101+
}
102+
};
103+
}
104+
}

0 commit comments

Comments
 (0)