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     */
022    package org.granite.tide.cdi;
023    
024    import javax.enterprise.context.spi.CreationalContext;
025    import javax.enterprise.inject.spi.Bean;
026    import javax.servlet.http.HttpSession;
027    
028    import org.granite.config.flex.Destination;
029    import org.granite.context.GraniteContext;
030    import org.granite.messaging.webapp.HttpGraniteContext;
031    import org.granite.tide.TideServiceInvoker;
032    import org.granite.tide.cdi.lazy.CDIInitializer;
033    
034    
035    /**
036     * @author William DRAI
037     */
038    public class CDIServiceInvoker extends TideServiceInvoker<CDIServiceFactory> {
039    
040            
041        public CDIServiceInvoker(Destination destination, CDIServiceFactory factory) {
042            super(destination, factory);
043        } 
044        
045        
046        @Override
047        public void logout() {
048            HttpGraniteContext context = (HttpGraniteContext)GraniteContext.getCurrentInstance();
049            HttpSession session = context.getSession(false);
050            if (session != null)
051                    session.invalidate();
052        }
053        
054        @Override
055        public Object initializeObject(Object parent, String[] propertyNames) {
056            @SuppressWarnings("unchecked")
057            Bean<CDIInitializer> iBean = (Bean<CDIInitializer>)factory.getManager().getBeans(CDIInitializer.class).iterator().next();
058            CDIInitializer initializer = (CDIInitializer)factory.getManager().getReference(iBean, CDIInitializer.class, factory.getManager().createCreationalContext(iBean));
059            
060            return initializer.lazyInitialize(parent, propertyNames);
061        }
062        
063        @Override
064        protected CDIServiceContext lookupContext() {
065            Bean<?> scBean = factory.getManager().getBeans(CDIServiceContext.class).iterator().next();
066            CreationalContext<?> cc = factory.getManager().createCreationalContext(scBean);
067            return (CDIServiceContext)factory.getManager().getReference(scBean, CDIServiceContext.class, cc);
068        }
069    }