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