Search in sources :

Example 31 with ResourceResolver

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

the class JobHandler method reschedule.

/**
     * Reschedule the job
     * Update the retry count and remove the started time.
     * @return <code>true</code> if rescheduling was successful, <code>false</code> otherwise.
     */
public boolean reschedule() {
    final ResourceResolver resolver = this.configuration.createResourceResolver();
    try {
        final Resource jobResource = resolver.getResource(job.getResourcePath());
        if (jobResource != null) {
            final ModifiableValueMap mvm = jobResource.adaptTo(ModifiableValueMap.class);
            mvm.put(Job.PROPERTY_JOB_RETRY_COUNT, job.getProperty(Job.PROPERTY_JOB_RETRY_COUNT, Integer.class));
            if (job.getProperty(Job.PROPERTY_RESULT_MESSAGE) != null) {
                mvm.put(Job.PROPERTY_RESULT_MESSAGE, job.getProperty(Job.PROPERTY_RESULT_MESSAGE));
            }
            mvm.remove(Job.PROPERTY_JOB_STARTED_TIME);
            mvm.put(JobImpl.PROPERTY_JOB_QUEUED, Calendar.getInstance());
            try {
                resolver.commit();
                return true;
            } catch (final PersistenceException pe) {
                this.configuration.getMainLogger().debug("Unable to update reschedule properties for job " + job.getId(), pe);
            }
        }
    } finally {
        resolver.close();
    }
    return false;
}
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 32 with ResourceResolver

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

the class JobHandler method persistJobProperties.

/**
     * Update the property of a job in the resource tree
     * @param propNames the property names to update
     * @return {@code true} if the update was successful.
     */
public boolean persistJobProperties(final String... propNames) {
    if (propNames != null) {
        final ResourceResolver resolver = this.configuration.createResourceResolver();
        try {
            final Resource jobResource = resolver.getResource(job.getResourcePath());
            if (jobResource != null) {
                final ModifiableValueMap mvm = jobResource.adaptTo(ModifiableValueMap.class);
                for (final String propName : propNames) {
                    final Object val = job.getProperty(propName);
                    if (val != null) {
                        if (val.getClass().isEnum()) {
                            mvm.put(propName, val.toString());
                        } else {
                            mvm.put(propName, val);
                        }
                    } else {
                        mvm.remove(propName);
                    }
                }
                resolver.commit();
                return true;
            } else {
                this.configuration.getMainLogger().debug("No job resource found at {}", job.getResourcePath());
            }
        } catch (final PersistenceException ignore) {
            this.configuration.getMainLogger().debug("Unable to persist properties", ignore);
        } finally {
            resolver.close();
        }
        return false;
    }
    return true;
}
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 33 with ResourceResolver

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

the class FsResourceProvider method listChildren.

/**
     * Returns an iterator of resources.
     */
