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