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