Java tutorial
/* * Copyright 2017 Regents of the University of California. Licensed under the Educational Community 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 * * https://opensource.org/licenses/ECL-2.0 * * Unless required under applicable law or agreed to in writing, software distributed under the License is distributed * in an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for * specific language governing permissions and limitations under the license. */ package org.opentestsystem.ap.irs.task; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Date; import java.util.function.Predicate; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.joda.time.DateTime; import org.opentestsystem.ap.common.config.ItemBankProperties; import org.opentestsystem.ap.common.repository.RepositoryUtil; import org.opentestsystem.ap.irs.config.TaskProperties; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Slf4j @Component /** * Deletes old items from the file system. Item data collects in the different item * banks. The items are a one time use and do not need to stay on disk for long. */ public class ItemCleanupTask { private final TaskProperties taskProperties; private final ItemBankProperties itemBankProperties; public ItemCleanupTask(final TaskProperties taskProperties, final ItemBankProperties itemBankProperties) { this.taskProperties = taskProperties; this.itemBankProperties = itemBankProperties; } @Scheduled(fixedRateString = "${tasks.itemCleanupRunEveryMillis}") public void cleanupOldItems() { log.debug("file cleanup: start"); final Path path = Paths.get(itemBankProperties.getLocalBaseDir()); final Date cleanupThresholdDate = new DateTime().minusMillis(taskProperties.getItemCleanupThresholdMillis()) .toDate(); cleanupFolder(path, cleanupThresholdDate); log.debug("IRS file cleanup: end"); } private void cleanupFolder(final Path rootPath, final Date cleanupThresholdDate) { log.debug("cleanupFolder: path {}, threshold {}", rootPath, cleanupThresholdDate); try { Files.walk(rootPath, 1).filter(Files::isDirectory).filter(isPathNotEqual(rootPath)) .filter(isFolderOlderThan(cleanupThresholdDate)).forEach(RepositoryUtil::deleteDirectory); } catch (IOException e) { log.debug("cleanupFolder: Folder not found: {}", e.getMessage()); } } /** * If the path last modified date is before the given date true is returned. False otherwise. * * @param date The date to compare the path last modified data against. * @return If the path last modified date is before the given date true is returned. False otherwise. */ private Predicate<Path> isFolderOlderThan(final Date date) { return path -> path.toFile().lastModified() < date.getTime(); } /** * If the paths are not equal then true is returned. * * @param pathToCompare The path to compare. * @return True if the two paths are not equal. */ private Predicate<Path> isPathNotEqual(final Path pathToCompare) { return path -> !StringUtils.equals(pathToCompare.toString(), path.toString()); } }