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.seam.async;
023    
024    import static org.jboss.seam.annotations.Install.FRAMEWORK;
025    
026    import org.granite.gravity.Gravity;
027    import org.granite.gravity.GravityManager;
028    import org.granite.tide.async.AsyncPublisher;
029    import org.jboss.seam.ScopeType;
030    import org.jboss.seam.annotations.AutoCreate;
031    import org.jboss.seam.annotations.Install;
032    import org.jboss.seam.annotations.Name;
033    import org.jboss.seam.annotations.Scope;
034    import org.jboss.seam.annotations.intercept.BypassInterceptors;
035    import org.jboss.seam.contexts.ServletLifecycle;
036    
037    import flex.messaging.messages.AsyncMessage;
038    
039    
040    /**
041     * Async publisher using Gravity to send messages to the client
042     * 
043     * @author William DRAI
044     */
045    @Name("org.granite.tide.seam.async.publisher")
046    @Install(precedence=FRAMEWORK, classDependencies={"org.granite.gravity.Gravity"})
047    @Scope(ScopeType.STATELESS)
048    @BypassInterceptors
049    @AutoCreate
050    public class SeamAsyncPublisher implements AsyncPublisher {
051        
052        public static final String DESTINATION_NAME = "seamAsync";
053        
054        private Gravity getGravity() {
055            return GravityManager.getGravity(ServletLifecycle.getServletContext());
056        }
057    
058        public void initThread() {
059            Gravity gravity = getGravity();
060            if (gravity == null)
061                    throw new RuntimeException("Gravity service not configured, it is required for asynchronous event publishing");
062            
063            gravity.initThread(null, null);
064        }
065        
066        public void publishMessage(String sessionId, Object body) {
067            AsyncMessage message = new AsyncMessage();
068            message.setHeader(AsyncMessage.SUBTOPIC_HEADER, "tide.events." + sessionId);
069            message.setDestination(DESTINATION_NAME);
070            message.setBody(body);
071            
072            getGravity().publishMessage(message);
073        }
074    }