You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SharedStateWriter.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.server.communication;
  17. import java.io.IOException;
  18. import java.io.Serializable;
  19. import java.io.Writer;
  20. import java.util.Collection;
  21. import java.util.HashSet;
  22. import java.util.Set;
  23. import com.vaadin.server.ClientConnector;
  24. import com.vaadin.server.PaintException;
  25. import com.vaadin.shared.communication.SharedState;
  26. import com.vaadin.ui.UI;
  27. import elemental.json.Json;
  28. import elemental.json.JsonException;
  29. import elemental.json.JsonObject;
  30. import elemental.json.impl.JsonUtil;
  31. /**
  32. * Serializes {@link SharedState shared state} changes to JSON.
  33. *
  34. * @author Vaadin Ltd
  35. * @since 7.1
  36. */
  37. public class SharedStateWriter implements Serializable {
  38. /**
  39. * Writes a JSON object containing the pending state changes of the dirty
  40. * connectors of the given UI.
  41. *
  42. * @param ui
  43. * The UI whose state changes should be written.
  44. * @param writer
  45. * The writer to use.
  46. * @return a set of connector ids with state changes
  47. * @throws IOException
  48. * If the serialization fails.
  49. */
  50. public Set<String> write(UI ui, Writer writer) throws IOException {
  51. Collection<ClientConnector> dirtyVisibleConnectors = ui
  52. .getConnectorTracker().getDirtyVisibleConnectors();
  53. Set<String> writtenConnectors = new HashSet<String>();
  54. JsonObject sharedStates = Json.createObject();
  55. for (ClientConnector connector : dirtyVisibleConnectors) {
  56. // encode and send shared state
  57. String connectorId = connector.getConnectorId();
  58. try {
  59. JsonObject stateJson = connector.encodeState();
  60. if (stateJson != null && stateJson.keys().length != 0) {
  61. sharedStates.put(connectorId, stateJson);
  62. writtenConnectors.add(connectorId);
  63. }
  64. } catch (JsonException e) {
  65. throw new PaintException(
  66. "Failed to serialize shared state for connector "
  67. + connector.getClass().getName() + " ("
  68. + connectorId + "): " + e.getMessage(), e);
  69. }
  70. }
  71. writer.write(JsonUtil.stringify(sharedStates));
  72. return writtenConnectors;
  73. }
  74. }