diff options
Diffstat (limited to 'sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java')
-rw-r--r-- | sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java | 59 |
1 files changed, 31 insertions, 28 deletions
diff --git a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java index ef8d1179a0..1835c062c3 100644 --- a/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java +++ b/sca-java-2.x/trunk/modules/binding-comet-runtime/src/main/java/org/apache/tuscany/sca/binding/comet/runtime/CometInvoker.java @@ -21,6 +21,7 @@ package org.apache.tuscany.sca.binding.comet.runtime; import org.apache.tuscany.sca.assembly.EndpointReference; import org.apache.tuscany.sca.binding.comet.runtime.callback.Status; +import org.apache.tuscany.sca.binding.comet.runtime.manager.CometSessionManager; import org.apache.tuscany.sca.core.invocation.Constants; import org.apache.tuscany.sca.core.invocation.impl.MessageImpl; import org.apache.tuscany.sca.interfacedef.Operation; @@ -28,37 +29,39 @@ import org.apache.tuscany.sca.invocation.Invoker; import org.apache.tuscany.sca.invocation.Message; import org.atmosphere.cpr.Broadcaster; +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + public class CometInvoker implements Invoker { - protected Operation operation; - protected EndpointReference endpoint; + private static Gson gson = new GsonBuilder().serializeNulls().create(); + + protected Operation operation; + protected EndpointReference endpoint; + + public CometInvoker(final Operation operation, final EndpointReference endpoint) { + this.operation = operation; + this.endpoint = endpoint; + } - public CometInvoker(final Operation operation, final EndpointReference endpoint) { - this.operation = operation; - this.endpoint = endpoint; - } + @Override + public Message invoke(final Message msg) { + String sessionId = (String) msg.getHeaders().get(Constants.RELATES_TO); + Broadcaster broadcaster = CometSessionManager.get(sessionId); + Message response = new MessageImpl(); + if (broadcaster == null) { + response.setBody(Status.CLIENT_DISCONNECTED); + } else if (broadcaster.getAtmosphereResources().isEmpty()) { + CometSessionManager.remove(sessionId); + response.setBody(Status.CLIENT_DISCONNECTED); + } else { + String callbackMethod = msg.getTo().getURI(); + Object[] body = msg.getBody(); + broadcaster.broadcast(callbackMethod + "($.secureEvalJSON('" + gson.toJson(body[0]) + "'))"); + response.setBody(Status.OK); + } + return response; - @Override - public Message invoke(final Message msg) { - return handleSendMessage(msg); - } + } - private Message handleSendMessage(Message msg) { - String sessionId = (String) msg.getHeaders().get(Constants.RELATES_TO); - Broadcaster broadcaster = CometComponentContext.broadcasters.get(sessionId); - Message response = new MessageImpl(); - if (broadcaster == null) { - response.setBody(Status.CLIENT_DISCONNECTED); - } else if (broadcaster.getAtmosphereResources().isEmpty()) { - CometComponentContext.broadcasters.remove(sessionId); - response.setBody(Status.CLIENT_DISCONNECTED); - } else { - String callbackMethod = msg.getTo().getURI(); - Object[] body = msg.getBody(); - broadcaster.broadcast(callbackMethod + "($.secureEvalJSON('" + CometComponentContext.gson.toJson(body[0]) - + "'))"); - response.setBody(Status.OK); - } - return response; - } } |