2017-04-04 08:22:11 +02:00
|
|
|
/**
|
|
|
|
*
|
2018-03-29 12:35:11 +02:00
|
|
|
* Copyright 2017-2018 Florian Schmaus
|
2017-04-04 08:22:11 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
package org.jivesoftware.smack;
|
|
|
|
|
2017-08-12 17:35:20 +02:00
|
|
|
import java.util.concurrent.ArrayBlockingQueue;
|
|
|
|
import java.util.concurrent.BlockingQueue;
|
|
|
|
import java.util.concurrent.CancellationException;
|
2017-04-04 08:22:11 +02:00
|
|
|
import java.util.concurrent.ExecutionException;
|
2017-08-12 17:35:20 +02:00
|
|
|
import java.util.concurrent.ExecutorService;
|
2017-04-04 08:22:11 +02:00
|
|
|
import java.util.concurrent.Future;
|
2017-08-12 17:35:20 +02:00
|
|
|
import java.util.concurrent.RejectedExecutionHandler;
|
|
|
|
import java.util.concurrent.ThreadFactory;
|
|
|
|
import java.util.concurrent.ThreadPoolExecutor;
|
2017-04-04 08:22:11 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import java.util.concurrent.TimeoutException;
|
|
|
|
|
|
|
|
import org.jivesoftware.smack.packet.Stanza;
|
2017-08-12 17:35:20 +02:00
|
|
|
import org.jivesoftware.smack.util.CallbackRecipient;
|
|
|
|
import org.jivesoftware.smack.util.ExceptionCallback;
|
|
|
|
import org.jivesoftware.smack.util.SuccessCallback;
|
2017-04-04 08:22:11 +02:00
|
|
|
|
2017-08-12 17:35:20 +02:00
|
|
|
public abstract class SmackFuture<V, E extends Exception> implements Future<V>, CallbackRecipient<V, E> {
|
2017-04-04 08:22:11 +02:00
|
|
|
|
|
|
|
private boolean cancelled;
|
|
|
|
|
2017-08-12 17:35:20 +02:00
|
|
|
protected V result;
|
2017-04-04 08:22:11 +02:00
|
|
|
|
2017-08-12 17:35:20 +02:00
|
|
|
protected E exception;
|
2017-04-04 08:22:11 +02:00
|
|
|
|
|
|
|
private SuccessCallback<V> successCallback;
|
|
|
|
|
2017-08-12 17:35:20 +02:00
|
|
|
private ExceptionCallback<E> exceptionCallback;
|
2017-04-04 08:22:11 +02:00
|
|
|
|
|
|
|
@Override
|
2018-03-29 12:35:11 +02:00
|
|
|
public final synchronized boolean cancel(boolean mayInterruptIfRunning) {
|
2017-04-04 08:22:11 +02:00
|
|
|
if (isDone()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cancelled = true;
|
2017-08-12 17:35:20 +02:00
|
|
|
|
|
|
|
if (mayInterruptIfRunning) {
|
|
|
|
notifyAll();
|
|
|
|
}
|
|
|
|
|
2017-04-04 08:22:11 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-29 12:35:11 +02:00
|
|
|
public final synchronized boolean isCancelled() {
|
2017-04-04 08:22:11 +02:00
|
|
|
return cancelled;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-29 12:35:11 +02:00
|
|
|
public final synchronized boolean isDone() {
|
2017-04-04 08:22:11 +02:00
|
|
|
return result != null;
|
|
|
|
}
|
|
|
|
|
2017-08-12 17:35:20 +02:00
|
|
|
@Override
|
|
|
|
public CallbackRecipient<V, E> onSuccess(SuccessCallback<V> successCallback) {
|
2017-04-04 08:22:11 +02:00
|
|
|
this.successCallback = successCallback;
|
|
|
|
maybeInvokeCallbacks();
|
2017-08-12 17:35:20 +02:00
|
|
|
return this;
|
2017-04-04 08:22:11 +02:00
|
|
|
}
|
|
|
|
|
2017-08-12 17:35:20 +02:00
|
|
|
@Override
|
|
|
|
public CallbackRecipient<V, E> onError(ExceptionCallback<E> exceptionCallback) {
|
|
|
|
this.exceptionCallback = exceptionCallback;
|
|
|
|
maybeInvokeCallbacks();
|
|
|
|
return this;
|
2017-04-04 08:22:11 +02:00
|
|
|
}
|
|
|
|
|
2017-12-13 23:10:11 +01:00
|
|
|
private V getOrThrowExecutionException() throws ExecutionException {
|
2017-08-12 17:35:20 +02:00
|
|
|
assert (result != null || exception != null || cancelled);
|
2017-04-04 08:22:11 +02:00
|
|
|
if (result != null) {
|
|
|
|
return result;
|
|
|
|
}
|
2017-08-12 17:35:20 +02:00
|
|
|
if (exception != null) {
|
|
|
|
throw new ExecutionException(exception);
|
|
|
|
}
|
2017-04-04 08:22:11 +02:00
|
|
|
|
2017-08-12 17:35:20 +02:00
|
|
|
assert (cancelled);
|
|
|
|
throw new CancellationException();
|
2017-04-04 08:22:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-29 12:35:11 +02:00
|
|
|
public final synchronized V get() throws InterruptedException, ExecutionException {
|
2017-08-12 17:35:20 +02:00
|
|
|
while (result == null && exception == null && !cancelled) {
|
2017-04-04 08:22:11 +02:00
|
|
|
wait();
|
|
|
|
}
|
|
|
|
|
2017-08-12 17:35:20 +02:00
|
|
|
return getOrThrowExecutionException();
|
|
|
|
}
|
|
|
|
|
2018-03-29 12:35:11 +02:00
|
|
|
public final synchronized V getOrThrow() throws E, InterruptedException {
|
2017-08-12 17:35:20 +02:00
|
|
|
while (result == null && exception == null && !cancelled) {
|
2017-08-16 13:45:51 +02:00
|
|
|
wait();
|
2017-08-12 17:35:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (exception != null) {
|
|
|
|
throw exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cancelled) {
|
|
|
|
throw new CancellationException();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert result != null;
|
|
|
|
return result;
|
2017-04-04 08:22:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-03-29 12:35:11 +02:00
|
|
|
public final synchronized V get(long timeout, TimeUnit unit)
|
2017-04-04 08:22:11 +02:00
|
|
|
throws InterruptedException, ExecutionException, TimeoutException {
|
|
|
|
final long deadline = System.currentTimeMillis() + unit.toMillis(timeout);
|
|
|
|
while (result != null && exception != null) {
|
|
|
|
final long waitTimeRemaining = deadline - System.currentTimeMillis();
|
|
|
|
if (waitTimeRemaining > 0) {
|
|
|
|
wait(waitTimeRemaining);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-12 17:35:20 +02:00
|
|
|
if (cancelled) {
|
|
|
|
throw new CancellationException();
|
|
|
|
}
|
|
|
|
|
2017-04-04 08:22:11 +02:00
|
|
|
if (result == null || exception == null) {
|
|
|
|
throw new TimeoutException();
|
|
|
|
}
|
|
|
|
|
2017-08-12 17:35:20 +02:00
|
|
|
return getOrThrowExecutionException();
|
2017-04-04 08:22:11 +02:00
|
|
|
}
|
|
|
|
|
2017-08-12 17:35:20 +02:00
|
|
|
private static final ExecutorService EXECUTOR_SERVICE;
|
|
|
|
|
|
|
|
static {
|
|
|
|
ThreadFactory threadFactory = new ThreadFactory() {
|
|
|
|
@Override
|
|
|
|
public Thread newThread(Runnable r) {
|
|
|
|
Thread thread = new Thread(r);
|
|
|
|
thread.setDaemon(true);
|
|
|
|
thread.setName("SmackFuture Thread");
|
|
|
|
return thread;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(128);
|
|
|
|
RejectedExecutionHandler rejectedExecutionHandler = new RejectedExecutionHandler() {
|
|
|
|
@Override
|
|
|
|
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
|
|
|
|
r.run();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
int cores = Runtime.getRuntime().availableProcessors();
|
|
|
|
int maximumPoolSize = cores <= 4 ? 2 : cores;
|
|
|
|
ExecutorService executorService = new ThreadPoolExecutor(0, maximumPoolSize, 60L, TimeUnit.SECONDS,
|
|
|
|
blockingQueue, threadFactory, rejectedExecutionHandler);
|
|
|
|
|
|
|
|
EXECUTOR_SERVICE = executorService;
|
|
|
|
}
|
|
|
|
|
|
|
|
@SuppressWarnings("FutureReturnValueIgnored")
|
2017-04-04 08:22:11 +02:00
|
|
|
protected final synchronized void maybeInvokeCallbacks() {
|
2017-08-12 17:35:20 +02:00
|
|
|
if (cancelled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-04 08:22:11 +02:00
|
|
|
if (result != null && successCallback != null) {
|
2017-08-12 17:35:20 +02:00
|
|
|
EXECUTOR_SERVICE.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
successCallback.onSuccess(result);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (exception != null && exceptionCallback != null) {
|
|
|
|
EXECUTOR_SERVICE.submit(new Runnable() {
|
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
exceptionCallback.processException(exception);
|
|
|
|
}
|
|
|
|
});
|
2017-04-04 08:22:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-12 17:35:20 +02:00
|
|
|
public static class InternalSmackFuture<V, E extends Exception> extends SmackFuture<V, E> {
|
|
|
|
public final synchronized void setResult(V result) {
|
|
|
|
this.result = result;
|
|
|
|
this.notifyAll();
|
2017-04-04 08:22:11 +02:00
|
|
|
|
2017-08-12 17:35:20 +02:00
|
|
|
maybeInvokeCallbacks();
|
|
|
|
}
|
2017-04-04 08:22:11 +02:00
|
|
|
|
2017-08-12 17:35:20 +02:00
|
|
|
public final synchronized void setException(E exception) {
|
|
|
|
this.exception = exception;
|
|
|
|
this.notifyAll();
|
2017-04-04 08:22:11 +02:00
|
|
|
|
2017-08-12 17:35:20 +02:00
|
|
|
maybeInvokeCallbacks();
|
|
|
|
}
|
2017-04-04 08:22:11 +02:00
|
|
|
}
|
|
|
|
|
2018-03-29 12:35:11 +02:00
|
|
|
public abstract static class InternalProcessStanzaSmackFuture<V, E extends Exception> extends InternalSmackFuture<V, E>
|
2017-08-12 17:35:20 +02:00
|
|
|
implements StanzaListener, ExceptionCallback<E> {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method checks if the given exception is <b>not</b> fatal. If this method returns <code>false</code>,
|
|
|
|
* then the future will automatically set the given exception as failure reason and notify potential waiting
|
|
|
|
* threads.
|
|
|
|
*
|
|
|
|
* @param exception the exception to check.
|
|
|
|
* @return <code>true</code> if the exception is not fatal, <code>false</code> otherwise.
|
|
|
|
*/
|
|
|
|
protected abstract boolean isNonFatalException(E exception);
|
|
|
|
|
|
|
|
protected abstract void handleStanza(Stanza stanza);
|
2017-04-04 08:22:11 +02:00
|
|
|
|
|
|
|
@Override
|
2018-03-29 12:35:11 +02:00
|
|
|
public final synchronized void processException(E exception) {
|
2017-04-04 08:22:11 +02:00
|
|
|
if (!isNonFatalException(exception)) {
|
|
|
|
this.exception = exception;
|
|
|
|
this.notifyAll();
|
|
|
|
|
|
|
|
maybeInvokeCallbacks();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper method for {@link #handleStanza(Stanza)}. Note that this method is <code>synchronized</code>.
|
|
|
|
*/
|
|
|
|
@Override
|
2018-03-29 12:35:11 +02:00
|
|
|
public final synchronized void processStanza(Stanza stanza) {
|
2017-04-04 08:22:11 +02:00
|
|
|
handleStanza(stanza);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-12 17:35:20 +02:00
|
|
|
* A simple version of InternalSmackFuture which implements isNonFatalException(E) as always returning
|
|
|
|
* <code>false</code> method.
|
2017-04-04 08:22:11 +02:00
|
|
|
*
|
|
|
|
* @param <V>
|
|
|
|
*/
|
2018-03-29 12:35:11 +02:00
|
|
|
public abstract static class SimpleInternalProcessStanzaSmackFuture<V, E extends Exception>
|
2017-08-12 17:35:20 +02:00
|
|
|
extends InternalProcessStanzaSmackFuture<V, E> {
|
2017-04-04 08:22:11 +02:00
|
|
|
@Override
|
2017-08-12 17:35:20 +02:00
|
|
|
protected boolean isNonFatalException(E exception) {
|
2017-04-04 08:22:11 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2017-08-12 17:35:20 +02:00
|
|
|
|
|
|
|
public static <V, E extends Exception> SmackFuture<V, E> from(V result) {
|
|
|
|
InternalSmackFuture<V, E> future = new InternalSmackFuture<>();
|
|
|
|
future.setResult(result);
|
|
|
|
return future;
|
|
|
|
}
|
|
|
|
|
2017-04-04 08:22:11 +02:00
|
|
|
}
|