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