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.cdi;
023
024import java.util.Set;
025
026import javax.enterprise.inject.spi.Bean;
027import javax.enterprise.inject.spi.BeanManager;
028
029import org.granite.cdi.CDIUtils;
030import org.granite.config.flex.Destination;
031import org.granite.context.GraniteContext;
032import org.granite.logging.Logger;
033import org.granite.messaging.service.ServiceException;
034import org.granite.messaging.service.ServiceFactory;
035import org.granite.messaging.service.ServiceInvoker;
036import org.granite.messaging.webapp.HttpGraniteContext;
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 CDIServiceFactory extends ServiceFactory {
048
049    private static final Logger log = Logger.getLogger(CDIServiceFactory.class);
050    
051    public static final String ENTITY_MANAGER_FACTORY_JNDI_NAME = "entity-manager-factory-jndi-name";
052    public static final String ENTITY_MANAGER_JNDI_NAME = "entity-manager-jndi-name";
053
054    private BeanManager manager;
055    
056    public BeanManager getManager() {
057        return manager;
058    }
059    
060
061    @Override
062    public void configure(XMap properties) throws ServiceException {
063        String sServiceExceptionHandler = properties.get("service-exception-handler");
064        if (sServiceExceptionHandler == null) {
065            XMap props = new XMap(properties);
066            props.put("service-exception-handler", "org.granite.messaging.service.ExtendedServiceExceptionHandler");
067            super.configure(props);
068        }
069        else
070            super.configure(properties);
071        
072        GraniteContext graniteContext = GraniteContext.getCurrentInstance();
073        try {
074                graniteContext.getGraniteConfig().registerExceptionConverter(PersistenceExceptionConverter.class);
075        }
076        catch (Throwable t) {
077                log.info(t, "JPA exception converter not registered (JPA not found on classpath)");
078        }
079
080        try {
081                manager = CDIUtils.lookupBeanManager(((HttpGraniteContext)graniteContext).getServletContext());
082        }
083        catch (Exception e) {
084                log.warn("Unable to find the CDI Manager in JNDI, lookup in ServletContext");
085                
086                manager = (BeanManager)((HttpGraniteContext)graniteContext).getServletContext().getAttribute("javax.enterprise.inject.spi.BeanManager");
087                if (manager == null) { 
088                        ServiceException se = new ServiceException(e.getMessage());
089                        throw se;
090                }
091        }
092        
093        // Find the JCDIServiceContext component
094        Set<Bean<?>> cc = manager.getBeans(CDIServiceContext.class);
095        if (cc.size() != 1) {
096            String msg = cc.isEmpty() 
097                ? "Unable to find the CDIServiceContext bean"
098                : "More than one CDIServiceContext bean found";
099            log.error(msg);
100            ServiceException e = new ServiceException(msg);
101            throw e;
102        }
103    }
104
105
106    @Override
107    public ServiceInvoker<?> getServiceInstance(RemotingMessage request) throws ServiceException {
108        String messageType = request.getClass().getName();
109        String destinationId = request.getDestination();
110
111        HttpGraniteContext context = (HttpGraniteContext)GraniteContext.getCurrentInstance();
112        Destination destination = context.getServicesConfig().findDestinationById(messageType, destinationId);
113        if (destination == null)
114            throw new ServiceException("No matching destination: " + destinationId);
115        
116        if (!destination.getProperties().containsKey(TideServiceInvoker.VALIDATOR_CLASS_NAME))
117                destination.getProperties().put(TideServiceInvoker.VALIDATOR_CLASS_NAME, "org.granite.tide.validation.BeanValidation");
118        
119        @SuppressWarnings("unchecked")
120        Bean<PersistenceConfiguration> pcBean = (Bean<PersistenceConfiguration>)manager.getBeans(PersistenceConfiguration.class).iterator().next();
121        PersistenceConfiguration persistenceConfiguration = (PersistenceConfiguration)manager.getReference(pcBean, PersistenceConfiguration.class, manager.createCreationalContext(pcBean));
122        if (destination.getProperties().containsKey(ENTITY_MANAGER_FACTORY_JNDI_NAME))
123                persistenceConfiguration.setEntityManagerFactoryJndiName(destination.getProperties().get(ENTITY_MANAGER_FACTORY_JNDI_NAME));
124        else if (destination.getProperties().containsKey(ENTITY_MANAGER_JNDI_NAME))
125                persistenceConfiguration.setEntityManagerJndiName(destination.getProperties().get(ENTITY_MANAGER_JNDI_NAME));
126        
127        // Create an instance of the component
128        CDIServiceInvoker invoker = new CDIServiceInvoker(destination, this);
129        return invoker;
130    }
131}