diff options
Diffstat (limited to 'plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/LocalJob.java')
-rw-r--r-- | plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/LocalJob.java | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/LocalJob.java b/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/LocalJob.java new file mode 100644 index 0000000..5b082a0 --- /dev/null +++ b/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/LocalJob.java | |||
@@ -0,0 +1,101 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010 Intel Corporation. | ||
3 | * All rights reserved. This program and the accompanying materials | ||
4 | * are made available under the terms of the Eclipse Public License v1.0 | ||
5 | * which accompanies this distribution, and is available at | ||
6 | * http://www.eclipse.org/legal/epl-v10.html | ||
7 | * | ||
8 | * Contributors: | ||
9 | * Intel - initial API and implementation | ||
10 | *******************************************************************************/ | ||
11 | |||
12 | package org.yocto.sdk.remotetools; | ||
13 | |||
14 | import java.io.File; | ||
15 | import java.io.IOException; | ||
16 | |||
17 | import org.eclipse.core.runtime.IProgressMonitor; | ||
18 | import org.eclipse.core.runtime.IStatus; | ||
19 | import org.eclipse.core.runtime.Status; | ||
20 | import org.eclipse.core.runtime.jobs.Job; | ||
21 | import org.eclipse.ui.IWorkbenchWindow; | ||
22 | import org.eclipse.jface.dialogs.MessageDialog; | ||
23 | import org.eclipse.swt.SWTException; | ||
24 | |||
25 | public class LocalJob extends Job { | ||
26 | |||
27 | public static final String LOCAL_JOB_FAMILY = "localJobFamily"; | ||
28 | private String[] cmdarray; | ||
29 | private String[] envp; | ||
30 | private File dir; | ||
31 | private int exitValue; | ||
32 | private Exception exception; | ||
33 | private IWorkbenchWindow window; | ||
34 | |||
35 | public LocalJob(String name, String[] cmdarray, String[] envp, File dir, IWorkbenchWindow window) { | ||
36 | super(name); | ||
37 | this.cmdarray=cmdarray; | ||
38 | this.envp=envp; | ||
39 | this.dir=dir; | ||
40 | this.window=window; | ||
41 | this.exitValue=0; | ||
42 | this.exception=null; | ||
43 | } | ||
44 | |||
45 | @Override | ||
46 | protected IStatus run(IProgressMonitor monitor) { | ||
47 | Process p=null; | ||
48 | boolean cancel=false; | ||
49 | |||
50 | try { | ||
51 | //start process | ||
52 | p=Runtime.getRuntime().exec(cmdarray,envp,dir); | ||
53 | |||
54 | //wait for completion | ||
55 | while (!cancel) { | ||
56 | |||
57 | if(monitor.isCanceled()) | ||
58 | cancel=true; | ||
59 | |||
60 | try { | ||
61 | exitValue=p.exitValue(); | ||
62 | break; | ||
63 | }catch (IllegalThreadStateException e) { | ||
64 | } | ||
65 | |||
66 | Thread.sleep(500); | ||
67 | } | ||
68 | |||
69 | }catch (IOException e) { | ||
70 | exception=e; | ||
71 | }catch (InterruptedException e){ | ||
72 | cancel=true; | ||
73 | }finally { | ||
74 | if(p!=null) | ||
75 | p.destroy(); | ||
76 | } | ||
77 | try { | ||
78 | if(exitValue!=0 || exception!=null) { | ||
79 | window.getWorkbench().getDisplay().syncExec(new Runnable() { | ||
80 | public void run() { | ||
81 | MessageDialog.openError(window.getShell(), | ||
82 | Messages.LocalJob_Title, | ||
83 | Messages.ErrorLocalJob + ": " + getName() + | ||
84 | (exitValue!=0 ? "\n\tExit value: " + new Integer(exitValue).toString() : new String("")) + | ||
85 | (exception!=null ? "\n\t" + exception.getMessage() : new String ("")) | ||
86 | ); | ||
87 | } | ||
88 | }); | ||
89 | } | ||
90 | }catch (SWTException e) { | ||
91 | e.printStackTrace(); | ||
92 | }catch (Exception e) { | ||
93 | e.printStackTrace(); | ||
94 | } | ||
95 | return (cancel!=true) ? Status.OK_STATUS : Status.CANCEL_STATUS; | ||
96 | } | ||
97 | |||
98 | public boolean belongsTo(Object family) { | ||
99 | return family == LOCAL_JOB_FAMILY; | ||
100 | } | ||
101 | } | ||