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.tide.seam;
022    
023    import org.granite.config.flex.Destination;
024    import org.granite.context.GraniteContext;
025    import org.granite.messaging.service.ServiceException;
026    import org.granite.messaging.service.ServiceFactory;
027    import org.granite.messaging.service.ServiceInvoker;
028    import org.granite.messaging.webapp.HttpGraniteContext;
029    import org.granite.tide.data.PersistenceExceptionConverter;
030    import org.granite.util.XMap;
031    import org.jboss.seam.Component;
032    import org.jboss.seam.log.Log;
033    import org.jboss.seam.log.Logging;
034    
035    import flex.messaging.messages.RemotingMessage;
036    
037    
038    /**
039     * @author William DRAI
040     */
041    public class SeamServiceFactory extends ServiceFactory {
042    
043        private static final long serialVersionUID = 1L;
044    
045        private static final Log log = Logging.getLog(SeamServiceFactory.class);
046    
047        private Component contextComponent = null;
048    
049    
050        @Override
051        public void configure(XMap properties) throws ServiceException {
052            String sServiceExceptionHandler = properties.get("service-exception-handler");
053            if (sServiceExceptionHandler == null) {
054                XMap props = new XMap(properties);
055                if (Component.forName("org.jboss.seam.international.statusMessages") != null)   // Seam 2.1
056                    props.put("service-exception-handler", "org.granite.tide.seam21.Seam21ServiceExceptionHandler");
057                else
058                    props.put("service-exception-handler", "org.granite.tide.seam.SeamServiceExceptionHandler");
059                super.configure(props);
060            }
061            else
062                super.configure(properties);
063            
064            GraniteContext graniteContext = GraniteContext.getCurrentInstance();
065            try {
066                    graniteContext.getGraniteConfig().registerExceptionConverter(PersistenceExceptionConverter.class);
067                }
068                catch (Throwable t) {
069                    log.info(t, "JPA exception converter not registered (JPA not found on classpath)");
070                }
071    
072            // Find the SeamServiceInvoker component
073            contextComponent = Component.forName("org.granite.tide.seam.serviceContext");
074            if (contextComponent == null) {
075                String msg = "Unable to find the SeamServiceContext component";
076                log.error(msg);
077                ServiceException e = new ServiceException(msg);
078                throw e;
079            }
080        }
081    
082    
083        @Override
084        public ServiceInvoker<?> getServiceInstance(RemotingMessage request) throws ServiceException {
085            String messageType = request.getClass().getName();
086            String destinationId = request.getDestination();
087    
088            HttpGraniteContext context = (HttpGraniteContext)GraniteContext.getCurrentInstance();
089            Destination destination = context.getServicesConfig().findDestinationById(messageType, destinationId);
090            if (destination == null)
091                throw new ServiceException("No matching destination: " + destinationId);
092    
093            // Create an instance of the component
094            SeamServiceInvoker invoker = new SeamServiceInvoker(destination, this);
095            return invoker;
096        }
097    
098    }