]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/runtime/org.argeo.slc.detached/src/main/java/org/argeo/slc/detached/DetachedServer.java
Clean up directories
[gpl/argeo-slc.git] / legacy / runtime / org.argeo.slc.detached / src / main / java / org / argeo / slc / detached / DetachedServer.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.slc.detached;
17
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;
26
27 /**
28 * When started, processes <code>DetachedRequest</code> through a
29 * <code>DetachedExecutionServer</code> and sends <code>DetachedAnswer</code>
30 * back
31 */
32 public class DetachedServer implements BundleContextAware, ApplicationContextAware {
33
34 private final static Log log = LogFactory.getLog(DetachedServer.class);
35
36 private boolean active = true;
37 private DetachedExecutionServer executionServer = null;
38
39 private boolean cacheObjects = true;
40
41 /** May be null */
42 private ApplicationContext applicationContext;
43 /** May be null */
44 private BundleContext bundleContext;
45
46 /**
47 * Used to receive Request and send answers
48 */
49 private DetachedDriver detachedDriver;
50
51 public synchronized void start() {
52
53 log.info("Starting DetachedServer");
54
55 Thread driverThread = new Thread(new Runnable() {
56
57 public void run() {
58 while (active) {
59 try {
60 // no timeout to receive a request
61 DetachedRequest request = detachedDriver.receiveRequest();
62 if (!active)
63 break;
64
65 String driverBundleName = null;
66 if (bundleContext != null)
67 driverBundleName = bundleContext.getBundle()
68 .getSymbolicName();
69
70 if (applicationContext != null && cacheObjects) {
71 try {
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);
80 } else {
81 log
82 .warn("Cannot cache object in request because no bean '"
83 + ref
84 + "' was found in application context"
85 + (driverBundleName != null ? " (driver bundle "
86 + driverBundleName
87 + ")"
88 : ""));
89 }
90 } catch (Exception e) {
91 if (log.isTraceEnabled())
92 log
93 .trace("Could not retrieve "
94 + request.getRef()
95 + " from driver application context because of "
96 + e);
97 driverBundleName = null;// do not publish bundle
98 // name
99 }
100 }
101
102 if (driverBundleName != null)
103 request.getProperties().put(
104 Constants.BUNDLE_SYMBOLICNAME,
105 driverBundleName);
106
107 DetachedAnswer answer = executionServer
108 .executeRequest(request);
109 detachedDriver.sendAnswer(answer);
110 } catch (Exception e) {
111 // if (e instanceof RuntimeException)
112 // throw (RuntimeException) e;
113 // else
114 e.printStackTrace();
115 }
116 }
117
118 }
119 }, "driverThread (" + getClass() + ")");
120 driverThread.start();
121
122 }
123
124 public void setExecutionServer(DetachedExecutionServer executionServer) {
125 this.executionServer = executionServer;
126 }
127
128 public synchronized void stop() {
129 active = false;
130 notifyAll();
131 }
132
133 public synchronized boolean isActive() {
134 return active;
135 }
136
137 public synchronized void setActive(boolean active) {
138 this.active = active;
139 }
140
141 public void setApplicationContext(ApplicationContext applicationContext)
142 throws BeansException {
143 this.applicationContext = applicationContext;
144 }
145
146 public void setBundleContext(BundleContext bundleContext) {
147 this.bundleContext = bundleContext;
148 }
149
150 public void setCacheObjects(boolean cacheObjects) {
151 this.cacheObjects = cacheObjects;
152 }
153
154 public void setDetachedDriver(DetachedDriver detachedDriver) {
155 this.detachedDriver = detachedDriver;
156 }
157
158 }