|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
BeanFactoryImpl.java | 100% | 100% | 100% | 100% |
|
1 |
// Copyright 2004 The Apache Software Foundation
|
|
2 |
//
|
|
3 |
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4 |
// you may not use this file except in compliance with the License.
|
|
5 |
// You may obtain a copy of the License at
|
|
6 |
//
|
|
7 |
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8 |
//
|
|
9 |
// Unless required by applicable law or agreed to in writing, software
|
|
10 |
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12 |
// See the License for the specific language governing permissions and
|
|
13 |
// limitations under the License.
|
|
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.commons.logging.Log;
|
|
24 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
25 |
import org.apache.hivemind.ErrorHandler;
|
|
26 |
import org.apache.hivemind.HiveMind;
|
|
27 |
import org.apache.hivemind.impl.BaseLocatable;
|
|
28 |
import org.apache.hivemind.lib.BeanFactory;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* Implementation of {@link org.apache.hivemind.lib.BeanFactory}.
|
|
32 |
*
|
|
33 |
* @author Howard Lewis Ship
|
|
34 |
*/
|
|
35 |
public class BeanFactoryImpl extends BaseLocatable implements BeanFactory |
|
36 |
{ |
|
37 |
private Log _log;
|
|
38 |
private ErrorHandler _errorHandler;
|
|
39 |
private Class _vendType;
|
|
40 |
private Map _contributions = new HashMap(); |
|
41 |
private Map _cache = new HashMap(); |
|
42 |
private boolean _defaultCacheable; |
|
43 |
|
|
44 | 11 |
public BeanFactoryImpl(
|
45 |
Log log, |
|
46 |
ErrorHandler errorHandler, |
|
47 |
Class vendType, |
|
48 |
List contributions, |
|
49 |
boolean defaultCacheable)
|
|
50 |
{ |
|
51 | 11 |
_log = log; |
52 | 11 |
_errorHandler = errorHandler; |
53 | 11 |
_vendType = vendType; |
54 | 11 |
_defaultCacheable = defaultCacheable; |
55 |
|
|
56 | 11 |
processContributions(contributions); |
57 |
} |
|
58 |
|
|
59 | 11 |
private void processContributions(List list) |
60 |
{ |
|
61 | 11 |
Iterator i = list.iterator(); |
62 |
|
|
63 | 11 |
while (i.hasNext())
|
64 |
{ |
|
65 | 13 |
BeanFactoryContribution c = (BeanFactoryContribution) i.next(); |
66 |
|
|
67 | 13 |
Class beanClass = c.getBeanClass(); |
68 |
|
|
69 | 13 |
if (beanClass.isInterface() || beanClass.isArray() || beanClass.isPrimitive())
|
70 |
{ |
|
71 | 3 |
_errorHandler.error( |
72 |
_log, |
|
73 |
FactoryMessages.invalidContributionClass(c), |
|
74 |
c.getLocation(), |
|
75 |
null);
|
|
76 | 3 |
continue;
|
77 |
} |
|
78 |
|
|
79 | 10 |
if (!_vendType.isAssignableFrom(beanClass))
|
80 |
{ |
|
81 | 1 |
_errorHandler.error( |
82 |
_log, |
|
83 |
FactoryMessages.wrongContributionType(c, _vendType), |
|
84 |
c.getLocation(), |
|
85 |
null);
|
|
86 | 1 |
continue;
|
87 |
} |
|
88 |
|
|
89 | 9 |
String name = c.getName(); |
90 |
|
|
91 | 9 |
BeanFactoryContribution existing = |
92 |
(BeanFactoryContribution) _contributions.get(name); |
|
93 |
|
|
94 | 9 |
if (existing != null) |
95 |
{ |
|
96 | 1 |
_errorHandler.error( |
97 |
_log, |
|
98 |
FactoryMessages.dupeContribution(existing), |
|
99 |
existing.getLocation(), |
|
100 |
null);
|
|
101 | 1 |
continue;
|
102 |
} |
|
103 |
|
|
104 |
// Passed the checks, good to go.
|
|
105 |
|
|
106 | 8 |
_contributions.put(name, c); |
107 |
} |
|
108 |
} |
|
109 |
|
|
110 | 14 |
public synchronized Object get(String locator) |
111 |
{ |
|
112 | 14 |
Object result = _cache.get(locator); |
113 |
|
|
114 | 14 |
if (result == null) |
115 | 13 |
result = create(locator); |
116 |
|
|
117 | 9 |
return result;
|
118 |
} |
|
119 |
|
|
120 |
// Implicitly synchronized by get()
|
|
121 |
|
|
122 | 13 |
private Object create(String locator)
|
123 |
{ |
|
124 | 13 |
int commax = locator.indexOf(',');
|
125 |
|
|
126 | 13 |
String name = commax < 0 ? locator.trim() : locator.substring(0, commax); |
127 | 13 |
String initializer = commax < 0 ? null : locator.substring(commax + 1).trim();
|
128 |
|
|
129 | 13 |
BeanFactoryContribution c = (BeanFactoryContribution) _contributions.get(name); |
130 |
|
|
131 | 13 |
if (c == null) |
132 | 4 |
throw new ApplicationRuntimeException(FactoryMessages.unknownContribution(name)); |
133 |
|
|
134 | 9 |
Object result = construct(c, initializer); |
135 |
|
|
136 | 8 |
if (c.getStoreResultInCache(_defaultCacheable))
|
137 | 6 |
_cache.put(locator, result); |
138 |
|
|
139 | 8 |
return result;
|
140 |
} |
|
141 |
|
|
142 | 9 |
private Object construct(BeanFactoryContribution contribution, String initializer)
|
143 |
{ |
|
144 | 9 |
Class beanClass = contribution.getBeanClass(); |
145 |
|
|
146 | 9 |
try
|
147 |
{ |
|
148 | 9 |
if (HiveMind.isBlank(initializer))
|
149 | 5 |
return beanClass.newInstance();
|
150 |
|
|
151 | 4 |
Constructor c = beanClass.getConstructor(new Class[] { String.class }); |
152 |
|
|
153 | 4 |
return c.newInstance(new Object[] { initializer }); |
154 |
} |
|
155 |
catch (Exception ex)
|
|
156 |
{ |
|
157 | 1 |
throw new ApplicationRuntimeException( |
158 |
FactoryMessages.unableToInstantiate(beanClass, ex), |
|
159 |
contribution.getLocation(), |
|
160 |
ex); |
|
161 |
|
|
162 |
} |
|
163 |
} |
|
164 |
|
|
165 |
} |
|
166 |
|
|