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.gravity.servlet3;
022    
023    import java.util.concurrent.atomic.AtomicReference;
024    
025    import javax.servlet.AsyncContext;
026    import javax.servlet.http.HttpServletRequest;
027    import javax.servlet.http.HttpServletResponse;
028    
029    import org.granite.gravity.AbstractChannel;
030    import org.granite.gravity.AbstractGravityServlet;
031    import org.granite.gravity.AsyncHttpContext;
032    import org.granite.gravity.Gravity;
033    import org.granite.logging.Logger;
034    
035    import flex.messaging.messages.Message;
036    
037    /**
038     * @author Franck WOLFF
039     */
040    public class AsyncChannel extends AbstractChannel {
041    
042        private static final Logger log = Logger.getLogger(AsyncChannel.class);
043        
044        private final AtomicReference<AsyncContext> asyncContext = new AtomicReference<AsyncContext>();
045    
046            public AsyncChannel(Gravity gravity, String id, AsyncChannelFactory factory, String clientType) {
047            super(gravity, id, factory, clientType);
048            }
049    
050            public void setAsyncContext(AsyncContext asyncContext) {
051            if (log.isDebugEnabled())
052                log.debug("Channel: %s got new asyncContext: %s", getId(), asyncContext);
053            
054            // Set this channel's async context.
055            AsyncContext previousAsyncContext = this.asyncContext.getAndSet(asyncContext);
056    
057            // Normally, we should have only two cases here:
058            //
059            // 1) this.asyncContext == null && asyncContext != null -> new (re)connect message.
060            // 2) this.asyncContext != null && asyncContext == null -> timeout.
061            //
062            // Not sure about what should be done if this.asyncContext != null && asyncContext != null, so
063            // warn about this case and close this.asyncContext if it is not the same as the asyncContext
064            // parameter.
065            if (previousAsyncContext != null) {
066                    if (asyncContext != null) {
067                            log.warn(
068                                    "Got a new non null asyncContext %s while current asyncContext %s isn't null",
069                                    asyncContext, this.asyncContext.get()
070                            );
071                    }
072                    if (previousAsyncContext != asyncContext) {
073                            try {
074                                    previousAsyncContext.complete();
075                            }
076                            catch (Exception e) {
077                                    log.debug(e, "Error while closing asyncContext");
078                            }
079                    }
080            }
081            
082            // Try to queue receiver if the new asyncContext isn't null.
083            if (asyncContext != null)
084                    queueReceiver();
085            }
086        
087        @Override
088            protected boolean hasAsyncHttpContext() {
089            return asyncContext.get() != null;
090            }
091    
092            @Override
093            protected AsyncHttpContext acquireAsyncHttpContext() {
094    
095                    AsyncContext asyncContext = this.asyncContext.getAndSet(null);
096                    if (asyncContext == null)
097                            return null;
098    
099            AsyncHttpContext context = null;
100    
101            try {
102                    HttpServletRequest request = null;
103                    HttpServletResponse response = null;
104                    try {
105                            request = (HttpServletRequest)asyncContext.getRequest();
106                            response = (HttpServletResponse)asyncContext.getResponse();
107                    } catch (Exception e) {
108                            log.warn("Illegal asyncContext: %s", asyncContext);
109                            return null;
110                    }
111                    if (request == null || response == null) {
112                            log.warn("Illegal asyncContext (request or response is null): %s", asyncContext);
113                            return null;
114                    }
115            
116                    Message requestMessage = AbstractGravityServlet.getConnectMessage(request);
117                    if (requestMessage == null) {
118                            log.warn("No request message while running channel: %s", getId());
119                            return null;
120                    }
121                            
122                    context = new AsyncHttpContext(request, response, requestMessage, asyncContext);
123            }
124            finally {
125                    if (context == null) {
126                            try {
127                                    asyncContext.complete();
128                            }
129                            catch (Exception e) {
130                                    log.debug(e, "Error while closing asyncContext: %s", asyncContext);
131                            }
132                    }
133            }
134            
135            return context;
136            }
137    
138            @Override
139            protected void releaseAsyncHttpContext(AsyncHttpContext context) {
140                    try {
141                            if (context != null && context.getObject() != null)
142                                    ((AsyncContext)context.getObject()).complete();
143                    }
144                    catch (Exception e) {
145                            log.warn(e, "Could not release asyncContext for channel: %s", this);
146                    }
147            }
148    
149            @Override
150            public void destroy() {
151                    try {
152                            super.destroy();
153                    }
154                    finally {
155                            close();
156                    }
157            }
158            
159            public void close() {
160                    AsyncContext asyncContext = this.asyncContext.getAndSet(null);
161                    if (asyncContext != null) {
162                            try {
163                                    asyncContext.complete();
164                            }
165                            catch (Exception e) {
166                                    log.debug(e, "Could not close asyncContext: %s for channel: %s", asyncContext, this);
167                            }
168                    }
169            }
170    }