diff options
author | Leif Åstrand <leif@vaadin.com> | 2013-04-09 12:19:11 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2013-04-09 11:58:52 +0000 |
commit | d779e637ef7a5fa8d575768a10f22f142e6f12d5 (patch) | |
tree | 2b3079d6ff81ef5192afbde8ae7f1d7067aad5dd /server/src | |
parent | 9002c6d6b1824c7025c7834c9def5e0260e9043b (diff) | |
download | vaadin-framework-d779e637ef7a5fa8d575768a10f22f142e6f12d5.tar.gz vaadin-framework-d779e637ef7a5fa8d575768a10f22f142e6f12d5.zip |
Don't bind PushConnection API to Atmosphere (#11551)
Change-Id: Iaf04069c724a603b15fbf09778d2cec983432272
Diffstat (limited to 'server/src')
4 files changed, 146 insertions, 107 deletions
diff --git a/server/src/com/vaadin/server/communication/AtmospherePushConnection.java b/server/src/com/vaadin/server/communication/AtmospherePushConnection.java new file mode 100644 index 0000000000..8b146e3d50 --- /dev/null +++ b/server/src/com/vaadin/server/communication/AtmospherePushConnection.java @@ -0,0 +1,135 @@ +/* + * Copyright 2000-2013 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ + +package com.vaadin.server.communication; + +import java.io.IOException; +import java.io.Serializable; +import java.io.StringWriter; +import java.io.Writer; + +import org.atmosphere.cpr.AtmosphereResource; +import org.json.JSONException; + +import com.vaadin.ui.UI; + +/** + * {@link PushConnection} implementation using the Atmosphere push support that + * is by default included in Vaadin. + * + * @author Vaadin Ltd + * @since 7.1 + */ +public class AtmospherePushConnection implements Serializable, PushConnection { + + private UI ui; + private boolean pending = true; + private AtmosphereResource resource; + + public AtmospherePushConnection(UI ui) { + this.ui = ui; + } + + @Override + public void push() { + if (!isConnected()) { + // Not currently connected; defer until connection established + setPending(true); + } else { + try { + push(true); + } catch (IOException e) { + // TODO Error handling + throw new RuntimeException("Push failed", e); + } + } + } + + /** + * Pushes pending state changes and client RPC calls to the client. + * + * @param async + * True if this push asynchronously originates from the server, + * false if it is a response to a client request. + * @throws IOException + */ + protected void push(boolean async) throws IOException { + Writer writer = new StringWriter(); + try { + new UidlWriter().write(getUI(), writer, false, false, async); + } catch (JSONException e) { + throw new IOException("Error writing UIDL", e); + } + // "Broadcast" the changes to the single client only + getResource().getBroadcaster().broadcast(writer.toString(), + getResource()); + } + + /** + * Associates this connection with the given AtmosphereResource. If there is + * a push pending, commits it. + * + * @param resource + * The AtmosphereResource representing the push channel. + * @throws IOException + */ + protected void connect(AtmosphereResource resource) throws IOException { + this.resource = resource; + if (isPending()) { + push(true); + setPending(false); + } + } + + /** + * Returns whether this connection is currently open. + */ + protected boolean isConnected() { + return resource != null + && resource.getBroadcaster().getAtmosphereResources() + .contains(resource); + } + + /** + * Marks that changes in the UI should be pushed as soon as a connection is + * established. + */ + protected void setPending(boolean pending) { + this.pending = pending; + } + + /** + * @return Whether the UI should be pushed as soon as a connection opens. + */ + protected boolean isPending() { + return pending; + } + + /** + * @return the UI associated with this connection. + */ + protected UI getUI() { + return ui; + } + + /** + * @return The AtmosphereResource associated with this connection or null if + * connection not open. + */ + protected AtmosphereResource getResource() { + return resource; + } +} diff --git a/server/src/com/vaadin/server/communication/PushConnection.java b/server/src/com/vaadin/server/communication/PushConnection.java index 2db9d42763..590219b1b5 100644 --- a/server/src/com/vaadin/server/communication/PushConnection.java +++ b/server/src/com/vaadin/server/communication/PushConnection.java @@ -16,14 +16,6 @@ package com.vaadin.server.communication; -import java.io.IOException; -import java.io.Serializable; -import java.io.StringWriter; -import java.io.Writer; - -import org.atmosphere.cpr.AtmosphereResource; -import org.json.JSONException; - import com.vaadin.ui.UI; /** @@ -33,15 +25,7 @@ import com.vaadin.ui.UI; * @author Vaadin Ltd * @since 7.1 */ -public class PushConnection implements Serializable { - - private UI ui; - private boolean pending = true; - private AtmosphereResource resource; - - public PushConnection(UI ui) { - this.ui = ui; - } +public interface PushConnection { /** * Pushes pending state changes and client RPC calls to the client. It is @@ -49,92 +33,6 @@ public class PushConnection implements Serializable { * <p> * This is internal API; please use {@link UI#push()} instead. */ - public void push() { - if (!isConnected()) { - // Not currently connected; defer until connection established - setPending(true); - } else { - try { - push(true); - } catch (IOException e) { - // TODO Error handling - throw new RuntimeException("Push failed", e); - } - } - } - - /** - * Pushes pending state changes and client RPC calls to the client. - * - * @param async - * True if this push asynchronously originates from the server, - * false if it is a response to a client request. - * @throws IOException - */ - protected void push(boolean async) throws IOException { - Writer writer = new StringWriter(); - try { - new UidlWriter().write(getUI(), writer, false, false, async); - } catch (JSONException e) { - throw new IOException("Error writing UIDL", e); - } - // "Broadcast" the changes to the single client only - getResource().getBroadcaster().broadcast(writer.toString(), - getResource()); - } - - /** - * Associates this connection with the given AtmosphereResource. If there is - * a push pending, commits it. - * - * @param resource - * The AtmosphereResource representing the push channel. - * @throws IOException - */ - protected void connect(AtmosphereResource resource) throws IOException { - this.resource = resource; - if (isPending()) { - push(true); - setPending(false); - } - } - - /** - * Returns whether this connection is currently open. - */ - protected boolean isConnected() { - return resource != null - && resource.getBroadcaster().getAtmosphereResources() - .contains(resource); - } - - /** - * Marks that changes in the UI should be pushed as soon as a connection is - * established. - */ - protected void setPending(boolean pending) { - this.pending = pending; - } - - /** - * @return Whether the UI should be pushed as soon as a connection opens. - */ - protected boolean isPending() { - return pending; - } - - /** - * @return the UI associated with this connection. - */ - protected UI getUI() { - return ui; - } + public void push(); - /** - * @return The AtmosphereResource associated with this connection or null if - * connection not open. - */ - protected AtmosphereResource getResource() { - return resource; - } -} +}
\ No newline at end of file diff --git a/server/src/com/vaadin/server/communication/PushHandler.java b/server/src/com/vaadin/server/communication/PushHandler.java index de4987a99a..b3b4551d44 100644 --- a/server/src/com/vaadin/server/communication/PushHandler.java +++ b/server/src/com/vaadin/server/communication/PushHandler.java @@ -75,7 +75,9 @@ public class PushHandler implements AtmosphereHandler { if (ui == null) { throw new RuntimeException("UI not found!"); } - PushConnection connection = ui.getPushConnection(); + assert ui.getPushConnection() instanceof AtmospherePushConnection; + AtmospherePushConnection connection = (AtmospherePushConnection) ui + .getPushConnection(); if (req.getMethod().equalsIgnoreCase("GET")) { /* diff --git a/server/src/com/vaadin/server/communication/UIInitHandler.java b/server/src/com/vaadin/server/communication/UIInitHandler.java index 8275ea3efd..e3d7264de9 100644 --- a/server/src/com/vaadin/server/communication/UIInitHandler.java +++ b/server/src/com/vaadin/server/communication/UIInitHandler.java @@ -207,7 +207,11 @@ public abstract class UIInitHandler extends SynchronizedRequestHandler { UI.setCurrent(ui); if (session.getPushMode() != PushMode.DISABLED) { - ui.setPushConnection(new PushConnection(ui)); + /* + * TODO This currently makes it very difficult to use any other push + * implementation than the bundled one. + */ + ui.setPushConnection(new AtmospherePushConnection(ui)); } ui.doInit(request, uiId.intValue()); |