Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3c0ef549400ab5638856a7faeb6d8518aa282e45 (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
/*******************************************************************************
 * Copyright (c) 2012 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.tcf.processes.core.model;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.core.runtime.Assert;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel;
import org.eclipse.tcf.te.tcf.processes.core.model.interfaces.runtime.IRuntimeModel;
import org.eclipse.tcf.te.tcf.processes.core.model.runtime.RuntimeModel;

/**
 * Processes service model manager implementation.
 */
public class ModelManager {
	// Reference to the runtime models
	/* default */ static final Map<String, IRuntimeModel> runtimeModels = new HashMap<String, IRuntimeModel>();

	/**
	 * Returns the runtime model instance for the given peer model
	 * <p>
	 * If not yet initialized, a new runtime model will be initialized before returning.
	 *
	 * @param peerModel The peer model instance. Must not be <code>null</code>.
	 * @return The runtime model.
	 */
	public static IRuntimeModel getRuntimeModel(final IPeerModel peerModel) {
		Assert.isNotNull(peerModel);

		// The result reference holder
		final AtomicReference<IRuntimeModel> runtimeModel = new AtomicReference<IRuntimeModel>();

		// Create the runnable to execute
		Runnable runnable = new Runnable() {
			@Override
            public void run() {
				Assert.isTrue(Protocol.isDispatchThread());

				// Get the peer id
				String id = peerModel.getPeerId();
				// Lookup the runtime model instance
				IRuntimeModel candidate = runtimeModels.get(id);
				// Initialize a new runtime model instance if necessary
				if (candidate == null) {
					candidate = initializeRuntimeModel(peerModel);
					if (candidate != null) runtimeModels.put(id, candidate);
				}
				// Store to the result reference holder
				runtimeModel.set(candidate);
			}
		};

		// Execute the runnable
		if (Protocol.isDispatchThread()) runnable.run();
		else Protocol.invokeAndWait(runnable);

		return runtimeModel.get();
	}

	/**
	 * Initialize the runtime model.
	 * <p>
	 * Must be called within the TCF dispatch thread.
	 *
	 * @param peerModel The peer model instance. Must not be <code>null</code>.
	 * @return The runtime model.
	 */
	protected static IRuntimeModel initializeRuntimeModel(IPeerModel peerModel) {
		Assert.isTrue(Protocol.isDispatchThread());
		IRuntimeModel runtimeModel = new RuntimeModel(peerModel);
		return runtimeModel;
	}

	/**
	 * Dispose the runtime model.
	 *
	 * @param peerModel The peer model instance. Must not be <code>null</code>.
	 */
	public static void disposeRuntimeModel(final IPeerModel peerModel) {
		Assert.isNotNull(peerModel);

		Runnable runnable = new Runnable() {
			@Override
            public void run() {
				Assert.isTrue(Protocol.isDispatchThread());

				// Get the peer id
				String id = peerModel.getPeerId();
				// Lookup the runtime model instance
				IRuntimeModel candidate = runtimeModels.remove(id);
				// Dispose it
				if (candidate != null) candidate.dispose();
			}
		};

		if (Protocol.isDispatchThread()) runnable.run();
		else Protocol.invokeAndWait(runnable);
	}

	/**
	 * Dispose all runtime models.
	 */
	public static void disposeAllRuntimeModels() {
		if (runtimeModels.isEmpty()) return;

		final IRuntimeModel[] models = runtimeModels.values().toArray(new IRuntimeModel[runtimeModels.values().size()]);
		runtimeModels.clear();

		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				for (IRuntimeModel model : models) {
					model.dispose();
				}
			}
		};

		if (Protocol.isDispatchThread()) runnable.run();
		else Protocol.invokeAndWait(runnable);
	}

}

Back to the top