001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2011 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.tide.seam.async;
022    
023    import static org.jboss.seam.annotations.Install.FRAMEWORK;
024    
025    import org.granite.gravity.Gravity;
026    import org.granite.gravity.GravityManager;
027    import org.granite.tide.async.AsyncPublisher;
028    import org.jboss.seam.ScopeType;
029    import org.jboss.seam.annotations.AutoCreate;
030    import org.jboss.seam.annotations.Install;
031    import org.jboss.seam.annotations.Name;
032    import org.jboss.seam.annotations.Scope;
033    import org.jboss.seam.annotations.intercept.BypassInterceptors;
034    import org.jboss.seam.contexts.ServletLifecycle;
035    
036    import flex.messaging.messages.AsyncMessage;
037    
038    
039    /**
040     * Async publisher using Gravity to send messages to the client
041     * 
042     * @author William DRAI
043     */
044    @Name("org.granite.tide.seam.async.publisher")
045    @Install(precedence=FRAMEWORK, classDependencies={"org.granite.gravity.Gravity"})
046    @Scope(ScopeType.STATELESS)
047    @BypassInterceptors
048    @AutoCreate
049    public class SeamAsyncPublisher implements AsyncPublisher {
050    
051        private static final long serialVersionUID = -5395975397632138270L;
052        
053        public static final String DESTINATION_NAME = "seamAsync";
054        
055        private Gravity getGravity() {
056            return GravityManager.getGravity(ServletLifecycle.getServletContext());
057        }
058    
059        public void initThread() {
060            Gravity gravity = getGravity();
061            if (gravity == null)
062                    throw new RuntimeException("Gravity service not configured, it is required for asynchronous event publishing");
063            
064            gravity.initThread();
065        }
066        
067        public void publishMessage(String sessionId, Object body) {
068            AsyncMessage message = new AsyncMessage();
069            message.setHeader(AsyncMessage.SUBTOPIC_HEADER, "tide.events." + sessionId);
070            message.setDestination(DESTINATION_NAME);
071            message.setBody(body);
072            
073            getGravity().publishMessage(message);
074        }
075    }