]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/drivers/AbstractDriver.java
support.jemmy: PopupMenuActuator added,
[gpl/argeo-slc.git] / runtime / org.argeo.slc.detached / src / main / java / org / argeo / slc / detached / drivers / AbstractDriver.java
1 package org.argeo.slc.detached.drivers;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import org.argeo.slc.detached.DetachedAnswer;
6 import org.argeo.slc.detached.DetachedDriver;
7 import org.argeo.slc.detached.DetachedExecutionServer;
8 import org.argeo.slc.detached.DetachedRequest;
9 import org.argeo.slc.detached.DetachedXmlConverter;
10 import org.osgi.framework.BundleContext;
11 import org.osgi.framework.Constants;
12 import org.springframework.beans.BeansException;
13 import org.springframework.context.ApplicationContext;
14 import org.springframework.context.ApplicationContextAware;
15 import org.springframework.osgi.context.BundleContextAware;
16
17 public abstract class AbstractDriver implements DetachedDriver,
18 BundleContextAware, ApplicationContextAware {
19 private final static Log log = LogFactory.getLog(AbstractDriver.class);
20
21 private boolean active = true;
22 private DetachedExecutionServer executionServer = null;
23 private long receiveAnswerTimeout = 10000l;
24
25 private DetachedXmlConverter xmlConverter = null;
26
27 private boolean cacheObjects = true;
28
29 /** May be null */
30 private ApplicationContext applicationContext;
31 /** May be null */
32 private BundleContext bundleContext;
33
34 public synchronized void start() {
35
36 Thread driverThread = new Thread(new Runnable() {
37
38 public void run() {
39 while (active) {
40 try {
41 DetachedRequest request = receiveRequest();
42 if (!active)
43 break;
44
45 String driverBundleName = null;
46 if (bundleContext != null)
47 driverBundleName = bundleContext.getBundle()
48 .getSymbolicName();
49
50 if (applicationContext != null && cacheObjects) {
51 try {
52 String ref = request.getRef();
53 if (applicationContext.containsBean(ref)) {
54 Object obj = applicationContext
55 .getBean(request.getRef());
56 request.setCachedObject(obj);
57 if (log.isTraceEnabled())
58 log.trace("Cached bean '" + ref
59 + "' in request " + request);
60 } else {
61 log
62 .warn("Cannot cache object in request because no bean '"
63 + ref
64 + "' was found in application context"
65 + (driverBundleName != null ? " (driver bundle "
66 + driverBundleName
67 + ")"
68 : ""));
69 }
70 } catch (Exception e) {
71 if (log.isTraceEnabled())
72 log
73 .trace("Could not retrieve "
74 + request.getRef()
75 + " from driver application context because of "
76 + e);
77 driverBundleName = null;// do not publish bundle
78 // name
79 }
80 }
81
82 if (driverBundleName != null)
83 request.getProperties().put(
84 Constants.BUNDLE_SYMBOLICNAME,
85 driverBundleName);
86
87 DetachedAnswer answer = executionServer
88 .executeRequest(request);
89 sendAnswer(answer);
90 } catch (Exception e) {
91 // if (e instanceof RuntimeException)
92 // throw (RuntimeException) e;
93 // else
94 e.printStackTrace();
95 }
96 }
97
98 }
99 }, "driverThread (" + getClass() + ")");
100 driverThread.start();
101
102 }
103
104 public void setExecutionServer(DetachedExecutionServer executionServer) {
105 this.executionServer = executionServer;
106 }
107
108 public synchronized void stop() {
109 active = false;
110 notifyAll();
111 }
112
113 public synchronized boolean isActive() {
114 return active;
115 }
116
117 public synchronized void setActive(boolean active) {
118 this.active = active;
119 }
120
121 public DetachedXmlConverter getXmlConverter() {
122 return xmlConverter;
123 }
124
125 public void setXmlConverter(DetachedXmlConverter xmlConverter) {
126 this.xmlConverter = xmlConverter;
127 }
128
129 public long getReceiveAnswerTimeout() {
130 return receiveAnswerTimeout;
131 }
132
133 public void setReceiveAnswerTimeout(long reveiveTimeout) {
134 this.receiveAnswerTimeout = reveiveTimeout;
135 }
136
137 public void setApplicationContext(ApplicationContext applicationContext)
138 throws BeansException {
139 this.applicationContext = applicationContext;
140 }
141
142 public void setBundleContext(BundleContext bundleContext) {
143 this.bundleContext = bundleContext;
144 }
145
146 public void setCacheObjects(boolean cacheObjects) {
147 this.cacheObjects = cacheObjects;
148 }
149
150 }