Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 22ed08b0db9b04594e588f517d0975533539e64a (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
/*******************************************************************************
 * Copyright (c) 2006, 2010 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.cdt.examples.dsf.timers;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.RejectedExecutionException;

import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.IDsfStatusConstants;
import org.eclipse.cdt.dsf.concurrent.Query;
import org.eclipse.cdt.dsf.concurrent.ThreadSafe;
import org.eclipse.cdt.dsf.concurrent.ThreadSafeAndProhibitedFromDsfExecutor;
import org.eclipse.cdt.dsf.service.DsfServices;
import org.eclipse.cdt.dsf.service.DsfSession;
import org.eclipse.cdt.examples.dsf.DsfExamplesPlugin;
import org.eclipse.cdt.examples.dsf.timers.AlarmService.TriggerDMContext;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ICellModifier;
import org.eclipse.swt.widgets.Shell;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.util.tracker.ServiceTracker;

/**
 * Cell modifier used to edit the trigger value.
 */
@ThreadSafeAndProhibitedFromDsfExecutor("fSession.getExecutor()")
public class TriggerCellModifier implements ICellModifier {

    private final DsfSession fSession;
    
    // Need to use the OSGi service tracker (instead of DsfServiceTracker), 
    // because it's being accessed on multiple threads.
    @ThreadSafe
    private ServiceTracker fServiceTracker;

    /**
     * Constructor for the modifier requires a valid session in order to 
     * initialize the service tracker.  
     * @param session DSF session this modifier will use.
     */
    public TriggerCellModifier(DsfSession session) {
        fSession = session;
    }

    @Override
    public boolean canModify(Object element, String property) {
        return TimersViewColumnPresentation.COL_VALUE.equals(property) && 
            getAlarmDMC(element) != null; 
    }

    @Override
    public Object getValue(Object element, String property) {
        if (!TimersViewColumnPresentation.COL_VALUE.equals(property)) return ""; 
        
        // Get the context and the session.  If element is not an trigger 
        // context or if the session is stale then bail out. 
        TriggerDMContext triggerCtx = getAlarmDMC(element);
        if (triggerCtx == null) return ""; 
        DsfSession session = DsfSession.getSession(triggerCtx.getSessionId());
        if (session == null) return ""; 
        
        // Create the query to request the value from service.  
        GetValueQuery query = new GetValueQuery(triggerCtx);
        try {
            session.getExecutor().execute(query);
        } catch (RejectedExecutionException e) {
            return "";
        }
        try {
            return query.get().toString();
        } catch (InterruptedException e) {
            assert false;
            return ""; 
        } catch (ExecutionException e) {
            return ""; 
        }
    }

    
    @Override
    public void modify(Object element, String property, Object value) {
        if (!TimersViewColumnPresentation.COL_VALUE.equals(property)) return;

        TriggerDMContext dmc = getAlarmDMC(element);
        if (dmc == null) return;
        DsfSession session = DsfSession.getSession(dmc.getSessionId());
        if (session == null) return;

        // Shell is used in displaying error dialogs.
        Shell shell = getShell();
        if (shell == null) return;
        
        Integer intValue = null;
        if (value instanceof String) {
            try {
                intValue = new Integer(((String)value).trim());
            } catch (NumberFormatException e) {
                MessageDialog.openError(shell, "Invalid Value", 
                    "Please enter a positive integer");  
                return;
            }
            if (intValue.intValue() <= 0) {
                MessageDialog.openError(shell, "Invalid Value", 
                    "Please enter a positive integer");  
                return;
            }
        }

        // Create the query to write the value to the service.
        SetValueQuery query = new SetValueQuery(dmc, intValue);

        try {
            session.getExecutor().execute(query);
        } catch (RejectedExecutionException e) {
            // View must be shutting down, no need to show error dialog.
        }
        try {
            // Return value is irrelevant, any error would come through with an exception.
            query.get().toString();
        } catch (InterruptedException e) {
            assert false;
        } catch (ExecutionException e) {
            // View must be shutting down, no need to show error dialog.
        }
    }

    /**
     * Need to dispose the cell modifier property because of the service 
     * tracker.
     */
    @ThreadSafe
    public synchronized void dispose() {
        if (fServiceTracker != null) {
            fServiceTracker.close();
        }
    }
    
    private Shell getShell() {
        if (DsfExamplesPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow() != null) {
            return DsfExamplesPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow().getShell();
        }
        return null;
    }

    private TriggerDMContext getAlarmDMC(Object element) {
        if (element instanceof IAdaptable) {
            return ((IAdaptable)element).getAdapter(TriggerDMContext.class);
        }
        return null;
    }

    @ThreadSafe
    private synchronized AlarmService getService(TriggerDMContext dmc) {
        // Create and initialize the service tracker if needed.
    	String serviceId = DsfServices.createServiceFilter( AlarmService.class, fSession.getId() ); 
        if (fServiceTracker == null) {
            try {
                fServiceTracker = new ServiceTracker(
                    DsfExamplesPlugin.getBundleContext(), 
                    DsfExamplesPlugin.getBundleContext().createFilter(serviceId), 
                    null);
                fServiceTracker.open();
            } catch (InvalidSyntaxException e) {
                return null;
            }
        }
        // Get the service.
        return (AlarmService)fServiceTracker.getService();
    }

    
    private class GetValueQuery extends Query<Integer> {
        final TriggerDMContext fDmc;
        
        private GetValueQuery(TriggerDMContext dmc) {
            super();
            fDmc = dmc; 
        }
        
        @Override
        protected void execute(final DataRequestMonitor<Integer> rm) {
            // Guard against the session being disposed.  If session is disposed
            // it could mean that the executor is shut-down, which in turn 
            // could mean that we can't execute the "done" argument.
            // In that case, cancel to notify waiting thread.
            final DsfSession session = DsfSession.getSession(fDmc.getSessionId());
            if (session == null) {
                rm.setStatus(new Status(IStatus.ERROR, DsfExamplesPlugin.PLUGIN_ID, IDsfStatusConstants.INVALID_STATE, "Debug session already shut down.", null)); //$NON-NLS-1$
                rm.done();
                return;
            }
                
            AlarmService service = getService(fDmc);
            if (service == null) {
                rm.setStatus(new Status(IStatus.ERROR, DsfExamplesPlugin.PLUGIN_ID, IDsfStatusConstants.INVALID_STATE, 
                                          "Service not available", null)); 
                rm.done();
                return;
            }
            
            int value = service.getTriggerValue(fDmc);
            if (value == -1) {
                rm.setStatus(new Status(IStatus.ERROR, DsfExamplesPlugin.PLUGIN_ID, IDsfStatusConstants.INVALID_HANDLE, 
                    "Invalid context", null)); 
                rm.done();                
                return;
            }
            
            rm.setData(value);
            rm.done();
        }
    }

    private class SetValueQuery extends Query<Object> {
        
        TriggerDMContext fDmc;
        int fValue;
        
        SetValueQuery(TriggerDMContext dmc, int value) {
            super();
            fDmc = dmc;
            fValue = value;
        }
        
        @Override
        protected void execute(final DataRequestMonitor<Object> rm) {
            // Guard against terminated session
            final DsfSession session = DsfSession.getSession(fDmc.getSessionId());
            if (session == null) {
                rm.setStatus(new Status(IStatus.ERROR, DsfExamplesPlugin.PLUGIN_ID, IDsfStatusConstants.INVALID_STATE, "Debug session already shut down.", null)); //$NON-NLS-1$
                rm.done();
                return;
            }

            // Guard against a disposed service
            AlarmService service = getService(fDmc);
            if (service == null) {
                rm.setStatus(new Status(IStatus.ERROR, DsfExamplesPlugin.PLUGIN_ID, IDsfStatusConstants.INVALID_STATE, 
                                          "Service not available", null)); 
                rm.done();
                return;
            }

            // Finally set the value and return.
            service.setTriggerValue(fDmc, fValue);
            
            // Return value is irrelevant.
            rm.setData(new Object());
            rm.done();
        }
    }
}

Back to the top