import org.argeo.slc.detached.admin.OpenSession;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
+import org.springframework.context.ApplicationContext;
+import org.springframework.osgi.context.BundleContextAware;
-public class DetachedExecutionServerImpl implements DetachedExecutionServer {
+public class DetachedExecutionServerImpl implements DetachedExecutionServer,
+ BundleContextAware {
private final static Log log = LogFactory
.getLog(DetachedExecutionServerImpl.class);
private final DetachedContextImpl detachedContext;
private final List sessions;
- private int skipCount = 0;
+ private int skipCount = 1;// start skipCount at 1 since the first step is
+ // always an open session
private BundleContext bundleContext;
- //private DetachedDriver driver;
-
- //private boolean active = false;
-
-// public void setDriver(DetachedDriver driver) {
-// this.driver = driver;
-// }
public DetachedExecutionServerImpl() {
detachedContext = new DetachedContextImpl();
sessions = new Vector();
}
- public synchronized DetachedAnswer executeStep(DetachedRequest request) {
+ public synchronized DetachedAnswer executeRequest(DetachedRequest request) {
DetachedAnswer answer = null;
try {
- DetachedStep step = null;
-
// Find action
ServiceReference[] refs = bundleContext.getAllServiceReferences(
- StaticRefProvider.class.getName(), null);
+ ApplicationContext.class.getName(), null);
Object obj = null;
for (int i = 0; i < refs.length; i++) {
- StaticRefProvider provider = (StaticRefProvider) bundleContext
+ ApplicationContext appContext = (ApplicationContext) bundleContext
.getService(refs[i]);
- obj = provider.getStaticRef(request.getRef());
+ try {
+ obj = appContext.getBean(request.getRef());
+ } catch (Exception e) {
+ // silent
+ if (log.isTraceEnabled())
+ log.trace("Could not find ref " + request.getRef(), e);
+ }
if (obj != null) {
break;
}
// Execute actions
if (obj instanceof DetachedStep) {
- if (getCurrentSession() == null)
- throw new DetachedException("No open session.");
-
- StringBuffer skippedLog = new StringBuffer();
- boolean execute = true;
- if (getPreviousSession() != null
- && !getPreviousSession().isClosed()) {
- if (getCurrentSession().getDoItAgainPolicy().equals(
- DetachedSession.SKIP_UNTIL_ERROR)) {
- // Skip execution of already successful steps
- if (getPreviousSession().getAnswers().size() > skipCount) {
- DetachedAnswer previousAnswer = (DetachedAnswer) getPreviousSession()
- .getAnswers().get(skipCount);
- DetachedRequest previousRequest = (DetachedRequest) getPreviousSession()
- .getRequests().get(skipCount);
- // Check paths
- if (!previousRequest.getPath().equals(
- request.getPath())) {
- String msg = "New request is not consistent with previous path. previousPath="
- + previousRequest.getPath()
- + ", newPath="
- + request.getPath()
- + "\n";
- skippedLog.append(msg);
- log.warn(msg);
- }
-
- if (previousAnswer.getStatus() != DetachedAnswer.ERROR) {
- execute = false;
- }
- } else {
- // went further as skip count, doing nothing.
- }
- }
- }
-
- if (execute) {
- step = (DetachedStep) obj;
- answer = step.execute(detachedContext, request);
- } else {
- skippedLog.append("Skipped path " + request.getPath()
- + " (skipCount=" + skipCount + ")");
- answer = new DetachedAnswer(request);
- answer.setStatus(DetachedAnswer.SKIPPED);
- answer.setLog(skippedLog.toString());
- }
+ answer = processStep((DetachedStep) obj, request);
} else if (obj instanceof DetachedAdminCommand) {
- if (obj instanceof OpenSession) {
- if (getCurrentSession() != null)
- throw new DetachedException(
- "There is already an open session #"
- + getCurrentSession().getUuid());
- sessions.add(((OpenSession) obj).execute(request,
- bundleContext));
- answer = new DetachedAnswer(request, "Session #"
- + getCurrentSession().getUuid() + " open.");
- } else if (obj instanceof CloseSession) {
- if (getCurrentSession() == null)
- throw new DetachedException(
- "There is no open session to close");
- answer = new DetachedAnswer(request, "Session #"
- + getCurrentSession().getUuid() + " closed.");
- answer.setStatus(DetachedAnswer.CLOSED_SESSION);
- }
+ answer = processAdminCommand((DetachedAdminCommand) obj,
+ request);
}
- if (answer == null)
+ if (answer == null) {
throw new DetachedException("Unknown action type "
+ obj.getClass() + " for action with ref "
+ request.getRef());
-
- } catch (DetachedException e) {
+ }
+ } catch (Exception e) {
answer = new DetachedAnswer(request);
answer.setStatus(DetachedAnswer.ERROR);
answer.setLog(e.getMessage());
- } catch (Exception e) {
- e.printStackTrace();
- throw new DetachedException(
- "Unexpected exception while executing request " + request,
- e);
}
getCurrentSession().getRequests().add(request);
getCurrentSession().getAnswers().add(answer);
+ if (log.isDebugEnabled())
+ log.debug("Processed '" + request.getRef() + "' (status="
+ + answer.getStatusAsString() + ", path="
+ + request.getPath() + ")");
+ return answer;
+ }
+
+ protected DetachedAnswer processStep(DetachedStep obj,
+ DetachedRequest request) {
+ DetachedAnswer answer;
+ if (getCurrentSession() == null)
+ throw new DetachedException("No open session.");
+
+ StringBuffer skippedLog = new StringBuffer();
+ boolean execute = true;
+ if (getPreviousSession() != null && !getPreviousSession().isClosed()) {
+ if (getCurrentSession().getDoItAgainPolicy().equals(
+ DetachedSession.SKIP_UNTIL_ERROR)) {
+ // Skip execution of already successful steps
+ if (getPreviousSession().getAnswers().size() > skipCount) {
+ DetachedAnswer previousAnswer = (DetachedAnswer) getPreviousSession()
+ .getAnswers().get(skipCount);
+ DetachedRequest previousRequest = (DetachedRequest) getPreviousSession()
+ .getRequests().get(skipCount);
+ // Check paths
+ if (!previousRequest.getPath().equals(request.getPath())) {
+ String msg = "New request is not consistent with previous path. previousPath="
+ + previousRequest.getPath()
+ + ", newPath="
+ + request.getPath() + "\n";
+ skippedLog.append(msg);
+ log.warn(msg);
+ }
+
+ if (previousAnswer.getStatus() != DetachedAnswer.ERROR) {
+ execute = false;
+ String msg = "Skipped path " + request.getPath()
+ + " (skipCount=" + skipCount + ")";
+ skippedLog.append(msg);
+ log.info(msg);
+ skipCount++;
+ } else {
+ log
+ .info("Path "
+ + request.getPath()
+ + " was previously in error, executing it again."
+ + " (skipCount=" + skipCount
+ + "). Reset skip count to 1");
+ skipCount = 1;
+ }
+ } else {
+ // went further as skip count, doing nothing.
+ }
+ }
+ }
+
+ if (execute) {
+ DetachedStep step = (DetachedStep) obj;
+ answer = step.execute(detachedContext, request);
+ } else {
+ answer = new DetachedAnswer(request);
+ answer.setStatus(DetachedAnswer.SKIPPED);
+ answer.setLog(skippedLog.toString());
+ }
+ return answer;
+ }
+
+ protected DetachedAnswer processAdminCommand(DetachedAdminCommand obj,
+ DetachedRequest request) {
+ DetachedAnswer answer;
+ if (obj instanceof OpenSession) {
+ if (getCurrentSession() != null)
+ throw new DetachedException(
+ "There is already an open session #"
+ + getCurrentSession().getUuid());
+ sessions.add(((OpenSession) obj).execute(request, bundleContext));
+ answer = new DetachedAnswer(request, "Session #"
+ + getCurrentSession().getUuid() + " open.");
+ } else if (obj instanceof CloseSession) {
+ if (getCurrentSession() == null)
+ throw new DetachedException("There is no open session to close");
+ answer = new DetachedAnswer(request, "Session #"
+ + getCurrentSession().getUuid() + " closed.");
+ answer.setStatus(DetachedAnswer.CLOSED_SESSION);
+ } else {
+ answer = null;
+ }
return answer;
}
return (DetachedSession) sessions.get(sessions.size() - 2);
}
- public void init(BundleContext bundleContext) {
+ public void setBundleContext(BundleContext bundleContext) {
this.bundleContext = bundleContext;
-// Thread driverThread = new Thread(new Runnable() {
-//
-// public void run() {
-// while (active) {
-// try {
-// DetachedRequest request = driver.receiveRequest();
-// DetachedAnswer answer = executeStep(request);
-// driver.sendAnswer(answer);
-// } catch (Exception e) {
-// if (e instanceof RuntimeException)
-// throw (RuntimeException) e;
-// else
-// e.printStackTrace();
-// }
-// }
-//
-// }
-// }, "driverThread");
-//
-// active = true;
-//
-// driverThread.start();
}
}