use of org.apache.sling.discovery.commons.providers.DefaultClusterView in project sling by apache.
the class Announcement method asClusterView.
/** create a clusterview from json **/
private static ClusterView asClusterView(final String localClusterViewJSON) {
JsonObject obj = jsonReaderFactory.createReader(new StringReader(localClusterViewJSON)).readObject();
DefaultClusterView clusterView = new DefaultClusterView(obj.getString("id"));
JsonArray instancesObj = obj.getJsonArray("instances");
for (int i = 0; i < instancesObj.size(); i++) {
JsonObject anInstance = instancesObj.getJsonObject(i);
clusterView.addInstanceDescription(asInstance(anInstance));
}
return clusterView;
}
use of org.apache.sling.discovery.commons.providers.DefaultClusterView in project sling by apache.
the class AnnouncementRegistryImplTest method testLists.
@Test
public void testLists() throws Exception {
try {
registry.listAnnouncementsInSameCluster(null);
fail("should complain");
} catch (IllegalArgumentException iae) {
// ok
}
try {
registry.listAnnouncementsInSameCluster(null);
fail("should complain");
} catch (IllegalArgumentException iae) {
// ok
}
assertEquals(0, registry.listLocalAnnouncements().size());
assertEquals(0, registry.listLocalIncomingAnnouncements().size());
DefaultClusterView localCluster = new DefaultClusterView(UUID.randomUUID().toString());
DefaultInstanceDescription instance = TopologyHelper.createInstanceDescription(slingId, true, localCluster);
assertEquals(0, registry.listAnnouncementsInSameCluster(localCluster).size());
assertEquals(0, registry.listLocalAnnouncements().size());
assertEquals(0, registry.listLocalIncomingAnnouncements().size());
Announcement ann = new Announcement(slingId);
ann.setLocalCluster(localCluster);
ann.setInherited(true);
registry.registerAnnouncement(ann);
assertEquals(1, registry.listAnnouncementsInSameCluster(localCluster).size());
assertEquals(1, registry.listLocalAnnouncements().size());
assertEquals(0, registry.listLocalIncomingAnnouncements().size());
ann.setInherited(true);
assertEquals(0, registry.listLocalIncomingAnnouncements().size());
assertTrue(registry.hasActiveAnnouncement(slingId));
assertFalse(registry.hasActiveAnnouncement(UUID.randomUUID().toString()));
registry.unregisterAnnouncement(slingId);
assertEquals(0, registry.listAnnouncementsInSameCluster(localCluster).size());
assertEquals(0, registry.listLocalAnnouncements().size());
assertEquals(0, registry.listLocalIncomingAnnouncements().size());
assertFalse(registry.hasActiveAnnouncement(slingId));
assertFalse(registry.hasActiveAnnouncement(UUID.randomUUID().toString()));
ann.setInherited(false);
registry.registerAnnouncement(ann);
assertEquals(1, registry.listAnnouncementsInSameCluster(localCluster).size());
assertEquals(1, registry.listLocalAnnouncements().size());
assertEquals(1, registry.listLocalIncomingAnnouncements().size());
assertTrue(registry.hasActiveAnnouncement(slingId));
assertFalse(registry.hasActiveAnnouncement(UUID.randomUUID().toString()));
registry.unregisterAnnouncement(slingId);
assertEquals(0, registry.listAnnouncementsInSameCluster(localCluster).size());
assertEquals(0, registry.listLocalAnnouncements().size());
assertEquals(0, registry.listLocalIncomingAnnouncements().size());
assertFalse(registry.hasActiveAnnouncement(slingId));
assertFalse(registry.hasActiveAnnouncement(UUID.randomUUID().toString()));
assertEquals(1, ann.listInstances().size());
registry.addAllExcept(ann, localCluster, new AnnouncementFilter() {
public boolean accept(String receivingSlingId, Announcement announcement) {
assertNotNull(receivingSlingId);
assertNotNull(announcement);
return true;
}
});
assertEquals(1, ann.listInstances().size());
registry.registerAnnouncement(createAnnouncement(createCluster(3), 1, false));
assertEquals(1, registry.listAnnouncementsInSameCluster(localCluster).size());
assertEquals(3, registry.listInstances(localCluster).size());
registry.addAllExcept(ann, localCluster, new AnnouncementFilter() {
public boolean accept(String receivingSlingId, Announcement announcement) {
assertNotNull(receivingSlingId);
assertNotNull(announcement);
return true;
}
});
assertEquals(4, ann.listInstances().size());
registry.registerAnnouncement(ann);
assertEquals(2, registry.listAnnouncementsInSameCluster(localCluster).size());
}
use of org.apache.sling.discovery.commons.providers.DefaultClusterView in project sling by apache.
the class TopologyHelper method createTopologyView.
public static DefaultTopologyView createTopologyView(String clusterViewId, String slingId) {
DefaultTopologyView t = new DefaultTopologyView();
DefaultClusterView c = new DefaultClusterView(clusterViewId);
DefaultInstanceDescription i = new DefaultInstanceDescription(c, true, false, slingId, new HashMap<String, String>());
Collection<InstanceDescription> instances = new LinkedList<InstanceDescription>();
instances.add(i);
t.addInstances(instances);
return t;
}
use of org.apache.sling.discovery.commons.providers.DefaultClusterView in project sling by apache.
the class TopologyHelper method cloneTopologyView.
public static DefaultTopologyView cloneTopologyView(DefaultTopologyView view, String newLeader) throws NoSuchFieldException {
final DefaultTopologyView clone = cloneTopologyView(view);
final DefaultClusterView cluster = (DefaultClusterView) clone.getClusterViews().iterator().next();
for (Iterator it = cluster.getInstances().iterator(); it.hasNext(); ) {
DefaultInstanceDescription id = (DefaultInstanceDescription) it.next();
PrivateAccessor.setField(id, "isLeader", id.getSlingId().equals(newLeader));
}
return clone;
}
use of org.apache.sling.discovery.commons.providers.DefaultClusterView in project sling by apache.
the class TopologyHelper method cloneTopologyView.
public static DefaultTopologyView cloneTopologyView(DefaultTopologyView original) {
DefaultTopologyView t = new DefaultTopologyView();
Iterator<ClusterView> it = original.getClusterViews().iterator();
while (it.hasNext()) {
DefaultClusterView c = (DefaultClusterView) it.next();
t.addInstances(clone(c).getInstances());
}
return t;
}
Aggregations