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
|
/*******************************************************************************
* Copyright (c) 2011 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.services.contexts.internal;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.core.runtime.IAdapterFactory;
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.tcf.locator.interfaces.IModelListener;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.ILocatorModel;
import org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel;
import org.eclipse.tcf.te.tcf.locator.listener.ModelAdapter;
import org.eclipse.tcf.te.tcf.locator.model.Model;
import org.eclipse.tcf.te.tcf.services.contexts.interfaces.IContextService;
/**
* Context service adapter factory implementation.
*/
public class AdapterFactory implements IAdapterFactory {
// Maintain a map of contexts service adapters per peer
/* default */ Map<IPeer, IContextService> adapters = new HashMap<IPeer, IContextService>();
private static final Class<?>[] CLASSES = new Class[] {
IContextService.class
};
/**
* Constructor.
*/
public AdapterFactory() {
final IModelListener listener = new ModelAdapter() {
/* (non-Javadoc)
* @see org.eclipse.tcf.te.tcf.locator.listener.ModelAdapter#locatorModelChanged(org.eclipse.tcf.te.tcf.locator.interfaces.nodes.ILocatorModel, org.eclipse.tcf.te.tcf.locator.interfaces.nodes.IPeerModel, boolean)
*/
@Override
public void locatorModelChanged(ILocatorModel model, IPeerModel peer, boolean added) {
// If a peer gets removed, remove the context service proxy
if (peer != null && peer.getPeer() != null && !added) {
IContextService adapter = adapters.remove(peer.getPeer());
if (adapter instanceof IDisposable) ((IDisposable)adapter).dispose();
}
}
};
Runnable runnable = new Runnable() {
@Override
public void run() {
Model.getModel().addListener(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(Object adaptableObject, Class adapterType) {
if (adaptableObject instanceof IPeer) {
// Lookup the adapter
IContextService adapter = adapters.get(adaptableObject);
// No adapter yet -> create a new one for this peer
if (adapter == null) {
adapter = new ContextServiceAdapter((IPeer)adaptableObject);
adapters.put((IPeer)adaptableObject, adapter);
}
return adapter;
}
return null;
}
/* (non-Javadoc)
* @see org.eclipse.core.runtime.IAdapterFactory#getAdapterList()
*/
@Override
public Class[] getAdapterList() {
return CLASSES;
}
}
|