01: /*
02: * Created on 6 Aug 2006
03: */
04: package uk.org.ponder.beanutil;
05:
06: import java.util.ArrayList;
07:
08: import uk.org.ponder.stringutil.StringList;
09:
10: /**
11: * A BeanLocator which accepts a set of "Fallback Locators", which are request
12: * beans of type {@link FallbackBeanLocator}, whose contained beans will be
13: * exported up to the root level - such paths are automatically permitted.
14: * Fallback locators are only initialised on the first query to this locator
15: * (probably via an EL expression) in order to minimise the possibility
16: * construct-time cycles.
17: */
18:
19: public class FallbackCapableBeanLocator implements WriteableBeanLocator {
20:
21: private WriteableBeanLocator wbl;
22: private StringList fallbacks;
23: private ArrayList fallbackbeans;
24:
25: public void setBeanLocator(WriteableBeanLocator wbl) {
26: this .wbl = wbl;
27: }
28:
29: public void setFallbackBeans(StringList fallbacks) {
30: this .fallbacks = fallbacks;
31: }
32:
33: public Object locateBean(String path) {
34: Object fetch = wbl.locateBean(path);
35: if (fetch != null) {
36: return fetch;
37: } else {
38: if (fallbackbeans == null) {
39: fetchFallbacks();
40: }
41: Object togo = tryLocateFallback(path);
42: return togo;
43: }
44: }
45:
46: private Object tryLocateFallback(String path) {
47: for (int i = 0; i < fallbackbeans.size(); ++i) {
48: FallbackBeanLocator fbl = (FallbackBeanLocator) fallbackbeans
49: .get(i);
50: Object togo = fbl.locateBean(path);
51: if (togo != null)
52: return togo;
53: }
54: return null;
55: }
56:
57: private void fetchFallbacks() {
58: fallbackbeans = new ArrayList();
59: for (int i = 0; i < fallbacks.size(); ++i) {
60: String fallback = fallbacks.stringAt(i);
61: FallbackBeanLocator fbl = (FallbackBeanLocator) wbl
62: .locateBean(fallback);
63: fallbackbeans.add(fbl);
64: }
65: }
66:
67: public boolean remove(String beanname) {
68: return wbl.remove(beanname);
69: }
70:
71: public void set(String beanname, Object toset) {
72: wbl.set(beanname, toset);
73: }
74:
75: }
|