summaryrefslogtreecommitdiffstats
path: root/plugins/org.yocto.sdk.remotetools/src/org/yocto/sdk/remotetools/actions/BaseModel.java
blob: dacd19212740cb949ad1034c7d9a0ae5f5be0707 (plain)
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
/*******************************************************************************
 * Copyright (c) 2010 Intel Corporation.
 * 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:
 * Intel - initial API and implementation
 *******************************************************************************/
package org.yocto.sdk.remotetools.actions;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.SubProgressMonitor;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.rse.core.model.IHost;
import org.yocto.remote.utils.RSEHelper;
import org.yocto.remote.utils.RemoteShellExec;

abstract public class BaseModel implements IRunnableWithProgress {
	protected IHost host;
	protected String taskName;
	protected String localScript;
	protected String remoteExec;
	protected String localFile;
	protected String remoteFile;

	private static final int WORKLOAD = 100;

	private static final int INIT_PERCENT = 5;
	private static final int PRE_PROCESS_PERCENT = 30;
	private static final int PROCESS_PERCENT = 30;
	private static final int POST_PROCESS_PERCENT = 30;
	private static final int CLEAN_PERCENT = 5;

	private static final String RUN_MSG = "Running task: ";
	private static final String INIT_MSG = "Initializing ";
	private static final String PRE_PROCESS_MSG = "Preparing ";
	private static final String PROCESS_MSG = "Processing ";
	private static final String POST_PROCESS_MSG = "Finishing ";
	private static final String CLEAN_MSG = "Cleaning ";
	private static final String DOTS = "...";
	private static final String FAILED_ERR_MSG = " failed with exit code ";

	public void preProcess(IProgressMonitor monitor) throws InvocationTargetException,	InterruptedException{
		//upload script to remote
		try {
			RSEHelper.putRemoteFileInPlugin(host, localScript, remoteExec, monitor);
		}catch (InterruptedException e){
			throw e;
		}catch (InvocationTargetException e) {
			throw e;
		}catch (Exception e) {
			throw new InvocationTargetException(e, e.getMessage());
		}
	}
	public void postProcess(IProgressMonitor monitor) throws InvocationTargetException,InterruptedException{}

	abstract public void process(IProgressMonitor monitor) throws InvocationTargetException,InterruptedException;
	
	public BaseModel(IHost host, String taskName, String localScript, String remoteExec) {
		this.host = host;
		this.taskName = taskName;
		this.localScript = localScript;
		this.remoteExec = remoteExec;
	}
	protected void init(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
	}
	
	protected void clean(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
	}

	public void run(IProgressMonitor monitor) throws InvocationTargetException,
    InterruptedException {
	
		try {
			monitor.beginTask(RUN_MSG + taskName, WORKLOAD);

			monitor.subTask(INIT_MSG + taskName + DOTS);
			init(new SubProgressMonitor(monitor, INIT_PERCENT));

			monitor.subTask(PRE_PROCESS_MSG + taskName + DOTS);
			preProcess(new SubProgressMonitor(monitor, PRE_PROCESS_PERCENT));

			monitor.subTask(PROCESS_MSG + taskName + DOTS);
			process(new SubProgressMonitor(monitor, PROCESS_PERCENT));

			monitor.subTask(POST_PROCESS_MSG + taskName + DOTS);
			postProcess(new SubProgressMonitor(monitor, POST_PROCESS_PERCENT));
		} catch (InterruptedException e){
			throw new InterruptedException("User cancelled!");
		} catch (InvocationTargetException e) {
			throw e;
		} finally {
			monitor.subTask(CLEAN_MSG + taskName + DOTS);
			clean(new SubProgressMonitor(monitor, CLEAN_PERCENT));
			monitor.done();
		}
	}

	protected void getDataFile(IProgressMonitor monitor) throws Exception {
		RSEHelper.getRemoteFile( host, localFile, remoteFile, monitor);
	}
	protected void runRemoteShellExec(IProgressMonitor monitor, String args, boolean cancelable) throws Exception {
		try {
			RemoteShellExec exec = new RemoteShellExec(host, remoteExec);
			exec.start(null, args, monitor);
			monitor.worked(1);
			checkTerminate(exec.getInputStream());
			int exit_code = exec.waitFor(cancelable ? monitor : null);
			exec.terminate();
			if(exit_code != 0)
				throw new Exception(taskName + FAILED_ERR_MSG + new Integer(exit_code).toString());
		} finally {
			monitor.done();
		}
	}
	protected void checkTerminate(InputStream inputStream) throws IOException {}
}