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.

ClientRpcWriter.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. * Copyright 2000-2016 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.lang.reflect.Type;
  21. import java.util.ArrayList;
  22. import java.util.Collection;
  23. import java.util.List;
  24. import com.vaadin.server.ClientConnector;
  25. import com.vaadin.server.ClientMethodInvocation;
  26. import com.vaadin.server.EncodeResult;
  27. import com.vaadin.server.JsonCodec;
  28. import com.vaadin.server.PaintException;
  29. import com.vaadin.shared.communication.ClientRpc;
  30. import com.vaadin.ui.UI;
  31. import elemental.json.Json;
  32. import elemental.json.JsonArray;
  33. import elemental.json.JsonException;
  34. import elemental.json.JsonValue;
  35. import elemental.json.impl.JsonUtil;
  36. /**
  37. * Serializes {@link ClientRpc client RPC} invocations to JSON.
  38. *
  39. * @author Vaadin Ltd
  40. * @since 7.1
  41. */
  42. public class ClientRpcWriter implements Serializable {
  43. /**
  44. * Writes a JSON object containing all pending client RPC invocations in the
  45. * given UI.
  46. *
  47. * @param ui
  48. * The {@link UI} whose RPC calls to write.
  49. * @param writer
  50. * The {@link Writer} used to write the JSON.
  51. * @throws IOException
  52. * If the serialization fails.
  53. */
  54. public void write(UI ui, Writer writer) throws IOException {
  55. Collection<ClientMethodInvocation> pendingInvocations = collectPendingRpcCalls(
  56. ui.getConnectorTracker().getDirtyVisibleConnectors());
  57. JsonArray rpcCalls = Json.createArray();
  58. for (ClientMethodInvocation invocation : pendingInvocations) {
  59. // add invocation to rpcCalls
  60. try {
  61. JsonArray invocationJson = Json.createArray();
  62. invocationJson.set(0,
  63. invocation.getConnector().getConnectorId());
  64. invocationJson.set(1, invocation.getInterfaceName());
  65. invocationJson.set(2, invocation.getMethodName());
  66. JsonArray paramJson = Json.createArray();
  67. for (int i = 0; i < invocation
  68. .getParameterTypes().length; ++i) {
  69. Type parameterType = invocation.getParameterTypes()[i];
  70. JsonValue referenceParameter = null;
  71. // TODO Use default values for RPC parameter types
  72. // if (!JsonCodec.isInternalType(parameterType)) {
  73. // try {
  74. // referenceParameter = parameterType.newInstance();
  75. // } catch (Exception e) {
  76. // logger.log(Level.WARNING,
  77. // "Error creating reference object for parameter of type "
  78. // + parameterType.getName());
  79. // }
  80. // }
  81. EncodeResult encodeResult = JsonCodec.encode(
  82. invocation.getParameters()[i], referenceParameter,
  83. parameterType, ui.getConnectorTracker());
  84. paramJson.set(i, encodeResult.getEncodedValue());
  85. }
  86. invocationJson.set(3, paramJson);
  87. rpcCalls.set(rpcCalls.length(), invocationJson);
  88. } catch (JsonException e) {
  89. throw new PaintException(
  90. "Failed to serialize RPC method call parameters for connector "
  91. + invocation.getConnector().getConnectorId()
  92. + " method " + invocation.getInterfaceName()
  93. + "." + invocation.getMethodName() + ": "
  94. + e.getMessage(),
  95. e);
  96. }
  97. }
  98. writer.write(JsonUtil.stringify(rpcCalls));
  99. }
  100. /**
  101. * Collects all pending RPC calls from listed {@link ClientConnector}s and
  102. * clears their RPC queues.
  103. *
  104. * @param rpcPendingQueue
  105. * list of {@link ClientConnector} of interest
  106. * @return ordered list of pending RPC calls
  107. */
  108. private Collection<ClientMethodInvocation> collectPendingRpcCalls(
  109. Collection<ClientConnector> rpcPendingQueue) {
  110. List<ClientMethodInvocation> pendingInvocations = new ArrayList<>();
  111. for (ClientConnector connector : rpcPendingQueue) {
  112. List<ClientMethodInvocation> paintablePendingRpc = connector
  113. .retrievePendingRpcCalls();
  114. if (null != paintablePendingRpc && !paintablePendingRpc.isEmpty()) {
  115. List<ClientMethodInvocation> oldPendingRpc = pendingInvocations;
  116. int totalCalls = pendingInvocations.size()
  117. + paintablePendingRpc.size();
  118. pendingInvocations = new ArrayList<>(totalCalls);
  119. // merge two ordered comparable lists
  120. for (int destIndex = 0, oldIndex = 0, paintableIndex = 0; destIndex < totalCalls; destIndex++) {
  121. if (paintableIndex >= paintablePendingRpc.size()
  122. || (oldIndex < oldPendingRpc.size() && oldPendingRpc
  123. .get(oldIndex).compareTo(paintablePendingRpc
  124. .get(paintableIndex)) <= 0)) {
  125. pendingInvocations.add(oldPendingRpc.get(oldIndex++));
  126. } else {
  127. pendingInvocations
  128. .add(paintablePendingRpc.get(paintableIndex++));
  129. }
  130. }
  131. }
  132. }
  133. return pendingInvocations;
  134. }
  135. }