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