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.ejb; 023 024import java.util.Map; 025 026import javax.naming.InitialContext; 027 028import org.granite.config.flex.Destination; 029import org.granite.context.GraniteContext; 030import org.granite.messaging.service.ExtendedServiceExceptionHandler; 031import org.granite.messaging.service.ServiceException; 032import org.granite.messaging.service.ServiceFactory; 033import org.granite.messaging.service.ServiceInvoker; 034import org.granite.scan.ScannedItemHandler; 035import org.granite.tide.TideServiceInvoker; 036import org.granite.tide.data.PersistenceExceptionConverter; 037import org.granite.util.XMap; 038 039import flex.messaging.messages.RemotingMessage; 040 041 042/** 043 * @author William DRAI 044 */ 045public class EjbServiceFactory extends ServiceFactory { 046 047 public static final String ENTITY_MANAGER_FACTORY_JNDI_NAME = "entity-manager-factory-jndi-name"; 048 public static final String ENTITY_MANAGER_JNDI_NAME = "entity-manager-jndi-name"; 049 050 private String lookup = null; 051 private InitialContext initialContext = null; 052 053 public static ScannedItemHandler getScannedItemHandler() { 054 return EjbScannedItemHandler.instance(true); 055 } 056 057 public String getLookup() { 058 return lookup; 059 } 060 061 public void setInitialContext(InitialContext ic) { 062 this.initialContext = ic; 063 } 064 065 066 @Override 067 public void configure(XMap properties) throws ServiceException { 068 String sServiceExceptionHandler = properties.get("service-exception-handler"); 069 if (sServiceExceptionHandler == null) { 070 XMap props = new XMap(properties); 071 props.put("service-exception-handler", ExtendedServiceExceptionHandler.class.getName()); 072 super.configure(props); 073 } 074 else 075 super.configure(properties); 076 077 GraniteContext graniteContext = GraniteContext.getCurrentInstance(); 078 graniteContext.getGraniteConfig().registerExceptionConverter(PersistenceExceptionConverter.class); 079 graniteContext.getGraniteConfig().registerExceptionConverter(EJBAccessExceptionConverter.class); 080 081 this.lookup = properties.get("lookup"); 082 } 083 084 085 @Override 086 public ServiceInvoker<?> getServiceInstance(RemotingMessage request) throws ServiceException { 087 String messageType = request.getClass().getName(); 088 String destinationId = request.getDestination(); 089 090 GraniteContext context = GraniteContext.getCurrentInstance(); 091 Map<String, Object> cache = context.getSessionMap(); 092 Destination destination = context.getServicesConfig().findDestinationById(messageType, destinationId); 093 String key = TideServiceInvoker.class.getName() + '.' + destinationId; 094 095 return getServiceInvoker(cache, destination, key); 096 } 097 098 private ServiceInvoker<?> getServiceInvoker(Map<String, Object> cache, Destination destination, String key) { 099 GraniteContext context = GraniteContext.getCurrentInstance(); 100 synchronized (context.getSessionLock()) { 101 ServiceInvoker<?> invoker = (ServiceInvoker<?>)cache.get(key); 102 if (invoker == null) { 103 String lookup = getLookup(); 104 105 if (destination.getProperties().containsKey("lookup")) 106 lookup = destination.getProperties().get("lookup"); 107 108 EjbServiceContext tideContext = new EjbServiceContext(lookup, initialContext); 109 110 if (destination.getProperties().containsKey(ENTITY_MANAGER_FACTORY_JNDI_NAME)) { 111 tideContext.setEntityManagerFactoryJndiName(destination.getProperties().get(ENTITY_MANAGER_FACTORY_JNDI_NAME)); 112 } 113 else if (destination.getProperties().containsKey(ENTITY_MANAGER_JNDI_NAME)) { 114 tideContext.setEntityManagerJndiName(destination.getProperties().get(ENTITY_MANAGER_JNDI_NAME)); 115 } 116 117 invoker = new TideServiceInvoker<EjbServiceFactory>(destination, this, tideContext); 118 cache.put(key, invoker); 119 } 120 return invoker; 121 } 122 } 123}