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 5.9KB

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