KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > repo > action > AsynchronousActionExecutionQueueImpl


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.repo.action;
18
19 import java.util.Set JavaDoc;
20 import java.util.concurrent.ArrayBlockingQueue JavaDoc;
21 import java.util.concurrent.ThreadPoolExecutor JavaDoc;
22 import java.util.concurrent.TimeUnit JavaDoc;
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 /**
32  * The asynchronous action execution queue implementation
33  *
34  * @author Roy Wetherall
35  */

36 public class AsynchronousActionExecutionQueueImpl extends ThreadPoolExecutor JavaDoc implements
37         AsynchronousActionExecutionQueue
38 {
39     /**
40      * Default pool values
41      */

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 JavaDoc TIME_UNIT = TimeUnit.SECONDS;
49
50     private static final int MAX_QUEUE_SIZE = 500;
51
52     /**
53      * The transaction service
54      */

55     private TransactionService transactionService;
56
57     /**
58      * The authentication component
59      */

60     private AuthenticationComponent authenticationComponent;
61
62     /**
63      * Default constructor
64      */

65     public AsynchronousActionExecutionQueueImpl()
66     {
67         super(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE, TIME_UNIT, new ArrayBlockingQueue JavaDoc<Runnable JavaDoc>(MAX_QUEUE_SIZE,
68                 true));
69     }
70
71     /**
72      * Set the transaction service
73      *
74      * @param transactionService
75      * the transaction service
76      */

77     public void setTransactionService(TransactionService transactionService)
78     {
79         this.transactionService = transactionService;
80     }
81
82     /**
83      * Set the authentication component
84      *
85      * @param authenticationComponent
86      * the authentication component
87      */

88     public void setAuthenticationComponent(AuthenticationComponent authenticationComponent)
89     {
90         this.authenticationComponent = authenticationComponent;
91     }
92
93     /**
94      * @see org.alfresco.repo.action.AsynchronousActionExecutionQueue#executeAction(org.alfresco.service.cmr.repository.NodeRef,
95      * org.alfresco.service.cmr.action.Action, boolean)
96      */

97     public void executeAction(RuntimeActionService actionService, Action action, NodeRef actionedUponNodeRef,
98             boolean checkConditions, Set JavaDoc<String JavaDoc> actionChain)
99     {
100         executeAction(actionService, action, actionedUponNodeRef, checkConditions, actionChain, null);
101     }
102
103     /**
104      * @see org.alfresco.repo.action.AsynchronousActionExecutionQueue#executeAction(org.alfresco.service.cmr.repository.NodeRef,
105      * org.alfresco.service.cmr.action.Action, boolean,
106      * org.alfresco.service.cmr.repository.NodeRef)
107      */

108     public void executeAction(RuntimeActionService actionService, Action action, NodeRef actionedUponNodeRef,
109             boolean checkConditions, Set JavaDoc<String JavaDoc> actionChain, NodeRef actionExecutionHistoryNodeRef)
110     {
111         execute(new ActionExecutionWrapper(actionService, transactionService, authenticationComponent, action,
112                 actionedUponNodeRef, checkConditions, actionExecutionHistoryNodeRef, actionChain));
113     }
114
115     /**
116      * @see java.util.concurrent.ThreadPoolExecutor#beforeExecute(java.lang.Thread,
117      * java.lang.Runnable)
118      */

119     @Override JavaDoc
120     protected void beforeExecute(Thread JavaDoc thread, Runnable JavaDoc runnable)
121     {
122         super.beforeExecute(thread, runnable);
123     }
124
125     /**
126      * @see java.util.concurrent.ThreadPoolExecutor#afterExecute(java.lang.Runnable,
127      * java.lang.Throwable)
128      */

129     @Override JavaDoc
130     protected void afterExecute(Runnable JavaDoc thread, Throwable JavaDoc runnable)
131     {
132         super.afterExecute(thread, runnable);
133     }
134
135     /**
136      * Runnable class to wrap the execution of the action.
137      */

138     private class ActionExecutionWrapper implements Runnable JavaDoc
139     {
140         /**
141          * Runtime action service
142          */

143         private RuntimeActionService actionService;
144
145         /**
146          * The transaction service
147          */

148         private TransactionService transactionService;
149
150         /**
151          * The authentication component
152          */

153         private AuthenticationComponent authenticationComponent;
154
155         /**
156          * The action
157          */

158         private Action action;
159
160         /**
161          * The actioned upon node reference
162          */

163         private NodeRef actionedUponNodeRef;
164
165         /**
166          * The check conditions value
167          */

168         private boolean checkConditions;
169
170         /**
171          * The action execution history node reference
172          */

173         private NodeRef actionExecutionHistoryNodeRef;
174
175         /**
176          * The action chain
177          */

178         private Set JavaDoc<String JavaDoc> actionChain;
179
180         /**
181          * Constructor
182          *
183          * @param actionService
184          * @param transactionService
185          * @param authenticationComponent
186          * @param action
187          * @param actionedUponNodeRef
188          * @param checkConditions
189          * @param actionExecutionHistoryNodeRef
190          */

191         public ActionExecutionWrapper(RuntimeActionService actionService, TransactionService transactionService,
192                 AuthenticationComponent authenticationComponent, Action action, NodeRef actionedUponNodeRef,
193                 boolean checkConditions, NodeRef actionExecutionHistoryNodeRef, Set JavaDoc<String JavaDoc> 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         /**
206          * Get the action
207          *
208          * @return the action
209          */

210         public Action getAction()
211         {
212             return this.action;
213         }
214
215         /**
216          * Get the actioned upon node reference
217          *
218          * @return the actioned upon node reference
219          */

220         public NodeRef getActionedUponNodeRef()
221         {
222             return this.actionedUponNodeRef;
223         }
224
225         /**
226          * Get the check conditions value
227          *
228          * @return the check conditions value
229          */

230         public boolean getCheckCondtions()
231         {
232             return this.checkConditions;
233         }
234
235         /**
236          * Get the action execution history node reference
237          *
238          * @return the action execution history node reference
239          */

240         public NodeRef getActionExecutionHistoryNodeRef()
241         {
242             return this.actionExecutionHistoryNodeRef;
243         }
244
245         /**
246          * Get the action chain
247          *
248          * @return the action chain
249          */

250         public Set JavaDoc<String JavaDoc> getActionChain()
251         {
252             return actionChain;
253         }
254
255         /**
256          * Executes the action via the action runtime service
257          *
258          * @see java.lang.Runnable#run()
259          */

260         public void run()
261         {
262             try
263             {
264                 // Get the run as user name
265
final String JavaDoc 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 JavaDoc>()
277                             {
278                                 public Object JavaDoc 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 JavaDoc exception)
296             {
297                 exception.printStackTrace();
298             }
299         }
300     }
301 }
302
Popular Tags