01: /*
02: * Created on 16 Nov 2006
03: */
04: package uk.org.ponder.rsac;
05:
06: import java.util.ArrayList;
07: import java.util.List;
08: import java.util.Map;
09:
10: import uk.org.ponder.beanutil.BeanUtil;
11: import uk.org.ponder.beanutil.WriteableBeanLocator;
12: import uk.org.ponder.util.Logger;
13: import uk.org.ponder.util.UniversalRuntimeException;
14:
15: /**
16: * A list of Runnables to be invoked on (that is, immediately *after*) RSAC
17: * context destruction. These are not registered as Spring standard listeners
18: * (beans) because they are intended to execute once their parent context is
19: * destroyed - all relevant configuration from RSAC must have been closed over.
20: *
21: * An RSACLazarus element may perform any "exterior" action, including (usually)
22: * firing another RSAC.
23: *
24: * @author Antranig Basman (antranig@caret.cam.ac.uk)
25: */
26:
27: public class RSACLazarusList implements Runnable {
28: /** The standard bean name within RSAC of this instance */
29: public static final String RSAC_LAZARUS_LIST = "RSACLazarusList";
30:
31: private List lazarusList = new ArrayList();
32:
33: private RSACBeanLocatorImpl rsacbl;
34:
35: public void queueRunnable(Runnable torun) {
36: lazarusList.add(torun);
37: }
38:
39: public void setRSACBeanLocator(RSACBeanLocatorImpl rsacbl) {
40: this .rsacbl = rsacbl;
41: }
42:
43: /** Invoked in the context of an *existing* RSAC request, returning
44: * a Runnable which may be invoked as a Lazarus element which will
45: * start a further one.
46: * @param newbeans a Map of any new beans to populate the initial,
47: * subsequent RSAC container (overriding any which have been inherited as
48: * "seeds" from the current invocation)
49: * @param rootbeanname the name of the "root bean" to be fetched to trigger
50: * the subsequent RSAC computation.
51: */
52: public Runnable getLazarusRunnable(final Map newbeans,
53: final String rootbeanname) {
54: return new Runnable() {
55: Map seedbeans = rsacbl.getSeedMap();
56:
57: public void run() {
58: rsacbl.startRequest();
59: try {
60: WriteableBeanLocator wbl = rsacbl.getBeanLocator();
61: BeanUtil.copyBeans(seedbeans, wbl);
62: BeanUtil.copyBeans(newbeans, wbl);
63: wbl.locateBean(rootbeanname);
64: } catch (Exception e) {
65: throw UniversalRuntimeException.accumulate(e,
66: "Error servicing RSACLazarus request");
67: } finally {
68: rsacbl.endRequest();
69: }
70: }
71: };
72: }
73:
74: public void run() {
75: for (int i = 0; i < lazarusList.size(); ++i) {
76: Runnable torun = (Runnable) lazarusList.get(i);
77: try {
78: torun.run();
79: } catch (Exception e) {
80: Logger.log.warn("Error invoking lazarus action: ", e);
81: }
82: }
83: }
84:
85: }
|