Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3039d7465db7d9caaa9e20b26049ecb2032db05f (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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/*******************************************************************************
 * Copyright (c) 2009 Red Hat, Inc.
 * 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:
 *     Red Hat - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.callgraph.core;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;

public abstract class SystemTapParser extends Job {
	protected IProgressMonitor monitor;
	protected String sourcePath;
	protected String viewID;
	protected SystemTapView view;
	protected boolean realTime = false;
	protected Object data;
	protected Object internalData;
	private String secondaryID = ""; //$NON-NLS-1$

	public boolean isDone;
	
	private RunTimeJob job;
	private boolean jobCancelled;

	public SystemTapParser() {
		super("Parsing data"); //$NON-NLS-1$
		this.sourcePath = PluginConstants.getDefaultIOPath();
		this.viewID = null;
		initialize();
		isDone = false;
		
		//PURELY FOR TESTING
		if (monitor == null){
			monitor = new NullProgressMonitor();
		}
	}
	

	public SystemTapParser(String name, String filePath) {
		super(name);
		// BY DEFAULT READ/WRITE FROM HERE
		if (filePath != null)
			this.sourcePath = filePath;
		else
			this.sourcePath = PluginConstants.getDefaultIOPath();
		this.viewID = null;
		initialize();
	}



	/**
	 * Initialize will be called in the constructors for this class. Use this
	 * method to initialize variables.
	 */
	protected abstract void initialize();
	

	/**
	 * Implement this method to execute parsing. The return from
	 * executeParsing() will be the return value of the run command.
	 * 
	 * SystemTapParser will call executeParsing() within its run method. (i.e.
	 * will execute in a separate, non-UI thread)
	 * 
	 * @return
	 */
	public abstract IStatus nonRealTimeParsing();


	
	
	/**
	 * Implement this method if your parser is to execute in realtime. This method 
	 * will be called as part of a while loop in a separate Job. Use the setInternalData
	 * method to initialize some data object for use in realTimeParsing. The default
	 * setInternalMethod method will set internalData to a BufferedReader
	 * <br> <br>
	 * After the isDone flag is set to true, the realTimeParsing() method will 
	 * be run one more time to catch any stragglers.
	 */
	public abstract IStatus realTimeParsing();
	

	/**
	 * Cleans names of form 'name").return', returning just the name
	 * 
	 * @param name
	 */
	protected String cleanFunctionName(String name) {
		return name.split("\"")[0]; //$NON-NLS-1$
	}

	/**
	 * Checks for quotations and brackets in the function name
	 * 
	 * @param name
	 */
	protected boolean isFunctionNameClean(String name) {
		if (name.contains("\"") || name.contains(")")) //$NON-NLS-1$ //$NON-NLS-2$
			return false;
		return true;
	}

	/**
	 * Creates a popup error dialog in a separate UI thread. Dialog title is
	 * 'Unexpected symbol,' name is 'ParseError' and body is the specified
	 * message.
	 * 
	 * @param message
	 */
	protected void parsingError(String message) {
		SystemTapUIErrorMessages mess = new SystemTapUIErrorMessages(
				Messages.getString("SystemTapParser.ParseErr"), //$NON-NLS-1$
				Messages.getString("SystemTapParser.ErrSymbol"), //$NON-NLS-1$
				message);
		mess.schedule();
	}

	/**
	 * Load the specified viewID by creating a StapUIJob. Does not return until the StapUIJob has.
	 * Returns true if the makeView was successful, false otherwise.
	 */
	protected boolean makeView() {
		// Create a UIJob to handle the rest
		if (viewID != null && viewID.length() > 0) {
			try {
			StapUIJob uijob = new StapUIJob(Messages
					.getString("StapGraphParser.JobName"), this, viewID); //$NON-NLS-1$
			uijob.schedule();
			uijob.join();
			view = uijob.getViewer();
			return true;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		return false;
	}

	@Override
	protected IStatus run(IProgressMonitor monitor) {
		// Generate real-time job
		IStatus returnStatus = Status.CANCEL_STATUS;
		this.monitor = monitor;
		if (this.monitor == null) {
			this.monitor = new NullProgressMonitor();
		}
		
		if (realTime && (job == null || job.getResult()==null)) {
			job = new RunTimeJob("RealTimeParser"); //$NON-NLS-1$
			job.schedule();
		} else {
			returnStatus = nonRealTimeParsing();
		}
		makeView();
				
	
		if (!returnStatus.isOK()){
			return returnStatus;
		}
		
		setData(this);
		return returnStatus;
	}
	
	public void printArrayListMap(HashMap<Integer, ArrayList<Integer>> blah) {
		int amt = 0;
		for (int a : blah.keySet()) {
			amt++;
			MP.print(a + " ::> "); //$NON-NLS-1$
			for (int c : blah.get(a)) {
				System.out.print(c + " "); //$NON-NLS-1$
			}
			MP.println(""); //$NON-NLS-1$
		}
	}

	@SuppressWarnings("unchecked")
	public void printMap(Map blah) {
		int amt = 0;
		for (Object a : blah.keySet()) {
			amt++;
			MP.println(a + " ::> " + blah.get(a)); //$NON-NLS-1$
		}
	}


	/**
	 * For easier JUnit testing only. Allows public access to run method without
	 * scheduling an extra job.
	 * 
	 * @param m
	 * @return
	 */
	public IStatus testRun(IProgressMonitor m, boolean realTime) {
		try {
			internalData = new BufferedReader(new FileReader(new File(sourcePath)));
		if (realTime)
			return realTimeParsing();
		else
			return nonRealTimeParsing();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return Status.CANCEL_STATUS;
	}

	public void launchFileErrorDialog() {
		SystemTapUIErrorMessages err = new SystemTapUIErrorMessages(Messages
				.getString("SystemTapParser.InvalidFile"), //$NON-NLS-1$
				Messages.getString("SystemTapParser.InvalidFile"), //$NON-NLS-1$
				Messages.getString("SystemTapParser.InvalidFileMsg1") + sourcePath + //$NON-NLS-1$
						Messages.getString("SystemTapParser.InvalidFileMsg2")); //$NON-NLS-1$
		err.schedule();
	}



	
	private class RunTimeJob extends Job {
		public RunTimeJob(String name) {
			super(name);
		}

		@Override
		public IStatus run(IProgressMonitor monitor) {
			IStatus returnStatus = Status.CANCEL_STATUS;       
	        try {
	        	setInternalData();
	            while (!isDone){
	            	returnStatus = realTimeParsing();
	            	if (monitor.isCanceled() || returnStatus == Status.CANCEL_STATUS)
	            		return Status.CANCEL_STATUS;
	            }
	            returnStatus = realTimeParsing();
		    } catch (Exception e) {
		       	e.printStackTrace();
	        }
			return returnStatus;
		}
	}
	
	/**
	 * Return the Data object.
	 * @return
	 */
	public Object getData() {
		return data;
	}
	
	
	/**
	 * Generic method for setting the internalData object. This will be called
	 * by a real-time-parser immediately before its main polling loop. By default,
	 * this method will attempt to create a bufferedReader around File(filePath)
	 * 
	 * @throws Exception
	 */
	protected void setInternalData() throws Exception {
		File file = new File(sourcePath);
		internalData = new BufferedReader(new FileReader(file));				
	}


	/**
	 * Returns the monitor
	 * 
	 * @return
	 */
	public IProgressMonitor getMonitor() {
		return monitor;
	}
	
	
	/**
	 * Gets the file to read from
	 * 
	 * @return
	 */
	public String getFile() {
		return sourcePath;
	}
	
	
	/**
	 * Sets the file to read from
	 * 
	 * @param source
	 */
	public void setSourcePath(String source) {
		this.sourcePath = source;
	}
	

	/**
	 * Slightly more graceful termination than cancelJob.
	 * Setting to true will cause the parser's RunTimeJob to terminate at the end
	 * of its current iteration. Does not offer as solid a guarantee
	 * of termination, however.
	 * @param val
	 */
	public void setDone(boolean val) {
		isDone = val;
	}

	
	public void setMonitor(IProgressMonitor m) {
		this.monitor = m;
	}
	
	/**
	 * Set whether or not this parser runs in real time. If viewID has already
	 * been set, this will also attempt to open the view.
	 * 
	 * @throws InterruptedException
	 */
	public void setRealTime(boolean val) throws InterruptedException {
		realTime = val;

	}
	
	/**
	 * Set the viewID to use for this parser -- see the callgraph.core view
	 * extension point. If realTime is set to true, this will also attempt to
	 * open the view.
	 * 
	 * @throws InterruptedException
	 */
	public void setViewID(String value) throws InterruptedException {
		viewID = value;
	}
	
	/**
	 * Called at the end of a non-realtime run. 
	 * Feel free to override this method if using non-realtime functions.
	 * The setData method will be called after executeParsing() is run. The getData() method
	 * will be used by the SystemTapView to get the data associated with this parser.
	 * <br><br>
	 * Alternatively, you can cast the parser within SystemTapView to your own parser class and access
	 * its data structures that way. 
	 */
	public void setData(Object obj) {
		data = obj;
	}
	
	/**
	 * Cancels the RunTimeJob affiliated with this parser. Returns the result
	 * of job.cancel() if job is not null, or false if job is null.
	 */
	public boolean cancelJob() {
		if (job != null) {
			jobCancelled = true;
			return job.cancel();
		}
		return false;
	}
	
	/**
	 * Returns true if job is not null and the job's last result was CANCEL_STATUS.
	 *  False otherwise.
	 */
	public boolean isJobCancelled() {
		if ((job != null && job.getResult() == Status.CANCEL_STATUS)
				|| jobCancelled) {
			return true;
		}
		return false;
	}

	
	public void setKillButtonEnabled(boolean val) {
		if (view != null) 
			view.setKillButtonEnabled(val);
		
	}

	public boolean isRealTime() {
		return realTime;
	}


	public void setSecondaryID(String secondaryID) {
		this.secondaryID = secondaryID;
	}


	public String getSecondaryID() {
		return secondaryID;
	}
}

Back to the top