Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b4f1798cb3517cc0c2f825eb885e850b18bc4eb4 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*******************************************************************************
 * Copyright (c) 2011 Wind River Systems 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.debug.test.services;

import java.util.Map;

import org.eclipse.tcf.debug.test.util.ICache;
import org.eclipse.tcf.debug.test.util.TokenCache;
import org.eclipse.tcf.protocol.IToken;
import org.eclipse.tcf.services.IBreakpoints;

/**
 * 
 */
public class BreakpointsCM extends AbstractCacheManager implements IBreakpoints.BreakpointsListener {

    private IBreakpoints fService;
    
    public BreakpointsCM(IBreakpoints service) {
        fService = service;
        fService.addListener(this);
    }

    @Override
    public void dispose() {
        fService.removeListener(this);
        // TODO Auto-generated method stub
        super.dispose();
    }

    private abstract static class DoneCommandCache extends TokenCache<Object> implements IBreakpoints.DoneCommand {
        public void doneCommand(IToken token, Exception error) {
            set(token, null, error);
        }
    }

    public ICache<Object> set(final Map<String,Object>[] properties, Object clientKey) {
        class MyCache extends DoneCommandCache {
            @Override
            protected IToken retrieveToken() {
                return fService.set(properties, this);
            }
        }
        return mapCache( new CommandKey<MyCache>(MyCache.class, clientKey) {
                @Override MyCache createCache() { return new MyCache(); }
            });                
    }

    public ICache<Object> add(final Map<String,Object> properties, Object clientKey) {
        class MyCache extends DoneCommandCache {
            @Override
            protected IToken retrieveToken() {
                return fService.add(properties, this);
            }
        }
        return mapCache( new CommandKey<MyCache>(MyCache.class, clientKey) {
                @Override MyCache createCache() { return new MyCache(); }
            });                
    }

    public ICache<Object> change(final Map<String,Object> properties, Object clientKey) {
        class MyCache extends DoneCommandCache {
            @Override
            protected IToken retrieveToken() {
                return fService.change(properties, this);
            }
        }
        return mapCache( new CommandKey<MyCache>(MyCache.class, clientKey) {
                @Override MyCache createCache() { return new MyCache(); }
            });                
    }
    
    public ICache<Object> enable(final String[] ids, Object clientKey) {
        class MyCache extends DoneCommandCache {
            @Override
            protected IToken retrieveToken() {
                return fService.enable(ids, this);
            }
        }
        return mapCache( new CommandKey<MyCache>(MyCache.class, clientKey) {
                @Override MyCache createCache() { return new MyCache(); }
            });                
    }

    public ICache<Object> disable(final String[] ids, Object clientKey) {
        class MyCache extends DoneCommandCache {
            @Override
            protected IToken retrieveToken() {
                return fService.disable(ids, this);
            }
        }
        return mapCache( new CommandKey<MyCache>(MyCache.class, clientKey) {
                @Override MyCache createCache() { return new MyCache(); }
            });                
    }

    public ICache<Object> remove(final String[] ids, Object clientKey) {
        class MyCache extends DoneCommandCache {
            @Override
            protected IToken retrieveToken() {
                return fService.remove(ids, this);
            }
        }
        return mapCache( new CommandKey<MyCache>(MyCache.class, clientKey) {
                @Override MyCache createCache() { return new MyCache(); }
            });                
    }
    
    private class IDsCache extends TokenCache<String[]> implements IBreakpoints.DoneGetIDs {            
        @Override
        protected IToken retrieveToken() {
            return fService.getIDs(this);
        } 
        
        public void doneGetIDs(IToken token, Exception error, String[] ids) {
            set(token, ids, error);
        }
        
        public void resetIDs() {
            // TODO: handle add/remove ids
            if (isValid()) reset();
        }
    }
    
    private class IDsCacheKey extends Key<IDsCache> {
        public IDsCacheKey() { 
            super(IDsCache.class);
        }
        
        @Override IDsCache createCache() { return new IDsCache(); }
    }
    
    public ICache<String[]> getIDs() {
        return mapCache( new IDsCacheKey() );
    }

    private class PropertiesCache extends TokenCache<Map<String,Object>> implements IBreakpoints.DoneGetProperties {
        String fId;
        
        public PropertiesCache(String id) {
            fId = id;
        }
        
        @Override
        protected IToken retrieveToken() {
            return fService.getProperties(fId, this);
        }
        
        public void doneGetProperties(IToken token, Exception error, Map<String, Object> properties) {
            set(token, properties, error);
        }
        
        public void setProperties(Map<String, Object> properties) {
            set(null, properties, null);
        }
        
        public void resetProperties() {
            if (isValid()) reset();
        }
    }
    
    private class PropertiesCacheKey extends IdKey<PropertiesCache> {
        public PropertiesCacheKey(String id) {
            super(PropertiesCache.class, id);
        }
        
        @Override PropertiesCache createCache() { return new PropertiesCache(fId); }
    };     

    public ICache<Map<String,Object>> getProperties(String id) {
        return mapCache( new PropertiesCacheKey(id) );
    }
    
    private class StatusCache extends TokenCache<Map<String,Object>> implements IBreakpoints.DoneGetStatus {
        String fId;
        
        public StatusCache(String id) {
            fId = id;
        }
        
        @Override
        protected IToken retrieveToken() {
            return fService.getStatus(fId, this);
        }
        
        public void doneGetStatus(IToken token, Exception error, Map<String, Object> status) {
            set(token, status, error);
        }
        
        public void setStatus(Map<String, Object> status) {
            set(null, status, null);
        }
    }
    
    private class StatusCacheKey extends IdKey<StatusCache> {
        public StatusCacheKey(String id) {
            super(StatusCache.class, id);
        }
        @Override StatusCache createCache() { return new StatusCache(fId); }
    }  
    
