]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/drivers/AbstractDriver.java
Completely refactor the way the execution server looks for steps
[gpl/argeo-slc.git] / runtime / org.argeo.slc.detached / src / main / java / org / argeo / slc / detached / drivers / AbstractDriver.java
index 78e82df055eaed52181432a7fbbb9362d24b3764..a8b3dffa6df3095119596d474569e19f17d862e7 100644 (file)
@@ -1,18 +1,36 @@
 package org.argeo.slc.detached.drivers;
 
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.argeo.slc.detached.DetachedAnswer;
 import org.argeo.slc.detached.DetachedDriver;
 import org.argeo.slc.detached.DetachedExecutionServer;
 import org.argeo.slc.detached.DetachedRequest;
 import org.argeo.slc.detached.DetachedXmlConverter;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.springframework.beans.BeansException;
+import org.springframework.context.ApplicationContext;
+import org.springframework.context.ApplicationContextAware;
+import org.springframework.osgi.context.BundleContextAware;
+
+public abstract class AbstractDriver implements DetachedDriver,
+               BundleContextAware, ApplicationContextAware {
+       private final static Log log = LogFactory.getLog(AbstractDriver.class);
 
-public abstract class AbstractDriver implements DetachedDriver {
        private boolean active = true;
        private DetachedExecutionServer executionServer = null;
        private long receiveAnswerTimeout = 10000l;
 
        private DetachedXmlConverter xmlConverter = null;
 
+       private boolean cacheObjects = true;
+
+       /** May be null */
+       private ApplicationContext applicationContext;
+       /** May be null */
+       private BundleContext bundleContext;
+
        public synchronized void start() {
 
                Thread driverThread = new Thread(new Runnable() {
@@ -23,19 +41,62 @@ public abstract class AbstractDriver implements DetachedDriver {
                                                DetachedRequest request = receiveRequest();
                                                if (!active)
                                                        break;
+
+                                               String driverBundleName = null;
+                                               if (bundleContext != null)
+                                                       driverBundleName = bundleContext.getBundle()
+                                                                       .getSymbolicName();
+
+                                               if (applicationContext != null && cacheObjects) {
+                                                       try {
+                                                               String ref = request.getRef();
+                                                               if (applicationContext.containsBean(ref)) {
+                                                                       Object obj = applicationContext
+                                                                                       .getBean(request.getRef());
+                                                                       request.setCachedObject(obj);
+                                                                       if (log.isTraceEnabled())
+                                                                               log.trace("Cached bean '" + ref
+                                                                                               + "' in request " + request);
+                                                               } else {
+                                                                       log
+                                                                                       .warn("Cannot cache object in request because no bean '"
+                                                                                                       + ref
+                                                                                                       + "' was found in application context"
+                                                                                                       + (driverBundleName != null ? " (driver bundle "
+                                                                                                                       + driverBundleName
+                                                                                                                       + ")"
+                                                                                                                       : ""));
+                                                               }
+                                                       } catch (Exception e) {
+                                                               if (log.isTraceEnabled())
+                                                                       log
+                                                                                       .trace("Could not retrieve "
+                                                                                                       + request.getRef()
+                                                                                                       + " from driver application context because of "
+                                                                                                       + e);
+                                                               driverBundleName = null;// do not publish bundle
+                                                                                                               // name
+                                                       }
+                                               }
+
+                                               if (driverBundleName != null)
+                                                       request.getProperties().put(
+                                                                       Constants.BUNDLE_SYMBOLICNAME,
+                                                                       driverBundleName);
+
                                                DetachedAnswer answer = executionServer
                                                                .executeRequest(request);
                                                sendAnswer(answer);
                                        } catch (Exception e) {
-//                                             if (e instanceof RuntimeException)
-//                                                     throw (RuntimeException) e;
-//                                             else
-                                                       e.printStackTrace();
+                                               // if (e instanceof RuntimeException)
+                                               // throw (RuntimeException) e;
+                                               // else
+                                               e.printStackTrace();
                                        }
                                }
 
                        }
-               }, "driverThread ("+getClass()+")");
+               }, "driverThread (" + getClass() + ")");
                driverThread.start();
 
        }
@@ -73,4 +134,17 @@ public abstract class AbstractDriver implements DetachedDriver {
                this.receiveAnswerTimeout = reveiveTimeout;
        }
 
+       public void setApplicationContext(ApplicationContext applicationContext)
+                       throws BeansException {
+               this.applicationContext = applicationContext;
+       }
+
+       public void setBundleContext(BundleContext bundleContext) {
+               this.bundleContext = bundleContext;
+       }
+
+       public void setCacheObjects(boolean cacheObjects) {
+               this.cacheObjects = cacheObjects;
+       }
+
 }