summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/actions/BaseModel.java
diff options
context:
space:
mode:
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.java126
1 files changed, 126 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..6686d39
--- /dev/null
+++ b/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/actions/BaseModel.java
@@ -0,0 +1,126 @@
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 *******************************************************************************/
11package org.yocto.sdk.remotetools.actions;
12
13import java.io.IOException;
14import java.io.InputStream;
15import java.lang.reflect.InvocationTargetException;
16
17import org.eclipse.core.runtime.IProgressMonitor;
18import org.eclipse.core.runtime.SubProgressMonitor;
19import org.eclipse.jface.operation.IRunnableWithProgress;
20import org.eclipse.rse.core.model.IHost;
21import org.yocto.remote.utils.RemoteHelper;
22import org.yocto.remote.utils.RemoteShellExec;
23
24abstract 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 RemoteHelper.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 public void setHost (IHost host) {
64 this.host = host;
65 }
66 abstract public void process(IProgressMonitor monitor) throws InvocationTargetException,InterruptedException;
67
68 public BaseModel(IHost host, String taskName, String localScript, String remoteExec) {
69 this.host = host;
70 this.taskName = taskName;
71 this.localScript = localScript;
72 this.remoteExec = remoteExec;
73 }
74 protected void init(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
75 }
76
77 protected void clean(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
78 }
79
80 public void run(IProgressMonitor monitor) throws InvocationTargetException,
81 InterruptedException {
82
83 try {
84 monitor.beginTask(RUN_MSG + taskName, WORKLOAD);
85
86 monitor.subTask(INIT_MSG + taskName + DOTS);
87 init(new SubProgressMonitor(monitor, INIT_PERCENT));
88
89 monitor.subTask(PRE_PROCESS_MSG + taskName + DOTS);
90 preProcess(new SubProgressMonitor(monitor, PRE_PROCESS_PERCENT));
91
92 monitor.subTask(PROCESS_MSG + taskName + DOTS);
93 process(new SubProgressMonitor(monitor, PROCESS_PERCENT));
94
95 monitor.subTask(POST_PROCESS_MSG + taskName + DOTS);
96 postProcess(new SubProgressMonitor(monitor, POST_PROCESS_PERCENT));
97 } catch (InterruptedException e){
98 throw new InterruptedException("User cancelled!");
99 } catch (InvocationTargetException e) {
100 throw e;
101 } finally {
102 monitor.subTask(CLEAN_MSG + taskName + DOTS);
103 clean(new SubProgressMonitor(monitor, CLEAN_PERCENT));
104 monitor.done();
105 }
106 }
107
108 protected void getDataFile(IProgressMonitor monitor) throws Exception {
109 RemoteHelper.getRemoteFile( host, localFile, remoteFile, monitor);
110 }
111 protected void runRemoteShellExec(IProgressMonitor monitor, String args, boolean cancelable) throws Exception {
112 try {
113 RemoteShellExec exec = new RemoteShellExec(host, remoteExec);
114 exec.start(null, args, monitor);
115 monitor.worked(1);
116 checkTerminate(exec.getInputStream());
117 int exit_code = exec.waitFor(cancelable ? monitor : null);
118 exec.terminate();
119 if(exit_code != 0)
120 throw new Exception(taskName + FAILED_ERR_MSG + new Integer(exit_code).toString());
121 } finally {
122 monitor.done();
123 }
124 }
125 protected void checkTerminate(InputStream inputStream) throws IOException {}
126}