Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: e38874844359673fa9740cc8478a8f5cbf1e3fc9 (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
/****************************************************************************
 * Copyright (c) 2014 Composent, Inc. and others.
 * 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:
 *    Composent, Inc. - initial API and implementation
 *****************************************************************************/
package org.eclipse.ecf.remoteservice.asyncproxy;

import java.lang.reflect.Method;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Future;

import org.eclipse.equinox.concurrent.future.IFuture;
import org.osgi.util.promise.Deferred;
import org.osgi.util.promise.Promise;

public abstract class AbstractAsyncProxyRemoteService {

	@SuppressWarnings("rawtypes")
	protected abstract IFuture callAsync(AbstractAsyncProxyRemoteCall call);
	
	@SuppressWarnings("rawtypes")
	protected abstract Future callFutureAsync(AbstractAsyncProxyRemoteCall call);
	
	protected abstract void callCompletableAsync(AbstractAsyncProxyRemoteCall call, IAsyncProxyCompletable completable);

	protected boolean isReturnAsync(Object proxy, Method method, Object[] args) {
		@SuppressWarnings("rawtypes")
		Class returnType = method.getReturnType();
		return (CompletableFuture.class.isAssignableFrom(returnType) || CompletionStage.class.isAssignableFrom(returnType) ||
				Future.class.isAssignableFrom(returnType) || IFuture.class.isAssignableFrom(returnType));
	}
	
	@SuppressWarnings("unchecked")
	protected Object callFuture(AbstractAsyncProxyRemoteCall call, @SuppressWarnings("rawtypes") Class returnType) {
		if (Promise.class.isAssignableFrom(returnType)) {
			@SuppressWarnings("rawtypes")
			Deferred d = new Deferred();
			callCompletableAsync(call, (r,hadException,exception) -> {
				if (hadException) d.fail(exception);
				else d.resolve(r);
			});
			return d.getPromise();
		}
		// If the result value is a CompletableFuture then
		// we callCompletableAsync
		if (CompletableFuture.class.isAssignableFrom(returnType) || CompletionStage.class.isAssignableFrom(returnType)) {
			@SuppressWarnings("rawtypes")
		    CompletableFuture result = new CompletableFuture();
			callCompletableAsync(call, (r,hadException,exception) -> {
				if (hadException) result.completeExceptionally(exception);
				else result.complete(r);
			});
			// And return the CompletableFuture
			return result;
		}
		// Else if it's an IFuture then return
		// IFuture result of callAsync
		if (IFuture.class.isAssignableFrom(returnType))
			return callAsync(call);
		// Else it must be a Future return value
		return callFutureAsync(call);
	}

}

Back to the top