@SuppressWarnings("unchecked")
@Override
public Iterator<Resource> listChildren(final ResolveContext<Object> ctx, final Resource parent) {
    ResourceResolver resolver = ctx.getResourceResolver();
    List<Iterator<Resource>> allChildren = new ArrayList<>();
    Iterator<Resource> children;
    boolean askParentResourceProvider;
    if (fsMode == FsMode.FILEVAULT_XML) {
        // filevault: always ask provider, it checks itself if children matches the filters
        askParentResourceProvider = true;
        children = fileVaultMapper.getChildren(resolver, parent);
        if (children != null) {
            allChildren.add(children);
        }
    } else {
        // Sling-Initial-Content: get all matching folders/files and content files
        askParentResourceProvider = this.overlayParentResourceProvider;
        children = contentFileMapper.getChildren(resolver, parent);
        if (children != null) {
            allChildren.add(children);
        }
        children = fileMapper.getChildren(resolver, parent);
        if (children != null) {
            allChildren.add(children);
        }
    }
    // get children from from shadowed provider
    if (askParentResourceProvider) {
        final ResourceProvider parentResourceProvider = ctx.getParentResourceProvider();
        if (parentResourceProvider != null) {
            children = parentResourceProvider.listChildren(ctx.getParentResolveContext(), parent);
            if (children != null) {
                if (fsMode == FsMode.FILEVAULT_XML) {
                    // filevault: include all children from parent resource provider that do not match the filters
                    allChildren.add(IteratorUtils.filteredIterator(children, new Predicate() {

                        @Override
                        public boolean evaluate(Object object) {
                            Resource child = (Resource) object;
                            return !fileVaultMapper.pathMatches(child.getPath());
                        }
                    }));
                } else {
                    allChildren.add(children);
                }
            }
        }
    }
    if (allChildren.isEmpty()) {
        return null;
    } else if (allChildren.size() == 1) {
        return allChildren.get(0);
    } else {
        // merge all children from the different iterators, but filter out potential duplicates with same resource name
        return IteratorUtils.filteredIterator(IteratorUtils.chainedIterator(allChildren), new Predicate() {

            private Set<String> names = new HashSet<>();

            @Override
            public boolean evaluate(Object object) {
                Resource resource = (Resource) object;
                return names.add(resource.getName());
            }
        });
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ResourceProvider(org.apache.sling.spi.resource.provider.ResourceProvider) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Resource(org.apache.sling.api.resource.Resource) Predicate(org.apache.commons.collections.Predicate)

Example 34 with ResourceResolver

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

the class AbstractJobHandlingTest method cleanup.

public void cleanup() {
    // clean job area
    final ServiceReference<ResourceResolverFactory> ref = this.bc.getServiceReference(ResourceResolverFactory.class);
    final ResourceResolverFactory factory = this.bc.getService(ref);
    ResourceResolver resolver = null;
    try {
        resolver = factory.getAdministrativeResourceResolver(null);
        final Resource rsrc = resolver.getResource("/var/eventing");
        if (rsrc != null) {
            delete(rsrc);
            resolver.commit();
        }
    } catch (final LoginException le) {
    // ignore
    } catch (final PersistenceException e) {
    // ignore
    } catch (final Exception e) {
    // sometimes an NPE is thrown from the repository, as we
    // are in the cleanup, we can ignore this
    } finally {
        if (resolver != null) {
            resolver.close();
        }
    }
    // unregister all services
    for (final ServiceRegistration<?> reg : this.registrations) {
        reg.unregister();
    }
    this.registrations.clear();
    // remove all configurations
    try {
        final org.osgi.service.cm.Configuration[] cfgs = this.configAdmin.listConfigurations(null);
        if (cfgs != null) {
            for (final org.osgi.service.cm.Configuration c : cfgs) {
                try {
                    c.delete();
                } catch (final IOException io) {
                // ignore
                }
            }
        }
    } catch (final IOException io) {
    // ignore
    } catch (final InvalidSyntaxException e) {
    // ignore
    }
    this.sleep(1000);
}
Also used : Configuration(org.ops4j.pax.exam.Configuration) JobManagerConfiguration(org.apache.sling.event.impl.jobs.config.JobManagerConfiguration) Resource(org.apache.sling.api.resource.Resource) IOException(java.io.IOException) PersistenceException(org.apache.sling.api.resource.PersistenceException) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) LoginException(org.apache.sling.api.resource.LoginException) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Example 35 with ResourceResolver

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

the class JobQueueImpl method removeAll.

/**
     * @see org.apache.sling.event.jobs.Queue#removeAll()
     */
@Override
public synchronized void removeAll() {
    final Set<String> topics = this.cache.getTopics();
    logger.debug("Removing all jobs for queue {} : {}", queueName, topics);
    if (!topics.isEmpty()) {
        final ResourceResolver resolver = this.services.configuration.createResourceResolver();
        try {
            final Resource baseResource = resolver.getResource(this.services.configuration.getLocalJobsPath());
            // sanity check - should never be null
            if (baseResource != null) {
                final BatchResourceRemover brr = new BatchResourceRemover();
                for (final String t : topics) {
                    final Resource topicResource = baseResource.getChild(t.replace('/', '.'));
                    if (topicResource != null) {
                        JobTopicTraverser.traverse(logger, topicResource, new JobTopicTraverser.JobCallback() {

                            @Override
                            public boolean handle(final JobImpl job) {
                                final Resource jobResource = topicResource.getResourceResolver().getResource(job.getResourcePath());
                                // sanity check
                                if (jobResource != null) {
                                    try {
                                        brr.delete(jobResource);
                                    } catch (final PersistenceException ignore) {
                                        logger.error("Unable to remove job " + job, ignore);
                                        topicResource.getResourceResolver().revert();
                                        topicResource.getResourceResolver().refresh();
                                    }
                                }
                                return true;
                            }
                        });
                    }
                }
                try {
                    resolver.commit();
                } catch (final PersistenceException ignore) {
                    logger.error("Unable to remove jobs", ignore);
                }
            }
        } finally {
            resolver.close();
        }
    }
}
Also used : BatchResourceRemover(org.apache.sling.event.impl.support.BatchResourceRemover) JobImpl(org.apache.sling.event.impl.jobs.JobImpl) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) JobTopicTraverser(org.apache.sling.event.impl.jobs.JobTopicTraverser)

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