001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022package org.granite.spring;
023
024import javax.servlet.ServletContext;
025
026import org.granite.config.flex.Destination;
027import org.granite.context.GraniteContext;
028import org.granite.messaging.service.ServiceException;
029import org.granite.messaging.service.ServiceInvoker;
030import org.granite.messaging.service.SimpleServiceFactory;
031import org.granite.messaging.webapp.HttpGraniteContext;
032import org.granite.util.XMap;
033import org.springframework.beans.BeansException;
034import org.springframework.beans.factory.NoSuchBeanDefinitionException;
035import org.springframework.context.ApplicationContext;
036import org.springframework.web.context.support.WebApplicationContextUtils;
037
038import flex.messaging.messages.RemotingMessage;
039
040/**
041 * @author Igor SAZHNEV
042 */
043public class SpringServiceFactory extends SimpleServiceFactory {
044
045    private ApplicationContext springContext = null;
046
047    @Override
048    public void configure(XMap properties)throws ServiceException {
049        super.configure(properties);
050
051        // getting spring context from container
052        GraniteContext context = GraniteContext.getCurrentInstance();
053        ServletContext sc = ((HttpGraniteContext)context).getServletContext();
054        springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
055    }
056
057    @Override
058    public ServiceInvoker<?> getServiceInstance(RemotingMessage request) throws ServiceException {
059        String messageType = request.getClass().getName();
060        String destinationId = request.getDestination();
061
062        GraniteContext context = GraniteContext.getCurrentInstance();
063        Destination destination = context.getServicesConfig().findDestinationById(messageType, destinationId);
064        if (destination == null)
065            throw new ServiceException("No matching destination: " + destinationId);
066        
067        // all we need is to get bean name
068        // Scopes are configured in Spring config file, not in destination
069        String beanName = destination.getProperties().get("source");
070        if (beanName == null)
071                beanName = destination.getId();
072        
073        try {
074            Object bean = springContext.getBean(beanName);
075            return createSpringServiceInvoker(destination, this, bean);
076        } 
077        catch (NoSuchBeanDefinitionException nexc) {
078            String msg = "Spring service named '" + beanName + "' does not exist.";
079            ServiceException e = new ServiceException(msg, nexc);
080            throw e;
081        } 
082        catch (BeansException bexc) {
083            String msg = "Unable to create Spring service named '" + beanName + "'";
084            ServiceException e = new ServiceException(msg, bexc);
085            throw e;
086        }
087    }
088
089    @Override
090    public String toString() {
091        return toString("\n  springContext: " + springContext);
092    }
093    
094    protected ServiceInvoker<?> createSpringServiceInvoker(Destination destination, SpringServiceFactory factory, Object bean) {
095        return new SpringServiceInvoker(destination, this, bean);
096    }
097}