001    package org.apache.fulcrum.spring;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *   http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import org.springframework.beans.factory.BeanFactory;
023    import org.apache.avalon.framework.service.ServiceManager;
024    import org.apache.avalon.framework.service.ServiceException;
025    
026    /**
027     * Wraps a Spring bean factory to implement an Avalon service
028     * lookup - this allows to delegate a service lookup to Spring.
029     *
030     * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
031     */
032    public class BeanFactoryServiceManager implements ServiceManager
033    {
034        /** Spring's bean factory */
035        private BeanFactory beanFactory;
036    
037        /**
038         * Constructor
039         *
040         * @param beanFactory Spring's bean factory
041         */
042        public BeanFactoryServiceManager(BeanFactory beanFactory)
043        {
044            this.beanFactory = beanFactory;
045        }
046    
047        /** @see org.apache.avalon.framework.service.ServiceManager#lookup(String) */
048        public Object lookup(String key) throws ServiceException
049        {
050            try
051            {
052                return beanFactory.getBean(key);
053            }
054            catch(Exception e)
055            {
056                throw new ServiceException(key, "Unable to lookup service using Spring's BeanFactory", e);
057            }
058        }
059    
060        /** @see org.apache.avalon.framework.service.ServiceManager#hasService(String) */
061        public boolean hasService(String key)
062        {
063            return beanFactory.containsBean(key);
064        }
065    
066        /** @see org.apache.avalon.framework.service.ServiceManager#release(Object) */
067        public void release(Object o)
068        {
069            // nothing to do for Spring beans
070        }
071    }