Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 60926969a7eb0f20988cea90bd63b4c130ebbf61 (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
/*******************************************************************************
 * Copyright (c) 2014 Wind River Systems, 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:
 *    Markus Schorn - initial API and implementation
 *******************************************************************************/
package org.eclipse.tcf.te.tcf.remote.core.operation;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.tcf.protocol.Protocol;
import org.eclipse.tcf.te.tcf.remote.core.activator.CoreBundleActivator;

public abstract class TCFOperation <R> {
	private IStatus fError;
	private R fResult;
	private boolean fDone;


	protected final boolean shallAbort(IStatus status) {
		synchronized (this) {
			if (fDone)
				return true;
			if (!status.isOK()) {
				setError(status);
				return true;
			}
			return false;
		}
	}

	protected final boolean shallAbort(Throwable error) {
		synchronized (this) {
			if (fDone)
				return true;
			if (error != null) {
				setError(error);
				return true;
			}
			return false;
		}
	}

	protected final void setError(Throwable error) {
		setError(createStatus(error.getMessage(), error));
	}

	protected final Status createStatus(String msg, Throwable error) {
	    return new Status(IStatus.ERROR, CoreBundleActivator.getUniqueIdentifier(), msg, error);
    }

	protected final void setError(IStatus error) {
    	synchronized (this) {
    		fError = error;
    		fDone = true;
    		notifyAll();
		}
    }

	protected void setResult(R result) {
    	synchronized (this) {
    		fResult = result;
    		fDone = true;
    		notifyAll();
		}
    }

    protected R waitForResult(SubMonitor sm) throws CoreException, InterruptedException, OperationCanceledException {
    	synchronized (this) {
    		while (!fDone) {
    			if (sm.isCanceled()) {
    				fDone = true;
    				throw new OperationCanceledException();
    			}
    			wait(1000);
    		}
    		if (fError != null) {
    			throw new CoreException(fError);
    		}
    		return fResult;
    	}
    }

    public final R execute(SubMonitor sm) throws CoreException, OperationCanceledException {
		Protocol.invokeLater(new Runnable() {
			@Override
			public void run() {
				doExecute();
			}
		});
		try {
	        return waitForResult(sm);
        } catch (InterruptedException e) {
        	Thread.currentThread().interrupt();
        	throw new OperationCanceledException();
        }
    }

	protected abstract void doExecute();
}

Back to the top