<commit_before>/*
   Copyright 2019 Integration Partners

   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.
*/
package nl.nn.adapterframework.stream;

import org.apache.log4j.Logger;

import nl.nn.adapterframework.util.LogUtil;

public class ThreadConnector {
	protected Logger log = LogUtil.getLogger(this);

	private ThreadLifeCycleEventListener<Object> threadLifeCycleEventListener;
	private Thread parentThread;
	private Object threadInfo;
	
	public ThreadConnector(Object owner, ThreadLifeCycleEventListener<Object> threadLifeCycleEventListener, String correlationID) {
		super();
		this.threadLifeCycleEventListener=threadLifeCycleEventListener;
		threadInfo=threadLifeCycleEventListener!=null?threadLifeCycleEventListener.announceChildThread(owner, correlationID):null;
		parentThread=Thread.currentThread();
	}

	
	public Object startThread(Object input) {
		Thread currentThread = Thread.currentThread();
		if (currentThread!=parentThread) {
			currentThread.setName(parentThread.getName()+"/"+currentThread.getName());
			// Commented out code below. Do not set contextClassLoader, contextClassLoader is not reliable outside configure().
			// if (currentThread.getContextClassLoader()!=parentThread.getContextClassLoader()) {
			//	currentThread.setContextClassLoader(parentThread.getContextClassLoader());
			// }
		} else {
			threadLifeCycleEventListener=null;
		}
		if (threadLifeCycleEventListener!=null) {
			return threadLifeCycleEventListener.threadCreated(threadInfo, input);
		}
		return input;
	}

	public Object endThread(Object response) {
		if (threadLifeCycleEventListener!=null) {
			return threadLifeCycleEventListener.threadEnded(threadInfo, response);
		}
		return response;
	}

	public Throwable abortThread(Throwable t) {
		if (threadLifeCycleEventListener!=null) {
			Throwable t2 = threadLifeCycleEventListener.threadAborted(threadInfo, t);
			if (t2==null) {
				log.warn("Exception ignored by threadLifeCycleEventListener ("+t.getClass().getName()+"): "+t.getMessage());
			} else {
				return t2;
			}
		}
		return t;
	}
	
}
<commit_msg>Propagate the ThreadLocal hideRegex to subthreads (#346)<commit_after>/*
   Copyright 2019 Integration Partners

   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.
*/
package nl.nn.adapterframework.stream;

import org.apache.log4j.Logger;

import nl.nn.adapterframework.util.LogUtil;

public class ThreadConnector {
	protected Logger log = LogUtil.getLogger(this);

	private ThreadLifeCycleEventListener<Object> threadLifeCycleEventListener;
	private Thread parentThread;
	private Object threadInfo;
	private String hideRegex;
	
	public ThreadConnector(Object owner, ThreadLifeCycleEventListener<Object> threadLifeCycleEventListener, String correlationID) {
		super();
		this.threadLifeCycleEventListener=threadLifeCycleEventListener;
		threadInfo=threadLifeCycleEventListener!=null?threadLifeCycleEventListener.announceChildThread(owner, correlationID):null;
		parentThread=Thread.currentThread();
		hideRegex=LogUtil.getThreadHideRegex();
	}

	
	public Object startThread(Object input) {
		Thread currentThread = Thread.currentThread();
		if (currentThread!=parentThread) {
			currentThread.setName(parentThread.getName()+"/"+currentThread.getName());
			LogUtil.setThreadHideRegex(hideRegex);
			// Commented out code below. Do not set contextClassLoader, contextClassLoader is not reliable outside configure().
			// if (currentThread.getContextClassLoader()!=parentThread.getContextClassLoader()) {
			//	currentThread.setContextClassLoader(parentThread.getContextClassLoader());
			// }
		} else {
			threadLifeCycleEventListener=null;
		}
		if (threadLifeCycleEventListener!=null) {
			return threadLifeCycleEventListener.threadCreated(threadInfo, input);
		}
		return input;
	}

	public Object endThread(Object response) {
		try {
			if (threadLifeCycleEventListener!=null) {
				return threadLifeCycleEventListener.threadEnded(threadInfo, response);
			}
			return response;
		} finally {
			LogUtil.removeThreadHideRegex();
		}
	}

	public Throwable abortThread(Throwable t) {
		try {
			if (threadLifeCycleEventListener!=null) {
				Throwable t2 = threadLifeCycleEventListener.threadAborted(threadInfo, t);
				if (t2==null) {
					log.warn("Exception ignored by threadLifeCycleEventListener ("+t.getClass().getName()+"): "+t.getMessage());
				} else {
					return t2;
				}
			}
			return t;
		} finally {
			LogUtil.removeThreadHideRegex();
		}
	}
	
}

{'fraction_non_alphanumeric': 0.08913802609814372, 'fraction_numerical': 0.004962323102370888, 'mean_word_length': 5.066889632107023, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1464390', 'n_tokens_mistral': 1602, 'n_tokens_neox': 1465, 'n_words': 391}