|
|||||||||||||||||||
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 | |||||||||||||||
DefaultImplementationBuilderImpl.java | 100% | 95.2% | 100% | 97% |
|
1 |
// Copyright 2004, 2005 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.impl;
|
|
16 |
|
|
17 |
import java.util.Collections;
|
|
18 |
import java.util.HashMap;
|
|
19 |
import java.util.Map;
|
|
20 |
|
|
21 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
22 |
import org.apache.hivemind.impl.BaseLocatable;
|
|
23 |
import org.apache.hivemind.lib.DefaultImplementationBuilder;
|
|
24 |
import org.apache.hivemind.service.ClassFab;
|
|
25 |
import org.apache.hivemind.service.ClassFabUtils;
|
|
26 |
import org.apache.hivemind.service.ClassFactory;
|
|
27 |
import org.apache.hivemind.service.MethodIterator;
|
|
28 |
|
|
29 |
/**
|
|
30 |
* Implemenation of {@link org.apache.hivemind.lib.DefaultImplementationBuilder}.
|
|
31 |
*
|
|
32 |
* @author Howard Lewis Ship
|
|
33 |
*/
|
|
34 |
public class DefaultImplementationBuilderImpl extends BaseLocatable implements |
|
35 |
DefaultImplementationBuilder |
|
36 |
{ |
|
37 |
private Map _instances = Collections.synchronizedMap(new HashMap()); |
|
38 |
|
|
39 |
private ClassFactory _classFactory;
|
|
40 |
|
|
41 | 10 |
public Object buildDefaultImplementation(Class interfaceType)
|
42 |
{ |
|
43 | 10 |
Object result = _instances.get(interfaceType); |
44 |
|
|
45 | 10 |
if (result == null) |
46 |
{ |
|
47 | 9 |
result = create(interfaceType); |
48 |
|
|
49 | 8 |
_instances.put(interfaceType, result); |
50 |
} |
|
51 |
|
|
52 | 9 |
return result;
|
53 |
} |
|
54 |
|
|
55 | 9 |
private Object create(Class interfaceType)
|
56 |
{ |
|
57 | 9 |
Class defaultClass = createClass(interfaceType); |
58 |
|
|
59 | 8 |
try
|
60 |
{ |
|
61 | 8 |
return defaultClass.newInstance();
|
62 |
} |
|
63 |
catch (Exception ex)
|
|
64 |
{ |
|
65 | 0 |
throw new ApplicationRuntimeException(ImplMessages.unableToCreateDefaultImplementation( |
66 |
interfaceType, |
|
67 |
ex), ex); |
|
68 |
} |
|
69 |
} |
|
70 |
|
|
71 | 9 |
private Class createClass(Class interfaceType)
|
72 |
{ |
|
73 | 9 |
if (!interfaceType.isInterface())
|
74 | 1 |
throw new ApplicationRuntimeException(ImplMessages.notAnInterface(interfaceType)); |
75 |
|
|
76 | 8 |
String name = ClassFabUtils.generateClassName(interfaceType); |
77 |
|
|
78 | 8 |
ClassFab cf = _classFactory.newClass(name, Object.class);
|
79 |
|
|
80 | 8 |
cf.addInterface(interfaceType); |
81 |
|
|
82 | 8 |
MethodIterator mi = new MethodIterator(interfaceType);
|
83 |
|
|
84 | 8 |
while (mi.hasNext())
|
85 |
{ |
|
86 | 10 |
ClassFabUtils.addNoOpMethod(cf, mi.next()); |
87 |
} |
|
88 |
|
|
89 | 8 |
if (!mi.getToString())
|
90 | 7 |
ClassFabUtils.addToStringMethod(cf, ImplMessages |
91 |
.defaultImplementationDescription(interfaceType)); |
|
92 |
|
|
93 | 8 |
return cf.createClass();
|
94 |
} |
|
95 |
|
|
96 | 14 |
public void setClassFactory(ClassFactory factory) |
97 |
{ |
|
98 | 14 |
_classFactory = factory; |
99 |
} |
|
100 |
|
|
101 |
} |
|