001/* 002 * Copyright 2020 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.extraction.execution; 017 018import java.nio.file.Path; 019import java.time.Instant; 020import java.time.temporal.ChronoUnit; 021 022import org.apache.avalon.framework.activity.Initializable; 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025import org.quartz.JobDataMap; 026import org.quartz.JobExecutionContext; 027 028import org.ametys.cms.schedule.AbstractDeleteFilesSchedulable; 029import org.ametys.core.right.RightManager; 030import org.ametys.core.right.RightManager.RightResult; 031import org.ametys.core.schedule.Schedulable; 032import org.ametys.core.schedule.progression.ContainerProgressionTracker; 033import org.ametys.core.user.CurrentUserProvider; 034import org.ametys.core.user.UserIdentity; 035import org.ametys.plugins.core.schedule.Scheduler; 036import org.ametys.plugins.extraction.ExtractionConstants; 037import org.ametys.runtime.util.AmetysHomeHelper; 038 039/** 040 * A {@link Schedulable} job for deleting old extractions 041 */ 042public class DeleteExtractionsSchedulable extends AbstractDeleteFilesSchedulable implements Initializable 043{ 044 /** Parameter for workspace */ 045 public static final String LIFETIME_KEY = "lifetime"; 046 047 private static final String __JOBDATAMAP_LIFETIME_KEY = Scheduler.PARAM_VALUES_PREFIX + LIFETIME_KEY; 048 049 /** Right Manager */ 050 protected RightManager _rightManager; 051 052 /** Current user provider */ 053 protected CurrentUserProvider _currentUserProvider; 054 055 private Path _rootPath; 056 057 @Override 058 public void service(ServiceManager manager) throws ServiceException 059 { 060 super.service(manager); 061 _rightManager = (RightManager) manager.lookup(RightManager.ROLE); 062 _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE); 063 } 064 065 @Override 066 public void initialize() throws Exception 067 { 068 _rootPath = AmetysHomeHelper.getAmetysHomeData().toPath().resolve(ExtractionConstants.RESULT_EXTRACTION_DIR_NAME); 069 } 070 071 @Override 072 public void execute(JobExecutionContext context, ContainerProgressionTracker progressionTracker) throws Exception 073 { 074 UserIdentity user = _currentUserProvider.getUser(); 075 if (_rightManager.hasRight(user, ExtractionConstants.MODIFY_EXTRACTION_RIGHT_ID, "/${WorkspaceName}") != RightResult.RIGHT_ALLOW) 076 { 077 throw new IllegalStateException("User " + user + " try to delete extractions with no sufficient rights"); 078 } 079 080 super.execute(context, progressionTracker); 081 } 082 083 @Override 084 protected DeleteFilesConfiguration _getConfiguration(JobExecutionContext context) 085 { 086 DeleteFilesConfiguration configuration = new DeleteFilesConfiguration(_rootPath); 087 JobDataMap jobDataMap = context.getJobDetail().getJobDataMap(); 088 long lifetime = jobDataMap.getLong(__JOBDATAMAP_LIFETIME_KEY); 089 configuration.setAgeLimit(Instant.now().minus(lifetime, ChronoUnit.DAYS)); 090 return configuration; 091 } 092}