|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| BuilderFacet.java | 80% | 85% | 80% | 82.9% |
|
||||||||||||||
| 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 _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 getFacetValue(ServiceImplementationFactoryParameters factoryParameters, | |
| 46 | Class targetType); | |
| 47 | ||
| 48 | public abstract boolean isAssignableToType( | |
| 49 | ServiceImplementationFactoryParameters factoryParameters, Class targetType); | |
| 50 | ||
| 51 | 5113 | public String getPropertyName() |
| 52 | { | |
| 53 | 5113 | return _propertyName; |
| 54 | } | |
| 55 | ||
| 56 | 431 | public void setPropertyName(String string) |
| 57 | { | |
| 58 | 431 | _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 | 5114 | public String autowire(Object target, ServiceImplementationFactoryParameters factoryParameters) |
| 78 | { | |
| 79 | 5114 | if (_propertyName != null) |
| 80 | 431 | return null; |
| 81 | ||
| 82 | 4683 | String defaultPropertyName = getDefaultPropertyName(); |
| 83 | ||
| 84 | 4679 | if (defaultPropertyName == null) |
| 85 | 0 | return null; |
| 86 | ||
| 87 | 4680 | if (!PropertyUtils.isWritable(target, defaultPropertyName)) |
| 88 | 3938 | return null; |
| 89 | ||
| 90 | 745 | Class propertyType = PropertyUtils.getPropertyType(target, defaultPropertyName); |
| 91 | ||
| 92 | 745 | if (isAssignableToType(factoryParameters, propertyType)) |
| 93 | { | |
| 94 | 745 | Object facetValue = getFacetValue(factoryParameters, propertyType); |
| 95 | ||
| 96 | 745 | PropertyUtils.write(target, defaultPropertyName, facetValue); |
| 97 | ||
| 98 | 744 | Log log = factoryParameters.getLog(); |
| 99 | ||
| 100 | 745 | if (log.isDebugEnabled()) |
| 101 | 6 | log.debug("Autowired property " + defaultPropertyName + " to " + facetValue); |
| 102 | ||
| 103 | 745 | return defaultPropertyName; |
| 104 | } | |
| 105 | ||
| 106 | 0 | 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 | 0 | protected String getDefaultPropertyName() |
| 114 | { | |
| 115 | 0 | return null; |
| 116 | } | |
| 117 | ||
| 118 | /** @since 1.1 */ | |
| 119 | 5 | public boolean canAutowireConstructorParameter() |
| 120 | { | |
| 121 | 5 | return false; |
| 122 | } | |
| 123 | ||
| 124 | } |
|
||||||||||