KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > service > impl > BuilderPropertyFacet


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 JavaDoc;
18 import java.util.Map JavaDoc;
19
20 import org.apache.hivemind.Location;
21 import org.apache.hivemind.ServiceImplementationFactoryParameters;
22 import org.apache.hivemind.TranslatorManager;
23 import org.apache.hivemind.internal.Module;
24 import org.apache.hivemind.schema.Translator;
25 import org.apache.hivemind.util.ConstructorUtils;
26
27 /**
28  * Implementation of {@link org.apache.hivemind.service.impl.BuilderFacet} that stores a value. This
29  * corresponds to the <set> type elements and all constructor parameter elements. The value is
30  * not resolved until needed using a specified {@link Translator}.
31  *
32  * @author Howard Lewis Ship
33  */

34 public class BuilderPropertyFacet extends BuilderFacet
35 {
36     private String JavaDoc _translatorName;
37
38     private String JavaDoc _literalValue;
39
40     /**
41      * Cache for translated values to prevent calling
42      * {@link Translator#translate(Module, Class, String, Location)} twice.
43      */

44     private Map JavaDoc _valuesCache = new HashMap JavaDoc();
45
46     public Object JavaDoc getFacetValue(ServiceImplementationFactoryParameters factoryParameters,
47             Class JavaDoc targetType)
48     {
49         Object JavaDoc result = _valuesCache.get(targetType);
50
51         if (result == null)
52         {
53             TranslatorManager translatorManager = (TranslatorManager) factoryParameters.getInvokingModule().getService(TranslatorManager.class);
54             Translator translator = translatorManager.getTranslator(_translatorName);
55
56             result = translator.translate(
57                     factoryParameters.getInvokingModule(),
58                     targetType,
59                     _literalValue,
60                     getLocation());
61
62             _valuesCache.put(targetType, result);
63         }
64
65         return result;
66     }
67
68     public boolean isAssignableToType(ServiceImplementationFactoryParameters factoryParameters,
69             Class JavaDoc targetType)
70     {
71         // TODO should Translator declare an analoguous isAssignableToType method?
72
Object JavaDoc facetValue = getFacetValue(factoryParameters, targetType);
73
74         if (facetValue == null)
75             return !targetType.isPrimitive();
76
77         return ConstructorUtils.isCompatible(targetType, facetValue.getClass());
78     }
79
80     /** @since 1.1 */
81     public void setTranslator(String JavaDoc translatorName)
82     {
83         _translatorName = translatorName;
84     }
85
86     public void setValue(String JavaDoc value)
87     {
88         _literalValue = value;
89     }
90
91 }
Popular Tags