01: /*
02: * Created on 7 Aug 2006
03: */
04: package uk.org.ponder.springutil;
05:
06: import org.springframework.beans.BeansException;
07: import org.springframework.context.ApplicationContext;
08: import org.springframework.context.ApplicationContextAware;
09:
10: import uk.org.ponder.rsac.RSACBeanLocator;
11: import uk.org.ponder.stringutil.StringList;
12:
13: /**
14: * A {@link TargetListAggregatingBean} that will acquire all beans of a particular type
15: * (excluding Factory products) and register either their names, or the beans
16: * themselves, onto the TLAB target.
17: *
18: * @author Antranig Basman (antranig@caret.cam.ac.uk)
19: *
20: */
21:
22: public class ByClassTLAB extends StaticTLAB implements
23: ApplicationContextAware {
24: private Class targetClass;
25: private boolean deliverBeans = true;
26: private RSACBeanLocator rsacbeanlocator;
27:
28: public void setTargetClass(Class targetClass) {
29: this .targetClass = targetClass;
30: }
31:
32: /**
33: * If set to <code>false</code> (the default is <code>true</code>) this
34: * will deliver a list the actual beans as the TLAB value rather than just a
35: * list of their names.
36: */
37: public void setDeliverBeans(boolean deliverBeans) {
38: this .deliverBeans = deliverBeans;
39: }
40:
41: public void setRSACBeanLocator(RSACBeanLocator rsacbeanlocator) {
42: this .rsacbeanlocator = rsacbeanlocator;
43: }
44:
45: public void init() {
46: if (rsacbeanlocator != null) {
47: String[] beannames = rsacbeanlocator
48: .beanNamesForClass(targetClass);
49: applyNames(beannames);
50: }
51: }
52:
53: private void applyNames(String[] beannames) {
54: if (deliverBeans) {
55: setValueRefs(beannames);
56: } else {
57: setValue(new StringList(beannames));
58: }
59: }
60:
61: public void setApplicationContext(
62: ApplicationContext applicationContext)
63: throws BeansException {
64: String[] beannames = applicationContext.getBeanNamesForType(
65: targetClass, false, false);
66: applyNames(beannames);
67: }
68:
69: }
|