Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3db3549ff210983d5585798732bb4d86f2221a17 (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
/*******************************************************************************
 * Copyright (c) 2012, 2013 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.launch.core.internal.adapters;

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

import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.debug.core.DebugPlugin;
import org.eclipse.debug.core.ILaunch;
import org.eclipse.debug.core.ILaunchConfiguration;
import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
import org.eclipse.debug.core.ILaunchListener;
import org.eclipse.tcf.protocol.IPeer;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.runtime.interfaces.IDisposable;
import org.eclipse.tcf.te.runtime.stepper.interfaces.IStepContext;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerNode;
import org.eclipse.tcf.te.tcf.locator.interfaces.services.IPeerModelLookupService;
import org.eclipse.tcf.te.tcf.locator.model.Model;

/**
 * Adapter factory implementation.
 */
public class AdapterFactory implements IAdapterFactory {
	// Maintain a map of step context adapters per peer model
	/* default */ Map<ILaunch, IStepContext> adapters = new HashMap<ILaunch, IStepContext>();
	AttachLaunchConfigAdapter attachLaunchConfigAdapter = new AttachLaunchConfigAdapter();

	private static final Class<?>[] CLASSES = new Class[] {
		IStepContext.class,
		ILaunchConfiguration.class,
		ILaunchConfigurationWorkingCopy.class,
	};

	/**
	 * Constructor.
	 */
	public AdapterFactory() {
		final ILaunchListener  listener = new ILaunchListener() {
			@Override
			public void launchRemoved(ILaunch launch) {
				IStepContext adapter = adapters.remove(launch);
				if (adapter instanceof IDisposable) {
					((IDisposable)adapter).dispose();
				}
			}
			@Override
			public void launchChanged(ILaunch launch) {
				IStepContext adapter = adapters.remove(launch);
				if (adapter instanceof IDisposable) {
					((IDisposable)adapter).dispose();
				}
			}
			@Override
			public void launchAdded(ILaunch launch) {
			}
		};

		Runnable runnable = new Runnable() {
			@Override
			public void run() {
				DebugPlugin.getDefault().getLaunchManager().addLaunchListener(listener);
			}
		};

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

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.IAdapterFactory#getAdapter(java.lang.Object, java.lang.Class)
	 */
	@Override
	public Object getAdapter(final Object adaptableObject, final Class adapterType) {
		if (adaptableObject instanceof ILaunch) {
			if (IStepContext.class.equals(adapterType)) {
				// Lookup the adapter
				IStepContext adapter = adapters.get(adaptableObject);
				// No adapter yet -> create a new one for this peer
				if (adapter == null) {
					adapter = new LaunchStepContext((ILaunch)adaptableObject);
					adapters.put((ILaunch)adaptableObject, adapter);
				}
				return adapter;
			}
		}
		else if (adaptableObject instanceof IPeerNode) {
			if (ILaunchConfiguration.class.equals(adapterType)) {
				return attachLaunchConfigAdapter.getAttachLaunchConfig((IPeerNode)adaptableObject);
			}
			if (ILaunchConfigurationWorkingCopy.class.equals(adapterType)) {
				ILaunchConfiguration launchConfig = attachLaunchConfigAdapter.getAttachLaunchConfig((IPeerNode)adaptableObject);
				try {
					return launchConfig.getWorkingCopy();
				}
				catch (Exception e) {
					return launchConfig;
				}
			}
		}
		else if (adaptableObject instanceof IPeer) {
			final IPeerModelLookupService service = Model.getModel().getService(IPeerModelLookupService.class);
			final AtomicReference<IPeerNode> peerNode = new AtomicReference<IPeerNode>();

			if (service != null) {
				Runnable runnable = new Runnable() {
					@Override
					public void run() {
						peerNode.set(service.lkupPeerModelById(((IPeer)adaptableObject).getID()));
					}
				};
				if (Protocol.isDispatchThread()) runnable.run();
				else Protocol.invokeAndWait(runnable);
			}

			if (peerNode.get() != null) return getAdapter(peerNode.get(), adapterType);
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.core.runtime.IAdapterFactory#getAdapterList()
	 */
	@Override
	public Class[] getAdapterList() {
		return CLASSES;
	}

}

Back to the top