Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 2bf00327738ee0f2c30a1fb3484fa76c9803c83b (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*******************************************************************************
 * 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.nodes;

import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.services.IProcesses;
import org.eclipse.tcf.services.ISysMonitor;
import org.eclipse.tcf.services.ISysMonitor.SysMonitorContext;
import org.eclipse.tcf.te.core.interfaces.IFilterable;
import org.eclipse.tcf.te.runtime.model.ContainerModelNode;
import org.eclipse.tcf.te.runtime.model.contexts.AsyncRefreshableCtxAdapter;
import org.eclipse.tcf.te.runtime.model.interfaces.contexts.IAsyncRefreshableCtx;
import org.eclipse.tcf.te.runtime.model.interfaces.contexts.IAsyncRefreshableCtx.QueryState;
import org.eclipse.tcf.te.runtime.model.interfaces.contexts.IAsyncRefreshableCtx.QueryType;
import org.eclipse.tcf.te.tcf.core.model.interfaces.IModel;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNode;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNodeProvider;
import org.eclipse.tcf.te.tcf.processes.core.model.interfaces.IProcessContextNode;
import org.eclipse.tcf.te.tcf.processes.core.model.interfaces.IProcessContextNodeProperties;

/**
 * A process context node implementation.
 */
public class ProcessContextNode extends ContainerModelNode implements IProcessContextNode, IPeerNodeProvider, IFilterable {
	// Reference to the agent side process context object
	private IProcesses.ProcessContext pContext = null;
	// Reference to the agent side system monitor context object
	private ISysMonitor.SysMonitorContext sContext = null;

	// The node type
	private TYPE type = TYPE.Unknown;

	// Context nodes needs asynchronous refreshes
	private final IAsyncRefreshableCtx refreshableCtxAdapter = new AsyncRefreshableCtxAdapter();

	/**
	 * Constructor.
	 */
	public ProcessContextNode() {
		super();
		setChangeEventsEnabled(true);

		// No initial context query required
		refreshableCtxAdapter.setQueryState(QueryType.CONTEXT, QueryState.DONE);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.properties.PropertiesContainer#checkThreadAccess()
	 */
	@Override
	protected boolean checkThreadAccess() {
	    return Protocol.isDispatchThread();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.model.ModelNode#getName()
	 */
	@Override
	public String getName() {
		Assert.isTrue(checkThreadAccess(), "Illegal Thread Access"); //$NON-NLS-1$
		// If the node is associated with an agent side context, take the name
		// from the agent side context first
		String name = pContext != null ? pContext.getName() : null;

		// If the name is empty or the same as the context ID -> reset
		if (name != null && ("".equals(name.trim()) || name.equals(pContext.getID()))) { //$NON-NLS-1$
			name = null;
		}

		// Take the last part of the image name if available
		String file = sContext != null ? sContext.getFile() : null;
		if (file != null && !"".equals(file)) { //$NON-NLS-1$
			IPath path = new Path(file);
			// If it is a typical Windows path with a drive letter, take only the
			// last segment. In all other cases, take "file" as it is
			if (path.getDevice() != null && path.getDevice().endsWith(":") && path.getDevice().length() == 2) { //$NON-NLS-1$
				name = path.lastSegment();
			} else {
				name = file;
			}
		}

		// Fallback to the context ID
		if (name == null || "".equals(name.trim())) name = pContext != null ? pContext.getID() : null; //$NON-NLS-1$

		// Fallback to the local node properties
		if (name == null || "".equals(name.trim())) name = getStringProperty(IProcessContextNodeProperties.PROPERTY_NAME); //$NON-NLS-1$
		if (name == null || "".equals(name.trim())) name = getStringProperty(IProcessContextNodeProperties.PROPERTY_ID); //$NON-NLS-1$

	    return name != null && !"".equals(name.trim()) ? name.trim() : super.getName(); //$NON-NLS-1$
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.processes.core.model.interfaces.IProcessContextNode#setType(org.eclipse.tcf.te.tcf.processes.core.model.interfaces.IProcessContextNode.TYPE)
	 */
	@Override
	public void setType(TYPE type) {
		Assert.isNotNull(type);
		this.type = type;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.processes.core.model.interfaces.IProcessContextNode#getType()
	 */
    @Override
    public TYPE getType() {
	    return type;
    }

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.processes.core.model.interfaces.IProcessContextNode#setContext(org.eclipse.tcf.services.IProcesses.ProcessContext)
	 */
	@Override
	public final void setProcessContext(IProcesses.ProcessContext context) {
		Assert.isTrue(checkThreadAccess(), "Illegal Thread Access"); //$NON-NLS-1$
		this.pContext = context;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.processes.core.model.interfaces.IProcessContextNode#getContext()
	 */
	@Override
	public final IProcesses.ProcessContext getProcessContext() {
		Assert.isTrue(checkThreadAccess(), "Illegal Thread Access"); //$NON-NLS-1$
	    return pContext;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.processes.core.model.interfaces.IProcessContextNode#setSysMonitorContext(org.eclipse.tcf.services.ISysMonitor.SysMonitorContext)
	 */
    @Override
    public void setSysMonitorContext(SysMonitorContext context) {
		Assert.isTrue(checkThreadAccess(), "Illegal Thread Access"); //$NON-NLS-1$
		this.sContext = context;
    }

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.processes.core.model.interfaces.IProcessContextNode#getSysMonitorContext()
	 */
    @Override
    public SysMonitorContext getSysMonitorContext() {
		Assert.isTrue(checkThreadAccess(), "Illegal Thread Access"); //$NON-NLS-1$
	    return sContext;
    }

    /* (non-Javadoc)
     * @see org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNodeProvider#getPeerModel()
     */
    @Override
    public IPeerNode getPeerModel() {
        return (IPeerNode)getAdapter(IPeerNode.class);
    }

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.tcf.processes.core.model.interfaces.IProcessContextNode#isComplete()
	 */
	@Override
	public boolean isComplete() {
		Assert.isTrue(checkThreadAccess(), "Illegal Thread Access"); //$NON-NLS-1$

		 // Return true if one of the contexts is not null.
		if (getProcessContext() != null || getSysMonitorContext() != null) {
			return true;
		}

		boolean complete = true;

		// ID is mandatory and must not be empty
		String id = getStringProperty(IProcessContextNodeProperties.PROPERTY_ID);
		complete &= id != null && !"".equals(id); //$NON-NLS-1$

	    return complete;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.PlatformObject#getAdapter(java.lang.Class)
	 */
	@Override
	public Object getAdapter(final Class adapter) {
		// NOTE: The getAdapter(...) method can be invoked from many place and
		//       many threads where we cannot control the calls. Therefore, this
		//       method is allowed be called from any thread.
		final AtomicReference<Object> object = new AtomicReference<Object>();
		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				object.set(doGetAdapter(adapter));
			}
		};

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

		return object.get() != null ? object.get() : super.getAdapter(adapter);
	}

	/**
	 * Returns an object which is an instance of the given class associated with this object.
	 * Returns <code>null</code> if no such object can be found.
	 * <p>
	 * This method must be called within the TCF dispatch thread!
	 *
	 * @param adapter The adapter class to look up.
	 * @return The adapter or <code>null</code>.
	 */
	protected Object doGetAdapter(Class<?> adapter) {
		Assert.isTrue(checkThreadAccess(), "Illegal Thread Access"); //$NON-NLS-1$

		if (IProcesses.ProcessContext.class.isAssignableFrom(adapter)) {
			return pContext;
		}
		if (ISysMonitor.SysMonitorContext.class.isAssignableFrom(adapter)) {
			return sContext;
		}
		if (IPeerNodeProvider.class.isAssignableFrom(adapter)) {
			IModel model = getParent(IModel.class);
			if (model instanceof IPeerNodeProvider) return model;
		}
		if (IPeerNode.class.isAssignableFrom(adapter)) {
			IModel model = getParent(IModel.class);
			if (model instanceof IPeerNodeProvider) return ((IPeerNodeProvider)model).getPeerModel();
		}
		if (IAsyncRefreshableCtx.class.isAssignableFrom(adapter)) {
			return refreshableCtxAdapter;
		}

		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.tcf.te.runtime.model.ModelNode#toString()
	 */
	@Override
	public String toString() {
		final AtomicReference<String> toString = new AtomicReference<String>(super.toString());

		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				StringBuilder buffer = new StringBuilder(toString.get());
				buffer.deleteCharAt(buffer.length() - 1);
				buffer.append(", system monitor properties="); //$NON-NLS-1$
				buffer.append(getSysMonitorContext() != null ? getSysMonitorContext().toString() : "{}"); //$NON-NLS-1$
				buffer.append(", process properties="); //$NON-NLS-1$
				buffer.append(getProcessContext() != null ? getProcessContext().toString() : "{}"); //$NON-NLS-1$
				buffer.append("}"); //$NON-NLS-1$
				toString.set(buffer.toString());
			}
		};

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

	    return toString.get();
	}
}

Back to the top