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 021package org.granite.tide.seam; 022 023import org.granite.config.flex.Destination; 024import org.granite.context.GraniteContext; 025import org.granite.messaging.service.ServiceException; 026import org.granite.messaging.service.ServiceFactory; 027import org.granite.messaging.service.ServiceInvoker; 028import org.granite.messaging.webapp.HttpGraniteContext; 029import org.granite.tide.data.PersistenceExceptionConverter; 030import org.granite.util.XMap; 031import org.jboss.seam.Component; 032import org.jboss.seam.log.Log; 033import org.jboss.seam.log.Logging; 034 035import flex.messaging.messages.RemotingMessage; 036 037 038/** 039 * @author William DRAI 040 */ 041public class SeamServiceFactory extends ServiceFactory { 042 043 private static final Log log = Logging.getLog(SeamServiceFactory.class); 044 045 private Component contextComponent = null; 046 047 048 @Override 049 public void configure(XMap properties) throws ServiceException { 050 String sServiceExceptionHandler = properties.get("service-exception-handler"); 051 if (sServiceExceptionHandler == null) { 052 XMap props = new XMap(properties); 053 if (Component.forName("org.jboss.seam.international.statusMessages") != null) // Seam 2.1 054 props.put("service-exception-handler", "org.granite.tide.seam21.Seam21ServiceExceptionHandler"); 055 else 056 props.put("service-exception-handler", "org.granite.tide.seam.SeamServiceExceptionHandler"); 057 super.configure(props); 058 } 059 else 060 super.configure(properties); 061 062 GraniteContext graniteContext = GraniteContext.getCurrentInstance(); 063 try { 064 graniteContext.getGraniteConfig().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 = 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}