Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7edfa1f862cb089ede1fbe7ea043f7301e115e3c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*******************************************************************************
 * Copyright (c) 2008, 2009 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 * 
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.utility.internal;

import org.eclipse.jpt.utility.Command;
import org.eclipse.jpt.utility.CommandExecutor;

/**
 * This implementation of the CommandExecutor interface allows the client to
 * specify a different Command Executor for each thread.
 */
public class ThreadLocalCommandExecutor implements CommandExecutor {
	protected final ThreadLocal<CommandExecutor> threadLocal;
	protected final CommandExecutor defaultCommandExecutor;

	/**
	 * The default command executor simply executes the command directly.
	 */
	public ThreadLocalCommandExecutor() {
		this(CommandExecutor.Default.instance());
	}

	public ThreadLocalCommandExecutor(CommandExecutor defaultCommandExecutor) {
		super();
		this.defaultCommandExecutor = defaultCommandExecutor;
		this.threadLocal = this.buildThreadLocal();
	}

	protected ThreadLocal<CommandExecutor> buildThreadLocal() {
		return new ThreadLocal<CommandExecutor>();
	}

	public void execute(Command command) {
		this.getThreadLocalCommandExecutor().execute(command);
	}

	protected CommandExecutor getThreadLocalCommandExecutor() {
		CommandExecutor ce = this.threadLocal.get();
		return (ce != null) ? ce : this.defaultCommandExecutor;
	}

	/**
	 * Set the current thread's command executor to the specified value.
	 */
	public void set(CommandExecutor commandExecutor) {
		this.threadLocal.set(commandExecutor);
	}

	/**
	 * Return the string representation of the current thread's command
	 * executor.
	 */
	@Override
	public String toString() {
		return this.getThreadLocalCommandExecutor().toString();
	}

}

Back to the top