All Downloads are FREE. Search and download functionalities are using the official Maven repository.

reactor.core.publisher.ContextTrackingFunctionWrapper Maven / Gradle / Ivy

Go to download

Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC

There is a newer version: 3.40.2
Show newest version
/*
 * Copyright (c) 2019-2021 VMware Inc. or its affiliates, 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
 *
 *   https://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 reactor.core.publisher;

import java.util.function.Function;

import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import reactor.core.CorePublisher;
import reactor.core.CoreSubscriber;
import reactor.core.publisher.FluxContextWrite.ContextWriteSubscriber;
import reactor.util.context.Context;

/**
 * This {@link Function} wrapper is used by operators like {@link Flux#transform(Function)}
 * to implement the context loss detection.
 *
 */
class ContextTrackingFunctionWrapper implements Function, CorePublisher> {

	static final String CONTEXT_MARKER_PREFIX = "reactor.core.context.marker.";

	final Function, ? extends Publisher> transformer;

	final String marker;

	ContextTrackingFunctionWrapper(Function, ? extends Publisher> transformer) {
		this(transformer, transformer.toString());
	}

	ContextTrackingFunctionWrapper(
			Function, ? extends Publisher> transformer,
			String marker
	) {
		this.transformer = transformer;
		this.marker = marker;
	}

	@Override
	public CorePublisher apply(Publisher source) {
		String key = CONTEXT_MARKER_PREFIX + System.identityHashCode(source);

		// Wrap source with a logic that will check whether the key is still there and remove it
		source = Operators.liftPublisher((p, actual) -> {
			Context ctx = actual.currentContext();

			if (!ctx.hasKey(key)) {
				throw new IllegalStateException("Context loss after applying " + marker);
			}

			Context newContext = ctx.delete(key);
			return new ContextWriteSubscriber<>(actual, newContext);
		}).apply(source);

		Publisher result = transformer.apply(source);

		// It is okay to return `CorePublisher` here since `transform` will use `from()` anyways
		return new CorePublisher() {
			@Override
			public void subscribe(CoreSubscriber actual) {
				Context ctx = actual.currentContext().put(key, true);
				CoreSubscriber subscriber = new ContextWriteSubscriber<>(actual, ctx);

				if (result instanceof CorePublisher) {
					((CorePublisher) result).subscribe(subscriber);
				}
				else {
					result.subscribe(subscriber);
				}
			}

			@Override
			public void subscribe(Subscriber subscriber) {
				subscribe(Operators.toCoreSubscriber(subscriber));
			}
		};
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy