1 package org
.argeo
.slc
.detached
.drivers
;
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
;
17 public abstract class AbstractDriver
implements DetachedDriver
,
18 BundleContextAware
, ApplicationContextAware
{
19 private final static Log log
= LogFactory
.getLog(AbstractDriver
.class);
21 private boolean active
= true;
22 private DetachedExecutionServer executionServer
= null;
23 private long receiveAnswerTimeout
= 10000l;
25 private DetachedXmlConverter xmlConverter
= null;
27 private boolean cacheObjects
= true;
30 private ApplicationContext applicationContext
;
32 private BundleContext bundleContext
;
34 public synchronized void start() {
36 Thread driverThread
= new Thread(new Runnable() {
41 DetachedRequest request
= receiveRequest();
45 String driverBundleName
= null;
46 if (bundleContext
!= null)
47 driverBundleName
= bundleContext
.getBundle()
50 if (applicationContext
!= null && cacheObjects
) {
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
);
62 .warn("Cannot cache object in request because no bean '"
64 + "' was found in application context"
65 + (driverBundleName
!= null ?
" (driver bundle "
70 } catch (Exception e
) {
71 if (log
.isTraceEnabled())
73 .trace("Could not retrieve "
75 + " from driver application context because of "
77 driverBundleName
= null;// do not publish bundle
82 if (driverBundleName
!= null)
83 request
.getProperties().put(
84 Constants
.BUNDLE_SYMBOLICNAME
,
87 DetachedAnswer answer
= executionServer
88 .executeRequest(request
);
90 } catch (Exception e
) {
91 // if (e instanceof RuntimeException)
92 // throw (RuntimeException) e;
99 }, "driverThread (" + getClass() + ")");
100 driverThread
.start();
104 public void setExecutionServer(DetachedExecutionServer executionServer
) {
105 this.executionServer
= executionServer
;
108 public synchronized void stop() {
113 public synchronized boolean isActive() {
117 public synchronized void setActive(boolean active
) {
118 this.active
= active
;
121 public DetachedXmlConverter
getXmlConverter() {
125 public void setXmlConverter(DetachedXmlConverter xmlConverter
) {
126 this.xmlConverter
= xmlConverter
;
129 public long getReceiveAnswerTimeout() {
130 return receiveAnswerTimeout
;
133 public void setReceiveAnswerTimeout(long reveiveTimeout
) {
134 this.receiveAnswerTimeout
= reveiveTimeout
;
137 public void setApplicationContext(ApplicationContext applicationContext
)
138 throws BeansException
{
139 this.applicationContext
= applicationContext
;
142 public void setBundleContext(BundleContext bundleContext
) {
143 this.bundleContext
= bundleContext
;
146 public void setCacheObjects(boolean cacheObjects
) {
147 this.cacheObjects
= cacheObjects
;