protected void activate()

in src/main/java/org/apache/sling/discovery/oak/OakDiscoveryService.java [214:315]


    protected void activate(final BundleContext bundleContext) {
        logger.debug("OakDiscoveryService activating...");

        if (settingsService == null) {
            throw new IllegalStateException("settingsService not found");
        }
        if (oakViewChecker == null) {
            throw new IllegalStateException("heartbeatHandler not found");
        }

        slingId = settingsService.getSlingId();

        ClusterSyncService consistencyService;
        if (config.getSyncTokenEnabled()) {
            //TODO: ConsistencyHistory is implemented a little bit hacky ..
            ClusterSyncHistory consistencyHistory = new ClusterSyncHistory();
            oakBacklogClusterSyncService.setConsistencyHistory(consistencyHistory);
            syncTokenService.setConsistencyHistory(consistencyHistory);
            JoinerDelay joinerDelay = new JoinerDelay(config.getJoinerDelayMillis(), scheduler);
            consistencyService = new ClusterSyncServiceChain(oakBacklogClusterSyncService, syncTokenService, joinerDelay);
        } else {
            consistencyService = oakBacklogClusterSyncService;

        }
        viewStateManager = ViewStateManagerFactory.newViewStateManager(viewStateManagerLock, consistencyService);

        if (config.getMinEventDelay() > 0) {
            viewStateManager.installMinEventDelayHandler(this, scheduler, config.getMinEventDelay());
        }

        final String isolatedClusterId = UUID.randomUUID().toString();
        {
            // create a pre-voting/isolated topologyView which would be used until the first voting has finished.
            // this way for the single-instance case the clusterId can
            // remain the same between a getTopology() that is invoked before the first TOPOLOGY_INIT and afterwards
            DefaultClusterView isolatedCluster = new DefaultClusterView(isolatedClusterId);
            Map<String, String> emptyProperties = new HashMap<>();
            DefaultInstanceDescription isolatedInstance =
                    new DefaultInstanceDescription(isolatedCluster, true, true, slingId, emptyProperties);
            Collection<InstanceDescription> col = new ArrayList<>();
            col.add(isolatedInstance);
            final DefaultTopologyView topology = new DefaultTopologyView();
            topology.addInstances(col);
            topology.setNotCurrent();
            setOldView(topology);
        }
        setOldView((DefaultTopologyView) getTopology());
        getOldView().setNotCurrent();

        // make sure the first heartbeat is issued as soon as possible - which
        // is right after this service starts. since the two (discoveryservice
        // and heartbeatHandler need to know each other, the discoveryservice
        // is passed on to the heartbeatHandler in this initialize call).
        oakViewChecker.initialize(this);

        viewStateManagerLock.lock();
        try {
            viewStateManager.handleActivated();

            doUpdateProperties();

            DefaultTopologyView newView = (DefaultTopologyView) getTopology();
            if (newView.isCurrent()) {
                viewStateManager.handleNewView(newView);
            } else {
                // SLING-3750: just issue a log.info about the delaying
                logger.info("activate: this instance is in isolated mode and must yet finish voting before it can send out TOPOLOGY_INIT.");
            }
            activated = true;
            setOldView(newView);

            // in case bind got called before activate we now have pending listeners,
            // bind them to the viewstatemanager too
            for (TopologyEventListener listener : pendingListeners) {
                viewStateManager.bind(listener);
            }
            pendingListeners.clear();

            viewStateManager.bind(changePropagationListener);
        } finally {
            if (viewStateManagerLock != null) {
                viewStateManagerLock.unlock();
            }
        }

        URL[] topologyConnectorURLs = config.getTopologyConnectorURLs();
        if (topologyConnectorURLs != null) {
            for (int i = 0; i < topologyConnectorURLs.length; i++) {
                final URL aURL = topologyConnectorURLs[i];
                if (aURL != null) {
                    try {
                        logger.info("activate: registering outgoing topology connector to " + aURL);
                        connectorRegistry.registerOutgoingConnector(clusterViewService, aURL);
                    } catch (final Exception e) {
                        logger.info("activate: could not register url: " + aURL + " due to: " + e, e);
                    }
                }
            }
        }

        logger.debug("OakDiscoveryService activated.");
    }