1 15 package org.apache.hivemind.service.impl; 16 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 import java.util.Set ; 22 import java.util.TreeMap ; 23 24 import org.apache.commons.logging.Log; 25 import org.apache.commons.logging.LogFactory; 26 import org.apache.hivemind.ApplicationRuntimeException; 27 import org.apache.hivemind.ErrorHandler; 28 import org.apache.hivemind.internal.RegistryInfrastructure; 29 import org.apache.hivemind.service.Autowiring; 30 import org.apache.hivemind.service.AutowiringStrategy; 31 import org.apache.hivemind.util.PropertyUtils; 32 33 44 public class AutowiringImpl implements Autowiring 45 { 46 private static final Log LOG = LogFactory.getLog(AutowiringImpl.class); 47 48 private Map _strategies = new TreeMap (); 49 50 private RegistryInfrastructure _registry; 51 52 private ErrorHandler _errorHandler; 53 54 private static Set SKIPPED_PROPERTY_TYPES = new HashSet (); 55 56 static { 57 SKIPPED_PROPERTY_TYPES.add(String .class); 58 SKIPPED_PROPERTY_TYPES.add(Double .class); 59 SKIPPED_PROPERTY_TYPES.add(Integer .class); 60 SKIPPED_PROPERTY_TYPES.add(Float .class); 61 SKIPPED_PROPERTY_TYPES.add(Byte .class); 62 SKIPPED_PROPERTY_TYPES.add(Short .class); 63 SKIPPED_PROPERTY_TYPES.add(Character .class); 64 } 65 66 71 public AutowiringImpl(RegistryInfrastructure registry, List strategyContributions, ErrorHandler errorHandler) 72 { 73 _registry = registry; 74 _errorHandler = errorHandler; 75 for (Iterator iter = strategyContributions.iterator(); iter.hasNext();) 77 { 78 AutowiringStrategyContribution c = (AutowiringStrategyContribution) iter.next(); 79 _strategies.put(c.getName(), c.getStrategy()); 80 } 81 } 82 83 86 public Object autowireProperties(Object target) 87 { 88 Set writeablePropertiesSet = new HashSet (PropertyUtils.getWriteableProperties(target)); 89 String [] writableProperties = (String []) writeablePropertiesSet.toArray(new String [writeablePropertiesSet.size()]); 90 return autowireProperties(target, writableProperties); 91 } 92 93 96 public Object autowireProperties(Object target, String [] propertyNames) 97 { 98 for (int i = 0; i < propertyNames.length; i++) 99 { 100 String propertyName = propertyNames[i]; 101 if (isPropertyWirable(target, propertyName)) { 102 autowirePropertyAllStrategies(target, propertyName); 103 } 104 } 105 return target; 106 } 107 108 115 private void autowirePropertyAllStrategies(Object target, String propertyName) 116 { 117 try 118 { 119 for (Iterator iter = _strategies.values().iterator(); iter.hasNext();) 120 { 121 AutowiringStrategy strategy = (AutowiringStrategy) iter.next(); 122 boolean isWired = strategy.autowireProperty(_registry, target, propertyName); 123 if (isWired) 125 break; 126 } 127 } 128 catch (Exception ex) 129 { 130 _errorHandler.error( 131 LOG, 132 ServiceMessages.autowirePropertyFailure(propertyName, target.getClass(), ex), 133 null, 134 ex); 135 } 136 } 137 138 141 public Object autowireProperties(String strategy, Object target) 142 { 143 Set writeablePropertiesSet = new HashSet (PropertyUtils.getWriteableProperties(target)); 144 String [] writableProperties = (String []) writeablePropertiesSet.toArray(new String [writeablePropertiesSet.size()]); 145 return autowireProperties(strategy, target, writableProperties); 146 } 147 148 151 public Object autowireProperties(String strategyName, Object target, String [] propertyNames) 152 { 153 for (int i = 0; i < propertyNames.length; i++) 154 { 155 String propertyName = propertyNames[i]; 156 if (isPropertyWirable(target, propertyName)) { 157 autowireProperty(strategyName, target, propertyName); 158 } 159 } 160 return target; 161 } 162 163 168 private boolean isPropertyWirable(Object target, String propertyName) 169 { 170 Class propertyType = PropertyUtils.getPropertyType(target, propertyName); 171 if (propertyType.isPrimitive() || SKIPPED_PROPERTY_TYPES.contains(propertyType)) { 172 return false; 173 } else { 174 if (PropertyUtils.isReadable(target, propertyName)) 176 return PropertyUtils.read(target, propertyName) == null; 177 else return true; 178 } 179 } 180 181 188 private boolean autowireProperty(String strategyName, Object target, String propertyName) 189 { 190 try 191 { 192 AutowiringStrategy strategy = (AutowiringStrategy) _strategies.get(strategyName); 193 if (strategy == null) { 194 throw new ApplicationRuntimeException(ServiceMessages.unknownStrategy(strategyName)); 195 } 196 return strategy.autowireProperty(_registry, target, propertyName); 197 } 198 catch (Exception ex) 199 { 200 _errorHandler.error( 201 LOG, 202 ServiceMessages.autowirePropertyFailure(propertyName, target.getClass(), ex), 203 null, 204 ex); 205 } 206 return false; 207 } 208 209 } 210 | Popular Tags |