]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedExecutionServerImpl.java
Rename packages
[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 org.osgi.framework.BundleContext;
4 import org.osgi.framework.ServiceReference;
5
6 public class DetachedExecutionServerImpl implements DetachedExecutionServer {
7 private final DetachedContextImpl detachedContext;
8
9 private BundleContext bundleContext;
10 private DetachedDriver driver;
11
12 private boolean active = false;
13
14 public void setDriver(DetachedDriver driver) {
15 this.driver = driver;
16 }
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 init(BundleContext bundleContext) {
55 this.bundleContext = bundleContext;
56 Thread driverThread = new Thread(new Runnable() {
57
58 public void run() {
59 while (active) {
60 try {
61 DetachedStepRequest request = driver.receiveRequest();
62 executeStep(request);
63 } catch (Exception e) {
64 if (e instanceof RuntimeException)
65 throw (RuntimeException) e;
66 else
67 e.printStackTrace();
68 }
69 }
70
71 }
72 }, "driverThread");
73
74 active = true;
75
76 driverThread.start();
77 }
78
79 }