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;
}
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;
}
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());
}
});
}
}
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);
}
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();
}
}
}
Aggregations