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.FactoryBean;
023    import org.springframework.beans.factory.InitializingBean;
024    import org.apache.avalon.framework.service.ServiceManager;
025    
026    /**
027     * A Spring factory bean to lookup Avalon service and get a hold on
028     * them as Spring bean. Such spring bean can than be injected as dependency
029     * into other beans.
030     *
031     * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
032     */
033    
034    public class AvalonServiceFactoryBean implements FactoryBean, InitializingBean {
035    
036        /** the Avalon ServiceManager */
037        private ServiceManager serviceManager;
038    
039        /** the name of the Avalon service to resolve */
040        private String serviceName;
041    
042        /** the resolved Avalon service */
043        private Object service;
044    
045        /**
046         * Resolve the service name to a service.
047         * 
048         * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
049         */
050        public void afterPropertiesSet() throws Exception
051        {
052            this.service = this.serviceManager.lookup(this.serviceName);            
053        }
054    
055        /**
056         * Set the Avalon ServiceManager to lookup the Avalon service.
057         *
058         * @param serviceManager the Avalon ServiceManager
059         */
060        public void setServiceManager(ServiceManager serviceManager)
061        {
062            this.serviceManager = serviceManager;
063        }
064    
065        /**
066         * Set the name of the Avalon service to be resolved by
067         * this Spring bean factory.
068         *
069         * @param serviceName the Avalon service name
070         */
071        public void setServiceName(String serviceName)
072        {
073            this.serviceName = serviceName;
074        }
075    
076        /** @see org.springframework.beans.factory.FactoryBean#getObject() */
077        public Object getObject() throws Exception
078        {
079            return this.service;
080        }
081    
082        /** @see org.springframework.beans.factory.FactoryBean#getObjectType() */
083        public Class getObjectType()
084        {
085            return this.service.getClass();
086        }
087    
088        /** @see org.springframework.beans.factory.FactoryBean#isSingleton() */
089        public boolean isSingleton()
090        {
091            return true;
092        }
093    }