Commit 45550442922802a0fd1d62ca68bdb3b27d1a20fc

Authored by Rui Fernandes
1 parent ff6672dc76
Exists in master

configuration

src/alfresco/extension/trashcan-cleaner-context.xml
1 <?xml version='1.0' encoding='UTF-8'?> 1 <?xml version='1.0' encoding='UTF-8'?>
2 <!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'> 2 <!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
3 3
4 <beans> 4 <beans>
5 <bean name="trashcanCleaner" class="org.springframework.scheduling.quartz.JobDetailBean"> 5
6 <property name="jobClass" value="org.alfresco.trashcan.TrashcanCleanerJob" /> 6 <bean id="trashcanCleaner" class="org.alfresco.util.CronTriggerBean">
7 <property name="jobDataAsMap"> 7 <property name="jobDetail">
8 <bean id="tempFileCleanerJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean">
9 <property name="jobClass">
10 <value>org.alfresco.trashcan.TrashcanCleanerJob</value>
11 </property>
12 <property name="jobDataAsMap">
8 <map> 13 <map>
9 <entry key="nodeRegistry" value-ref="nodeService" /> 14 <entry key="nodeService" value-ref="nodeService" />
10 <entry key="transactionService" value-ref="transactionService" /> 15 <entry key="transactionService" value-ref="transactionService" />
11 <entry key="authenticationComponent" value-ref="authenticationComponent" /> 16 <entry key="authenticationComponent" value-ref="authenticationComponent" />
12 <entry key="jobLockService" value-ref="jobLockService" /> 17 <entry key="jobLockService" value-ref="jobLockService" />
13 <entry key="trashcan.daysToKeep" value="${trashcan.daysToKeep}" /> 18 <entry key="trashcan.daysToKeep" value="${trashcan.daysToKeep}" />
14 <entry key="trashcan.deleteBatchCount" value="${trashcan.deleteBatchCount}" /> 19 <entry key="trashcan.deleteBatchCount" value="${trashcan.deleteBatchCount}" />
15 </map> 20 </map>
16 </property> 21 </property>
17 </bean>
18
19 <bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
20 <property name="jobDetail" ref="trashcanCleaner" />
21 <!-- Repeat hourly on the half hour -->
22 <property name="cronExpression">
23 <value>${trashcan.cron}</value>
24 </property>
25 </bean>
26 </beans>
22 </bean>
src/org/alfresco/trashcan/TrashcanCleanerJob.java
1 package org.alfresco.trashcan; 1 package org.alfresco.trashcan;
2 2
3 import org.alfresco.repo.security.authentication.AuthenticationComponent; 3 import org.alfresco.repo.security.authentication.AuthenticationComponent;
4 import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback; 4 import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
5 import org.alfresco.schedule.AbstractScheduledLockedJob; 5 import org.alfresco.schedule.AbstractScheduledLockedJob;
6 import org.alfresco.service.cmr.repository.NodeService; 6 import org.alfresco.service.cmr.repository.NodeService;
7 import org.alfresco.service.transaction.TransactionService; 7 import org.alfresco.service.transaction.TransactionService;
8 import org.quartz.JobExecutionContext; 8 import org.quartz.JobExecutionContext;
9 import org.quartz.JobExecutionException; 9 import org.quartz.JobExecutionException;
10 10
11 /** 11 /**
12 * 12 *
13 * @author rjmfernandes@gmail.com 13 * @author rjmfernandes@gmail.com
14 * 14 *
15 */ 15 */
16 public class TrashcanCleanerJob extends AbstractScheduledLockedJob { 16 public class TrashcanCleanerJob extends AbstractScheduledLockedJob {
17 17
18 protected NodeService nodeService; 18 protected NodeService nodeService;
19 protected TransactionService transactionService; 19 protected TransactionService transactionService;
20 protected AuthenticationComponent authenticationComponent; 20 protected AuthenticationComponent authenticationComponent;
21 private int deleteBatchCount = 1000; 21 private int deleteBatchCount = 1000;
22 private int daysToKeep = -1; 22 private int daysToKeep = -1;
23 23
24 @Override 24 @Override
25 public void executeJob(JobExecutionContext jobContext) 25 public void executeJob(JobExecutionContext jobContext)
26 throws JobExecutionException { 26 throws JobExecutionException {
27 setUp(jobContext); 27 setUp(jobContext);
28 authenticationComponent.setSystemUserAsCurrentUser(); 28 authenticationComponent.setSystemUserAsCurrentUser();
29 cleanInTransaction(); 29 cleanInTransaction();
30 } 30 }
31 31
32 private void cleanInTransaction() { 32 private void cleanInTransaction() {
33 RetryingTransactionCallback<Object> txnWork = new RetryingTransactionCallback<Object>() { 33 RetryingTransactionCallback<Object> txnWork = new RetryingTransactionCallback<Object>() {
34 public Object execute() throws Exception { 34 public Object execute() throws Exception {
35 TrashcanCleaner cleaner = new TrashcanCleaner(nodeService); 35 TrashcanCleaner cleaner = new TrashcanCleaner(nodeService);
36 cleaner.clean(); 36 cleaner.clean();
37 return null; 37 return null;
38 } 38 }
39 }; 39 };
40 transactionService.getRetryingTransactionHelper().doInTransaction( 40 transactionService.getRetryingTransactionHelper().doInTransaction(
41 txnWork); 41 txnWork);
42 } 42 }
43 43
44 private void setUp(JobExecutionContext jobContext) { 44 private void setUp(JobExecutionContext jobContext) {
45 nodeService = (NodeService) jobContext.getJobDetail().getJobDataMap() 45 nodeService = (NodeService) jobContext.getJobDetail().getJobDataMap()
46 .get("nodeService"); 46 .get("nodeService");
47 transactionService = (TransactionService) jobContext.getJobDetail() 47 transactionService = (TransactionService) jobContext.getJobDetail()
48 .getJobDataMap().get("transactionService"); 48 .getJobDataMap().get("transactionService");
49 authenticationComponent = (AuthenticationComponent) jobContext 49 authenticationComponent = (AuthenticationComponent) jobContext
50 .getJobDetail().getJobDataMap().get("authenticationComponent"); 50 .getJobDetail().getJobDataMap().get("authenticationComponent");
51 Integer c_daysToKeep = (Integer) jobContext.getJobDetail() 51 daysToKeep = getSetupValue("trashcan.daysToKeep", daysToKeep,
52 .getJobDataMap().get("trashcan.daysToKeep"); 52 jobContext);
53 Integer c_deleteBatchCount = (Integer) jobContext.getJobDetail() 53 deleteBatchCount = getSetupValue("trashcan.deleteBatchCount",
54 .getJobDataMap().get("trashcan.deleteBatchCount"); 54 deleteBatchCount, jobContext);
55 daysToKeep = c_daysToKeep != null ? c_daysToKeep : daysToKeep;
56 deleteBatchCount = c_deleteBatchCount != null ? c_deleteBatchCount
57 : deleteBatchCount;
58 55
59 } 56 }
60 57
58 private static int getSetupValue(String parameterName, int defaultValue,
59 JobExecutionContext jobContext) {
60 String parameterValue = (String) jobContext.getJobDetail()
61 .getJobDataMap().get(parameterName);
62 return parameterValue != null && !parameterValue.trim().equals("") ? Integer
63 .parseInt(parameterValue)
64 : defaultValue;
65 }