public static JobImpl readJob()

in src/main/java/org/apache/sling/event/impl/jobs/Utility.java [177:252]


    public static JobImpl readJob(final Logger logger, final Resource resource) {
        JobImpl job = null;
        if ( resource != null ) {
            try {
                final ValueMap vm = ResourceHelper.getValueMap(resource);

                // check job topic and job id
                final String errorMessage = Utility.checkJobTopic(vm.get(ResourceHelper.PROPERTY_JOB_TOPIC));
                final String jobId = vm.get(ResourceHelper.PROPERTY_JOB_ID, String.class);
                if ( errorMessage == null && jobId != null ) {
                    final String topic = vm.get(ResourceHelper.PROPERTY_JOB_TOPIC, String.class);
                    final Map<String, Object> jobProperties = ResourceHelper.cloneValueMap(vm);

                    jobProperties.put(JobImpl.PROPERTY_RESOURCE_PATH, resource.getPath());
                    // convert to integers (JCR supports only long...)
                    jobProperties.put(Job.PROPERTY_JOB_RETRIES, vm.get(Job.PROPERTY_JOB_RETRIES, Integer.class));
                    jobProperties.put(Job.PROPERTY_JOB_RETRY_COUNT, vm.get(Job.PROPERTY_JOB_RETRY_COUNT, Integer.class));
                    if ( vm.get(Job.PROPERTY_JOB_PROGRESS_STEPS) != null ) {
                        jobProperties.put(Job.PROPERTY_JOB_PROGRESS_STEPS, vm.get(Job.PROPERTY_JOB_PROGRESS_STEPS, Integer.class));
                    }
                    if ( vm.get(Job.PROPERTY_JOB_PROGRESS_STEP) != null ) {
                        jobProperties.put(Job.PROPERTY_JOB_PROGRESS_STEP, vm.get(Job.PROPERTY_JOB_PROGRESS_STEP, Integer.class));
                    }
                    @SuppressWarnings("unchecked")
                    final List<Exception> readErrorList = (List<Exception>) jobProperties.get(ResourceHelper.PROPERTY_MARKER_READ_ERROR_LIST);
                    if ( readErrorList != null ) {
                        for(final Exception e : readErrorList) {
                            final int c = readErrorWarnCount.getAndIncrement();
                            final String prefix;
                            if ( c == READ_ERROR_SILENCE_LIMIT ) {
                                logger.warn("Too many 'Unable to read job from ' messages - silencing 99% of them from now on.");
                                continue;
                            } else if ( c > READ_ERROR_SILENCE_LIMIT && c % 100 != 0 ) {
                                // SLING-9906 : then silence the log altogether
                                continue;
                            } else if ( c > READ_ERROR_SILENCE_LIMIT ) {
                                prefix = "[unsilenced] ";
                            } else {
                                prefix = "";
                            }
                            if ( e.getCause() != null && e.getCause() instanceof ClassNotFoundException ) {
                                // SLING-9906 : suppress exception in ClassNotFoundException case
                                // as this can happen many times in case of a not deployed class.
                                logger.warn(prefix + "Unable to read job from " + resource.getPath() + ", exception: " + e + ", cause: " + e.getCause());
                            } else {
                                logger.warn(prefix + "Unable to read job from " + resource.getPath(), e);
                            }
                        }
                    }
                    job = new JobImpl(topic,
                            jobId,
                            jobProperties);
                } else {
                    if ( errorMessage != null ) {
                        logger.warn("{} : {}", errorMessage, resource.getPath());
                    } else if ( jobId == null ) {
                        logger.warn("Discarding job - no job id found : {}", resource.getPath());
                    }
                    // remove the job as the topic is invalid anyway
                    try {
                        resource.getResourceResolver().delete(resource);
                        resource.getResourceResolver().commit();
                    } catch ( final PersistenceException ignore) {
                        logger.debug("Unable to remove job resource.", ignore);
                    }
                }
            } catch (final InstantiationException ie) {
                // something happened with the resource in the meantime
                logger.debug("Unable to instantiate resource.", ie);
            } catch (final RuntimeException re) {
                logger.debug("Unable to read resource.", re);
            }

        }
        return job;
    }