Skip to content

Commit

Permalink
Merge pull request #38 from tadamski/WEJBHTTP-34-api-master
Browse files Browse the repository at this point in the history
[WEJBHTTP-34] Updates to EJB over HTTP discovery
  • Loading branch information
fl4via committed Jul 6, 2020
2 parents 8ad2ee8 + a468980 commit 352e063
Showing 1 changed file with 32 additions and 8 deletions.
Expand Up @@ -38,6 +38,7 @@
import org.wildfly.security.auth.client.AuthenticationConfiguration;
import org.wildfly.security.auth.client.AuthenticationContext;
import org.wildfly.security.auth.client.AuthenticationContextConfigurationClient;
import org.wildfly.security.manager.WildFlySecurityManager;
import org.xnio.IoUtils;

import javax.net.ssl.SSLContext;
Expand All @@ -47,6 +48,7 @@
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

Expand All @@ -64,24 +66,35 @@ public final class HttpEJBDiscoveryProvider implements DiscoveryProvider {

private static final AuthenticationContextConfigurationClient AUTH_CONFIGURATION_CLIENT = doPrivileged(AuthenticationContextConfigurationClient.ACTION);

private static final long CACHE_REFRESH_TIMEOUT = TimeUnit.MILLISECONDS.toNanos(Long.parseLong(
WildFlySecurityManager.getPropertyPrivileged("org.wildfly.httpclient.ejb.discovery.cache-refresh-timeout", "300000")));

private Set<ServiceURL> serviceURLCache = new HashSet<>();
private AtomicBoolean shouldRefreshCache = new AtomicBoolean(true);
private AtomicBoolean cacheInvalid = new AtomicBoolean(true);
private long cacheRefreshTimestamp = 0L;

HttpEJBDiscoveryProvider() {
}

public DiscoveryRequest discover(final ServiceType serviceType, final FilterSpec filterSpec, final DiscoveryResult discoveryResult) {
final EJBClientContext ejbClientContext = getCurrent();

if (shouldRefreshCache.get()) {
if (shouldRefreshCache()) {
refreshCache(ejbClientContext);
}

searchCache(discoveryResult, filterSpec);
searchCache(discoveryResult, filterSpec, ejbClientContext);

return DiscoveryRequest.NULL;
}

private boolean shouldRefreshCache() {
if (cacheInvalid.get() || ((System.nanoTime() - cacheRefreshTimestamp) > CACHE_REFRESH_TIMEOUT)) {
return true;
}
return false;
}

private boolean supportsScheme(String s) {
switch (s) {
case "http":
Expand All @@ -91,13 +104,23 @@ private boolean supportsScheme(String s) {
return false;
}

private void searchCache(final DiscoveryResult discoveryResult, final FilterSpec filterSpec) {
private void searchCache(final DiscoveryResult discoveryResult, final FilterSpec filterSpec, final EJBClientContext ejbClientContext) {
final boolean resultsPresent = doSearchCache(discoveryResult, filterSpec);
if(!resultsPresent){
refreshCache(ejbClientContext);
}
discoveryResult.complete();
}

private boolean doSearchCache(final DiscoveryResult discoveryResult, final FilterSpec filterSpec) {
boolean resultsPresent = false;
for (ServiceURL serviceURL : serviceURLCache) {
if (serviceURL.satisfies(filterSpec)) {
discoveryResult.addMatch(serviceURL.getLocationURI());
resultsPresent = true;
}
}
discoveryResult.complete();
return resultsPresent;
}

private void refreshCache(final EJBClientContext ejbClientContext){
Expand All @@ -111,11 +134,12 @@ private void refreshCache(final EJBClientContext ejbClientContext){
}
try {
outstandingLatch.await();
shouldRefreshCache.set(false);
cacheInvalid.set(false);
cacheRefreshTimestamp = System.nanoTime();
} catch(InterruptedException e){
EjbHttpClientMessages.MESSAGES.httpDiscoveryInterrupted(e);
}
shouldRefreshCache.set(false);
cacheInvalid.set(false);
}

private void disoverFromConnection(final EJBClientConnection connection, final CountDownLatch outstandingLatch) {
Expand Down Expand Up @@ -201,7 +225,7 @@ private ServiceURL createServiceURL(final URI newUri, final EJBModuleIdentifier

@Override
public void processMissingTarget(URI location, Exception cause) {
shouldRefreshCache.set(true);
cacheInvalid.set(true);
}
}

0 comments on commit 352e063

Please sign in to comment.