|
|||||||||||||||||||
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 | |||||||||||||||
BuilderPropertyFacet.java | 100% | 100% | 100% | 100% |
|
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.service.impl;
|
|
16 |
|
|
17 |
import java.util.HashMap;
|
|
18 |
import java.util.Map;
|
|
19 |
|
|
20 |
import org.apache.hivemind.ApplicationRuntimeException;
|
|
21 |
import org.apache.hivemind.ServiceImplementationFactoryParameters;
|
|
22 |
import org.apache.hivemind.schema.Translator;
|
|
23 |
import org.apache.hivemind.util.ConstructorUtils;
|
|
24 |
|
|
25 |
/**
|
|
26 |
* Implementation of {@link org.apache.hivemind.service.impl.BuilderFacet}that stores a value. This
|
|
27 |
* corresponds to the <set> type elements and all constructor parameter elements. The value is
|
|
28 |
* not resolved until needed using a specified {@link Translator}.
|
|
29 |
*
|
|
30 |
* @author Howard Lewis Ship
|
|
31 |
*/
|
|
32 |
public class BuilderPropertyFacet extends BuilderFacet |
|
33 |
{ |
|
34 |
private String _translatorName;
|
|
35 |
|
|
36 |
private String _literalValue;
|
|
37 |
|
|
38 |
/**
|
|
39 |
* Cache for translated values to prevent calling
|
|
40 |
* {@link Translator#translate(Module, Class, String, Location)} twice.
|
|
41 |
*/
|
|
42 |
private Map _valuesCache = new HashMap(); |
|
43 |
|
|
44 | 423 |
public Object getFacetValue(ServiceImplementationFactoryParameters factoryParameters,
|
45 |
Class targetType) |
|
46 |
{ |
|
47 | 423 |
Object result = _valuesCache.get(targetType); |
48 |
|
|
49 | 423 |
if (result == null) |
50 |
{ |
|
51 | 414 |
Translator translator = factoryParameters.getInvokingModule().getTranslator( |
52 |
_translatorName); |
|
53 |
|
|
54 | 414 |
result = translator.translate( |
55 |
factoryParameters.getInvokingModule(), |
|
56 |
targetType, |
|
57 |
_literalValue, |
|
58 |
getLocation()); |
|
59 |
|
|
60 | 412 |
_valuesCache.put(targetType, result); |
61 |
} |
|
62 |
|
|
63 | 421 |
return result;
|
64 |
} |
|
65 |
|
|
66 | 20 |
public boolean isAssignableToType(ServiceImplementationFactoryParameters factoryParameters, |
67 |
Class targetType) |
|
68 |
{ |
|
69 | 20 |
try
|
70 |
{ |
|
71 |
// TODO should Translator declare an analoguous isAssignableToType method?
|
|
72 | 20 |
Object facetValue = getFacetValue(factoryParameters, targetType); |
73 |
|
|
74 | 19 |
if (facetValue == null) |
75 | 1 |
return !targetType.isPrimitive();
|
76 |
|
|
77 | 18 |
return ConstructorUtils.isCompatible(targetType, facetValue.getClass());
|
78 |
} |
|
79 |
catch (ApplicationRuntimeException e)
|
|
80 |
{ |
|
81 | 1 |
return false; |
82 |
} |
|
83 |
} |
|
84 |
|
|
85 |
/** @since 1.1 */
|
|
86 | 403 |
public void setTranslator(String translatorName) |
87 |
{ |
|
88 | 403 |
_translatorName = translatorName; |
89 |
} |
|
90 |
|
|
91 | 402 |
public void setValue(String value) |
92 |
{ |
|
93 | 402 |
_literalValue = value; |
94 |
} |
|
95 |
|
|
96 |
} |
|