diff options
Diffstat (limited to 'plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/actions/BaseModel.java')
-rw-r--r-- | plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/actions/BaseModel.java | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/actions/BaseModel.java b/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/actions/BaseModel.java new file mode 100644 index 0000000..dacd192 --- /dev/null +++ b/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/actions/BaseModel.java | |||
@@ -0,0 +1,123 @@ | |||
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 | package org.yocto.sdk.remotetools.actions; | ||
12 | |||
13 | import java.io.IOException; | ||
14 | import java.io.InputStream; | ||
15 | import java.lang.reflect.InvocationTargetException; | ||
16 | |||
17 | import org.eclipse.core.runtime.IProgressMonitor; | ||
18 | import org.eclipse.core.runtime.SubProgressMonitor; | ||
19 | import org.eclipse.jface.operation.IRunnableWithProgress; | ||
20 | import org.eclipse.rse.core.model.IHost; | ||
21 | import org.yocto.remote.utils.RSEHelper; | ||
22 | import org.yocto.remote.utils.RemoteShellExec; | ||
23 | |||
24 | abstract public class BaseModel implements IRunnableWithProgress { | ||
25 | protected IHost host; | ||
26 | protected String taskName; | ||
27 | protected String localScript; | ||
28 | protected String remoteExec; | ||
29 | protected String localFile; | ||
30 | protected String remoteFile; | ||
31 | |||
32 | private static final int WORKLOAD = 100; | ||
33 | |||
34 | private static final int INIT_PERCENT = 5; | ||
35 | private static final int PRE_PROCESS_PERCENT = 30; | ||
36 | private static final int PROCESS_PERCENT = 30; | ||
37 | private static final int POST_PROCESS_PERCENT = 30; | ||
38 | private static final int CLEAN_PERCENT = 5; | ||
39 | |||
40 | private static final String RUN_MSG = "Running task: "; | ||
41 | private static final String INIT_MSG = "Initializing "; | ||
42 | private static final String PRE_PROCESS_MSG = "Preparing "; | ||
43 | private static final String PROCESS_MSG = "Processing "; | ||
44 | private static final String POST_PROCESS_MSG = "Finishing "; | ||
45 | private static final String CLEAN_MSG = "Cleaning "; | ||
46 | private static final String DOTS = "..."; | ||
47 | private static final String FAILED_ERR_MSG = " failed with exit code "; | ||
48 | |||
49 | public void preProcess(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException{ | ||
50 | //upload script to remote | ||
51 | try { | ||
52 | RSEHelper.putRemoteFileInPlugin(host, localScript, remoteExec, monitor); | ||
53 | }catch (InterruptedException e){ | ||
54 | throw e; | ||
55 | }catch (InvocationTargetException e) { | ||
56 | throw e; | ||
57 | }catch (Exception e) { | ||
58 | throw new InvocationTargetException(e, e.getMessage()); | ||
59 | } | ||
60 | } | ||
61 | public void postProcess(IProgressMonitor monitor) throws InvocationTargetException,InterruptedException{} | ||
62 | |||
63 | abstract public void process(IProgressMonitor monitor) throws InvocationTargetException,InterruptedException; | ||
64 | |||
65 | public BaseModel(IHost host, String taskName, String localScript, String remoteExec) { | ||
66 | this.host = host; | ||
67 | this.taskName = taskName; | ||
68 | this.localScript = localScript; | ||
69 | this.remoteExec = remoteExec; | ||
70 | } | ||
71 | protected void init(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { | ||
72 | } | ||
73 | |||
74 | protected void clean(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { | ||
75 | } | ||
76 | |||
77 | public void run(IProgressMonitor monitor) throws InvocationTargetException, | ||
78 | InterruptedException { | ||
79 | |||
80 | try { | ||
81 | monitor.beginTask(RUN_MSG + taskName, WORKLOAD); | ||
82 | |||
83 | monitor.subTask(INIT_MSG + taskName + DOTS); | ||
84 | init(new SubProgressMonitor(monitor, INIT_PERCENT)); | ||
85 | |||
86 | monitor.subTask(PRE_PROCESS_MSG + taskName + DOTS); | ||
87 | preProcess(new SubProgressMonitor(monitor, PRE_PROCESS_PERCENT)); | ||
88 | |||
89 | monitor.subTask(PROCESS_MSG + taskName + DOTS); | ||
90 | process(new SubProgressMonitor(monitor, PROCESS_PERCENT)); | ||
91 | |||
92 | monitor.subTask(POST_PROCESS_MSG + taskName + DOTS); | ||
93 | postProcess(new SubProgressMonitor(monitor, POST_PROCESS_PERCENT)); | ||
94 | } catch (InterruptedException e){ | ||
95 | throw new InterruptedException("User cancelled!"); | ||
96 | } catch (InvocationTargetException e) { | ||
97 | throw e; | ||
98 | } finally { | ||
99 | monitor.subTask(CLEAN_MSG + taskName + DOTS); | ||
100 | clean(new SubProgressMonitor(monitor, CLEAN_PERCENT)); | ||
101 | monitor.done(); | ||
102 | } | ||
103 | } | ||
104 | |||
105 | protected void getDataFile(IProgressMonitor monitor) throws Exception { | ||
106 | RSEHelper.getRemoteFile( host, localFile, remoteFile, monitor); | ||
107 | } | ||
108 | protected void runRemoteShellExec(IProgressMonitor monitor, String args, boolean cancelable) throws Exception { | ||
109 | try { | ||
110 | RemoteShellExec exec = new RemoteShellExec(host, remoteExec); | ||
111 | exec.start(null, args, monitor); | ||
112 | monitor.worked(1); | ||
113 | checkTerminate(exec.getInputStream()); | ||
114 | int exit_code = exec.waitFor(cancelable ? monitor : null); | ||
115 | exec.terminate(); | ||
116 | if(exit_code != 0) | ||
117 | throw new Exception(taskName + FAILED_ERR_MSG + new Integer(exit_code).toString()); | ||
118 | } finally { | ||
119 | monitor.done(); | ||
120 | } | ||
121 | } | ||
122 | protected void checkTerminate(InputStream inputStream) throws IOException {} | ||
123 | } | ||