public synchronized Collection<Announcement> listAnnouncementsInSameCluster(final ClusterView localClusterView) {
if (localClusterView==null) {
throw new IllegalArgumentException("clusterView must not be null");
}
ResourceResolver resourceResolver = null;
final Collection<Announcement> incomingAnnouncements = new LinkedList<Announcement>();
final InstanceDescription localInstance = getLocalInstanceDescription(localClusterView);
try {
resourceResolver = resourceResolverFactory
.getAdministrativeResourceResolver(null);
Resource clusterInstancesResource = ResourceHelper
.getOrCreateResource(
resourceResolver,
config.getClusterInstancesPath());
Iterator<Resource> it0 = clusterInstancesResource.getChildren()
.iterator();
while (it0.hasNext()) {
Resource aClusterInstanceResource = it0.next();
final String instanceId = aClusterInstanceResource.getName();
if (localInstance!=null && localInstance.getSlingId().equals(instanceId)) {
// this is the local instance then - which we serve from the cache only
fillWithCachedAnnouncements(incomingAnnouncements);
continue;
}
//TODO: add ClusterView.contains(instanceSlingId) for convenience to next api change
if (!contains(localClusterView, instanceId)) {
// then the instance is not in my view, hence ignore its announcements
// (corresponds to earlier expiry-handling)
continue;
}
final Resource announcementsResource = aClusterInstanceResource
.getChild("announcements");
if (announcementsResource == null) {
continue;
}
Iterator<Resource> it = announcementsResource.getChildren()
.iterator();
Announcement topologyAnnouncement;
while (it.hasNext()) {
Resource anAnnouncement = it.next();
topologyAnnouncement = Announcement
.fromJSON(anAnnouncement
.adaptTo(ValueMap.class).get(
"topologyAnnouncement",
String.class));
incomingAnnouncements.add(topologyAnnouncement);
// SLING-3389: no longer check for expired announcements -
// instead make use of the fact that this instance
// has a clusterView and that every live instance
// is responsible of cleaning up expired announcements
// with the repository
}
}
// since SLING-3389 this method does only read operations, hence
// no commit necessary anymore - close happens in below finally block
} catch (LoginException e) {
logger.error(
"listAnnouncementsInSameCluster: could not log in administratively: " + e, e);
throw new RuntimeException("Could not log in to repository (" + e
+ ")", e);
} catch (PersistenceException e) {
logger.error("listAnnouncementsInSameCluster: got a PersistenceException: " + e, e);
throw new RuntimeException(
"Exception while talking to repository (" + e + ")", e);
} catch (JSONException e) {
logger.error("listAnnouncementsInSameCluster: got a JSONException: " + e, e);
throw new RuntimeException("Exception while converting json (" + e
+ ")", e);
} finally {
if (resourceResolver != null) {
resourceResolver.close();
}
}
if (logger.isDebugEnabled()) {
logger.debug("listAnnouncementsInSameCluster: result: "+incomingAnnouncements.size());
}