-
Notifications
You must be signed in to change notification settings - Fork 78
/
ActionServiceImpl.java
1898 lines (1683 loc) · 73.7 KB
/
ActionServiceImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2022 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
* the paid license agreement will prevail. Otherwise, the software is
* provided under the following open source license terms:
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.repo.action;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.action.access.ActionAccessRestriction;
import org.alfresco.repo.action.evaluator.ActionConditionEvaluator;
import org.alfresco.repo.action.executer.ActionExecuter;
import org.alfresco.repo.action.executer.CompositeActionExecuter;
import org.alfresco.repo.action.executer.LoggingAwareExecuter;
import org.alfresco.repo.copy.CopyBehaviourCallback;
import org.alfresco.repo.copy.CopyDetails;
import org.alfresco.repo.copy.CopyServicePolicies;
import org.alfresco.repo.copy.DefaultCopyBehaviourCallback;
import org.alfresco.repo.policy.JavaBehaviour;
import org.alfresco.repo.policy.PolicyComponent;
import org.alfresco.repo.security.authentication.AuthenticationContext;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.repo.tenant.TenantUtil;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ActionCondition;
import org.alfresco.service.cmr.action.ActionConditionDefinition;
import org.alfresco.service.cmr.action.ActionDefinition;
import org.alfresco.service.cmr.action.ActionList;
import org.alfresco.service.cmr.action.ActionService;
import org.alfresco.service.cmr.action.ActionServiceException;
import org.alfresco.service.cmr.action.ActionServiceTransientException;
import org.alfresco.service.cmr.action.ActionStatus;
import org.alfresco.service.cmr.action.ActionTrackingService;
import org.alfresco.service.cmr.action.CompositeAction;
import org.alfresco.service.cmr.action.CompositeActionCondition;
import org.alfresco.service.cmr.action.ParameterConstraint;
import org.alfresco.service.cmr.action.ParameterizedItem;
import org.alfresco.service.cmr.dictionary.DictionaryService;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.namespace.DynamicNamespacePrefixResolver;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.namespace.RegexQNamePattern;
import org.alfresco.util.GUID;
import org.alfresco.util.PropertyCheck;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
/**
* Action service implementation
*
* @author Roy Wetherall
*/
public class ActionServiceImpl implements ActionService, RuntimeActionService, ApplicationContextAware,
CopyServicePolicies.OnCopyNodePolicy, CopyServicePolicies.OnCopyCompletePolicy
{
/**
* Transaction resource name
*/
private static final String POST_TRANSACTION_PENDING_ACTIONS = "postTransactionPendingActions";
/**
* Error message
*/
private static final String ERR_FAIL = "The action failed to execute due to an error.";
/**
* The logger
*/
private static Log logger = LogFactory.getLog(ActionServiceImpl.class);
/**
* Thread local containing the current action chain
*/
ThreadLocal<Set<String>> currentActionChain = new ThreadLocal<Set<String>>();
/**
* The application context used to retrieve defined actions
*/
private ApplicationContext applicationContext;
private NodeService nodeService;
private SearchService searchService;
private DictionaryService dictionaryService;
private AuthenticationContext authenticationContext;
private ActionTrackingService actionTrackingService;
private PolicyComponent policyComponent;
private ActionServiceMonitor monitor;
/**
* The asynchronous action execution queues map of name, queue
*/
private Map<String, AsynchronousActionExecutionQueue> asynchronousActionExecutionQueues = new HashMap<String, AsynchronousActionExecutionQueue>();
/**
* Action transaction listener
*/
private ActionTransactionListener transactionListener = new ActionTransactionListener(this);
/**
* All the condition definitions currently registered
*/
private Map<String, ActionConditionDefinition> conditionDefinitions = new HashMap<String, ActionConditionDefinition>();
/**
* All the action definitions currently registered
*/
private Map<String, ActionDefinition> actionDefinitions = new HashMap<String, ActionDefinition>();
/**
* Action access restricted executers
*/
private Map<String, ActionExecuter> actionExecuters = new HashMap<>();
/**
* All the parameter constraints
*/
private Map<String, ParameterConstraint> parameterConstraints = new HashMap<String, ParameterConstraint>();
/**
* Set the application context
*
* @param applicationContext the application context
*/
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException
{
this.applicationContext = applicationContext;
}
/**
* Set the node service
*
* @param nodeService the node service
*/
public void setNodeService(NodeService nodeService)
{
this.nodeService = nodeService;
}
/**
* Set the search service
*
* @param searchService the search service
*/
public void setSearchService(SearchService searchService)
{
this.searchService = searchService;
}
/**
* Set the authentication component
*
* @param authenticationContext the authentication component
*/
public void setAuthenticationContext(AuthenticationContext authenticationContext)
{
this.authenticationContext = authenticationContext;
}
/**
* Set the action tracking service
*
* @param actionTrackingService the action tracking service
*/
public void setActionTrackingService(ActionTrackingService actionTrackingService)
{
this.actionTrackingService = actionTrackingService;
}
/**
* Set the dictionary service
*
* @param dictionaryService the dictionary service
*/
public void setDictionaryService(DictionaryService dictionaryService)
{
this.dictionaryService = dictionaryService;
}
/**
* @param policyComponent used to set up the action-based policy behaviour
*/
public void setPolicyComponent(PolicyComponent policyComponent)
{
this.policyComponent = policyComponent;
}
/**
* @param monitor used to monitor running actions and execution times
*/
public void setMonitor(ActionServiceMonitor monitor)
{
this.monitor = monitor;
}
/**
* Set the asynchronous action execution queues
*
* @param asynchronousActionExecutionQueues the asynchronous action execution
* queues
* @deprecated Rather than inject a map, it is
* preferable to inject individual {@link AsynchronousActionExecutionQueue} instances
* during bean initialisation in a spring init-method.
*/
public void setAsynchronousActionExecutionQueues(
Map<String, AsynchronousActionExecutionQueue> asynchronousActionExecutionQueues)
{
this.asynchronousActionExecutionQueues = asynchronousActionExecutionQueues;
}
/**
* This method registers an {@link AsynchronousActionExecutionQueue} with the {@link ActionService}.
* @param key String
* @param asyncExecQueue AsynchronousActionExecutionQueue
* @since Thor Phase 2 Sprint 2
*/
public void registerAsynchronousActionExecutionQueue(String key, AsynchronousActionExecutionQueue asyncExecQueue)
{
this.asynchronousActionExecutionQueues.put(key, asyncExecQueue);
}
public void init()
{
PropertyCheck.mandatory(this, "policyComponent", policyComponent);
this.policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "getCopyCallback"),
ActionModel.TYPE_ACTION_PARAMETER, new JavaBehaviour(this, "getCopyCallback"));
this.policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "onCopyComplete"),
ActionModel.TYPE_ACTION_PARAMETER, new JavaBehaviour(this, "onCopyComplete"));
}
/**
* Gets the saved action folder reference
*
* @param nodeRef the node reference
* @return the node reference
*/
private NodeRef getSavedActionFolderRef(NodeRef nodeRef)
{
List<ChildAssociationRef> assocs = this.nodeService.getChildAssocs(nodeRef, RegexQNamePattern.MATCH_ALL,
ActionModel.ASSOC_ACTION_FOLDER);
if (assocs.size() != 1)
{
throw new ActionServiceException("Unable to retrieve the saved action folder reference.");
}
return assocs.get(0).getChildRef();
}
/**
* @see org.alfresco.service.cmr.action.ActionService#getActionDefinition(java.lang.String)
*/
public ActionDefinition getActionDefinition(String name)
{
// get direct access to action definition (i.e. ignoring public flag of
// executer)
ActionDefinition definition = null;
Object bean = this.applicationContext.getBean(name);
if (bean != null && bean instanceof ActionExecuter)
{
ActionExecuter executer = (ActionExecuter) bean;
definition = executer.getActionDefinition();
}
return definition;
}
/**
* @see org.alfresco.service.cmr.action.ActionService#getActionDefinitions()
*/
public List<ActionDefinition> getActionDefinitions()
{
return new ArrayList<>(this.actionDefinitions.values());
}
/**
* @see org.alfresco.service.cmr.action.ActionService#getActionDefinitions(org.alfresco.service.cmr.repository.NodeRef)
*/
public List<ActionDefinition> getActionDefinitions(NodeRef nodeRef)
{
if (nodeRef == null)
{
return getActionDefinitions();
}
else
{
// TODO for now we will only filter by type, we will introduce
// filtering by aspect later
QName nodeType = this.nodeService.getType(nodeRef);
List<ActionDefinition> result = new ArrayList<ActionDefinition>();
for (ActionDefinition actionDefinition : getActionDefinitions())
{
Set<QName> applicableTypes = actionDefinition.getApplicableTypes();
if (applicableTypes != null && applicableTypes.isEmpty() == false)
{
// First do a short-cut check directly against the type
if (applicableTypes.contains(nodeType))
{
result.add(actionDefinition);
}
else
{
// Have to do a check for all subtypes of the applicable types
for (QName applicableType : actionDefinition.getApplicableTypes())
{
if (this.dictionaryService.isSubClass(nodeType, applicableType))
{
result.add(actionDefinition);
break;
}
}
}
}
else
{
result.add(actionDefinition);
}
}
return result;
}
}
/**
* @see org.alfresco.service.cmr.action.ActionService#getActionConditionDefinition(java.lang.String)
*/
public ActionConditionDefinition getActionConditionDefinition(String name)
{
return this.conditionDefinitions.get(name);
}
/**
* @see org.alfresco.service.cmr.action.ActionService#getActionConditionDefinitions()
*/
public List<ActionConditionDefinition> getActionConditionDefinitions()
{
return new ArrayList<ActionConditionDefinition>(this.conditionDefinitions.values());
}
/**
* @see org.alfresco.service.cmr.action.ActionService#getParameterConstraint(java.lang.String)
*/
public ParameterConstraint getParameterConstraint(String name)
{
return this.parameterConstraints.get(name);
}
/**
* @see org.alfresco.service.cmr.action.ActionService#getParameterConstraints()
*/
public List<ParameterConstraint> getParameterConstraints()
{
return new ArrayList<ParameterConstraint>(this.parameterConstraints.values());
}
/**
* @see org.alfresco.service.cmr.action.ActionService#createActionCondition(java.lang.String)
*/
public ActionCondition createActionCondition(String name)
{
if (logger.isDebugEnabled())
logger.debug("Creating Action Condition - [" + name + "]");
if (CompositeActionCondition.COMPOSITE_CONDITION.equals(name))
{
return new CompositeActionConditionImpl(GUID.generate());
}
return new ActionConditionImpl(GUID.generate(), name);
}
/**
* @see org.alfresco.service.cmr.action.ActionService#createActionCondition(java.lang.String,
* java.util.Map)
*/
public ActionCondition createActionCondition(String name, Map<String, Serializable> params)
{
ActionCondition condition = createActionCondition(name);
condition.setParameterValues(params);
return condition;
}
/**
* @see org.alfresco.service.cmr.action.ActionService#createAction(String)
*/
public Action createAction(String name)
{
return new ActionImpl(null, GUID.generate(), name, null);
}
/**
* @see org.alfresco.service.cmr.action.ActionService#createAction(java.lang.String,
* java.util.Map)
*/
public Action createAction(String name, Map<String, Serializable> params)
{
Action action = createAction(name);
action.setParameterValues(params);
return action;
}
/**
* @see org.alfresco.service.cmr.action.ActionService#createCompositeAction()
*/
public CompositeAction createCompositeAction()
{
return new CompositeActionImpl(null, GUID.generate());
}
/**
* @see org.alfresco.service.cmr.action.ActionService#createCompositeActionCondition()
*/
public CompositeActionCondition createCompositeActionCondition()
{
return new CompositeActionConditionImpl(GUID.generate());
}
/**
* @see org.alfresco.service.cmr.action.ActionService#evaluateAction(org.alfresco.service.cmr.action.Action,
* org.alfresco.service.cmr.repository.NodeRef)
*/
public boolean evaluateAction(Action action, NodeRef actionedUponNodeRef)
{
boolean result = true;
if (action.hasActionConditions() == true)
{
List<ActionCondition> actionConditions = action.getActionConditions();
for (ActionCondition condition : actionConditions)
{
boolean tempresult = evaluateActionCondition(condition, actionedUponNodeRef);
if (logger.isDebugEnabled())
logger.debug("\tCondition " + condition.getActionConditionDefinitionName() + " Result - "
+ tempresult);
result = result && tempresult;
}
}
if (logger.isDebugEnabled())
logger.debug("\tAll Condition Evaluation Result - " + result);
return result;
}
/**
* Evaluates the actions by finding corresponding actionEvaluators in
* applicationContext (registered through Spring). Composite conditions are
* evaluated here as well. It is also possible to have composite actions
* inside composite actions.
*
* @see org.alfresco.service.cmr.action.ActionService#evaluateActionCondition(org.alfresco.service.cmr.action.ActionCondition,
* org.alfresco.service.cmr.repository.NodeRef)
*/
public boolean evaluateActionCondition(ActionCondition condition, NodeRef actionedUponNodeRef)
{
if (condition instanceof CompositeActionCondition)
{
CompositeActionCondition compositeCondition = (CompositeActionCondition) condition;
if (logger.isDebugEnabled())
{
logger.debug("Evaluating Composite Condition - BOOLEAN CONDITION IS "
+ (compositeCondition.isORCondition() ? "OR" : "AND"));
}
if (!compositeCondition.hasActionConditions())
{
throw new IllegalStateException("CompositeActionCondition has no subconditions.");
}
boolean result;
if (compositeCondition.isORCondition())
{
result = false;
}
else
{
result = true;
}
for (ActionCondition simplecondition : compositeCondition.getActionConditions())
{
if (logger.isDebugEnabled())
{
logger.debug(
"Evaluating composite condition " + simplecondition.getActionConditionDefinitionName());
}
if (compositeCondition.isORCondition())
{
result = result || evaluateSimpleCondition(simplecondition, actionedUponNodeRef);
// Short circuit for the OR condition
if (result)
break;
}
else
{
result = result && evaluateSimpleCondition(simplecondition, actionedUponNodeRef);
// Short circuit for the AND condition
if (!result)
break;
}
}
if (compositeCondition.getInvertCondition())
{
return !result;
}
else
{
return result;
}
}
else
{
return evaluateSimpleCondition(condition, actionedUponNodeRef);
}
}
private boolean evaluateSimpleCondition(ActionCondition condition, NodeRef actionedUponNodeRef)
{
if (logger.isDebugEnabled())
{
logger.debug("Evaluating simple condition " + condition.getActionConditionDefinitionName());
}
// Evaluate the condition
ActionConditionEvaluator evaluator = (ActionConditionEvaluator) this.applicationContext.getBean(condition
.getActionConditionDefinitionName());
return evaluator.evaluate(condition, actionedUponNodeRef);
}
/**
* @see org.alfresco.service.cmr.action.ActionService#executeAction(org.alfresco.service.cmr.action.Action,
* org.alfresco.service.cmr.repository.NodeRef, boolean)
*/
public void executeAction(Action action, NodeRef actionedUponNodeRef, boolean checkConditions)
{
executeAction(action, actionedUponNodeRef, checkConditions, action.getExecuteAsychronously());
}
/**
* @see org.alfresco.service.cmr.action.ActionService#executeAction(org.alfresco.service.cmr.action.Action,
* org.alfresco.service.cmr.repository.NodeRef, boolean)
*/
public void executeAction(Action action, NodeRef actionedUponNodeRef, boolean checkConditions,
boolean executeAsychronously)
{
Set<String> actionChain = this.currentActionChain.get();
// Like emails (see RuleServiceImpl), metadata extraction is now normally performed asynchronously.
// As a result we need to override the executeAsychronously value if this is the case so that
// changes to the actionedUponNodeRef will have been committed before the extract is performed.
executeAsychronously = isExecuteAsynchronously(action, actionedUponNodeRef, executeAsychronously);
if (executeAsychronously == false)
{
executeActionImpl(action, actionedUponNodeRef, checkConditions, false, actionChain);
}
else
{
verifyActionAccessRestrictions(action);
// Add to the post transaction pending action list
addPostTransactionPendingAction(action, actionedUponNodeRef, checkConditions, actionChain);
}
}
/**
* @see RuntimeActionService#verifyActionAccessRestrictions(Action action)
*/
@Override
public void verifyActionAccessRestrictions(Action action) {
getActionExecuter(action.getActionDefinitionName())
.verifyActionAccessRestrictions(action);
}
private ActionExecuter getActionExecuter(String actionDefName) {
if (!actionExecuters.containsKey(actionDefName)) {
actionExecuters.put(actionDefName, applicationContext.getBean(actionDefName, ActionExecuter.class));
}
return actionExecuters.get(actionDefName);
}
private boolean isExecuteAsynchronously(Action action, NodeRef actionedUponNodeRef, boolean executeAsynchronously)
{
if (executeAsynchronously == false)
{
String actionDefinitionName = action.getActionDefinitionName();
if (actionDefinitionName.equals(CompositeActionExecuter.NAME))
{
for (Action subAction : ((CompositeAction)action).getActions())
{
if (isExecuteAsynchronously(subAction, actionedUponNodeRef, false))
{
return true;
}
}
}
else
{
ActionExecuter executer = (ActionExecuter) this.applicationContext.getBean(actionDefinitionName);
executeAsynchronously = executer.isExecuteAsynchronously(actionedUponNodeRef);
}
}
return executeAsynchronously;
}
/**
* called by transaction service.
*/
public void postCommit()
{
for (PendingAction pendingAction : getPostTransactionPendingActions())
{
queueAction(pendingAction);
}
}
/**
*
*/
private void queueAction(PendingAction action)
{
// Get the right queue
AsynchronousActionExecutionQueue queue = getQueue(action.action);
// Queue the action for execution
queue.executeAction(this, action.getAction(), action.getActionedUponNodeRef(), action.getCheckConditions(),
action.getActionChain());
}
/**
* Queue a compensating action for execution against a specific node
*/
private void queueAction(Action compensatingAction, NodeRef actionedUponNodeRef)
{
// Get the right queue
AsynchronousActionExecutionQueue queue = getQueue(compensatingAction);
// Queue the action for execution
queue.executeAction(this, compensatingAction, actionedUponNodeRef, false, null);
}
/**
* Get the queue to use for asynchronous execution of the given action.
*/
private AsynchronousActionExecutionQueue getQueue(Action action)
{
ActionExecuter executer = (ActionExecuter) this.applicationContext.getBean(action.getActionDefinitionName());
AsynchronousActionExecutionQueue queue = null;
String queueName = executer.getQueueName();
if (queueName == null)
{
queue = asynchronousActionExecutionQueues.get("");
}
else
{
queue = asynchronousActionExecutionQueues.get(queueName);
}
if (queue == null)
{
// can't get queue
throw new ActionServiceException("Unable to get AsynchronousActionExecutionQueue name: " + queueName);
}
return queue;
}
/**
* Get whether the action should be tracked by the {@link ActionTrackingService} or not.
*
* @param action the action, which may or may not have any say about the status tracking
* @return <tt>true</tt> if the status must be tracked, otherwise <tt>false</tt>
*/
private boolean getTrackStatus(Action action)
{
Boolean trackStatusManual = action.getTrackStatus();
if (trackStatusManual != null)
{
return trackStatusManual.booleanValue();
}
// The flag has not be changed for the specific action, so use the value from the
// action definition.
ActionDefinition actionDef = getActionDefinition(action.getActionDefinitionName());
if (actionDef == null)
{
return false; // default to 'false' if the definition has disappeared
}
else
{
return actionDef.getTrackStatus();
}
}
@Override
public void executeActionImpl(Action action, NodeRef actionedUponNodeRef, boolean checkConditions,
boolean executedAsynchronously, Set<String> actionChain)
{
if (logger.isDebugEnabled() == true)
{
StringBuilder builder = new StringBuilder("Execute action impl action chain = ");
if (actionChain == null)
{
builder.append("null");
}
else
{
for (String value : actionChain)
{
builder.append(value).append(" ");
}
}
logger.debug(builder.toString());
logger.debug("Current action = " + action.getId());
}
// MNT-15365
if(actionedUponNodeRef != null && !this.nodeService.exists(actionedUponNodeRef))
{
if (logger.isDebugEnabled() == true)
{
logger.debug("Node = " + actionedUponNodeRef.getId() + " skip action because node doesn't exist");
}
return;
}
// get the current user early in case the process fails and we are
// unable to do it later
String currentUserName = this.authenticationContext.getCurrentUserName();
if (actionChain == null || actionChain.contains(action.getId()) == false)
{
if (logger.isDebugEnabled() == true)
{
logger.debug("Doing executeActionImpl");
}
try
{
Set<String> origActionChain = null;
if (actionChain == null)
{
actionChain = new HashSet<String>();
}
else
{
origActionChain = new HashSet<String>(actionChain);
}
actionChain.add(action.getId());
this.currentActionChain.set(actionChain);
if (logger.isDebugEnabled() == true)
{
logger.debug("Adding " + action.getActionDefinitionName() + ", " + action.getId() + " to action chain.");
}
try
{
// Check and execute now
if (checkConditions == false || evaluateAction(action, actionedUponNodeRef) == true)
{
if (getTrackStatus(action))
{
// Mark the action as starting
actionTrackingService.recordActionExecuting(action, actionedUponNodeRef);
}
RunningAction runningAction = monitor.actionStarted(action);
try
{
// Execute the action
directActionExecution(action, actionedUponNodeRef);
}
catch (Throwable e)
{
runningAction.setException(e);
throw e;
}
finally
{
monitor.actionCompleted(runningAction);
}
if (getTrackStatus(action))
{
// Mark it as having worked
actionTrackingService.recordActionComplete(action);
}
}
}
finally
{
if (origActionChain == null)
{
this.currentActionChain.remove();
}
else
{
this.currentActionChain.set(origActionChain);
}
if (logger.isDebugEnabled() == true)
{
logger.debug("Resetting the action chain.");
}
}
}
catch (ActionServiceTransientException transientException)
{
// This is a non-fatal exception which will be recorded as a failed action,
// but which will not lead to the execution of any compensating action
if (getTrackStatus(action))
{
actionTrackingService.recordActionFailure(action, transientException);
}
}
catch (Throwable exception)
{
// DH: No logging of the exception. Leave the logging decision
// to the client code,
// which can handle the rethrown exception.
if (executedAsynchronously == true)
{
// If one is specified, queue the compensating action ready
// for execution
Action compensatingAction = action.getCompensatingAction();
if (compensatingAction != null)
{
// Set the current user & tenant. These should be the same for the primary action and the compensating action.
((ActionImpl) compensatingAction).setRunAsUser(currentUserName);
((ActionImpl) compensatingAction).setTenantId(((ActionImpl)action).getTenantId());
queueAction(compensatingAction, actionedUponNodeRef);
}
}
if (getTrackStatus(action))
{
// Have the failure logged on the action
actionTrackingService.recordActionFailure(action, exception);
}
// Rethrow the exception
if (exception instanceof RuntimeException)
{
throw (RuntimeException) exception;
}
else
{
throw new ActionServiceException(ERR_FAIL, exception);
}
}
}
}
/**
* @see org.alfresco.repo.action.RuntimeActionService#directActionExecution(org.alfresco.service.cmr.action.Action,
* org.alfresco.service.cmr.repository.NodeRef)
*/
public void directActionExecution(Action action, NodeRef actionedUponNodeRef)
{
// Debug output
if (logger.isDebugEnabled())
{
logger.debug("The action is being executed as the user: "
+ this.authenticationContext.getCurrentUserName());
}
// Get the action executer and execute
ActionExecuter executer = (ActionExecuter) this.applicationContext.getBean(action.getActionDefinitionName());
executer.execute(action, actionedUponNodeRef);
}
/**
* @see org.alfresco.service.cmr.action.ActionService#executeAction(org.alfresco.service.cmr.action.Action,
* NodeRef)
*/
public void executeAction(Action action, NodeRef actionedUponNodeRef)
{
executeAction(action, actionedUponNodeRef, true);
}
/**
* @see org.alfresco.repo.action.RuntimeActionService#registerActionConditionEvaluator(org.alfresco.repo.action.evaluator.ActionConditionEvaluator)
*/
public void registerActionConditionEvaluator(ActionConditionEvaluator actionConditionEvaluator)
{
ActionConditionDefinition cond = actionConditionEvaluator.getActionConditionDefintion();
this.conditionDefinitions.put(cond.getName(), cond);
}
/**
* @see org.alfresco.repo.action.RuntimeActionService#registerActionExecuter(org.alfresco.repo.action.executer.ActionExecuter)
*/
public void registerActionExecuter(ActionExecuter actionExecuter)
{
ActionDefinition action = actionExecuter.getActionDefinition();
this.actionDefinitions.put(action.getName(), action);
}
/**
* @see org.alfresco.repo.action.RuntimeActionService#registerParameterConstraint(org.alfresco.service.cmr.action.ParameterConstraint)
*/
public void registerParameterConstraint(ParameterConstraint parameterConstraint)
{
this.parameterConstraints.put(parameterConstraint.getName(), parameterConstraint);
}
/**
* Gets the action node ref from the action id
*
* @param nodeRef the node reference
* @param actionId the action id
* @return the action node reference
*/
private NodeRef getActionNodeRefFromId(NodeRef nodeRef, String actionId)
{
NodeRef result = null;
if (this.nodeService.hasAspect(nodeRef, ActionModel.ASPECT_ACTIONS) == true)
{
DynamicNamespacePrefixResolver namespacePrefixResolver = new DynamicNamespacePrefixResolver();
namespacePrefixResolver.registerNamespace(NamespaceService.SYSTEM_MODEL_PREFIX,
NamespaceService.SYSTEM_MODEL_1_0_URI);
List<NodeRef> nodeRefs = searchService.selectNodes(getSavedActionFolderRef(nodeRef), "*[@sys:"
+ ContentModel.PROP_NODE_UUID.getLocalName() + "='" + actionId + "']", null,
namespacePrefixResolver, false);
if (nodeRefs.size() != 0)
{
result = nodeRefs.get(0);
}
}
return result;
}
/**
* @see org.alfresco.service.cmr.action.ActionService#saveAction(org.alfresco.service.cmr.repository.NodeRef,
* org.alfresco.service.cmr.action.Action)
*/
public void saveAction(NodeRef nodeRef, Action action)
{
NodeRef actionNodeRef = getActionNodeRefFromId(nodeRef, action.getId());
if (actionNodeRef == null)
{
if (this.nodeService.hasAspect(nodeRef, ActionModel.ASPECT_ACTIONS) == false)
{
// Apply the actionable aspect
this.nodeService.addAspect(nodeRef, ActionModel.ASPECT_ACTIONS, null);
}
// Create the action and reference
actionNodeRef = createActionNodeRef(action, getSavedActionFolderRef(nodeRef), ContentModel.ASSOC_CONTAINS,
ActionModel.ASSOC_NAME_ACTIONS);
}
saveActionImpl(actionNodeRef, action);
}
public NodeRef createActionNodeRef(Action action, NodeRef parentNodeRef, QName assocTypeName, QName assocName)
{