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