public EstablishedClusterView()

in src/main/java/org/apache/sling/discovery/impl/common/resource/EstablishedClusterView.java [51:115]


    public EstablishedClusterView(final Config config, final View view,
            final String localId) {
        super(view.getViewId(), view.getResource().getName());

        final Resource viewRes = view.getResource();
        if (viewRes == null) {
            throw new IllegalStateException("viewRes must not be null");
        }
        final ValueMap valueMap = viewRes.adaptTo(ValueMap.class);
        if (valueMap == null) {
            throw new IllegalStateException("valueMap must not be null");
        }
        String leaderId = valueMap.get("leaderId", String.class);
        final Resource members = viewRes.getChild("members");
        if (members == null) {
            throw new IllegalStateException("members must not be null");
        }
        final Iterator<Resource> it1 = members
                .getChildren().iterator();
        final List<Resource> instanceRess = new LinkedList<Resource>();
        while (it1.hasNext()) {
            instanceRess.add(it1.next());
        }

        Collections.sort(instanceRess, new Comparator<Resource>() {
            public int compare(Resource o1, Resource o2) {
                if (o1 == o2) {
                    return 0;
                }
                if (o1 == null) {
                    return 1;
                }
                if (o2 == null) {
                    return -1;
                }
                return o1.getName().compareTo(o2.getName());
            }
        });

        if (leaderId==null || leaderId.length()==0) {
        	// fallback to pre-SLING-3253: choose leader based on slingId alone.
	        final Resource leader = instanceRess.get(0);
	        leaderId = leader.getName();
        }
        InstanceDescription leaderInstance = null;

        for (Iterator<Resource> it2 = instanceRess.iterator(); it2.hasNext();) {
            Resource resource = it2.next();
            Resource instanceResource = resource.getResourceResolver()
                    .getResource(
                            config.getClusterInstancesPath() + "/"
                                    + resource.getName());
            String slingId = resource.getName();
            EstablishedInstanceDescription instance = new EstablishedInstanceDescription(
                    this, instanceResource, slingId, slingId.equals(leaderId),
                    slingId.equals(localId));
            if (instance.isLeader()) {
                if (leaderInstance != null) {
                    logger.error("More than one instance chosen as leader! overwritten leader="
                            + leaderInstance + ", new leader=" + instance);
                }
                leaderInstance = instance;
            }
        }
    }