for(ApiIds id : ids) {
// Get the ApiContext by its ID (this map is populated on all gateways)
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Getting api context for api id {}", apiId);
}
APIContext context = dataManager.getApiContextById(id.getApiContextId());
// Shouldn't be null...
if(context != null) {
// Prepare the list of TargetReference
List<TargetReference> targetReferences = new ArrayList<TargetReference>();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Registering Context {} ({} TargetHosts)", context.getId(), context.getTargetHosts().size());
}
// Store requested HealthCheck service for later use
ITargetHealthCheckService healthCheckService = null;
String healthCheckServiceName = null;
LoadBalancing lbConfig = context.getLoadBalancing();
if(lbConfig.getTargetHealthCheck() != null) {
healthCheckServiceName = lbConfig.getTargetHealthCheck().getType();
}
if(healthCheckServiceName != null) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Will use HealthCheck service ", healthCheckServiceName);
}
// Getting corresponding HealthCheck Service
healthCheckService = targetHealthCheckServices.get(healthCheckServiceName);
if(healthCheckService != null) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Found a HealthCheckService ({}) for this name: {}", healthCheckService.getClass().getName(), healthCheckServiceName);
}
} else {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("No HealthCheckService found for this name: {}", healthCheckServiceName);
}
}
} else {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Will NOT use HealthCheck service for following targets");
}
}
// For each TargetHost, check if there is already a corresponding ManagedTargetHost
for(TargetHost targetHost : context.getTargetHosts()) {
/*
* If the api has its own proxy settings, they were automatically set by the DataManager while loading the api. Nothing to do.
* If the api uses global proxy settings, we have to retrieve a reference on this shared instance
*/
targetHost.setForwardProxy( api.isUseGlobalProxy() ? globalForwardProxy : api.getForwardProxy() );
// Using a "hash differentiation string":
// We need to take into account the HealthCheckService associated to a TargetHost
// Ex: API #1 (http://www.apple.com|Ping), API #2 (http://www.apple.com|Telnet), API #3 (http://www.apple.com|Ping)
// API #1 and #3 must have the same "Managed target" and #2 another one
// (One HealthCheck service may check for a specific functionality status)
String hashDifferentiationString = healthCheckServiceName == null ? "" : healthCheckServiceName;
String managedReference = ManagedTargetHost.computeTargetHostHash(targetHost, hashDifferentiationString);
ManagedTargetHost target = targets.get(managedReference);
if(target == null) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("No corresponding ManagedTarget, creating a new one");
}
// Instantiating and remembering managed target
target = new ManagedTargetHost(targetHost);
target.setReference(managedReference);
targets.put(managedReference, target);
// Registering managed target on health check service
if(healthCheckService != null) {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Target #{} registered on HealthCheck service {}", managedReference, healthCheckService.getName());
}
healthCheckService.registerTarget(target);
target.setHealthCheckService(healthCheckService);
} else {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Target #{} not registered on HealthCheck service");
}
}
} else {
// We already have a ManagedTarget for this protocol+host+port+healthcheck
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("There is already a corresponding ManagedTarget: #{} ", managedReference);
}
}
// Incrementing counter of use (used for unregister method)
target.getNumberOfUse().incrementAndGet();
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("New usage for target #{}: {}", managedReference, target.getNumberOfUse());
}
// Adding this reference to the list that will be returned to the LoadBalancer later on
TargetReference targetReference = new TargetReference();
targetReference.setReference(managedReference);
targetReference.setTargetHost(targetHost);
targetReferences.add(targetReference);
}
// Put the list of TargetReferences in the appropriate map for this Api Context.
map.put(context.getId(), targetReferences);
} else {
// Context null
LOGGER.warn("No APIContext with that id {}, ignoring", id.getApiContextId());
}