KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.logging.Log;
18 import org.apache.hivemind.ServiceImplementationFactoryParameters;
19 import org.apache.hivemind.impl.BaseLocatable;
20 import org.apache.hivemind.util.PropertyUtils;
21
22 /**
23  * Represents one facet of constructing a service implementation instance. A facet is either a
24  * property to be set on the constructed instance, or a parameter to the instance class'
25  * constructor. Facets are nested properties within
26  * {@link org.apache.hivemind.service.impl.BuilderParameter}, and are used by
27  * {@link org.apache.hivemind.service.impl.BuilderFactory}.
28  *
29  * @author Howard Lewis Ship
30  */

31 public abstract class BuilderFacet extends BaseLocatable
32 {
33     private String JavaDoc _propertyName;
34
35     /**
36      * Implemented in subclasses to provide a specific value for the facet (for use as a constructor
37      * parameter, or as a value to set a property to).
38      *
39      * @param factoryParameters
40      * the parameters that define the service point and its environment
41      * @param targetType
42      * the desired property type (extracted from the property type of the property to be
43      * updated, when a property is known)
44      */

45     public abstract Object JavaDoc getFacetValue(ServiceImplementationFactoryParameters factoryParameters,
46             Class JavaDoc targetType);
47
48     public abstract boolean isAssignableToType(
49             ServiceImplementationFactoryParameters factoryParameters, Class JavaDoc targetType);
50
51     public String JavaDoc getPropertyName()
52     {
53         return _propertyName;
54     }
55
56     public void setPropertyName(String JavaDoc string)
57     {
58         _propertyName = string;
59     }
60
61     /**
62      * Attempts to autowire a property of the target. This requires that
63      * <ul>
64      * <li>The facet type defines a default property name and facet type
65      * <li>The facet instance does not have a specified property name
66      * <li>The (default) property is writeable
67      * <li>The (default) property is assignable from the facet type
68      * </ul>
69      * If all conditions are met, then the property is updated to the facet value, and the property
70      * name is returned. In all other cases, null is returned.
71      *
72      * @param target
73      * The service implementation being constructed
74      * @param factoryParameters
75      * the parameters that define the service point and its environment
76      */

77     public String JavaDoc autowire(Object JavaDoc target, ServiceImplementationFactoryParameters factoryParameters)
78     {
79         if (_propertyName != null)
80             return null;
81
82         String JavaDoc defaultPropertyName = getDefaultPropertyName();
83
84         if (defaultPropertyName == null)
85             return null;
86
87         if (!PropertyUtils.isWritable(target, defaultPropertyName))
88             return null;
89
90         Class JavaDoc propertyType = PropertyUtils.getPropertyType(target, defaultPropertyName);
91
92         if (isAssignableToType(factoryParameters, propertyType))
93         {
94             Object JavaDoc facetValue = getFacetValue(factoryParameters, propertyType);
95
96             PropertyUtils.write(target, defaultPropertyName, facetValue);
97
98             Log log = factoryParameters.getLog();
99
100             if (log.isDebugEnabled())
101                 log.debug("Autowired property " + defaultPropertyName + " to " + facetValue);
102
103             return defaultPropertyName;
104         }
105
106         return null;
107     }
108
109     /**
110      * Returns null. Subclasses can provide the default name for a property used by
111      * {@link #autowire(Object, ServiceImplementationFactoryParameters)}.
112      */

113     protected String JavaDoc getDefaultPropertyName()
114     {
115         return null;
116     }
117
118     /** @since 1.1 */
119     public boolean canAutowireConstructorParameter()
120     {
121         return false;
122     }
123
124 }
Popular Tags