in http/balancer/src/main/java/org/apache/karaf/cellar/http/balancer/LocalServletListener.java [45:113]
public synchronized void servletEvent(ServletEvent servletEvent) {
if (eventProducer.getSwitch().getStatus().equals(SwitchStatus.OFF)) {
LOGGER.warn("CELLAR HTTP BALANCER: cluster event producer is OFF");
return;
}
Servlet servlet = servletEvent.getServlet();
if (servlet != null && servlet.getClass().getName().equals(CellarBalancerProxyServlet.class.getName())) {
LOGGER.trace("CELLAR HTTP BALANCER: ignoring CellarBalancerProxyServlet servlet event");
return;
}
Set<Group> localGroups = groupManager.listLocalGroups();
BalancedServletUtil util = new BalancedServletUtil();
util.setClusterManager(clusterManager);
util.setConfigurationAdmin(configurationAdmin);
String alias = servletEvent.getAlias();
String location = util.constructLocation(alias);
for (Group group : localGroups) {
Map<String, List<String>> clusterServlets = clusterManager.getMap(Constants.BALANCER_MAP + Configurations.SEPARATOR + group.getName());
if (servletEvent.getType() == ServletEvent.DEPLOYED) {
// update the cluster servlets
List<String> locations = clusterServlets.get(alias);
if (locations == null) {
locations = new ArrayList<String>();
}
if (!locations.contains(location)) {
LOGGER.debug("CELLAR HTTP BALANCER: adding location {} to servlet {} on cluster", location, alias);
locations.add(location);
clusterServlets.put(alias, locations);
// send cluster event
ClusterBalancerEvent event = new ClusterBalancerEvent(alias, ClusterBalancerEvent.ADDING, locations);
event.setSourceGroup(group);
event.setSourceNode(clusterManager.getNode());
event.setLocal(clusterManager.getNode());
eventProducer.produce(event);
} else {
LOGGER.debug("CELLAR HTTP BALANCER: location {} already defined for servlet {} on cluster", location, alias);
}
} else if (servletEvent.getType() == ServletEvent.UNDEPLOYED) {
List<String> locations = clusterServlets.get(alias);
if (locations == null)
locations = new ArrayList<String>();
if (locations.contains(location)) {
LOGGER.debug("CELLAR HTTP BALANCER: removing location {} for servlet {} on cluster", location, alias);
locations.remove(location);
// update cluster state
clusterServlets.put(alias, locations);
// send cluster event
ClusterBalancerEvent event = new ClusterBalancerEvent(alias, ClusterBalancerEvent.REMOVING, locations);
event.setSourceGroup(group);
event.setSourceNode(clusterManager.getNode());
event.setLocal(clusterManager.getNode());
eventProducer.produce(event);
}
if (locations.isEmpty()) {
LOGGER.debug("CELLAR HTTP BALANCER: destroying servlet {} from cluster", alias);
// update the cluster servlets
clusterServlets.remove(alias);
}
}
}
}