-
Notifications
You must be signed in to change notification settings - Fork 10
/
AbstractSession.java
180 lines (160 loc) · 6.02 KB
/
AbstractSession.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
/*******************************************************************************
* Copyright (c) 2009, 2019 Xored Software Inc and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Xored Software Inc - initial API and implementation and/or initial documentation
*******************************************************************************/
package org.eclipse.rcptt.ecl.internal.core;
import static org.eclipse.rcptt.ecl.internal.core.CorePlugin.err;
import static org.eclipse.rcptt.ecl.internal.core.CorePlugin.log;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.rcptt.ecl.core.Binding;
import org.eclipse.rcptt.ecl.core.Command;
import org.eclipse.rcptt.ecl.core.CommandStack;
import org.eclipse.rcptt.ecl.core.ProcInstance;
import org.eclipse.rcptt.ecl.core.SessionListenerManager;
import org.eclipse.rcptt.ecl.runtime.CoreUtils;
import org.eclipse.rcptt.ecl.runtime.ICommandService;
import org.eclipse.rcptt.ecl.runtime.IPipe;
import org.eclipse.rcptt.ecl.runtime.IProcess;
import org.eclipse.rcptt.ecl.runtime.ISession;
public abstract class AbstractSession implements ISession {
private Map<String, Object> properties = null;
protected abstract void doExecute(final Command scriptlet, final ICommandService svc,
final List<Object> inputContent, final Process process);
protected abstract CommandStack getStack();
public abstract AbstractRootSession getRoot();
public IProcess execute(Command command) throws CoreException {
return execute(command, null, null);
}
// public IProcess execute(final Command scriptlet, IPipe in, IPipe out)
// throws CoreException
public IProcess execute(Command scriptlet, IPipe in, IPipe out) throws CoreException {
if (isClosed()) {
throw new CoreException(Status.CANCEL_STATUS);
}
final ICommandService svc = scriptlet instanceof ProcInstance ? new ProcInstanceService()
: CorePlugin.getScriptletManager().getScriptletService(scriptlet);
final IPipe tinput = in == null ? createPipe().close(Status.OK_STATUS) : in;
final IPipe output = out == null ? createPipe() : out;
final List<Object> inputContent = CoreUtils.readPipeContent(tinput);
final IPipe input = createPipe();
for (Object o : inputContent)
input.write(o);
input.close(Status.OK_STATUS);
CommandSession session = new CommandSession(getRoot(), new CommandStack(scriptlet, getStack()), this);
final Process process = new Process(session, input, output);
doExecute(scriptlet, svc, inputContent, process);
return process;
}
// protected void internalDoExecute(final Command scriptlet,
// final ICommandService svc, final List<Object> inputContent,
// final Process process)
protected void internalDoExecute(Command scriptlet, final ICommandService svc, final List<Object> inputContent,
final Process process) {
IStatus s = null;
CommandStack stack = ((AbstractSession) process.getSession()).getStack();
try {
resolveBindings(scriptlet, inputContent);
setupInputFeature(scriptlet, inputContent);
checkParams(scriptlet);
CommandStack.fireEnter(stack);
SessionListenerManager.beginCommand(scriptlet);
s = svc.service(scriptlet, process);
} catch (CoreException e) {
s = e.getStatus();
} catch (InterruptedException ie) {
log(s = err(ie));
} catch (Throwable t) {
log(s = err(t));
} finally {
try {
SessionListenerManager.endCommand(scriptlet, s);
} catch (Throwable e) {
log(s = err(e));
}
CommandStack.fireExit(stack);
try {
process.setStatus(s);
} catch (CoreException ioe) {
s = ioe.getStatus();
try {
process.setStatus(s);
} catch (CoreException e) {
// do nothing, just log
CorePlugin.log(e.getStatus());
}
}
}
}
protected void setupInputFeature(Command scriptlet, List<Object> inputContent) throws CoreException {
EStructuralFeature inputFeature = null;
for (EStructuralFeature feature : CoreUtils.getFeatures(scriptlet.eClass())) {
if (feature.getEAnnotation(CoreUtils.INPUT_ANN) != null && !scriptlet.eIsSet(feature)) {
if (inputFeature == null) {
inputFeature = feature;
} else {
IStatus status = new Status(IStatus.ERROR, CorePlugin.PLUGIN_ID,
"Command has more than one input param");
throw new CoreException(status);
}
}
}
if (inputFeature != null) {
CoreUtils.featureSafeSet(scriptlet, inputFeature, inputContent);
}
}
protected void resolveBindings(Command scriptlet, List<Object> inputContent)
throws CoreException, InterruptedException {
for (Binding binding : scriptlet.getBindings()) {
EStructuralFeature feature = binding.getFeature();
Command command = binding.getCommand();
IPipe in = createPipe();
for (Object o : inputContent)
in.write(o);
in.close(Status.OK_STATUS);
IPipe out = createPipe();
IProcess process = execute(command, in, out);
IStatus status = process.waitFor();
if (!status.isOK())
throw new CoreException(status);
CoreUtils.featureSafeSet(scriptlet, feature, CoreUtils.readPipeContent(out));
}
}
protected void checkParams(Command scriptlet) throws CoreException {
for (EStructuralFeature feature : scriptlet.eClass().getEStructuralFeatures()) {
CoreUtils.checkBounds(feature, scriptlet.eGet(feature));
}
}
public IPipe createPipe() {
return new Pipe();
}
public synchronized void putProperty(String key, Object value) {
if (value == null) {
if (properties != null) {
properties.remove(key);
}
} else {
if (properties == null) {
properties = new HashMap<String, Object>();
}
properties.put(key, value);
}
}
public synchronized Object getProperty(String key) {
if (properties != null) {
return properties.get(key);
}
return null;
}
}