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.gravity.generic;
023    
024    import org.granite.gravity.AbstractChannel;
025    import org.granite.gravity.AsyncHttpContext;
026    import org.granite.gravity.Gravity;
027    import org.granite.gravity.MessageReceivingException;
028    import org.granite.logging.Logger;
029    
030    import flex.messaging.messages.AsyncMessage;
031    
032    /**
033     * @author William DRAI
034     */
035    public class GenericChannel extends AbstractChannel {
036    
037        private static final Logger log = Logger.getLogger(GenericChannel.class);
038    
039        private WaitingContinuation continuation = null;
040    
041        public GenericChannel(Gravity gravity, String id, GenericChannelFactory factory, String clientType) {
042            super(gravity, id, factory, clientType);
043        }
044    
045        public void setContinuation(WaitingContinuation continuation) {
046            try {
047                if (this.continuation != null && this.continuation.isPending()) {
048                    log.debug("Set pending continuation for client: %s", getId());
049                    this.continuation.resume();
050                }
051            }
052            finally {
053                this.continuation = continuation;
054            }
055        }
056        
057        public void close() {
058            try {
059                if (this.continuation != null)
060                    this.continuation.reset();
061            }
062            finally {
063                this.continuation = null;
064            }
065        }
066        
067    
068        public void resume() {
069            try {
070                if (this.continuation != null) {
071                    log.debug("Resume pending continuation for client: %s", getId());
072                    this.continuation.resume();
073                }
074            }
075            finally {
076                this.continuation = null;
077            }
078        }
079        
080        @Override
081            public void receive(AsyncMessage message) throws MessageReceivingException {
082                    if (message == null)
083                            throw new NullPointerException("message cannot be null");
084                    
085                    receivedQueueLock.lock();
086                    try {
087                            receivedQueue.add(message);
088                    }
089                    catch (Exception e) {
090                            throw new MessageReceivingException(message, "Could not queue message", e);
091                    }
092                    finally {
093                            receivedQueueLock.unlock();
094                    }
095                    
096                    synchronized (this) {
097                            resume();
098                    }
099            }
100    
101            @Override
102            protected boolean hasAsyncHttpContext() {
103                    return false;
104            }
105    
106            @Override
107            protected void releaseAsyncHttpContext(AsyncHttpContext context) {
108            }
109    
110            @Override
111            protected AsyncHttpContext acquireAsyncHttpContext() {
112            return null;
113        }
114    
115        public boolean isLocal() {
116            return true;
117        }
118    
119            @Override
120            public void destroy() {
121                    try {
122                            super.destroy();
123                    }
124                    finally {
125                            synchronized (this) {
126                                    close();
127                            }
128                    }
129            }
130    }