use of org.apache.sling.api.resource.Resource in project sling by apache.
the class JobManagerImpl method findJobs.
/**
* @see org.apache.sling.event.jobs.JobManager#findJobs(org.apache.sling.event.jobs.JobManager.QueryType, java.lang.String, long, java.util.Map[])
*/
@Override
public Collection<Job> findJobs(final QueryType type, final String topic, final long limit, final Map<String, Object>... templates) {
final boolean isHistoryQuery = type == QueryType.HISTORY || type == QueryType.SUCCEEDED || type == QueryType.CANCELLED || type == QueryType.DROPPED || type == QueryType.ERROR || type == QueryType.GIVEN_UP || type == QueryType.STOPPED;
final List<Job> result = new ArrayList<>();
final ResourceResolver resolver = this.configuration.createResourceResolver();
final StringBuilder buf = new StringBuilder(64);
try {
buf.append("/jcr:root");
buf.append(this.configuration.getJobsBasePathWithSlash());
buf.append("/element(*,");
buf.append(ResourceHelper.RESOURCE_TYPE_JOB);
buf.append(")[@");
buf.append(ISO9075.encode(ResourceHelper.PROPERTY_JOB_TOPIC));
if (topic != null) {
buf.append(" = '");
buf.append(topic);
buf.append("'");
}
// restricting on the type - history or unfinished
if (isHistoryQuery) {
buf.append(" and @");
buf.append(ISO9075.encode(JobImpl.PROPERTY_FINISHED_STATE));
if (type == QueryType.SUCCEEDED || type == QueryType.DROPPED || type == QueryType.ERROR || type == QueryType.GIVEN_UP || type == QueryType.STOPPED) {
buf.append(" = '");
buf.append(type.name());
buf.append("'");
} else if (type == QueryType.CANCELLED) {
buf.append(" and (@");
buf.append(ISO9075.encode(JobImpl.PROPERTY_FINISHED_STATE));
buf.append(" = '");
buf.append(QueryType.DROPPED.name());
buf.append("' or @");
buf.append(ISO9075.encode(JobImpl.PROPERTY_FINISHED_STATE));
buf.append(" = '");
buf.append(QueryType.ERROR.name());
buf.append("' or @");
buf.append(ISO9075.encode(JobImpl.PROPERTY_FINISHED_STATE));
buf.append(" = '");
buf.append(QueryType.GIVEN_UP.name());
buf.append("' or @");
buf.append(ISO9075.encode(JobImpl.PROPERTY_FINISHED_STATE));
buf.append(" = '");
buf.append(QueryType.STOPPED.name());
buf.append("')");
}
} else {
buf.append(" and not(@");
buf.append(ISO9075.encode(JobImpl.PROPERTY_FINISHED_STATE));
buf.append(")");
if (type == QueryType.ACTIVE) {
buf.append(" and @");
buf.append(ISO9075.encode(Job.PROPERTY_JOB_STARTED_TIME));
} else if (type == QueryType.QUEUED) {
buf.append(" and not(@");
buf.append(ISO9075.encode(Job.PROPERTY_JOB_STARTED_TIME));
buf.append(")");
}
}
if (templates != null && templates.length > 0) {
int index = 0;
for (final Map<String, Object> template : templates) {
// skip empty templates
if (template.size() == 0) {
continue;
}
if (index == 0) {
buf.append(" and (");
} else {
buf.append(" or ");
}
buf.append('(');
final Iterator<Map.Entry<String, Object>> i = template.entrySet().iterator();
boolean first = true;
while (i.hasNext()) {
final Map.Entry<String, Object> current = i.next();
final String key = ISO9075.encode(current.getKey());
final char firstChar = key.length() > 0 ? key.charAt(0) : 0;
final String propName;
final Operation op;
if (firstChar == '=') {
propName = key.substring(1);
op = Operation.EQUALS;
} else if (firstChar == '<') {
final char secondChar = key.length() > 1 ? key.charAt(1) : 0;
if (secondChar == '=') {
op = Operation.LESS_OR_EQUALS;
propName = key.substring(2);
} else {
op = Operation.LESS;
propName = key.substring(1);
}
} else if (firstChar == '>') {
final char secondChar = key.length() > 1 ? key.charAt(1) : 0;
if (secondChar == '=') {
op = Operation.GREATER_OR_EQUALS;
propName = key.substring(2);
} else {
op = Operation.GREATER;
propName = key.substring(1);
}
} else {
propName = key;
op = Operation.EQUALS;
}
if (first) {
first = false;
buf.append('@');
} else {
buf.append(" and @");
}
buf.append(propName);
buf.append(' ');
switch(op) {
case EQUALS:
buf.append('=');
break;
case LESS:
buf.append('<');
break;
case LESS_OR_EQUALS:
buf.append("<=");
break;
case GREATER:
buf.append('>');
break;
case GREATER_OR_EQUALS:
buf.append(">=");
break;
}
buf.append(" '");
buf.append(current.getValue());
buf.append("'");
}
buf.append(')');
index++;
}
if (index > 0) {
buf.append(')');
}
}
buf.append("] order by @");
if (isHistoryQuery) {
buf.append(JobImpl.PROPERTY_FINISHED_DATE);
buf.append(" descending");
} else {
buf.append(Job.PROPERTY_JOB_CREATED);
buf.append(" ascending");
}
final Iterator<Resource> iter = resolver.findResources(buf.toString(), "xpath");
long count = 0;
while (iter.hasNext() && (limit < 1 || count < limit)) {
final Resource jobResource = iter.next();
// sanity check for the path
if (this.configuration.isJob(jobResource.getPath())) {
final JobImpl job = Utility.readJob(logger, jobResource);
if (job != null) {
count++;
result.add(job);
}
}
}
} catch (final QuerySyntaxException qse) {
logger.warn("Query syntax wrong " + buf.toString(), qse);
} finally {
resolver.close();
}
return result;
}
use of org.apache.sling.api.resource.Resource in project sling by apache.
the class Utility method getSortedChildren.
/**
* Helper method to read all children of a resource and sort them by name
* @param type The type of resources (for debugging)
* @param rsrc The parent resource
* @return Sorted list of children.
*/
public static List<Resource> getSortedChildren(final Logger logger, final String type, final Resource rsrc) {
final List<Resource> children = new ArrayList<>();
final Iterator<Resource> monthIter = rsrc.listChildren();
while (monthIter.hasNext()) {
final Resource monthResource = monthIter.next();
children.add(monthResource);
logger.debug("Found {} : {}", type, monthResource.getName());
}
Collections.sort(children, RESOURCE_COMPARATOR);
return children;
}
use of org.apache.sling.api.resource.Resource 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.Resource 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.Resource in project sling by apache.
the class ResourceHelperWithoutJcrTest method testMoveResource.
/**
* Test moveResource method that normally relies to a JCR move operation without JCR = fallback to resource API.
* @throws PersistenceException
*/
@Test
public void testMoveResource() throws PersistenceException {
// prepare some test content
Resource source = context.create().resource("/content/path1", VALUEMAP_1);
context.create().resource("/content/path1/child1", VALUEMAP_1);
context.create().resource("/content/path1/child1/child11", VALUEMAP_1);
context.create().resource("/content/path1/child1/child12", VALUEMAP_2);
context.create().resource("/content/path1/child2", VALUEMAP_2);
ResourceHelper.moveResource(source, "/content/path2");
assertNull(context.resourceResolver().getResource("/content/path1"));
Resource target = context.resourceResolver().getResource("/content/path2");
Resource target1 = context.resourceResolver().getResource("/content/path2/child1");
Resource target11 = context.resourceResolver().getResource("/content/path2/child1/child11");
Resource target12 = context.resourceResolver().getResource("/content/path2/child1/child12");
Resource target2 = context.resourceResolver().getResource("/content/path2/child2");
assertEquals(VALUEMAP_1, ResourceUtil.getValueMap(target));
assertEquals(VALUEMAP_1, ResourceUtil.getValueMap(target1));
assertEquals(VALUEMAP_1, ResourceUtil.getValueMap(target11));
assertEquals(VALUEMAP_2, ResourceUtil.getValueMap(target12));
assertEquals(VALUEMAP_2, ResourceUtil.getValueMap(target2));
}
Aggregations