1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.lib.factory; |
16 |
| |
17 |
| import java.lang.reflect.Constructor; |
18 |
| import java.util.HashMap; |
19 |
| import java.util.Iterator; |
20 |
| import java.util.List; |
21 |
| import java.util.Map; |
22 |
| |
23 |
| import org.apache.hivemind.ApplicationRuntimeException; |
24 |
| import org.apache.hivemind.ErrorLog; |
25 |
| import org.apache.hivemind.HiveMind; |
26 |
| import org.apache.hivemind.impl.BaseLocatable; |
27 |
| import org.apache.hivemind.lib.BeanFactory; |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| public class BeanFactoryImpl extends BaseLocatable implements BeanFactory |
35 |
| { |
36 |
| private ErrorLog _errorLog; |
37 |
| |
38 |
| private Class _vendType; |
39 |
| |
40 |
| private Map _contributions = new HashMap(); |
41 |
| |
42 |
| private Map _cache = new HashMap(); |
43 |
| |
44 |
| private boolean _defaultCacheable; |
45 |
| |
46 |
12
| public BeanFactoryImpl(ErrorLog errorLog, Class vendType, List contributions,
|
47 |
| boolean defaultCacheable) |
48 |
| { |
49 |
12
| _errorLog = errorLog;
|
50 |
12
| _vendType = vendType;
|
51 |
12
| _defaultCacheable = defaultCacheable;
|
52 |
| |
53 |
12
| processContributions(contributions);
|
54 |
| } |
55 |
| |
56 |
2
| public boolean contains(String locator)
|
57 |
| { |
58 |
2
| int commax = locator.indexOf(',');
|
59 |
| |
60 |
2
| String name = commax < 0 ? locator.trim() : locator.substring(0, commax);
|
61 |
| |
62 |
2
| return _contributions.containsKey(name);
|
63 |
| } |
64 |
| |
65 |
12
| private void processContributions(List list)
|
66 |
| { |
67 |
12
| Iterator i = list.iterator();
|
68 |
| |
69 |
12
| while (i.hasNext())
|
70 |
| { |
71 |
13
| BeanFactoryContribution c = (BeanFactoryContribution) i.next();
|
72 |
| |
73 |
13
| Class beanClass = c.getBeanClass();
|
74 |
| |
75 |
13
| if (beanClass.isInterface() || beanClass.isArray() || beanClass.isPrimitive())
|
76 |
| { |
77 |
3
| _errorLog.error(FactoryMessages.invalidContributionClass(c), c.getLocation(), null);
|
78 |
3
| continue;
|
79 |
| } |
80 |
| |
81 |
10
| if (!_vendType.isAssignableFrom(beanClass))
|
82 |
| { |
83 |
1
| _errorLog.error(FactoryMessages.wrongContributionType(c, _vendType), c
|
84 |
| .getLocation(), null); |
85 |
1
| continue;
|
86 |
| } |
87 |
| |
88 |
9
| _contributions.put(c.getName(), c);
|
89 |
| } |
90 |
| } |
91 |
| |
92 |
13
| public synchronized Object get(String locator)
|
93 |
| { |
94 |
13
| Object result = _cache.get(locator);
|
95 |
| |
96 |
13
| if (result == null)
|
97 |
12
| result = create(locator);
|
98 |
| |
99 |
8
| return result;
|
100 |
| } |
101 |
| |
102 |
| |
103 |
| |
104 |
12
| private Object create(String locator)
|
105 |
| { |
106 |
12
| int commax = locator.indexOf(',');
|
107 |
| |
108 |
12
| String name = commax < 0 ? locator.trim() : locator.substring(0, commax);
|
109 |
12
| String initializer = commax < 0 ? null : locator.substring(commax + 1).trim();
|
110 |
| |
111 |
12
| BeanFactoryContribution c = (BeanFactoryContribution) _contributions.get(name);
|
112 |
| |
113 |
12
| if (c == null)
|
114 |
4
| throw new ApplicationRuntimeException(FactoryMessages.unknownContribution(name));
|
115 |
| |
116 |
8
| Object result = construct(c, initializer);
|
117 |
| |
118 |
7
| if (c.getStoreResultInCache(_defaultCacheable))
|
119 |
5
| _cache.put(locator, result);
|
120 |
| |
121 |
7
| return result;
|
122 |
| } |
123 |
| |
124 |
8
| private Object construct(BeanFactoryContribution contribution, String initializer)
|
125 |
| { |
126 |
8
| Class beanClass = contribution.getBeanClass();
|
127 |
| |
128 |
8
| try
|
129 |
| { |
130 |
8
| if (HiveMind.isBlank(initializer))
|
131 |
4
| return beanClass.newInstance();
|
132 |
| |
133 |
4
| Constructor c = beanClass.getConstructor(new Class[]
|
134 |
| { String.class }); |
135 |
| |
136 |
4
| return c.newInstance(new Object[]
|
137 |
| { initializer }); |
138 |
| } |
139 |
| catch (Exception ex) |
140 |
| { |
141 |
1
| throw new ApplicationRuntimeException(FactoryMessages
|
142 |
| .unableToInstantiate(beanClass, ex), contribution.getLocation(), ex); |
143 |
| |
144 |
| } |
145 |
| } |
146 |
| |
147 |
| } |