]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedExecutionServerImpl.java
Start introducing detached ui
[gpl/argeo-slc.git] / org.argeo.slc.detached / src / main / java / org / argeo / slc / detached / DetachedExecutionServerImpl.java
1 package org.argeo.slc.detached;
2
3 import java.util.List;
4 import java.util.Vector;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.argeo.slc.detached.admin.CloseSession;
9 import org.argeo.slc.detached.admin.OpenSession;
10 import org.osgi.framework.BundleContext;
11 import org.osgi.framework.ServiceReference;
12
13 public class DetachedExecutionServerImpl implements DetachedExecutionServer {
14 private final static Log log = LogFactory
15 .getLog(DetachedExecutionServerImpl.class);
16
17 private final DetachedContextImpl detachedContext;
18 private final List sessions;
19
20 private int skipCount = 0;
21
22 private BundleContext bundleContext;
23 //private DetachedDriver driver;
24
25 //private boolean active = false;
26
27 // public void setDriver(DetachedDriver driver) {
28 // this.driver = driver;
29 // }
30
31 public DetachedExecutionServerImpl() {
32 detachedContext = new DetachedContextImpl();
33 sessions = new Vector();
34 }
35
36 public synchronized DetachedAnswer executeStep(DetachedRequest request) {
37 DetachedAnswer answer = null;
38 try {
39 DetachedStep step = null;
40
41 // Find action
42 ServiceReference[] refs = bundleContext.getAllServiceReferences(
43 StaticRefProvider.class.getName(), null);
44 Object obj = null;
45 for (int i = 0; i < refs.length; i++) {
46 StaticRefProvider provider = (StaticRefProvider) bundleContext
47 .getService(refs[i]);
48 obj = provider.getStaticRef(request.getRef());
49 if (obj != null) {
50 break;
51 }
52 }
53
54 if (obj == null)
55 throw new DetachedException("Could not find action with ref "
56 + request.getRef());
57
58 // Execute actions
59 if (obj instanceof DetachedStep) {
60 if (getCurrentSession() == null)
61 throw new DetachedException("No open session.");
62
63 StringBuffer skippedLog = new StringBuffer();
64 boolean execute = true;
65 if (getPreviousSession() != null
66 && !getPreviousSession().isClosed()) {
67 if (getCurrentSession().getDoItAgainPolicy().equals(
68 DetachedSession.SKIP_UNTIL_ERROR)) {
69 // Skip execution of already successful steps
70 if (getPreviousSession().getAnswers().size() > skipCount) {
71 DetachedAnswer previousAnswer = (DetachedAnswer) getPreviousSession()
72 .getAnswers().get(skipCount);
73 DetachedRequest previousRequest = (DetachedRequest) getPreviousSession()
74 .getRequests().get(skipCount);
75 // Check paths
76 if (!previousRequest.getPath().equals(
77 request.getPath())) {
78 String msg = "New request is not consistent with previous path. previousPath="
79 + previousRequest.getPath()
80 + ", newPath="
81 + request.getPath()
82 + "\n";
83 skippedLog.append(msg);
84 log.warn(msg);
85 }
86
87 if (previousAnswer.getStatus() != DetachedAnswer.ERROR) {
88 execute = false;
89 }
90 } else {
91 // went further as skip count, doing nothing.
92 }
93 }
94 }
95
96 if (execute) {
97 step = (DetachedStep) obj;
98 answer = step.execute(detachedContext, request);
99 } else {
100 skippedLog.append("Skipped path " + request.getPath()
101 + " (skipCount=" + skipCount + ")");
102 answer = new DetachedAnswer(request);
103 answer.setStatus(DetachedAnswer.SKIPPED);
104 answer.setLog(skippedLog.toString());
105 }
106
107 } else if (obj instanceof DetachedAdminCommand) {
108 if (obj instanceof OpenSession) {
109 if (getCurrentSession() != null)
110 throw new DetachedException(
111 "There is already an open session #"
112 + getCurrentSession().getUuid());
113 sessions.add(((OpenSession) obj).execute(request,
114 bundleContext));
115 answer = new DetachedAnswer(request, "Session #"
116 + getCurrentSession().getUuid() + " open.");
117 } else if (obj instanceof CloseSession) {
118 if (getCurrentSession() == null)
119 throw new DetachedException(
120 "There is no open session to close");
121 answer = new DetachedAnswer(request, "Session #"
122 + getCurrentSession().getUuid() + " closed.");
123 answer.setStatus(DetachedAnswer.CLOSED_SESSION);
124 }
125 }
126
127 if (answer == null)
128 throw new DetachedException("Unknown action type "
129 + obj.getClass() + " for action with ref "
130 + request.getRef());
131
132 } catch (DetachedException e) {
133 answer = new DetachedAnswer(request);
134 answer.setStatus(DetachedAnswer.ERROR);
135 answer.setLog(e.getMessage());
136 } catch (Exception e) {
137 e.printStackTrace();
138 throw new DetachedException(
139 "Unexpected exception while executing request " + request,
140 e);
141 }
142 getCurrentSession().getRequests().add(request);
143 getCurrentSession().getAnswers().add(answer);
144 return answer;
145 }
146
147 protected final DetachedSession getCurrentSession() {
148 if (sessions.size() == 0) {
149 return null;
150 } else {
151 DetachedSession session = (DetachedSession) sessions.get(sessions
152 .size() - 1);
153 List answers = session.getAnswers();
154 if (answers.size() > 0) {
155 DetachedAnswer lastAnswer = (DetachedAnswer) answers
156 .get(answers.size() - 1);
157 if (lastAnswer.getStatus() == DetachedAnswer.ERROR
158 || lastAnswer.getStatus() == DetachedAnswer.CLOSED_SESSION)
159 return null;
160 }
161 return session;
162 }
163 }
164
165 protected final DetachedSession getPreviousSession() {
166 if (sessions.size() < 2)
167 return null;
168 else
169 return (DetachedSession) sessions.get(sessions.size() - 2);
170 }
171
172 public void init(BundleContext bundleContext) {
173 this.bundleContext = bundleContext;
174 // Thread driverThread = new Thread(new Runnable() {
175 //
176 // public void run() {
177 // while (active) {
178 // try {
179 // DetachedRequest request = driver.receiveRequest();
180 // DetachedAnswer answer = executeStep(request);
181 // driver.sendAnswer(answer);
182 // } catch (Exception e) {
183 // if (e instanceof RuntimeException)
184 // throw (RuntimeException) e;
185 // else
186 // e.printStackTrace();
187 // }
188 // }
189 //
190 // }
191 // }, "driverThread");
192 //
193 // active = true;
194 //
195 // driverThread.start();
196 }
197
198 }