001    // Copyright 2004, 2005 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.hivemind.lib.impl;
016    
017    import org.apache.hivemind.ServiceImplementationFactory;
018    import org.apache.hivemind.ServiceImplementationFactoryParameters;
019    import org.apache.hivemind.impl.BaseLocatable;
020    import org.springframework.beans.factory.BeanFactory;
021    
022    /**
023     * Implementation of {@link org.apache.hivemind.ServiceImplementationFactory} that doesn't create
024     * beans, but instead it looks them up inside a Spring
025     * {@link org.springframework.beans.factory.BeanFactory}.
026     * 
027     * @author Howard Lewis Ship
028     */
029    public class SpringLookupFactory extends BaseLocatable implements ServiceImplementationFactory
030    {
031        /** @since 1.1 */
032        private BeanFactory _defaultBeanFactory;
033    
034        public Object createCoreServiceImplementation(
035                ServiceImplementationFactoryParameters factoryParameters)
036        {
037            SpringBeanParameter p = (SpringBeanParameter) factoryParameters.getFirstParameter();
038            String beanName = p.getName();
039    
040            BeanFactory f = p.getBeanFactory();
041    
042            if (f == null)
043                f = _defaultBeanFactory;
044    
045            return f.getBean(beanName, factoryParameters.getServiceInterface());
046        }
047    
048        /** @since 1.1 */
049        public void setDefaultBeanFactory(BeanFactory defaultBeanFactory)
050        {
051            _defaultBeanFactory = defaultBeanFactory;
052        }
053    }