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