X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=runtime%2Forg.argeo.slc.detached%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fdetached%2Fdrivers%2FAbstractDriver.java;h=a8b3dffa6df3095119596d474569e19f17d862e7;hb=b523980e22f7712f178df91ecad0e7f390c8ca2e;hp=78e82df055eaed52181432a7fbbb9362d24b3764;hpb=0de4227a834fd97898c97d181ff0cffc92e519d2;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/drivers/AbstractDriver.java b/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/drivers/AbstractDriver.java index 78e82df05..a8b3dffa6 100644 --- a/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/drivers/AbstractDriver.java +++ b/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/drivers/AbstractDriver.java @@ -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; + } + }