    public ICache<Map<String,Object>> getStatus(String id) {
        return mapCache( new StatusCacheKey(id) );      
    }

    public ICache<Map<String,Object>> getCapabilities(final String id) {
        class MyCache extends TokenCache<Map<String,Object>> implements IBreakpoints.DoneGetCapabilities {            
            @Override
            protected IToken retrieveToken() {
                return fService.getCapabilities(id, this);
            }
            public void doneGetCapabilities(IToken token, Exception error, Map<String, Object> capabilities) {
                set(token, capabilities, error);
            }            
        }
        return mapCache( new IdKey<MyCache>(MyCache.class, id) {
                @Override MyCache createCache() { return new MyCache(); }
            });                        
    }
    
    private class StatusChangedCache extends WaitForEventCache<Map<String, Object>> {}

    public ICache<Map<String, Object>> waitStatusChanged(String id, Object clientKey) {
        return mapCache(new IdEventKey<StatusChangedCache>(StatusChangedCache.class, id, clientKey) {
                @Override
                StatusChangedCache createCache() {
                    return new StatusChangedCache();
                }
            });
    }
        
    public void breakpointStatusChanged(String id, final Map<String, Object> status) {
        StatusCache statusCache = getCache(new StatusCacheKey(id));    
        if (statusCache != null) {
            statusCache.setStatus(status);
        }        
        
        // TODO: avoid iterating over all entries, use separate list for events.
        for (Map.Entry<Key<?>, ICache<?>> entry: fMap.entrySet()) {
            if (entry.getKey() instanceof IdEventKey) {
                IdEventKey<?> eventKey = (IdEventKey<?>)entry.getKey();
                if ( StatusChangedCache.class.equals( eventKey.getCacheClass() ) &&
                     eventKey.fId.equals(id) ) 
                {
                    ((StatusChangedCache)entry.getValue()).eventReceived(status);
                }
            }
        }
    }

    private void setBreakpointsProperties(Map<String, Object>[] bps) {
        for (Map<String, Object> bp : bps) {
            Object id = (String)bp.get(IBreakpoints.PROP_ID);
            if (id instanceof String) {
                PropertiesCache cache = mapCache(new  PropertiesCacheKey((String)id));
                cache.setProperties(bp);
            }
        }
    }
    
    private class ContextAddedCache extends WaitForEventCache<Map<String, Object>[]> {}

    public ICache<Map<String, Object>[]> waitContextAdded(Object clientKey) {
        return mapCache(new EventKey<ContextAddedCache>(ContextAddedCache.class, clientKey) {
                @Override
                ContextAddedCache createCache() {
                    return new ContextAddedCache();
                }
            });
    }

    public void contextAdded(Map<String, Object>[] bps) {
        IDsCache idsCache = getCache(new IDsCacheKey());    
        if (idsCache != null && idsCache.isValid()) {
            idsCache.resetIDs();
        }        
        
        setBreakpointsProperties(bps);
        
        // TODO: avoid iterating over all entries, use separate list for events.
        for (Map.Entry<Key<?>, ICache<?>> entry: fMap.entrySet()) {
            if (entry.getKey() instanceof EventKey) {
                EventKey<?> eventKey = (EventKey<?>)entry.getKey();
                if ( ContextAddedCache.class.equals( eventKey.getCacheClass() ) ) {
                    ((ContextAddedCache)entry.getValue()).eventReceived(bps);
                }
            }
        }
    }

    private class ContextChangedCache extends WaitForEventCache<Map<String, Object>[]> {}

    public ICache<Map<String, Object>[]> waitContextChanged(Object clientKey) {
        return mapCache(new EventKey<ContextChangedCache>(ContextChangedCache.class, clientKey) {
                @Override
                ContextChangedCache createCache() {
                    return new ContextChangedCache();
                }
            });
    }

    public void contextChanged(Map<String, Object>[] bps) {
        setBreakpointsProperties(bps);
        
        // TODO: avoid iterating over all entries, use separate list for events.
        for (Map.Entry<Key<?>, ICache<?>> entry: fMap.entrySet()) {
            if (entry.getKey() instanceof EventKey) {
                EventKey<?> eventKey = (EventKey<?>)entry.getKey();
                if ( ContextChangedCache.class.equals( eventKey.getCacheClass() ) ) {
                    ((ContextChangedCache)entry.getValue()).eventReceived(bps);
                }
            }
        }
    }
    
    private class ContextRemovedCache extends WaitForEventCache<String[]> {}

    public ICache<String[]> waitContextRemoved(Object clientKey) {
        return mapCache(new EventKey<ContextRemovedCache>(ContextRemovedCache.class, clientKey) {
                @Override
                ContextRemovedCache createCache() {
                    return new ContextRemovedCache();
                }
            });
    }

    public void contextRemoved(String[] ids) {
        IDsCache idsCache = getCache(new IDsCacheKey());    
        if (idsCache != null) {
            idsCache.resetIDs();
        }
        
        for (String id : ids) {
            PropertiesCache cache = mapCache(new  PropertiesCacheKey(id));
            cache.resetProperties();
        }        

        // TODO: avoid iterating over all entries, use separate list for events.
        for (Map.Entry<Key<?>, ICache<?>> entry: fMap.entrySet()) {
            if (entry.getKey() instanceof EventKey) {
                EventKey<?> eventKey = (EventKey<?>)entry.getKey();
                if ( ContextRemovedCache.class.equals( eventKey.getCacheClass() ) ) {
                    ((ContextRemovedCache)entry.getValue()).eventReceived(ids);
                }
            }
        }
    }
    
    
}

Back to the top