KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.commons.logging.Log;
18 import org.apache.commons.logging.LogFactory;
19 import org.apache.hivemind.internal.RegistryInfrastructure;
20 import org.apache.hivemind.service.AutowiringStrategy;
21 import org.apache.hivemind.util.PropertyUtils;
22
23 /**
24  * Implementation of {@link AutowiringStrategy} that searches for
25  * a matching service by the type of a property.
26  *
27  * @author Achim Huegen
28  */

29 public class AutowiringByTypeStrategy implements AutowiringStrategy
30 {
31     private static final Log LOG = LogFactory.getLog(AutowiringByTypeStrategy.class);
32
33     public boolean autowireProperty(RegistryInfrastructure registry, Object JavaDoc target, String JavaDoc propertyName)
34     {
35         Class JavaDoc propertyType = PropertyUtils.getPropertyType(target, propertyName);
36
37         // search for a property with an interface that matches the property type
38
if (registry.containsService(propertyType, null))
39         {
40             Object JavaDoc collaboratingService = registry.getService(propertyType, null);
41             PropertyUtils.write(target, propertyName, collaboratingService);
42
43             if (LOG.isDebugEnabled())
44             {
45                 LOG.debug("Autowired property " + propertyName + " by type to " + collaboratingService);
46             }
47             return true;
48         } else {
49             return false;
50         }
51     }
52
53 }
54
Popular Tags