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

com.openhtmltopdf.util.ThreadCtx Maven / Gradle / Ivy

Go to download

Open HTML to PDF is a CSS 2.1 renderer written in Java. This artifact contains the core rendering and layout code.

The newest version!
package com.openhtmltopdf.util;

import com.openhtmltopdf.layout.SharedContext;

import java.io.Closeable;
import java.util.function.Consumer;

/**
 * Because OpenHTMLtoPDF is designed to run in a single thread at all times for one invocation,
 * we can use a ThreadLocal to store pseudo global variables.
 * This MUST be set up in the appropriate renderer.
 */
public class ThreadCtx {

	private static final ThreadLocal data = ThreadLocal.withInitial(ThreadData::new);
	private static final ThreadLocal> diagnosticConsumer = new ThreadLocal<>();



	public static ThreadData get() {
		return data.get();
	}

	static void addDiagnostic(Diagnostic diagnostic) {
		Consumer consumer = diagnosticConsumer.get();
		if (consumer != null) {
			consumer.accept(diagnostic);
		}
	}
	
	public static void cleanup() {
		data.remove();
	}

	public static Closeable applyDiagnosticConsumer(Consumer consumer) {
		diagnosticConsumer.set(consumer);
		return diagnosticConsumer::remove;
	}

	public static class ThreadData {
		private ThreadData() { }
		private SharedContext sharedContext;
		
		public SharedContext sharedContext() {
			if (this.sharedContext == null)
				throw new NullPointerException("SharedContext must be registered in renderer.");
			
			return this.sharedContext;
		}
		
		public void setSharedContext(SharedContext sharedContext) {
			this.sharedContext = sharedContext;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy