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