]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.autoui/src/main/java/org/argeo/slc/autoui/internal/DetachedExecutionServerImpl.java
Introduce detached execution server.
[gpl/argeo-slc.git] / org.argeo.slc.autoui / src / main / java / org / argeo / slc / autoui / internal / DetachedExecutionServerImpl.java
1 package org.argeo.slc.autoui.internal;
2
3 import org.argeo.slc.autoui.DetachedContextImpl;
4 import org.argeo.slc.autoui.DetachedException;
5 import org.argeo.slc.autoui.DetachedExecutionServer;
6 import org.argeo.slc.autoui.DetachedStep;
7 import org.argeo.slc.autoui.DetachedStepAnswer;
8 import org.argeo.slc.autoui.DetachedStepRequest;
9 import org.argeo.slc.autoui.StaticRefProvider;
10 import org.osgi.framework.BundleContext;
11 import org.osgi.framework.ServiceReference;
12
13 public class DetachedExecutionServerImpl implements DetachedExecutionServer {
14 private final DetachedContextImpl detachedContext;
15
16 private BundleContext bundleContext;
17
18 public DetachedExecutionServerImpl() {
19 detachedContext = new DetachedContextImpl();
20 }
21
22 public DetachedStepAnswer executeStep(DetachedStepRequest request) {
23 try {
24 DetachedStep step = null;
25
26 // Find step
27 ServiceReference[] refs = bundleContext.getAllServiceReferences(
28 StaticRefProvider.class.getName(), null);
29 for (int i = 0; i < refs.length; i++) {
30 StaticRefProvider provider = (StaticRefProvider) bundleContext
31 .getService(refs[i]);
32 Object obj = provider.getStaticRef(request.getStepRef());
33 if (obj != null) {
34 step = (DetachedStep) obj;
35 break;
36 }
37 }
38
39 if (step == null)
40 throw new DetachedException("Could not find step with ref "
41 + request.getStepRef());
42
43 return step.execute(detachedContext, request);
44 } catch (DetachedException e) {
45 throw e;
46 } catch (Exception e) {
47 e.printStackTrace();
48 throw new DetachedException(
49 "Unexpected exception while executing request " + request,
50 e);
51 }
52 }
53
54 public void setBundleContext(BundleContext bundleContext) {
55 this.bundleContext = bundleContext;
56 }
57
58 }