OffloadingResourceUtil.java 4.47 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.api.resource.ModifiableValueMap
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.osgi.service.event.Event
 */
package com.adobe.granite.offloading.impl.util;

import com.adobe.granite.offloading.api.OffloadingJobProperties;
import com.adobe.granite.offloading.impl.OffloadingStatus;
import com.adobe.granite.offloading.impl.util.OffloadingUtil;
import java.util.Calendar;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.ModifiableValueMap;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.osgi.service.event.Event;

public class OffloadingResourceUtil {
    public static void markOffloadingJobFinished(Resource jobResource) {
        OffloadingResourceUtil.setProperty(jobResource, OffloadingJobProperties.STATUS.propertyName(), OffloadingStatus.FINISHED.getStatusValue());
    }

    public static void markOffloadingJobFailed(Resource jobResource) {
        OffloadingResourceUtil.setProperty(jobResource, OffloadingJobProperties.STATUS.propertyName(), OffloadingStatus.FAILED.getStatusValue());
    }

    public static void markOffloadingJobCancelled(Resource jobResource) {
        OffloadingResourceUtil.setProperty(jobResource, OffloadingJobProperties.STATUS.propertyName(), OffloadingStatus.CANCELLED.getStatusValue());
    }

    public static void markOffloadingJobStarted(Resource jobResource) {
        OffloadingResourceUtil.setProperty(jobResource, OffloadingJobProperties.STATUS.propertyName(), OffloadingStatus.STARTED.getStatusValue());
    }

    public static void markJobStarted(Resource jobResource) {
        OffloadingResourceUtil.setProperty(jobResource, "event.job.started.time", Calendar.getInstance());
    }

    public static void markJobNotStarted(Resource jobResource) {
        OffloadingResourceUtil.unsetProperty(jobResource, "event.job.started.time");
    }

    public static void setProperty(Resource resource, String property, Object value) {
        ModifiableValueMap jobProperties = (ModifiableValueMap)resource.adaptTo(ModifiableValueMap.class);
        jobProperties.put((Object)property, value);
    }

    public static void unsetProperty(Resource resource, String property) {
        ModifiableValueMap jobProperties = (ModifiableValueMap)resource.adaptTo(ModifiableValueMap.class);
        if (jobProperties.containsKey((Object)property)) {
            jobProperties.remove((Object)property);
        }
    }

    public static Resource getJobResourceFromEvent(ResourceResolver resolver, Event event) {
        String path = (String)event.getProperty("path");
        if (StringUtils.isNotBlank((String)path)) {
            return resolver.getResource(path);
        }
        return null;
    }

    public static Resource getJobResourceFromJobNotification(ResourceResolver resolver, Event event, String offloadingJobsBasePath) {
        String jobId = (String)event.getProperty("slingevent:eventId");
        String targetInstance = (String)event.getProperty("event.job.application");
        String jobTopic = (String)event.getProperty("event.job.topic");
        if (StringUtils.isNotBlank((String)jobId) && StringUtils.isNotBlank((String)targetInstance) && StringUtils.isNotBlank((String)jobTopic)) {
            String offloadingJobPath = OffloadingUtil.getJobPathFromJobId(offloadingJobsBasePath, targetInstance, jobTopic, jobId);
            return resolver.getResource(offloadingJobPath);
        }
        return null;
    }

    public static void deleteJob(Resource offloadingJobResource, String slingJobPathPrefix, String offloadingJobPathPrefix) throws PersistenceException {
        String offloadingJobPath = offloadingJobResource.getPath();
        String jobPath = offloadingJobPath.replace(offloadingJobPathPrefix, slingJobPathPrefix);
        OffloadingResourceUtil.deleteJob(offloadingJobResource.getResourceResolver(), jobPath);
        OffloadingResourceUtil.deleteJob(offloadingJobResource.getResourceResolver(), offloadingJobPath);
    }

    public static void deleteJob(ResourceResolver resolver, String jobPath) throws PersistenceException {
        Resource jobResource = resolver.getResource(jobPath);
        if (jobResource != null) {
            resolver.delete(jobResource);
        }
    }
}