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
|
/*****************************************************************************
* Copyright (c) 2015 Christian W. Damus and others.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Christian W. Damus - Initial API and implementation
*
*****************************************************************************/
package org.eclipse.papyrus.infra.core.utils;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eclipse.emf.common.command.Command;
import org.eclipse.emf.transaction.RecordingCommand;
import org.eclipse.emf.transaction.Transaction;
import org.eclipse.emf.transaction.TransactionalEditingDomain;
import org.eclipse.emf.transaction.TransactionalEditingDomainEvent;
import org.eclipse.emf.transaction.TransactionalEditingDomainListener;
import org.eclipse.emf.transaction.impl.EMFCommandTransaction;
import org.eclipse.emf.transaction.impl.InternalTransactionalCommandStack;
import org.eclipse.emf.transaction.util.TransactionUtil;
import org.eclipse.papyrus.infra.core.Activator;
import com.google.common.collect.Maps;
/**
* An {@link Executor} that executes {@link Runnable}s at the pre-commit phase of the active
* write transaction of a {@link TransactionalEditingDomain} or at some other time if no
* write transaction is active.
*/
class TransactionPrecommitExecutor implements Executor, TransactionalEditingDomainListener {
private final Executor fallback;
private final AtomicBoolean writeActive = new AtomicBoolean();
private final BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>();
private final Map<?, ?> options;
TransactionPrecommitExecutor(TransactionalEditingDomain domain, Executor fallback, Map<?, ?> options) {
super();
this.fallback = fallback;
this.options = ((options != null) && options.isEmpty()) ? null : options;
TransactionUtil.getAdapter(domain, TransactionalEditingDomain.Lifecycle.class).addTransactionalEditingDomainListener(this);
}
@Override
public void execute(Runnable command) {
if (writeActive.get()) {
queue.offer(command);
} else {
fallback.execute(command);
}
}
//
// Editing Domain Lifecycle handling
//
@Override
public void editingDomainDisposing(TransactionalEditingDomainEvent event) {
queue.clear();
}
//
// Transaction lifecycle handling
//
@Override
public void transactionStarted(TransactionalEditingDomainEvent event) {
writeActive.set(!event.getTransaction().isReadOnly());
}
@Override
public void transactionClosed(TransactionalEditingDomainEvent event) {
writeActive.set(false);
if (queue.peek() != null) {
// Punt the remaining tasks
for (Runnable next = queue.poll(); next != null; next = queue.poll()) {
fallback.execute(next);
}
}
}
@Override
public void transactionStarting(TransactionalEditingDomainEvent event) {
// Pass
}
@Override
public void transactionInterrupted(TransactionalEditingDomainEvent event) {
// Pass
}
@Override
public void transactionClosing(TransactionalEditingDomainEvent event) {
if (queue.peek() != null) {
// Inject tasks into the transaction as a trigger command
Command trigger = new RecordingCommand(event.getSource(), "Deferred Tasks") {
@Override
protected void doExecute() {
for (Runnable next = queue.poll(); next != null; next = queue.poll()) {
try {
next.run();
} catch (Exception e) {
Activator.log.error("Uncaught exception in transaction pre-commit task.", e); //$NON-NLS-1$
}
}
}
};
final Transaction transaction = event.getTransaction();
final Command triggeringCommand;
if (transaction instanceof EMFCommandTransaction) {
triggeringCommand = ((EMFCommandTransaction) transaction).getCommand();
} else {
triggeringCommand = null;
}
final InternalTransactionalCommandStack stack = (InternalTransactionalCommandStack) event.getSource().getCommandStack();
try {
Map<?, ?> options = transaction.getOptions();
if (this.options != null) {
Map<Object, Object> mergedOptions = Maps.newHashMap(options);
mergedOptions.putAll(this.options);
options = mergedOptions;
}
stack.executeTriggers(triggeringCommand, Collections.singletonList(trigger), options);
} catch (Exception e) {
Activator.log.error("Failed to execute transaction pre-commit tasks.", e); //$NON-NLS-1$
}
}
}
}
|