use of org.apache.sling.api.resource.ValueMap in project sling by apache.
the class UpgradeTask method upgradeBridgedJobs.
/**
* Upgrade bridged jobs
* @param rootResource The root resource (topic resource)
*/
private void upgradeBridgedJobs(final Resource topicResource) {
final String topicName = topicResource.getName().replace('.', '/');
final QueueConfigurationManager qcm = configuration.getQueueConfigurationManager();
if (qcm == null) {
return;
}
final QueueInfo info = qcm.getQueueInfo(topicName);
JobTopicTraverser.traverse(logger, topicResource, new JobTopicTraverser.ResourceCallback() {
@Override
public boolean handle(final Resource rsrc) {
try {
final ValueMap vm = ResourceHelper.getValueMap(rsrc);
final String targetId = caps.detectTarget(topicName, vm, info);
final Map<String, Object> props = new HashMap<>(vm);
final String newPath;
if (targetId != null) {
newPath = configuration.getAssginedJobsPath() + '/' + targetId + '/' + topicResource.getName() + rsrc.getPath().substring(topicResource.getPath().length());
props.put(Job.PROPERTY_JOB_QUEUE_NAME, info.queueName);
props.put(Job.PROPERTY_JOB_TARGET_INSTANCE, targetId);
} else {
newPath = configuration.getUnassignedJobsPath() + '/' + topicResource.getName() + rsrc.getPath().substring(topicResource.getPath().length());
props.remove(Job.PROPERTY_JOB_QUEUE_NAME);
props.remove(Job.PROPERTY_JOB_TARGET_INSTANCE);
}
props.remove(Job.PROPERTY_JOB_STARTED_TIME);
try {
ResourceHelper.getOrCreateResource(topicResource.getResourceResolver(), newPath, props);
topicResource.getResourceResolver().delete(rsrc);
topicResource.getResourceResolver().commit();
} catch (final PersistenceException pe) {
logger.warn("Unable to move job from previous version " + rsrc.getPath(), pe);
topicResource.getResourceResolver().refresh();
topicResource.getResourceResolver().revert();
}
} catch (final InstantiationException ie) {
logger.warn("Unable to move job from previous version " + rsrc.getPath(), ie);
topicResource.getResourceResolver().refresh();
topicResource.getResourceResolver().revert();
}
return caps.isActive();
}
});
}
use of org.apache.sling.api.resource.ValueMap in project sling by apache.
the class ResourceHelper method cloneValueMap.
public static Map<String, Object> cloneValueMap(final ValueMap vm) throws InstantiationException {
List<Exception> hasReadError = null;
try {
final Map<String, Object> result = new HashMap<>(vm);
for (final Map.Entry<String, Object> entry : result.entrySet()) {
if (entry.getKey().equals(PROPERTY_SCHEDULE_INFO)) {
final String[] infoArray = vm.get(entry.getKey(), String[].class);
if (infoArray == null || infoArray.length == 0) {
if (hasReadError == null) {
hasReadError = new ArrayList<>();
}
hasReadError.add(new Exception("Unable to deserialize property '" + entry.getKey() + "' : " + entry.getValue()));
} else {
final List<ScheduleInfo> infos = new ArrayList<>();
for (final String i : infoArray) {
final ScheduleInfoImpl info = ScheduleInfoImpl.deserialize(i);
if (info != null) {
infos.add(info);
}
}
if (infos.size() < infoArray.length) {
if (hasReadError == null) {
hasReadError = new ArrayList<>();
}
hasReadError.add(new Exception("Unable to deserialize property '" + entry.getKey() + "' : " + Arrays.toString(infoArray)));
} else {
entry.setValue(infos);
}
}
}
if (entry.getValue() instanceof InputStream) {
final Object value = vm.get(entry.getKey(), Serializable.class);
if (value != null) {
entry.setValue(value);
} else {
if (hasReadError == null) {
hasReadError = new ArrayList<>();
}
// let's find out which class might be missing
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream((InputStream) entry.getValue());
ois.readObject();
hasReadError.add(new Exception("Unable to deserialize property '" + entry.getKey() + "'"));
} catch (final ClassNotFoundException cnfe) {
hasReadError.add(new Exception("Unable to deserialize property '" + entry.getKey() + "'", cnfe));
} catch (final IOException ioe) {
hasReadError.add(new RuntimeException("Unable to deserialize property '" + entry.getKey() + "'", ioe));
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException ignore) {
// ignore
}
}
}
}
}
}
if (hasReadError != null) {
result.put(PROPERTY_MARKER_READ_ERROR_LIST, hasReadError);
}
return result;
} catch (final IllegalArgumentException iae) {
// the JCR implementation might throw an IAE if something goes wrong
throw (InstantiationException) new InstantiationException(iae.getMessage()).initCause(iae);
}
}
use of org.apache.sling.api.resource.ValueMap in project sling by apache.
the class ScheduledJobHandler method readScheduledJob.
/**
* Read a scheduled job from the resource
* @return The job or <code>null</code>
*/
private Map<String, Object> readScheduledJob(final Resource eventResource) {
try {
final ValueMap vm = ResourceHelper.getValueMap(eventResource);
final Map<String, Object> properties = ResourceHelper.cloneValueMap(vm);
@SuppressWarnings("unchecked") final List<Exception> readErrorList = (List<Exception>) properties.remove(ResourceHelper.PROPERTY_MARKER_READ_ERROR_LIST);
if (readErrorList != null) {
for (final Exception e : readErrorList) {
logger.warn("Unable to read scheduled job from " + eventResource.getPath(), e);
}
} else {
return properties;
}
} catch (final InstantiationException ie) {
// something happened with the resource in the meantime
this.ignoreException(ie);
}
return null;
}
use of org.apache.sling.api.resource.ValueMap in project sling by apache.
the class CheckTopologyTask method assignJobs.
/**
* Try to assign all jobs from the jobs root.
* The jobs are stored by topic
* @param jobsRoot The root of the jobs
* @param unassign Whether to unassign the job if no instance is found.
*/
private void assignJobs(final Resource jobsRoot, final boolean unassign) {
final ResourceResolver resolver = jobsRoot.getResourceResolver();
final Iterator<Resource> topicIter = jobsRoot.listChildren();
while (caps.isActive() && topicIter.hasNext()) {
final Resource topicResource = topicIter.next();
final String topicName = topicResource.getName().replace('.', '/');
logger.debug("Found topic {}", topicName);
// first check if there is an instance for these topics
final List<InstanceDescription> potentialTargets = caps.getPotentialTargets(topicName);
if (potentialTargets != null && potentialTargets.size() > 0) {
final QueueConfigurationManager qcm = this.configuration.getQueueConfigurationManager();
if (qcm == null) {
break;
}
final QueueInfo info = qcm.getQueueInfo(topicName);
logger.debug("Found queue {} for {}", info.queueConfiguration, topicName);
JobTopicTraverser.traverse(this.logger, topicResource, new JobTopicTraverser.ResourceCallback() {
@Override
public boolean handle(final Resource rsrc) {
try {
final ValueMap vm = ResourceHelper.getValueMap(rsrc);
final String targetId = caps.detectTarget(topicName, vm, info);
if (targetId != null) {
final String newPath = configuration.getAssginedJobsPath() + '/' + targetId + '/' + topicResource.getName() + rsrc.getPath().substring(topicResource.getPath().length());
final Map<String, Object> props = new HashMap<>(vm);
props.put(Job.PROPERTY_JOB_QUEUE_NAME, info.queueName);
props.put(Job.PROPERTY_JOB_TARGET_INSTANCE, targetId);
props.remove(Job.PROPERTY_JOB_STARTED_TIME);
try {
ResourceHelper.getOrCreateResource(resolver, newPath, props);
resolver.delete(rsrc);
resolver.commit();
final String jobId = vm.get(ResourceHelper.PROPERTY_JOB_ID, String.class);
configuration.getAuditLogger().debug("REASSIGN OK {} : {}", targetId, jobId);
} catch (final PersistenceException pe) {
logger.warn("Unable to move unassigned job from " + rsrc.getPath() + " to " + newPath, pe);
resolver.refresh();
resolver.revert();
}
}
} catch (final InstantiationException ie) {
// something happened with the resource in the meantime
logger.warn("Unable to move unassigned job from " + rsrc.getPath(), ie);
resolver.refresh();
resolver.revert();
}
return caps.isActive();
}
});
}
// now unassign if there are still jobs
if (caps.isActive() && unassign) {
// we have to move everything to the unassigned area
JobTopicTraverser.traverse(this.logger, topicResource, new JobTopicTraverser.ResourceCallback() {
@Override
public boolean handle(final Resource rsrc) {
try {
final ValueMap vm = ResourceHelper.getValueMap(rsrc);
final String newPath = configuration.getUnassignedJobsPath() + '/' + topicResource.getName() + rsrc.getPath().substring(topicResource.getPath().length());
final Map<String, Object> props = new HashMap<>(vm);
props.remove(Job.PROPERTY_JOB_QUEUE_NAME);
props.remove(Job.PROPERTY_JOB_TARGET_INSTANCE);
props.remove(Job.PROPERTY_JOB_STARTED_TIME);
try {
ResourceHelper.getOrCreateResource(resolver, newPath, props);
resolver.delete(rsrc);
resolver.commit();
final String jobId = vm.get(ResourceHelper.PROPERTY_JOB_ID, String.class);
configuration.getAuditLogger().debug("REUNASSIGN OK : {}", jobId);
} catch (final PersistenceException pe) {
logger.warn("Unable to unassigned job from " + rsrc.getPath() + " to " + newPath, pe);
resolver.refresh();
resolver.revert();
}
} catch (final InstantiationException ie) {
// something happened with the resource in the meantime
logger.warn("Unable to unassigned job from " + rsrc.getPath(), ie);
resolver.refresh();
resolver.revert();
}
return caps.isActive();
}
});
}
}
}
use of org.apache.sling.api.resource.ValueMap in project sling by apache.
the class FileResource method getResourceType.
public String getResourceType() {
if (resourceType == null) {
ValueMap props = getValueMap();
resourceType = props.get("sling:resourceType", String.class);
if (resourceType == null) {
// fallback to jcr:primaryType when resource type not set
resourceType = props.get("jcr:primaryType", String.class);
}
}
return resourceType;
}
Aggregations