01: /*
02: * Created on 03-Feb-2006
03: */
04: package uk.org.ponder.beanutil.entity;
05:
06: import java.util.HashMap;
07: import java.util.Map;
08:
09: import uk.org.ponder.beanutil.BeanLocator;
10: import uk.org.ponder.beanutil.support.ELEvaluator;
11: import uk.org.ponder.reflect.DeepBeanCloner;
12: import uk.org.ponder.util.ObjectFactory;
13:
14: /**
15: * Will "explode" a bean prototype into a "freely indexed" address space by
16: * cloning or instantiation. Has four main instantiation strategies, to cope
17: * with increasing orders of dependence:
18: * <p>
19: * 1. A simple classname - the class designates a default-constructible bean.
20: * <p>
21: * 2. An "exemplar" instance - the instance will be deep-cloned for each
22: * required
23: * <p>
24: * 3. An EL path from which a prototype instance will be fetched (assumed to be
25: * the path of a non-singleton bean)
26: * <p>
27: * 4. An instance of ObjectFactory delivered to setFactory - this will be done
28: * automatically by RSAC on receiving an inner bean definition.
29: *
30: * @author Antranig Basman (amb26@ponder.org.uk)
31: *
32: */
33:
34: public class BeanExploder implements BeanLocator {
35: private Object exemplar;
36:
37: private Map beans = new HashMap();
38:
39: private DeepBeanCloner deepbeancloner;
40: private Class beanclass;
41:
42: private String beanname;
43:
44: private ELEvaluator elEvaluator;
45:
46: private ObjectFactory factory;
47:
48: public void setBeanClass(Class beanclass) {
49: this .beanclass = beanclass;
50: }
51:
52: // Should be the path to a non-singleton Spring bean
53: public void setBeanName(String beanname) {
54: this .beanname = beanname;
55: }
56:
57: public void setExemplar(Object exemplar) {
58: this .exemplar = exemplar;
59: }
60:
61: public void setDeepBeanCloner(DeepBeanCloner deepbeancloner) {
62: this .deepbeancloner = deepbeancloner;
63: }
64:
65: public void setELEvaluator(ELEvaluator elEvaluator) {
66: this .elEvaluator = elEvaluator;
67: }
68:
69: public void setFactory(ObjectFactory factory) {
70: this .factory = factory;
71: }
72:
73: public Object locateBean(String path) {
74: Object togo = beans.get(path);
75: if (togo == null) {
76: if (factory != null) {
77: togo = factory.getObject();
78: } else if (beanname != null) {
79: togo = elEvaluator.getBean(beanname);
80: } else if (beanclass != null) {
81: togo = deepbeancloner.getReflectiveCache().construct(
82: beanclass);
83: } else if (exemplar != null) {
84: togo = deepbeancloner.cloneBean(exemplar);
85: }
86: beans.put(path, togo);
87: }
88: return togo;
89: }
90:
91: }
|