KickJava   Java API By Example, From Geeks To Geeks.

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


1 // Copyright 2007 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.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.Set JavaDoc;
22 import java.util.TreeMap JavaDoc;
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 /**
34  * Implementation of {@link Autowiring}.
35  * Skips properties of primitive type and the standard java data types (String, Double etc.).
36  * Properties that are already assigned are skipped too.
37  * Delegates the autowiring to implementations of {@link AutowiringStrategy}.
38  * If errors occur they are passed to an {@link ErrorHandler}. Depending on its
39  * implementation the wiring either continues with the next property or an exception
40  * is thrown.
41  *
42  * @author Achim Huegen
43  */

44 public class AutowiringImpl implements Autowiring
45 {
46     private static final Log LOG = LogFactory.getLog(AutowiringImpl.class);
47
48     private Map JavaDoc _strategies = new TreeMap JavaDoc();
49
50     private RegistryInfrastructure _registry;
51     
52     private ErrorHandler _errorHandler;
53     
54     private static Set JavaDoc SKIPPED_PROPERTY_TYPES = new HashSet JavaDoc();
55     
56     static {
57         SKIPPED_PROPERTY_TYPES.add(String JavaDoc.class);
58         SKIPPED_PROPERTY_TYPES.add(Double JavaDoc.class);
59         SKIPPED_PROPERTY_TYPES.add(Integer JavaDoc.class);
60         SKIPPED_PROPERTY_TYPES.add(Float JavaDoc.class);
61         SKIPPED_PROPERTY_TYPES.add(Byte JavaDoc.class);
62         SKIPPED_PROPERTY_TYPES.add(Short JavaDoc.class);
63         SKIPPED_PROPERTY_TYPES.add(Character JavaDoc.class);
64     }
65     
66     /**
67      * @param registry reference to registry
68      * @param strategyContributions list with instances of {@link AutowiringStrategyContribution}
69      * @param errorHandler handler for dealing with recoverable errors.
70      */

71     public AutowiringImpl(RegistryInfrastructure registry, List JavaDoc strategyContributions, ErrorHandler errorHandler)
72     {
73         _registry = registry;
74         _errorHandler = errorHandler;
75         // Add the strategies in default order to map
76
for (Iterator JavaDoc iter = strategyContributions.iterator(); iter.hasNext();)
77         {
78             AutowiringStrategyContribution c = (AutowiringStrategyContribution) iter.next();
79             _strategies.put(c.getName(), c.getStrategy());
80         }
81     }
82
83     /**
84      * @see org.apache.hivemind.service.Autowiring#autowireProperties(java.lang.Object)
85      */

86     public Object JavaDoc autowireProperties(Object JavaDoc target)
87     {
88         Set JavaDoc writeablePropertiesSet = new HashSet JavaDoc(PropertyUtils.getWriteableProperties(target));
89         String JavaDoc[] writableProperties = (String JavaDoc[]) writeablePropertiesSet.toArray(new String JavaDoc[writeablePropertiesSet.size()]);
90         return autowireProperties(target, writableProperties);
91     }
92     
93     /**
94      * @see org.apache.hivemind.service.Autowiring#autowireProperties(java.lang.Object, java.lang.String[])
95      */

96     public Object JavaDoc autowireProperties(Object JavaDoc target, String JavaDoc[] propertyNames)
97     {
98         for (int i = 0; i < propertyNames.length; i++)
99         {
100             String JavaDoc propertyName = propertyNames[i];
101             if (isPropertyWirable(target, propertyName)) {
102                 autowirePropertyAllStrategies(target, propertyName);
103             }
104         }
105         return target;
106     }
107
108     /**
109      * Wires a single property by calling all available strategies in the configured order
110      * until a strategy signals that the property has been wired.
111      *
112      * @param target
113      * @param propertyName
114      */

115     private void autowirePropertyAllStrategies(Object JavaDoc target, String JavaDoc propertyName)
116     {
117         try
118         {
119             for (Iterator JavaDoc iter = _strategies.values().iterator(); iter.hasNext();)
120             {
121                 AutowiringStrategy strategy = (AutowiringStrategy) iter.next();
122                 boolean isWired = strategy.autowireProperty(_registry, target, propertyName);
123                 // Stop if strategy has wired the property
124
if (isWired)
125                     break;
126             }
127         }
128         catch (Exception JavaDoc ex)
129         {
130             _errorHandler.error(
131                     LOG,
132                     ServiceMessages.autowirePropertyFailure(propertyName, target.getClass(), ex),
133                     null,
134                     ex);
135         }
136     }
137     
138     /**
139      * @see org.apache.hivemind.service.Autowiring#autowireProperties(java.lang.String, java.lang.Object)
140      */

141     public Object JavaDoc autowireProperties(String JavaDoc strategy, Object JavaDoc target)
142     {
143         Set JavaDoc writeablePropertiesSet = new HashSet JavaDoc(PropertyUtils.getWriteableProperties(target));
144         String JavaDoc[] writableProperties = (String JavaDoc[]) writeablePropertiesSet.toArray(new String JavaDoc[writeablePropertiesSet.size()]);
145         return autowireProperties(strategy, target, writableProperties);
146     }
147
148     /**
149      * @see org.apache.hivemind.service.Autowiring#autowireProperties(java.lang.String, java.lang.Object, java.lang.String[])
150      */

151     public Object JavaDoc autowireProperties(String JavaDoc strategyName, Object JavaDoc target, String JavaDoc[] propertyNames)
152     {
153         for (int i = 0; i < propertyNames.length; i++)
154         {
155             String JavaDoc propertyName = propertyNames[i];
156             if (isPropertyWirable(target, propertyName)) {
157                 autowireProperty(strategyName, target, propertyName);
158             }
159         }
160         return target;
161     }
162     
163     /**
164      * @return true if the property is wirable. Primitive types and the types in
165      * SKIPPED_PROPERTY_TYPES are ignored. If the property is already assigned it is
166      * ignored too.
167      */

168     private boolean isPropertyWirable(Object JavaDoc target, String JavaDoc propertyName)
169     {
170         Class JavaDoc propertyType = PropertyUtils.getPropertyType(target, propertyName);
171         if (propertyType.isPrimitive() || SKIPPED_PROPERTY_TYPES.contains(propertyType)) {
172             return false;
173         } else {
174             // Don't wire if value is already assigned
175
if (PropertyUtils.isReadable(target, propertyName))
176                 return PropertyUtils.read(target, propertyName) == null;
177             else return true;
178         }
179     }
180
181     /**
182      * Wires a single property by using the strategy <code>strategyName</code>
183      * @param strategyName
184      * @param target
185      * @param propertyName
186      * @return true if wiring succeeded
187      */

188     private boolean autowireProperty(String JavaDoc strategyName, Object JavaDoc target, String JavaDoc 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 JavaDoc 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