All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.acs.commons.packagegarbagecollector.PackageGarbageCollectionJob Maven / Gradle / Ivy

There is a newer version: 6.9.4
Show newest version
/*-
 * #%L
 * ACS AEM Commons Bundle
 * %%
 * Copyright (C) 2013 - 2022 Adobe
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package com.adobe.acs.commons.packagegarbagecollector;

import org.apache.jackrabbit.JcrConstants;
import org.apache.jackrabbit.vault.packaging.JcrPackage;
import org.apache.jackrabbit.vault.packaging.JcrPackageDefinition;
import org.apache.jackrabbit.vault.packaging.JcrPackageManager;
import org.apache.jackrabbit.vault.packaging.PackageId;
import org.apache.jackrabbit.vault.packaging.Packaging;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.event.jobs.Job;
import org.apache.sling.event.jobs.consumer.JobConsumer;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nonnull;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Period;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

import static com.adobe.acs.commons.packagegarbagecollector.PackageGarbageCollectionScheduler.*;

@Component(
        service = JobConsumer.class,
        immediate = true,
        property = {JobConsumer.PROPERTY_TOPICS + "=" + PackageGarbageCollectionScheduler.JOB_TOPIC})
public class PackageGarbageCollectionJob implements JobConsumer {
    public static final DateTimeFormatter LOCALIZED_DATE_FORMATTER = DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM);
    private static final Logger LOG = LoggerFactory.getLogger(PackageGarbageCollectionJob.class);

    private static final String SERVICE_USER = "package-garbage-collection";

    @Reference
    Packaging packaging;

    @Reference
    ResourceResolverFactory resourceResolverFactory;

    @Override
    public JobResult process(Job job) {
        String groupName = job.getProperty(GROUP_NAME, String.class);
        Integer maxAgeInDays = job.getProperty(MAX_AGE_IN_DAYS, Integer.class);
        boolean removeNotInstalledPackages = job.getProperty(REMOVE_NOT_INSTALLED_PACKAGES, false);
        int packagesRemoved = 0;
        LOG.debug("Job Configuration: ["
                + "Group Name: {}, "
                + "Service User: {}, "
                + "Age of Package {} days,"
                + "Remove not installed packages: {}]", groupName, SERVICE_USER, maxAgeInDays, removeNotInstalledPackages);

        try (ResourceResolver resourceResolver = resourceResolverFactory.getServiceResourceResolver(
                Collections.singletonMap(ResourceResolverFactory.SUBSERVICE, SERVICE_USER))) {
            Session session = resourceResolver.adaptTo(Session.class);
            JcrPackageManager packageManager = packaging.getPackageManager(session);
            List packages = packageManager.listPackages(groupName, false);

            for (JcrPackage jcrPackage : packages) {
                String packageDescription = getPackageDescription(jcrPackage);
                LOG.info("Processing package {}", packageDescription);

                if (isPackageOldEnough(jcrPackage, maxAgeInDays)) {
                    if (removeNotInstalledPackages && !isInstalled(jcrPackage)) {
                        packageManager.remove(jcrPackage);
                        packagesRemoved++;
                        LOG.info("Deleted not-installed package {}", packageDescription);
                    } else if (isInstalled(jcrPackage) && !isLatestInstalled(jcrPackage, packageManager.listPackages(groupName, false))) {
                        packageManager.remove(jcrPackage);
                        packagesRemoved++;
                        LOG.info("Deleted installed package {} since it is not the latest installed version.", packageDescription);
                    } else {
                        LOG.info("Not removing package because it's the current installed one {}", packageDescription);
                    }
                } else {
                    LOG.debug("Not removing package because it's not old enough {}", packageDescription);
                }
            }
        } catch (LoginException | RepositoryException | IOException e) {
            if (packagesRemoved > 0) {
                LOG.error("Package Garbage Collector job partially failed - Removed {} packages", packagesRemoved);
            }
            LOG.error("Unable to finish clearing packages", e);
            return JobResult.FAILED;
        }
        LOG.info("Package Garbage Collector job finished - Removed {} packages", packagesRemoved);
        return JobResult.OK;
    }

    private boolean isInstalled(JcrPackage jcrPackage) {
        PackageDefinition definition = new PackageDefinition(jcrPackage);
        return definition.getLastUnpacked() != null;
    }

    private boolean isLatestInstalled(JcrPackage jcrPackage, List installedPackages) {
        Optional lastInstalledPackageOptional = installedPackages.stream().filter(installedPackage -> {
                    PackageDefinition definition = new PackageDefinition(installedPackage);
                    return definition.isSameNameAndGroup(jcrPackage);
                })
                .filter(pkg -> new PackageDefinition(pkg).getLastUnpacked() != null)
                .max(Comparator.comparing(pkg -> new PackageDefinition(pkg).getLastUnpacked()));

        if (lastInstalledPackageOptional.isPresent()) {
            JcrPackage lastInstalledPackage = lastInstalledPackageOptional.get();
            PackageDefinition lastInstalledPackageDefinition = new PackageDefinition(lastInstalledPackage);
            PackageDefinition thisPackageDefinition = new PackageDefinition(jcrPackage);

            // If it's not actually installed yet.
            if (lastInstalledPackageDefinition.getLastUnpacked() == null) {
                // This should never be here since this check is guarded by isInstalled() above.
                return false;
            }

            return lastInstalledPackageDefinition.hasSamePid(thisPackageDefinition);
        }

        return false;
    }

    static class PackageDefinition {
        JcrPackage jcrPackage;

        public PackageDefinition(@Nonnull JcrPackage jcrPackage) {
            this.jcrPackage = jcrPackage;
        }

        public Calendar getLastUnpacked() {
            try {
                JcrPackageDefinition definition = jcrPackage.getDefinition();
                if (definition != null) {
                    return definition.getLastUnpacked();
                }
                return null;
            } catch (RepositoryException ex) {
                return null;
            }
        }

        public boolean isSameNameAndGroup(JcrPackage otherPackage) {
            Optional otherPackageId = getPid(otherPackage);
            Optional thisPackageId = getPid(jcrPackage);
            if (otherPackageId.isPresent() && thisPackageId.isPresent()) {
                return otherPackageId.get().getGroup().equals(thisPackageId.get().getGroup())
                        && otherPackageId.get().getName().equals(thisPackageId.get().getName());
            }
            return false;
        }

        public PackageId getId() {
            try {
                JcrPackageDefinition definition = jcrPackage.getDefinition();
                if (definition != null) {
                    return definition.getId();
                }
                return null;
            } catch (RepositoryException ex) {
                return null;
            }
        }

        private Optional getPid(JcrPackage jcrPkg) {
            try {
                return Optional.ofNullable(jcrPkg.getDefinition()).map(JcrPackageDefinition::getId);
            } catch (RepositoryException ex) {
                return Optional.empty();
            }
        }

        public boolean hasSamePid(PackageDefinition jcrPkg) {
            try {
                Optional pkgId = Optional.ofNullable(jcrPkg.getId());
                return pkgId.map(packageId -> packageId.equals(getId())).orElse(false);
            } catch (NullPointerException ex) {
                return false;
            }
        }
    }

    private boolean isPackageOldEnough(JcrPackage jcrPackage, Integer maxAgeInDays) throws RepositoryException, IOException {
        Period maxAge = Period.ofDays(maxAgeInDays);
        LocalDate oldestAge = LocalDate.now().minus(maxAge);
        Calendar packageCreatedAtCalendar = jcrPackage.getPackage().getCreated();

        try {
            if (packageCreatedAtCalendar == null) {
                // Try getting the created at directly from the JCR node that represents the package.
                packageCreatedAtCalendar = jcrPackage.getDefinition().getNode().getProperty(JcrConstants.JCR_CREATED).getValue().getDate();

                if (packageCreatedAtCalendar == null) {
                    // This should not happen, but if it does, we don't want to delete the package.
                    LOG.warn("Package [ {} ] has no created date, assuming it's NOT old enough", jcrPackage.getNode().getPath());
                    return false;
                }
            }
        } catch (RepositoryException e) {
            LOG.error("Unable to get created date for package [ {} ]", jcrPackage.getNode().getPath(), e);
            return false;
        }

        LocalDate packageCreatedAt = LocalDateTime.ofInstant(
                packageCreatedAtCalendar.toInstant(),
                packageCreatedAtCalendar.getTimeZone().toZoneId()).toLocalDate();
        String packageDescription = getPackageDescription(jcrPackage);

        if (LOG.isDebugEnabled()) {
            LOG.debug("Checking if package is old enough: Name: {}, Created At: {}, Oldest Age: {}",
                    packageDescription, packageCreatedAt.format(LOCALIZED_DATE_FORMATTER), oldestAge.format(LOCALIZED_DATE_FORMATTER));
        }
        return !packageCreatedAt.isAfter(oldestAge);
    }

    private String getPackageDescription(JcrPackage jcrPackage) throws RepositoryException {
        JcrPackageDefinition definition = jcrPackage.getDefinition();
        Node packageNode = jcrPackage.getNode();
        if (definition != null && packageNode != null) {
            return String.format("%s:%s:v%s [%s]", definition.getId().getName(), definition.getId().getGroup(), definition.getId().getVersionString(), packageNode.getPath());
        }
        return "Unknown package";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy