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 ApplicationContext springContext = null;
045
046 @Override
047 public void configure(XMap properties)throws ServiceException {
048 super.configure(properties);
049
050 // getting spring context from container
051 GraniteContext context = GraniteContext.getCurrentInstance();
052 ServletContext sc = ((HttpGraniteContext)context).getServletContext();
053 springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
054 }
055
056 @Override
057 public ServiceInvoker<?> getServiceInstance(RemotingMessage request) throws ServiceException {
058 String messageType = request.getClass().getName();
059 String destinationId = request.getDestination();
060
061 GraniteContext context = GraniteContext.getCurrentInstance();
062 Destination destination = context.getServicesConfig().findDestinationById(messageType, destinationId);
063 if (destination == null)
064 throw new ServiceException("No matching destination: " + destinationId);
065
066 // all we need is to get bean name
067 // Scopes are configured in Spring config file, not in destination
068 String beanName = destination.getProperties().get("source");
069 if (beanName == null)
070 beanName = destination.getId();
071
072 try {
073 Object bean = springContext.getBean(beanName);
074 return createSpringServiceInvoker(destination, this, bean);
075 }
076 catch (NoSuchBeanDefinitionException nexc) {
077 String msg = "Spring service named '" + beanName + "' does not exist.";
078 ServiceException e = new ServiceException(msg, nexc);
079 throw e;
080 }
081 catch (BeansException bexc) {
082 String msg = "Unable to create Spring service named '" + beanName + "'";
083 ServiceException e = new ServiceException(msg, bexc);
084 throw e;
085 }
086 }
087
088 @Override
089 public String toString() {
090 return toString("\n springContext: " + springContext);
091 }
092
093 protected ServiceInvoker<?> createSpringServiceInvoker(Destination destination, SpringServiceFactory factory, Object bean) {
094 return new SpringServiceInvoker(destination, this, bean);
095 }
096 }