protected void activate()

in src/main/java/org/apache/sling/resourceresolver/impl/ResourceResolverFactoryActivator.java [221:346]


    protected void activate(
            final BundleContext bundleContext,
            final ResourceResolverFactoryConfig config,
            final VanityPathConfigurer.DeprecatedVanityConfig deprecatedVanityConfig) {
        this.vanityPathConfigurer.setConfiguration(config, deprecatedVanityConfig);
        this.bundleContext = bundleContext;
        this.config = config;

        final BidiMap<String, String> virtuals = new TreeBidiMap<>();
        for (int i = 0;
                config.resource_resolver_virtual() != null && i < config.resource_resolver_virtual().length;
                i++) {
            final String[] parts = Mapping.split(config.resource_resolver_virtual()[i]);
            virtuals.put(parts[0], parts[2]);
        }
        virtualURLMap = virtuals;

        final List<Mapping> maps = new ArrayList<>();
        for (int i = 0;
                config.resource_resolver_mapping() != null && i < config.resource_resolver_mapping().length;
                i++) {
            maps.add(new Mapping(config.resource_resolver_mapping()[i]));
        }
        final Mapping[] tmp = maps.toArray(new Mapping[maps.size()]);

        // check whether direct mappings are allowed
        if (config.resource_resolver_allowDirect()) {
            final Mapping[] tmp2 = new Mapping[tmp.length + 1];
            tmp2[0] = Mapping.DIRECT;
            System.arraycopy(tmp, 0, tmp2, 1, tmp.length);
            mappings = tmp2;
        } else {
            mappings = tmp;
        }

        // from configuration if available
        final List<String> searchPathList = new ArrayList<>();
        if (config.resource_resolver_searchpath() != null && config.resource_resolver_searchpath().length > 0) {
            for (String path : config.resource_resolver_searchpath()) {
                // ensure leading slash
                if (!path.startsWith("/")) {
                    path = "/".concat(path);
                }
                // ensure trailing slash
                if (!path.endsWith("/")) {
                    path = path.concat("/");
                }
                searchPathList.add(path);
            }
        }
        if (searchPathList.isEmpty()) {
            searchPathList.add("/");
        }
        this.searchPath = Collections.unmodifiableList(searchPathList);

        // the root of the resolver mappings
        mapRoot = config.resource_resolver_map_location();
        mapRootPrefix = mapRoot + '/';

        final String[] paths = config.resource_resolver_map_observation();
        this.observationPaths = new Path[paths.length];
        for (int i = 0; i < paths.length; i++) {
            this.observationPaths[i] = new Path(paths[i]);
        }

        // optimize alias path allow list
        String[] aliasLocationsPrefix = config.resource_resolver_allowed_alias_locations();
        if (aliasLocationsPrefix != null) {
            final Set<String> prefixSet = new TreeSet<>();
            for (final String prefix : aliasLocationsPrefix) {
                String value = prefix.trim();
                if (!value.isEmpty()) {
                    if (value.startsWith("/")) { // absolute path should be given
                        // path must not end with "/" to be valid absolute path
                        prefixSet.add(StringUtils.removeEnd(value, "/"));
                    } else {
                        logger.warn(
                                "Path [{}] is ignored. As only absolute paths are allowed for alias optimization",
                                value);
                    }
                }
            }
            if (!prefixSet.isEmpty()) {
                this.allowedAliasLocations = Collections.unmodifiableSet(prefixSet);
            }
        }
        if (!config.resource_resolver_optimize_alias_resolution()) {
            logger.warn(
                    "The non-optimized alias resolution is used, which has been found to have problems (see SLING-12025). "
                            + "Please migrate to the optimized alias resolution, as the non-optimized version will be removed");
        }

        // for testing: if we run unit test, both trackers are set from the outside
        final boolean hasPreRegisteredResourceProviderTracker = this.resourceProviderTracker != null;
        if (!hasPreRegisteredResourceProviderTracker) {
            this.resourceProviderTracker = new ResourceProviderTracker();
            this.changeListenerWhiteboard = new ResourceChangeListenerWhiteboard();
            this.changeListenerWhiteboard.activate(this.bundleContext, this.resourceProviderTracker, searchPath);
        }

        // check for required property
        Set<String> requiredResourceProvidersLegacy = getStringSet(config.resource_resolver_required_providers());
        Set<String> requiredResourceProviderNames = getStringSet(config.resource_resolver_required_providernames());

        final FactoryPreconditions factoryPreconditions = new FactoryPreconditions(
                resourceProviderTracker, requiredResourceProviderNames, requiredResourceProvidersLegacy);
        factoryRegistrationHandler.configure(this, factoryPreconditions);

        if (!hasPreRegisteredResourceProviderTracker) {
            this.resourceProviderTracker.activate(this.bundleContext, this.eventAdmin, new ChangeListener() {

                @Override
                public void providerAdded() {
                    factoryRegistrationHandler.maybeRegisterFactory();
                }

                @Override
                public void providerRemoved(final boolean stateful, final boolean isUsed) {
                    if (isUsed && (stateful || config.resource_resolver_providerhandling_paranoid())) {
                        factoryRegistrationHandler.unregisterFactory();
                    }
                    factoryRegistrationHandler.maybeRegisterFactory();
                }
            });
        }
    }