Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.openbase.jul.iface.Shutdownable;
import org.openbase.jul.schedule.GlobalScheduledExecutorService;
import org.openbase.jul.schedule.SyncObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.openbase.type.domotic.authentication.TicketAuthenticatorWrapperType.TicketAuthenticatorWrapper;

Expand All @@ -49,10 +50,6 @@
*/
public abstract class AbstractAuthenticationFuture<RETURN, INTERNAL> implements Future<RETURN> {

private static final List<AbstractAuthenticationFuture> authenticatedFutureList = new ArrayList<>();
private static final SyncObject listSync = new SyncObject("AuthenticatedFutureListSync");
private static ScheduledFuture responseVerificationFuture = null;

private final Future<INTERNAL> internalFuture;
private final SessionManager sessionManager;
private final Class<RETURN> returnClass;
Expand Down Expand Up @@ -83,38 +80,7 @@ public AbstractAuthenticationFuture(final Future<INTERNAL> internalFuture, final
this.sessionManager = sessionManager;
this.wrapper = wrapper;

synchronized (listSync) {
if (responseVerificationFuture == null) {
// create a task which makes sure that get is called on all of these futures so that tickets are renewed
try {
responseVerificationFuture = GlobalScheduledExecutorService.scheduleAtFixedRate(() -> {
synchronized (listSync) {
for (final AbstractAuthenticationFuture future : new ArrayList<>(authenticatedFutureList)) {
if (future.isCancelled()) {
authenticatedFutureList.remove(future);
}

if (future.isDone()) {
try {
future.get();
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
} catch (ExecutionException ex) {
authenticatedFutureList.remove(future);
}
}
}
}
}, 1, 5, TimeUnit.SECONDS);
Shutdownable.registerShutdownHook(() -> responseVerificationFuture.cancel(true));
} catch (CouldNotPerformException ex) {
if (!ExceptionProcessor.isCausedBySystemShutdown(ex)) {
ExceptionPrinter.printHistory("Could not initialize task which makes sure that authenticated response are verified", ex, LoggerFactory.getLogger(AbstractAuthenticationFuture.class));
}
}
}
authenticatedFutureList.add(this);
}
AuthenticationFutureList.INSTANCE.addFuture(this);
}

/**
Expand Down Expand Up @@ -214,9 +180,7 @@ private void verifyResponse(TicketAuthenticatorWrapper ticketAuthenticatorWrappe
} catch (CouldNotPerformException ex) {
throw new CouldNotPerformException("Could not verify ServiceServer Response", ex);
} finally {
synchronized (listSync) {
authenticatedFutureList.remove(this);
}
AuthenticationFutureList.INSTANCE.removeFuture(this);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.openbase.bco.authentication.lib.future

import org.openbase.jul.iface.Shutdownable
import org.openbase.jul.schedule.GlobalScheduledExecutorService
import org.openbase.jul.schedule.SyncObject
import org.slf4j.LoggerFactory
import java.util.concurrent.ExecutionException
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException

object AuthenticationFutureList {

private const val SCHEDULE_RATE_IN_S = 5L
private const val INITIAL_DELAY_IN_S = 1L
private const val FUTURE_TIMEOUT_IN_MS = 1L

private val listSync = SyncObject("AuthenticatedFutureListSync")
private val authenticatedFutureList: MutableList<AbstractAuthenticationFuture<*, *>> = ArrayList()
private val logger = LoggerFactory.getLogger("AuthenticationFuture")

private fun isFailed(future: AbstractAuthenticationFuture<*, *>): Boolean {
try {
// Note: the abstract authentication future will remove itself from this list if
// get finished successfully.
future.get(FUTURE_TIMEOUT_IN_MS, TimeUnit.MILLISECONDS)
} catch (ex: ExecutionException) {
return true
} catch (ex: TimeoutException) {
return false
}
return false
}

init {
synchronized(listSync) {
// create a task which makes sure that get is called on all of these futures so that tickets are renewed
val responseVerificationFuture = GlobalScheduledExecutorService.scheduleAtFixedRate(
{
synchronized(listSync) {
authenticatedFutureList
.filter { it.isCancelled || isFailed(it) }
.let { authenticatedFutureList.removeAll(it) }

}
}, INITIAL_DELAY_IN_S, SCHEDULE_RATE_IN_S, TimeUnit.SECONDS
)
Shutdownable.registerShutdownHook { responseVerificationFuture.cancel(true) }
}
}

fun addFuture(authenticationFuture: AbstractAuthenticationFuture<*, *>) {
synchronized(listSync) { authenticatedFutureList.add(authenticationFuture) }
}

fun removeFuture(authenticationFuture: AbstractAuthenticationFuture<*, *>) {
synchronized(listSync) { authenticatedFutureList.remove(authenticationFuture) }
}
}