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     */
022    package org.granite.tide.spring;
023    
024    import java.util.Map;
025    
026    import javax.servlet.ServletContext;
027    
028    import org.granite.config.flex.Destination;
029    import org.granite.context.GraniteContext;
030    import org.granite.logging.Logger;
031    import org.granite.messaging.service.ExtendedServiceExceptionHandler;
032    import org.granite.messaging.service.ServiceException;
033    import org.granite.messaging.service.ServiceFactory;
034    import org.granite.messaging.service.ServiceInvoker;
035    import org.granite.messaging.webapp.HttpGraniteContext;
036    import org.granite.tide.TideServiceInvoker;
037    import org.granite.tide.data.PersistenceExceptionConverter;
038    import org.granite.util.TypeUtil;
039    import org.granite.util.XMap;
040    import org.springframework.context.ApplicationContext;
041    import org.springframework.web.context.support.WebApplicationContextUtils;
042    
043    import flex.messaging.messages.RemotingMessage;
044    
045    
046    /**
047     * @author Sebastien Deleuze
048     */
049    public class SpringServiceFactory extends ServiceFactory {
050        
051            private static final Logger log = Logger.getLogger(SpringServiceFactory.class);
052        
053        public static final String PERSISTENCE_MANAGER_BEAN_NAME = "persistence-manager-bean-name";
054        public static final String ENTITY_MANAGER_FACTORY_BEAN_NAME = "entity-manager-factory-bean-name";
055        
056        private ApplicationContext springContext = null;
057        
058        public void setApplicationContext(ApplicationContext applicationContext) {
059            this.springContext = applicationContext;
060        }
061    
062    
063        @Override
064        public void configure(XMap properties) throws ServiceException {
065            String sServiceExceptionHandler = properties.get("service-exception-handler");
066            if (sServiceExceptionHandler == null) {
067                XMap props = new XMap(properties);
068                props.put("service-exception-handler", ExtendedServiceExceptionHandler.class.getName());
069                super.configure(props);
070            }
071            else
072                super.configure(properties);
073            
074            GraniteContext graniteContext = GraniteContext.getCurrentInstance();
075            try {
076                    graniteContext.getGraniteConfig().registerExceptionConverter(PersistenceExceptionConverter.class);
077            }
078            catch (Throwable t) {
079                    log.info(t, "JPA exception converter not registered (JPA not found on classpath)");
080            }
081        }
082    
083    
084        @Override
085        public ServiceInvoker<?> getServiceInstance(RemotingMessage request) throws ServiceException {
086            
087            String messageType = request.getClass().getName();
088            String destinationId = request.getDestination();
089    
090            GraniteContext context = GraniteContext.getCurrentInstance();
091            Map<String, Object> cache = context.getSessionMap(false);
092            if (cache == null)
093                    cache = context.getRequestMap();
094            Destination destination = context.getServicesConfig().findDestinationById(messageType, destinationId);
095            if (destination == null)
096                throw new ServiceException("No matching destination: " + destinationId);
097            
098            String key = TideServiceInvoker.class.getName() + '.' + destinationId;
099            
100            return getServiceInvoker(cache, destination, key);
101            
102        }
103    
104        private ServiceInvoker<?> getServiceInvoker(Map<String, Object> cache, Destination destination, String key) {
105            GraniteContext context = GraniteContext.getCurrentInstance();
106            if (context.getSessionMap(false) == null)
107                    return internalGetServiceInvoker(cache, destination, key);
108            
109            synchronized (context.getSessionLock()) {
110                    return internalGetServiceInvoker(cache, destination, key);
111            }
112        }
113        
114        private ServiceInvoker<?> internalGetServiceInvoker(Map<String, Object> cache, Destination destination, String key) {
115            ServiceInvoker<?> invoker = (ServiceInvoker<?>)cache.get(key);
116            if (invoker == null) {
117                SpringServiceContext tideContext = null;
118                ServletContext sc = ((HttpGraniteContext)GraniteContext.getCurrentInstance()).getServletContext();
119                ApplicationContext springContext = this.springContext != null ? this.springContext : WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
120                Map<String, ?> beans = springContext.getBeansOfType(SpringServiceContext.class);
121                if (beans.size() > 1)
122                    throw new RuntimeException("More than one SpringServiceContext bean found");
123                else if (beans.size() == 1)
124                    tideContext = (SpringServiceContext)beans.values().iterator().next();
125                else {
126                    // Try to create Spring MVC context when Spring MVC available
127                    String className = "org.granite.tide.spring.SpringMVCServiceContext";
128                    try {
129                            Class<SpringServiceContext> clazz = TypeUtil.forName(className, SpringServiceContext.class);
130                            tideContext = clazz.getConstructor(ApplicationContext.class).newInstance(springContext);
131                    }
132                    catch (Exception e) {
133                            tideContext = new SpringServiceContext(springContext);
134                    }
135                }
136                
137                String persistenceManagerBeanName = destination.getProperties().get(PERSISTENCE_MANAGER_BEAN_NAME);
138                    tideContext.setPersistenceManagerBeanName(persistenceManagerBeanName);
139                    
140                    String entityManagerFactoryBeanName = destination.getProperties().get(ENTITY_MANAGER_FACTORY_BEAN_NAME);
141                    tideContext.setEntityManagerFactoryBeanName(entityManagerFactoryBeanName);
142                
143                invoker = new TideServiceInvoker<SpringServiceFactory>(destination, this, tideContext);
144                cache.put(key, invoker);
145            }
146            return invoker;
147        }
148    }