/**
* GRANITE DATA SERVICES
* Copyright (C) 2006-2015 GRANITE DATA SERVICES S.A.S.
*
* This file is part of the Granite Data Services Platform.
*
* Granite Data Services is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* Granite Data Services is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA, or see <http://www.gnu.org/licenses/>.
*/
package org.granite.spring;
import javax.servlet.ServletContext;
import org.granite.config.flex.Destination;
import org.granite.config.flex.ServicesConfig;
import org.granite.context.GraniteContext;
import org.granite.messaging.service.ServiceException;
import org.granite.messaging.service.ServiceInvoker;
import org.granite.messaging.service.SimpleServiceFactory;
import org.granite.messaging.webapp.HttpGraniteContext;
import org.granite.util.XMap;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import flex.messaging.messages.RemotingMessage;
/**
* @author Igor SAZHNEV
*/
public class SpringServiceFactory extends SimpleServiceFactory {
private ApplicationContext springContext = null;
@Override
public void configure(XMap properties)throws ServiceException {
super.configure(properties);
// getting spring context from container
GraniteContext context = GraniteContext.getCurrentInstance();
ServletContext sc = ((HttpGraniteContext)context).getServletContext();
springContext = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
}
@Override
public ServiceInvoker<?> getServiceInstance(RemotingMessage request) throws ServiceException {
String messageType = request.getClass().getName();
String destinationId = request.getDestination();
ServicesConfig servicesConfig = GraniteContext.getCurrentInstance().getServicesConfig();
Destination destination = servicesConfig.findDestinationById(messageType, destinationId);
if (destination == null)
throw new ServiceException("No matching destination: " + destinationId);
// all we need is to get bean name
// Scopes are configured in Spring config file, not in destination
String beanName = destination.getProperties().get("source");
if (beanName == null)
beanName = destination.getId();
try {
Object bean = springContext.getBean(beanName);
return createSpringServiceInvoker(destination, this, bean);
}
catch (NoSuchBeanDefinitionException nexc) {
String msg = "Spring service named '" + beanName + "' does not exist.";
ServiceException e = new ServiceException(msg, nexc);
throw e;
}
catch (BeansException bexc) {
String msg = "Unable to create Spring service named '" + beanName + "'";
ServiceException e = new ServiceException(msg, bexc);
throw e;
}
}
@Override
public String toString() {
return toString("\n springContext: " + springContext);
}
protected ServiceInvoker<?> createSpringServiceInvoker(Destination destination, SpringServiceFactory factory, Object bean) {
return new SpringServiceInvoker(destination, this, bean);
}
}