1 17 package org.alfresco.repo.content; 18 19 import java.util.List ; 20 21 import org.alfresco.error.AlfrescoRuntimeException; 22 import org.alfresco.service.cmr.repository.ContentReader; 23 import org.alfresco.service.cmr.search.SearchService; 24 import org.quartz.Job; 25 import org.quartz.JobDataMap; 26 import org.quartz.JobExecutionContext; 27 import org.quartz.JobExecutionException; 28 29 41 public class ContentStoreCleanupJob implements Job 42 { 43 47 public void execute(JobExecutionContext context) throws JobExecutionException 48 { 49 JobDataMap jobData = context.getJobDetail().getJobDataMap(); 50 Object contentStoreObj = jobData.get("contentStore"); 52 if (contentStoreObj == null || !(contentStoreObj instanceof ContentStore)) 53 { 54 throw new AlfrescoRuntimeException( 55 "ContentStoreCleanupJob data must contain valid 'contentStore' reference"); 56 } 57 ContentStore contentStore = (ContentStore) contentStoreObj; 58 Object searcherObj = jobData.get("searcher"); 60 if (searcherObj == null || !(searcherObj instanceof SearchService)) 61 { 62 throw new AlfrescoRuntimeException( 63 "ContentStoreCleanupJob data must contain valid 'searcher' reference"); 64 } 65 SearchService searcher = (SearchService) searcherObj; 66 Object protectHoursObj = jobData.get("protectHours"); 68 if (protectHoursObj == null || !(protectHoursObj instanceof String )) 69 { 70 throw new AlfrescoRuntimeException( 71 "ContentStoreCleanupJob data must contain valid 'protectHours' value"); 72 } 73 long protectHours = 24L; 74 try 75 { 76 protectHours = Long.parseLong((String ) protectHoursObj); 77 } 78 catch (NumberFormatException e) 79 { 80 throw new AlfrescoRuntimeException( 81 "ContentStoreCleanupJob data 'protectHours' value is not a valid integer"); 82 } 83 84 long protectMillis = protectHours * 3600L * 1000L; long now = System.currentTimeMillis(); 86 long lastModifiedSafeTimeMs = (now - protectMillis); 88 List <String > contentUrls = contentStore.listUrls(); 90 for (String contentUrl : contentUrls) 91 { 92 94 ContentReader reader = contentStore.getReader(contentUrl); 96 if (reader == null || !reader.exists()) 97 { 98 continue; 100 } 101 long lastModified = reader.getLastModified(); 102 if (lastModified >= lastModifiedSafeTimeMs) 103 { 104 continue; 106 } 107 108 boolean result = contentStore.delete(contentUrl); 110 System.out.println(contentUrl + ": " + Boolean.toString(result)); 111 } 112 113 throw new UnsupportedOperationException (); 116 } 117 } 118 | Popular Tags |