Search in sources :

Example 36 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class QueueJobCache method loadJobs.

/**
     * Load the next N x numberOf(topics) jobs
     * @param checkingTopics The set of topics to check.
     */
private void loadJobs(final String queueName, final Set<String> checkingTopics, final StatisticsManager statisticsManager) {
    logger.debug("Starting jobs loading from {}...", checkingTopics);
    final Map<String, List<JobImpl>> topicCache = new HashMap<String, List<JobImpl>>();
    final ResourceResolver resolver = this.configuration.createResourceResolver();
    try {
        final Resource baseResource = resolver.getResource(this.configuration.getLocalJobsPath());
        // sanity check - should never be null
        if (baseResource != null) {
            for (final String topic : checkingTopics) {
                final Resource topicResource = baseResource.getChild(topic.replace('/', '.'));
                if (topicResource != null) {
                    topicCache.put(topic, loadJobs(queueName, topic, topicResource, statisticsManager));
                }
            }
        }
    } finally {
        resolver.close();
    }
    orderTopics(topicCache);
    logger.debug("Finished jobs loading {}", this.cache.size());
}
Also used : JobImpl(org.apache.sling.event.impl.jobs.JobImpl) HashMap(java.util.HashMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) ArrayList(java.util.ArrayList) List(java.util.List)

Example 37 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class QueueManager method scanTopics.

/**
     * Scan the resource tree for topics.
     */
private Set<String> scanTopics() {
    final Set<String> topics = new HashSet<>();
    final ResourceResolver resolver = this.configuration.createResourceResolver();
    try {
        final Resource baseResource = resolver.getResource(this.configuration.getLocalJobsPath());
        // sanity check - should never be null
        if (baseResource != null) {
            final Iterator<Resource> topicIter = baseResource.listChildren();
            while (topicIter.hasNext()) {
                final Resource topicResource = topicIter.next();
                final String topic = topicResource.getName().replace('.', '/');
                logger.debug("Found topic {}", topic);
                topics.add(topic);
            }
        }
    } finally {
        resolver.close();
    }
    return topics;
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) HashSet(java.util.HashSet)

Example 38 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class JobSchedulerImpl method setSuspended.

/**
     * Change the suspended flag for a scheduled job
     * @param info The schedule info
     * @param flag The corresponding flag
     */
public void setSuspended(final ScheduledJobInfoImpl info, final boolean flag) {
    final ResourceResolver resolver = configuration.createResourceResolver();
    try {
        final StringBuilder sb = new StringBuilder(this.configuration.getScheduledJobsPath(true));
        sb.append(ResourceHelper.filterName(info.getName()));
        final String path = sb.toString();
        final Resource eventResource = resolver.getResource(path);
        if (eventResource != null) {
            final ModifiableValueMap mvm = eventResource.adaptTo(ModifiableValueMap.class);
            if (flag) {
                mvm.put(ResourceHelper.PROPERTY_SCHEDULE_SUSPENDED, Boolean.TRUE);
            } else {
                mvm.remove(ResourceHelper.PROPERTY_SCHEDULE_SUSPENDED);
            }
            resolver.commit();
        }
        if (flag) {
            this.stopScheduledJob(info);
        } else {
            this.startScheduledJob(info);
        }
    } catch (final PersistenceException pe) {
        // we ignore the exception if removing fails
        ignoreException(pe);
    } finally {
        resolver.close();
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 39 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class ScheduledJobHandler method remove.

/**
     * Remove a scheduled job
     * @param info The schedule info
     */
public void remove(final ScheduledJobInfoImpl info) {
    final String scheduleKey = ResourceHelper.filterName(info.getName());
    final ResourceResolver resolver = configuration.createResourceResolver();
    try {
        final StringBuilder sb = new StringBuilder(configuration.getScheduledJobsPath(true));
        sb.append(scheduleKey);
        final String path = sb.toString();
        final Resource eventResource = resolver.getResource(path);
        if (eventResource != null) {
            resolver.delete(eventResource);
            resolver.commit();
        }
    } catch (final PersistenceException pe) {
        // we ignore the exception if removing fails
        ignoreException(pe);
    } finally {
        resolver.close();
    }
    synchronized (this.scheduledJobs) {
        final Holder h = scheduledJobs.remove(scheduleKey);
        if (h != null && h.info != null) {
            jobScheduler.unscheduleJob(h.info);
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Example 40 with ResourceResolver

use of org.apache.sling.api.resource.ResourceResolver in project sling by apache.

the class ScheduledJobHandler method scan.

private void scan() {
    final ResourceResolver resolver = configuration.createResourceResolver();
    if (resolver != null) {
        try {
            logger.debug("Scanning for scheduled jobs...");
            final String path = this.configuration.getScheduledJobsPath(false);
            final Resource startResource = resolver.getResource(path);
            if (startResource != null) {
                final Map<String, Holder> newScheduledJobs = new HashMap<String, Holder>();
                synchronized (this.scheduledJobs) {
                    for (final Resource rsrc : startResource.getChildren()) {
                        if (!isRunning.get()) {
                            break;
                        }
                        handleAddOrUpdate(newScheduledJobs, rsrc);
                    }
                    if (isRunning.get()) {
                        for (final Holder h : this.scheduledJobs.values()) {
                            if (h.info != null) {
                                this.jobScheduler.unscheduleJob(h.info);
                            }
                        }
                        this.scheduledJobs.clear();
                        this.scheduledJobs.putAll(newScheduledJobs);
                    }
                }
            }
            logger.debug("Finished scanning for scheduled jobs...");
        } finally {
            resolver.close();
        }
    }
}
Also used : HashMap(java.util.HashMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource)

Aggregations

ResourceResolver (org.apache.sling.api.resource.ResourceResolver)339 Resource (org.apache.sling.api.resource.Resource)168 Test (org.junit.Test)131 HashMap (java.util.HashMap)65 LoginException (org.apache.sling.api.resource.LoginException)53 PersistenceException (org.apache.sling.api.resource.PersistenceException)52 Session (javax.jcr.Session)31 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)29 ValueMap (org.apache.sling.api.resource.ValueMap)27 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)26 ArrayList (java.util.ArrayList)23 DistributionRequest (org.apache.sling.distribution.DistributionRequest)23 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)21 Map (java.util.Map)19 Before (org.junit.Before)19 IOException (java.io.IOException)17 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)17 ChildResource (org.apache.sling.validation.model.ChildResource)17 HashSet (java.util.HashSet)16 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)15