public void handleEvent()

in src/main/java/org/apache/sling/validation/impl/resourcemodel/ResourceValidationModelProviderImpl.java [132:190]


    public void handleEvent(Event event) {
        String path = (String) event.getProperty(SlingConstants.PROPERTY_PATH);
        if (path == null) {
            LOG.warn("Received event {}, but could not get the affected path", event);
            return;
        }
        Set<String> resourceTypesToInvalidate = new HashSet<>();
        switch (event.getTopic()) {
        case SlingConstants.TOPIC_RESOURCE_REMOVED:
            // find cache entries below the removed resource
            for (Entry<String, List<ValidationModel>> validationModelByResourceType : validationModelCacheByResourceType.entrySet()) {
                for (ValidationModel model : validationModelByResourceType.getValue()) {
                    if (model.getSource().startsWith(path)) {
                        LOG.debug("Invalidate validation model at {}, because resource at {} has been removed", model.getSource(), path);
                        resourceTypesToInvalidate.add(validationModelByResourceType.getKey());
                    }
                }
            }
            break;
        default:
            // only consider additions/changes of resources with resource type = validation model resource type
            String resourceType = (String) event.getProperty(SlingConstants.PROPERTY_RESOURCE_TYPE);
            if (resourceType == null) {
                LOG.warn("Received event {}, but could not get the modified/added resource type", event);
                return;
            }
            if (VALIDATION_MODEL_RESOURCE_TYPE.equals(resourceType)) {
                // retrieve the resource types covered by the newly added model
                String resourceTypeToInvalidate = null;
                try {
                    resourceTypeToInvalidate = getResourceTypeOfValidationModel(path);
                } catch (Exception e) {
                    LOG.warn("Could not get covered resource type of newly added validation model at " + path, e);
                }
                if (resourceTypeToInvalidate != null) {
                    LOG.debug(
                            "Invalidate validation models for resource type {}, because resource at {} provides a new/modified validation model for that type",
                            resourceType, path);
                    resourceTypesToInvalidate.add(resourceTypeToInvalidate);
                } else {
                    LOG.debug("Resource at {} provides a new/modified validation model but could not yet determine for which resource type",
                            path);
                }
            }
            // or paths already covered by the cache
            for (Entry<String, List<ValidationModel>> validationModelByResourceType : validationModelCacheByResourceType.entrySet()) {
                for (ValidationModel model : validationModelByResourceType.getValue()) {
                    if (path.startsWith(model.getSource())) {
                        LOG.debug("Invalidate validation model at {}, because resource below (at {}) has been modified", model.getSource(),
                                path);
                        resourceTypesToInvalidate.add(validationModelByResourceType.getKey());
                    }
                }
            }
        }
        for (String resourceTypeToInvalidate : resourceTypesToInvalidate) {
            validationModelCacheByResourceType.remove(resourceTypeToInvalidate);
        }
    }