use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class FindUnfinishedJobsTask method initTopic.
/**
* Initialize a topic and update all jobs from that topic.
* Reset started time and increase retry count of unfinished jobs
* @param topicResource The topic resource
*/
private void initTopic(final Resource topicResource) {
logger.debug("Initializing topic {}...", topicResource.getName());
JobTopicTraverser.traverse(logger, topicResource, new JobTopicTraverser.JobCallback() {
@Override
public boolean handle(final JobImpl job) {
if (job.getProcessingStarted() != null) {
logger.debug("Found unfinished job {}", job.getId());
job.retry();
try {
final Resource jobResource = topicResource.getResourceResolver().getResource(job.getResourcePath());
// sanity check
if (jobResource != null) {
final ModifiableValueMap mvm = jobResource.adaptTo(ModifiableValueMap.class);
mvm.remove(Job.PROPERTY_JOB_STARTED_TIME);
mvm.put(Job.PROPERTY_JOB_RETRY_COUNT, job.getRetryCount());
if (job.getProperty(JobImpl.PROPERTY_JOB_QUEUED, Calendar.class) == null) {
mvm.put(JobImpl.PROPERTY_JOB_QUEUED, Calendar.getInstance());
}
jobResource.getResourceResolver().commit();
}
} catch (final PersistenceException ignore) {
logger.error("Unable to update unfinished job " + job, ignore);
}
} else if (job.getProperty(JobImpl.PROPERTY_JOB_QUEUED, Calendar.class) == null) {
logger.debug("Found job without queued date {}", job.getId());
try {
final Resource jobResource = topicResource.getResourceResolver().getResource(job.getResourcePath());
// sanity check
if (jobResource != null) {
final ModifiableValueMap mvm = jobResource.adaptTo(ModifiableValueMap.class);
mvm.put(JobImpl.PROPERTY_JOB_QUEUED, Calendar.getInstance());
jobResource.getResourceResolver().commit();
}
} catch (final PersistenceException ignore) {
logger.error("Unable to update queued date for job " + job.getId(), ignore);
}
}
return true;
}
});
logger.debug("Topic {} initialized", topicResource.getName());
}
use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class ResourceBuilderImpl method resource.
@Override
public ResourceBuilder resource(String path, Map<String, Object> properties) {
Resource r = null;
final String parentPath;
final String fullPath;
boolean absolutePath = isAbsolutePath(path);
if (absolutePath) {
parentPath = ResourceUtil.getParent(path);
fullPath = path;
} else {
checkRelativePath(path);
parentPath = parentPath(path);
fullPath = currentParent.getPath() + "/" + path;
}
final Resource myParent = ensureResourceExists(parentPath);
try {
r = currentParent.getResourceResolver().getResource(fullPath);
if (r == null) {
r = currentParent.getResourceResolver().create(myParent, ResourceUtil.getName(fullPath), properties);
} else {
// Resource exists, set our properties
final ModifiableValueMap mvm = r.adaptTo(ModifiableValueMap.class);
if (mvm == null) {
throw new IllegalStateException("Cannot modify properties of " + r.getPath());
}
for (Map.Entry<String, Object> e : properties.entrySet()) {
mvm.put(e.getKey(), e.getValue());
}
}
} catch (PersistenceException pex) {
throw new RuntimeException("PersistenceException while creating Resource " + fullPath, pex);
}
if (r == null) {
throw new RuntimeException("Failed to get or create resource " + fullPath);
} else if (hierarchyMode || absolutePath) {
return cloneResourceBuilder(r, this.intermediatePrimaryType, true);
}
return this;
}
use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class ResourceBuilderImpl method ensureResourceExists.
/** Create a Resource at the specified path if none exists yet,
* using the current intermediate primary type. "Stolen" from
* the sling-mock module's ContentBuilder class.
* @param path Resource path
* @return Resource at path (existing or newly created)
*/
protected final Resource ensureResourceExists(String path) {
if (path == null || path.length() == 0 || path.equals("/")) {
return resourceResolver.getResource("/");
}
Resource resource = resourceResolver.getResource(path);
if (resource != null) {
return resource;
}
String parentPath = ResourceUtil.getParent(path);
String name = ResourceUtil.getName(path);
Resource parentResource = ensureResourceExists(parentPath);
try {
resource = resourceResolver.create(parentResource, name, MapArgsConverter.toMap(JCR_PRIMARYTYPE, intermediatePrimaryType));
return resource;
} catch (PersistenceException ex) {
throw new RuntimeException("Unable to create intermediate resource at " + path, ex);
}
}
use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class JcrResourceProvider method delete.
@Override
public void delete(@Nonnull final ResolveContext<JcrProviderState> ctx, @Nonnull final Resource resource) throws PersistenceException {
// try to adapt to Item
Item item = resource.adaptTo(Item.class);
try {
if (item == null) {
final String jcrPath = resource.getPath();
if (jcrPath == null) {
logger.debug("delete: {} maps to an empty JCR path", resource.getPath());
throw new PersistenceException("Unable to delete resource", null, resource.getPath(), null);
}
item = ctx.getProviderState().getSession().getItem(jcrPath);
}
item.remove();
} catch (final RepositoryException e) {
throw new PersistenceException("Unable to delete resource", e, resource.getPath(), null);
}
}
use of org.apache.sling.api.resource.PersistenceException in project sling by apache.
the class JcrResourceProvider method move.
@Override
public boolean move(@Nonnull final ResolveContext<JcrProviderState> ctx, final String srcAbsPath, final String destAbsPath) throws PersistenceException {
final String srcNodePath = srcAbsPath;
final String dstNodePath = destAbsPath + '/' + ResourceUtil.getName(srcAbsPath);
try {
ctx.getProviderState().getSession().move(srcNodePath, dstNodePath);
return true;
} catch (final RepositoryException e) {
throw new PersistenceException("Unable to move resource to " + destAbsPath, e, srcAbsPath, null);
}
}
Aggregations