2 * Copyright (C) 2007-2012 Mathieu Baudier
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.detached
;
18 import org
.apache
.commons
.logging
.Log
;
19 import org
.apache
.commons
.logging
.LogFactory
;
20 import org
.osgi
.framework
.BundleContext
;
21 import org
.osgi
.framework
.Constants
;
22 import org
.springframework
.beans
.BeansException
;
23 import org
.springframework
.context
.ApplicationContext
;
24 import org
.springframework
.context
.ApplicationContextAware
;
25 import org
.springframework
.osgi
.context
.BundleContextAware
;
28 * When started, processes <code>DetachedRequest</code> through a
29 * <code>DetachedExecutionServer</code> and sends <code>DetachedAnswer</code>
32 public class DetachedServer
implements BundleContextAware
, ApplicationContextAware
{
34 private final static Log log
= LogFactory
.getLog(DetachedServer
.class);
36 private boolean active
= true;
37 private DetachedExecutionServer executionServer
= null;
39 private boolean cacheObjects
= true;
42 private ApplicationContext applicationContext
;
44 private BundleContext bundleContext
;
47 * Used to receive Request and send answers
49 private DetachedDriver detachedDriver
;
51 public synchronized void start() {
53 log
.info("Starting DetachedServer");
55 Thread driverThread
= new Thread(new Runnable() {
60 // no timeout to receive a request
61 DetachedRequest request
= detachedDriver
.receiveRequest();
65 String driverBundleName
= null;
66 if (bundleContext
!= null)
67 driverBundleName
= bundleContext
.getBundle()
70 if (applicationContext
!= null && cacheObjects
) {
72 String ref
= request
.getRef();
73 if (applicationContext
.containsBean(ref
)) {
74 Object obj
= applicationContext
75 .getBean(request
.getRef());
76 request
.setCachedObject(obj
);
77 if (log
.isTraceEnabled())
78 log
.trace("Cached bean '" + ref
79 + "' in request " + request
);
82 .warn("Cannot cache object in request because no bean '"
84 + "' was found in application context"
85 + (driverBundleName
!= null ?
" (driver bundle "
90 } catch (Exception e
) {
91 if (log
.isTraceEnabled())
93 .trace("Could not retrieve "
95 + " from driver application context because of "
97 driverBundleName
= null;// do not publish bundle
102 if (driverBundleName
!= null)
103 request
.getProperties().put(
104 Constants
.BUNDLE_SYMBOLICNAME
,
107 DetachedAnswer answer
= executionServer
108 .executeRequest(request
);
109 detachedDriver
.sendAnswer(answer
);
110 } catch (Exception e
) {
111 // if (e instanceof RuntimeException)
112 // throw (RuntimeException) e;
119 }, "driverThread (" + getClass() + ")");
120 driverThread
.start();
124 public void setExecutionServer(DetachedExecutionServer executionServer
) {
125 this.executionServer
= executionServer
;
128 public synchronized void stop() {
133 public synchronized boolean isActive() {
137 public synchronized void setActive(boolean active
) {
138 this.active
= active
;
141 public void setApplicationContext(ApplicationContext applicationContext
)
142 throws BeansException
{
143 this.applicationContext
= applicationContext
;
146 public void setBundleContext(BundleContext bundleContext
) {
147 this.bundleContext
= bundleContext
;
150 public void setCacheObjects(boolean cacheObjects
) {
151 this.cacheObjects
= cacheObjects
;
154 public void setDetachedDriver(DetachedDriver detachedDriver
) {
155 this.detachedDriver
= detachedDriver
;