1 17 package org.alfresco.repo.action; 18 19 import java.util.Set ; 20 import java.util.concurrent.ArrayBlockingQueue ; 21 import java.util.concurrent.ThreadPoolExecutor ; 22 import java.util.concurrent.TimeUnit ; 23 24 import org.alfresco.repo.security.authentication.AuthenticationComponent; 25 import org.alfresco.repo.transaction.TransactionUtil; 26 import org.alfresco.service.cmr.action.Action; 27 import org.alfresco.service.cmr.action.ActionServiceException; 28 import org.alfresco.service.cmr.repository.NodeRef; 29 import org.alfresco.service.transaction.TransactionService; 30 31 36 public class AsynchronousActionExecutionQueueImpl extends ThreadPoolExecutor implements 37 AsynchronousActionExecutionQueue 38 { 39 42 private static final int CORE_POOL_SIZE = 2; 43 44 private static final int MAX_POOL_SIZE = 5; 45 46 private static final long KEEP_ALIVE = 30; 47 48 private static final TimeUnit TIME_UNIT = TimeUnit.SECONDS; 49 50 private static final int MAX_QUEUE_SIZE = 500; 51 52 55 private TransactionService transactionService; 56 57 60 private AuthenticationComponent authenticationComponent; 61 62 65 public AsynchronousActionExecutionQueueImpl() 66 { 67 super(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE, TIME_UNIT, new ArrayBlockingQueue <Runnable >(MAX_QUEUE_SIZE, 68 true)); 69 } 70 71 77 public void setTransactionService(TransactionService transactionService) 78 { 79 this.transactionService = transactionService; 80 } 81 82 88 public void setAuthenticationComponent(AuthenticationComponent authenticationComponent) 89 { 90 this.authenticationComponent = authenticationComponent; 91 } 92 93 97 public void executeAction(RuntimeActionService actionService, Action action, NodeRef actionedUponNodeRef, 98 boolean checkConditions, Set <String > actionChain) 99 { 100 executeAction(actionService, action, actionedUponNodeRef, checkConditions, actionChain, null); 101 } 102 103 108 public void executeAction(RuntimeActionService actionService, Action action, NodeRef actionedUponNodeRef, 109 boolean checkConditions, Set <String > actionChain, NodeRef actionExecutionHistoryNodeRef) 110 { 111 execute(new ActionExecutionWrapper(actionService, transactionService, authenticationComponent, action, 112 actionedUponNodeRef, checkConditions, actionExecutionHistoryNodeRef, actionChain)); 113 } 114 115 119 @Override 120 protected void beforeExecute(Thread thread, Runnable runnable) 121 { 122 super.beforeExecute(thread, runnable); 123 } 124 125 129 @Override 130 protected void afterExecute(Runnable thread, Throwable runnable) 131 { 132 super.afterExecute(thread, runnable); 133 } 134 135 138 private class ActionExecutionWrapper implements Runnable 139 { 140 143 private RuntimeActionService actionService; 144 145 148 private TransactionService transactionService; 149 150 153 private AuthenticationComponent authenticationComponent; 154 155 158 private Action action; 159 160 163 private NodeRef actionedUponNodeRef; 164 165 168 private boolean checkConditions; 169 170 173 private NodeRef actionExecutionHistoryNodeRef; 174 175 178 private Set <String > actionChain; 179 180 191 public ActionExecutionWrapper(RuntimeActionService actionService, TransactionService transactionService, 192 AuthenticationComponent authenticationComponent, Action action, NodeRef actionedUponNodeRef, 193 boolean checkConditions, NodeRef actionExecutionHistoryNodeRef, Set <String > actionChain) 194 { 195 this.actionService = actionService; 196 this.transactionService = transactionService; 197 this.authenticationComponent = authenticationComponent; 198 this.actionedUponNodeRef = actionedUponNodeRef; 199 this.action = action; 200 this.checkConditions = checkConditions; 201 this.actionExecutionHistoryNodeRef = actionExecutionHistoryNodeRef; 202 this.actionChain = actionChain; 203 } 204 205 210 public Action getAction() 211 { 212 return this.action; 213 } 214 215 220 public NodeRef getActionedUponNodeRef() 221 { 222 return this.actionedUponNodeRef; 223 } 224 225 230 public boolean getCheckCondtions() 231 { 232 return this.checkConditions; 233 } 234 235 240 public NodeRef getActionExecutionHistoryNodeRef() 241 { 242 return this.actionExecutionHistoryNodeRef; 243 } 244 245 250 public Set <String > getActionChain() 251 { 252 return actionChain; 253 } 254 255 260 public void run() 261 { 262 try 263 { 264 final String userName = ((ActionImpl)ActionExecutionWrapper.this.action).getRunAsUser(); 266 if (userName == null) 267 { 268 throw new ActionServiceException("Cannot execute action asynchronously since run as user is 'null'"); 269 } 270 271 ActionExecutionWrapper.this.authenticationComponent.setCurrentUser(userName); 272 273 try 274 { 275 TransactionUtil.executeInNonPropagatingUserTransaction(this.transactionService, 276 new TransactionUtil.TransactionWork<Object >() 277 { 278 public Object doWork() 279 { 280 ActionExecutionWrapper.this.actionService.executeActionImpl( 281 ActionExecutionWrapper.this.action, 282 ActionExecutionWrapper.this.actionedUponNodeRef, 283 ActionExecutionWrapper.this.checkConditions, true, 284 ActionExecutionWrapper.this.actionChain); 285 286 return null; 287 } 288 }); 289 } 290 finally 291 { 292 ActionExecutionWrapper.this.authenticationComponent.clearCurrentSecurityContext(); 293 } 294 } 295 catch (Throwable exception) 296 { 297 exception.printStackTrace(); 298 } 299 } 300 } 301 } 302 | Popular Tags |