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.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.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(ui
  56. .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, invocation.getConnector().getConnectorId());
  63. invocationJson.set(1, invocation.getInterfaceName());
  64. invocationJson.set(2, invocation.getMethodName());
  65. JsonArray paramJson = Json.createArray();
  66. for (int i = 0; i < invocation.getParameterTypes().length; ++i) {
  67. Type parameterType = invocation.getParameterTypes()[i];
  68. JsonValue referenceParameter = null;
  69. // TODO Use default values for RPC parameter types
  70. // if (!JsonCodec.isInternalType(parameterType)) {
  71. // try {
  72. // referenceParameter = parameterType.newInstance();
  73. // } catch (Exception e) {
  74. // logger.log(Level.WARNING,
  75. // "Error creating reference object for parameter of type "
  76. // + parameterType.getName());
  77. // }
  78. // }
  79. EncodeResult encodeResult = JsonCodec.encode(
  80. invocation.getParameters()[i], referenceParameter,
  81. parameterType, ui.getConnectorTracker());
  82. paramJson.set(i, encodeResult.getEncodedValue());
  83. }
  84. invocationJson.set(3, paramJson);
  85. rpcCalls.set(rpcCalls.length(), invocationJson);
  86. } catch (JsonException e) {
  87. throw new PaintException(
  88. "Failed to serialize RPC method call parameters for connector "
  89. + invocation.getConnector().getConnectorId()
  90. + " method " + invocation.getInterfaceName()
  91. + "." + invocation.getMethodName() + ": "
  92. + e.getMessage(), e);
  93. }
  94. }
  95. writer.write(JsonUtil.stringify(rpcCalls));
  96. }
  97. /**
  98. * Collects all pending RPC calls from listed {@link ClientConnector}s and
  99. * clears their RPC queues.
  100. *
  101. * @param rpcPendingQueue
  102. * list of {@link ClientConnector} of interest
  103. * @return ordered list of pending RPC calls
  104. */
  105. private Collection<ClientMethodInvocation> collectPendingRpcCalls(
  106. Collection<ClientConnector> rpcPendingQueue) {
  107. List<ClientMethodInvocation> pendingInvocations = new ArrayList<ClientMethodInvocation>();
  108. for (ClientConnector connector : rpcPendingQueue) {
  109. List<ClientMethodInvocation> paintablePendingRpc = connector
  110. .retrievePendingRpcCalls();
  111. if (null != paintablePendingRpc && !paintablePendingRpc.isEmpty()) {
  112. List<ClientMethodInvocation> oldPendingRpc = pendingInvocations;
  113. int totalCalls = pendingInvocations.size()
  114. + paintablePendingRpc.size();
  115. pendingInvocations = new ArrayList<ClientMethodInvocation>(
  116. totalCalls);
  117. // merge two ordered comparable lists
  118. for (int destIndex = 0, oldIndex = 0, paintableIndex = 0; destIndex < totalCalls; destIndex++) {
  119. if (paintableIndex >= paintablePendingRpc.size()
  120. || (oldIndex < oldPendingRpc.size() && ((Comparable<ClientMethodInvocation>) oldPendingRpc
  121. .get(oldIndex))
  122. .compareTo(paintablePendingRpc
  123. .get(paintableIndex)) <= 0)) {
  124. pendingInvocations.add(oldPendingRpc.get(oldIndex++));
  125. } else {
  126. pendingInvocations.add(paintablePendingRpc
  127. .get(paintableIndex++));
  128. }
  129. }
  130. }
  131. }
  132. return pendingInvocations;
  133. }
  134. }