Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 297d54757ed9b544bcc2f2e10a9d08218c685031 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*******************************************************************************
 * Copyright (c) 2011, 2014 Wind River Systems, Inc. and others. 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:
 * Wind River Systems - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.runtime.concurrent;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.eclipse.core.runtime.Assert;
import org.eclipse.tcf.te.runtime.concurrent.interfaces.IExecutor;
import org.eclipse.tcf.te.runtime.extensions.AbstractExtensionPointManager;
import org.eclipse.tcf.te.runtime.extensions.ExecutableExtensionProxy;


/**
 * Class is providing the entry points to create or query the executor service
 * instances.
 */
public final class Executors {

	/**
	 * Execution service extension point manager.
	 */
	protected static class ExecutorServiceExtensionPointManager extends AbstractExtensionPointManager<IExecutor> {

		/* (non-Javadoc)
		 * @see org.eclipse.tcf.te.runtime.extensions.AbstractExtensionPointManager#getExtensionPointId()
		 */
		@Override
		protected String getExtensionPointId() {
			return "org.eclipse.tcf.te.runtime.concurrent.executorServices"; //$NON-NLS-1$
		}

		/* (non-Javadoc)
		 * @see org.eclipse.tcf.te.runtime.extensions.AbstractExtensionPointManager#getConfigurationElementName()
		 */
		@Override
		protected String getConfigurationElementName() {
			return "executorService"; //$NON-NLS-1$
		}

		/**
		 * Returns the list of all contributed executors.
		 *
		 * @return The list of contributed executors, or an empty array.
		 */
		public IExecutor[] getExecutors() {
			List<IExecutor> contributions = new ArrayList<IExecutor>();
			Collection<ExecutableExtensionProxy<IExecutor>> proxies = getExtensions().values();
			for (ExecutableExtensionProxy<IExecutor> proxy : proxies)
				if (proxy.getInstance() != null
						&& !contributions.contains(proxy.getInstance()))
					contributions.add(proxy.getInstance());

			return contributions.toArray(new IExecutor[contributions.size()]);
		}

		/**
		 * Returns the executor identified by its unique id. If no executor with
		 * the specified id is registered, <code>null</code> is returned.
		 *
		 * @param id
		 *            The unique id of the executor. Must not be
		 *            <code>null</code>
		 * @param newInstance
		 *            Specify <code>true</code> to get a new executor service
		 *            instance, <code>false</code> otherwise.
		 *
		 * @return The executor instance or <code>null</code>.
		 */
		public IExecutor getExecutor(String id, boolean newInstance) {
			Assert.isNotNull(id);

			IExecutor executorService = null;
			if (getExtensions().containsKey(id)) {
				ExecutableExtensionProxy<IExecutor> proxy = getExtensions().get(id);
				// Get the extension instance
				executorService = newInstance ? proxy.newInstance() : proxy.getInstance();
			}

			return executorService;
		}
	}

	// Reference to the executor service extension point manager
	private final static ExecutorServiceExtensionPointManager EXTENSION_POINT_MANAGER = new ExecutorServiceExtensionPointManager();

	/**
	 * Constructor.
	 * <p>
	 * <b>Note:</b> The class cannot be instantiated.
	 */
	private Executors() {
	}

	/**
	 * Creates an instance of the executor registered with the specified id. If
	 * no executor is registered under the given id, the method will return
	 * <code>null</code>.
	 *
	 * @param id
	 *            The id of the executor. Must not be <code>null</code>.
	 * @return The new executor instance or <code>null</code>.
	 */
	public static IExecutor newExecutor(String id) {
		Assert.isNotNull(id);
		return EXTENSION_POINT_MANAGER.getExecutor(id, true);
	}

	/**
	 * Returns the shared instance of the executor registered with the specified
	 * id. If the shared instance hasn't been created yet, the instance will be
	 * created and saved. Subsequent calls to this method with the same id will
	 * return always the same executor instance. If no executor is registered
	 * under the given id, the method will return <code>null</code>.
	 *
	 * @param id
	 *            The id of the executor. Must not be <code>null</code>.
	 * @return The new executor instance or <code>null</code>.
	 */
	public static IExecutor getSharedExecutor(String id) {
		Assert.isNotNull(id);
		return EXTENSION_POINT_MANAGER.getExecutor(id, false);
	}

	/**
	 * Returns the shared instances of all registered executors.
	 *
	 * @return All executor instances or an empty array.
	 */
	public static IExecutor[] getAllSharedExecutors() {
		return EXTENSION_POINT_MANAGER.getExecutors();
	}
}

Back to the top