<commit_before>/*
 * Copyright 2010 <NAME>
 *
 * 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 groovyx.remote.client

import groovyx.remote.*

/**
 * Executes closures in a server, communicating via a Transport.
 */
class RemoteControl {
	
	final Transport transport
	protected final CommandGenerator commandGenerator

	/**
	 * If set, {@code null} will be used as the return value if the actual return value was not serializable.
	 * 
	 * This prevents a UnserializableReturnException from being thrown.
	 */
	boolean useNullIfResultWasUnserializable = false

	/**
	 * If set, the string representation of the actual return value will be used if the actual return value was not serializable.
	 * 
	 * This prevents a UnserializableReturnException from being thrown.
	 */
	boolean useStringRepresentationIfResultWasUnserializable = false	
	
	/**
	 * Creates a remote using the given transport and the current thread's contextClassLoader.
	 * 
	 * @see RemoteControl(Transport, ClassLoader)
	 */
	RemoteControl(Transport transport) {
		this(transport, Thread.currentThread().contextClassLoader)
	}

	/**
	 * Creates a remote using the given transport and the given classLoader.
	 */
	RemoteControl(Transport transport, ClassLoader classLoader) {
		this(transport, new CommandGenerator(classLoader))
	}
	
	/**
	 * Hook for subclasses to provide a custom command generator.
	 */
	protected RemoteControl(Transport transport, CommandGenerator commandGenerator) {
		this.transport = transport
		this.commandGenerator = commandGenerator
	}
	
	def exec(Closure[] commands) {
		processResult(sendCommandChain(generateCommandChain(commands)))
	}
	
	def call(Closure[] commands) {
		exec(commands)
	}
	
	protected CommandChain generateCommandChain(Closure[] commands) {
		new CommandChain(commands: commands.collect { commandGenerator.generate(it) })
	}
	
	protected Result sendCommandChain(CommandChain commandChain) {
		transport.send(commandChain)
	}
	
	protected processResult(Result result) {
		if (result.wasNull) {
			null
		} else if (result.wasUnserializable) {
			if (useNullIfResultWasUnserializable) {
				null
			} else if (useStringRepresentationIfResultWasUnserializable) {
				result.stringRepresentation
			} else {
				throw new UnserializableReturnException(result)
			}
		} else if (result.thrown) {
			throw new RemoteException(result.thrown)
		} else {
			result.value
		}
	}
}
<commit_msg>whitespace.<commit_after>/*
 * Copyright 2010 <NAME>
 *
 * 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 groovyx.remote.client

import groovyx.remote.*

/**
 * Executes closures in a server, communicating via a Transport.
 */
class RemoteControl {
	
	final Transport transport
	protected final CommandGenerator commandGenerator

	/**
	 * If set, {@code null} will be used as the return value if the actual return value was not serializable.
	 * 
	 * This prevents a UnserializableReturnException from being thrown.
	 */
	boolean useNullIfResultWasUnserializable = false

	/**
	 * If set, the string representation of the actual return value will be used if the actual return value was not serializable.
	 * 
	 * This prevents a UnserializableReturnException from being thrown.
	 */
	boolean useStringRepresentationIfResultWasUnserializable = false
	
	/**
	 * Creates a remote using the given transport and the current thread's contextClassLoader.
	 * 
	 * @see RemoteControl(Transport, ClassLoader)
	 */
	RemoteControl(Transport transport) {
		this(transport, Thread.currentThread().contextClassLoader)
	}

	/**
	 * Creates a remote using the given transport and the given classLoader.
	 */
	RemoteControl(Transport transport, ClassLoader classLoader) {
		this(transport, new CommandGenerator(classLoader))
	}
	
	/**
	 * Hook for subclasses to provide a custom command generator.
	 */
	protected RemoteControl(Transport transport, CommandGenerator commandGenerator) {
		this.transport = transport
		this.commandGenerator = commandGenerator
	}
	
	def exec(Closure[] commands) {
		processResult(sendCommandChain(generateCommandChain(commands)))
	}
	
	def call(Closure[] commands) {
		exec(commands)
	}
	
	protected CommandChain generateCommandChain(Closure[] commands) {
		new CommandChain(commands: commands.collect { commandGenerator.generate(it) })
	}
	
	protected Result sendCommandChain(CommandChain commandChain) {
		transport.send(commandChain)
	}
	
	protected processResult(Result result) {
		if (result.wasNull) {
			null
		} else if (result.wasUnserializable) {
			if (useNullIfResultWasUnserializable) {
				null
			} else if (useStringRepresentationIfResultWasUnserializable) {
				result.stringRepresentation
			} else {
				throw new UnserializableReturnException(result)
			}
		} else if (result.thrown) {
			throw new RemoteException(result.thrown)
		} else {
			result.value
		}
	}
}

{'fraction_non_alphanumeric': 0.07760836045913996, 'fraction_numerical': 0.0027411341442521845, 'mean_word_length': 4.245283018867925, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '5454426', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1497, 'n_words': 536}