VersionPurgeTask.java 5.6 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.api.VersionManager
 *  com.day.cq.wcm.api.VersionManager$PurgeInfo
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.sling.commons.osgi.PropertiesUtil
 *  org.apache.sling.event.jobs.Job
 *  org.apache.sling.event.jobs.consumer.JobExecutionContext
 *  org.apache.sling.event.jobs.consumer.JobExecutionContext$ResultBuilder
 *  org.apache.sling.event.jobs.consumer.JobExecutionResult
 *  org.apache.sling.event.jobs.consumer.JobExecutor
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.osgi.service.component.ComponentContext
 */
package com.day.cq.wcm.core.impl;

import com.day.cq.wcm.api.VersionManager;
import java.util.Dictionary;
import java.util.List;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.commons.osgi.PropertiesUtil;
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.consumer.JobExecutionContext;
import org.apache.sling.event.jobs.consumer.JobExecutionResult;
import org.apache.sling.event.jobs.consumer.JobExecutor;
import org.apache.sling.jcr.api.SlingRepository;
import org.osgi.service.component.ComponentContext;

@Component(label="%versionpurge.name", description="%versionpurge.description", metatype=1)
@Service(value={JobExecutor.class})
@Properties(value={@Property(name="job.topics", value={"com/adobe/granite/maintenance/job/VersionPurgeTask"}, propertyPrivate=1), @Property(name="granite.maintenance.name", value={"com.day.cq.wcm.core.impl.VersionPurgeTask"}, propertyPrivate=1), @Property(name="granite.maintenance.title", value={"Version Purge"}, propertyPrivate=1), @Property(name="granite.maintenance.schedule", value={"weekly"}), @Property(name="granite.maintenance.isConservative", boolValue={0})})
public class VersionPurgeTask
implements JobExecutor {
    @Reference
    private VersionManager versionManager;
    @Reference
    private SlingRepository repository;
    private static final String[] DEFAULT_PATHS = new String[]{"/content"};
    private static final boolean DEFAULT_RECURSIVE = true;
    private static final int DEFAULT_MAX_VERSIONS = 5;
    private static final int DEFAULT_MAX_AGE_DAYS = 30;
    private static final String VERSION_PURGE = "version-purge";
    @Property(value={"/content"}, cardinality=Integer.MAX_VALUE)
    private static final String PROPERTY_PATHS = "versionpurge.paths";
    @Property(boolValue={1})
    private static final String PROPERTY_RECURSIVE = "versionpurge.recursive";
    @Property(intValue={5})
    private static final String PROPERTY_MAX_VERSIONS = "versionpurge.maxVersions";
    @Property(intValue={30})
    private static final String PROPERTY_MAX_AGE_DAYS = "versionpurge.maxAgeDays";
    private String[] paths;
    private boolean recursive;
    private int maxVersions;
    private int maxAgeDays;

    @Activate
    protected void activate(ComponentContext context) {
        Dictionary props = context.getProperties();
        this.paths = PropertiesUtil.toStringArray(props.get("versionpurge.paths"), (String[])DEFAULT_PATHS);
        this.recursive = PropertiesUtil.toBoolean(props.get("versionpurge.recursive"), (boolean)true);
        this.maxVersions = PropertiesUtil.toInteger(props.get("versionpurge.maxVersions"), (int)5);
        this.maxAgeDays = PropertiesUtil.toInteger(props.get("versionpurge.maxAgeDays"), (int)30);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public JobExecutionResult process(Job job, JobExecutionContext context) {
        Session session = null;
        try {
            session = this.repository.loginService("version-purge", null);
            for (String p : this.paths) {
                for (VersionManager.PurgeInfo info : this.versionManager.purgeVersions(session, p, false, this.recursive, this.maxVersions, this.maxAgeDays)) {
                    if (info.isRetained()) continue;
                    context.log("Purged version {0} of {1}", new Object[]{info.getVersionName(), info.getVersionedNodePath()});
                }
            }
        }
        catch (RepositoryException e) {
            context.log("Exception while purging versions: {0}", new Object[]{e});
            JobExecutionResult len$ = context.result().message(e.getMessage()).failed();
            return len$;
        }
        finally {
            if (session != null) {
                session.logout();
            }
        }
        return context.result().succeeded();
    }

    protected void bindVersionManager(VersionManager versionManager) {
        this.versionManager = versionManager;
    }

    protected void unbindVersionManager(VersionManager versionManager) {
        if (this.versionManager == versionManager) {
            this.versionManager = null;
        }
    }

    protected void bindRepository(SlingRepository slingRepository) {
        this.repository = slingRepository;
    }

    protected void unbindRepository(SlingRepository slingRepository) {
        if (this.repository == slingRepository) {
            this.repository = null;
        }
    }
}