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.

JavaScriptCallbackHelper.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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;
  17. import java.io.Serializable;
  18. import java.lang.reflect.Method;
  19. import java.util.HashMap;
  20. import java.util.HashSet;
  21. import java.util.Map;
  22. import java.util.Set;
  23. import com.vaadin.shared.JavaScriptConnectorState;
  24. import com.vaadin.ui.AbstractJavaScriptComponent;
  25. import com.vaadin.ui.JavaScript.JavaScriptCallbackRpc;
  26. import com.vaadin.ui.JavaScriptFunction;
  27. import com.vaadin.util.ReflectTools;
  28. import elemental.json.JsonArray;
  29. import elemental.json.JsonException;
  30. /**
  31. * Internal helper class used to implement functionality common to
  32. * {@link AbstractJavaScriptComponent} and {@link AbstractJavaScriptExtension}.
  33. * Corresponding support in client-side code is in
  34. * {@link com.vaadin.client.JavaScriptConnectorHelper}.
  35. * <p>
  36. * You should most likely no use this class directly.
  37. *
  38. * @author Vaadin Ltd
  39. * @since 7.0.0
  40. */
  41. public class JavaScriptCallbackHelper implements Serializable {
  42. private static final Method CALL_METHOD = ReflectTools.findMethod(
  43. JavaScriptCallbackRpc.class, "call", String.class, JsonArray.class);
  44. private AbstractClientConnector connector;
  45. private Map<String, JavaScriptFunction> callbacks = new HashMap<String, JavaScriptFunction>();
  46. private JavaScriptCallbackRpc javascriptCallbackRpc;
  47. public JavaScriptCallbackHelper(AbstractClientConnector connector) {
  48. this.connector = connector;
  49. }
  50. public void registerCallback(String functionName,
  51. JavaScriptFunction javaScriptCallback) {
  52. callbacks.put(functionName, javaScriptCallback);
  53. JavaScriptConnectorState state = getConnectorState();
  54. state.getCallbackNames().add(functionName);
  55. ensureRpc();
  56. }
  57. private JavaScriptConnectorState getConnectorState() {
  58. JavaScriptConnectorState state = (JavaScriptConnectorState) connector
  59. .getState();
  60. return state;
  61. }
  62. private void ensureRpc() {
  63. if (javascriptCallbackRpc == null) {
  64. javascriptCallbackRpc = new JavaScriptCallbackRpc() {
  65. @Override
  66. public void call(String name, JsonArray arguments) {
  67. JavaScriptFunction callback = callbacks.get(name);
  68. try {
  69. callback.call(arguments);
  70. } catch (JsonException e) {
  71. throw new IllegalArgumentException(e);
  72. }
  73. }
  74. };
  75. connector.registerRpc(javascriptCallbackRpc);
  76. }
  77. }
  78. public void invokeCallback(String name, Object... arguments) {
  79. if (callbacks.containsKey(name)) {
  80. throw new IllegalStateException(
  81. "Can't call callback "
  82. + name
  83. + " on the client because a callback with the same name is registered on the server.");
  84. }
  85. JsonArray args = (JsonArray) JsonCodec.encode(arguments, null, Object[].class, null).getEncodedValue();
  86. connector.addMethodInvocationToQueue(
  87. JavaScriptCallbackRpc.class.getName(), CALL_METHOD,
  88. new Object[] { name, args });
  89. }
  90. public void registerRpc(Class<?> rpcInterfaceType) {
  91. if (rpcInterfaceType == JavaScriptCallbackRpc.class) {
  92. // Ignore
  93. return;
  94. }
  95. Map<String, Set<String>> rpcInterfaces = getConnectorState()
  96. .getRpcInterfaces();
  97. String interfaceName = rpcInterfaceType.getName();
  98. if (!rpcInterfaces.containsKey(interfaceName)) {
  99. Set<String> methodNames = new HashSet<String>();
  100. for (Method method : rpcInterfaceType.getMethods()) {
  101. methodNames.add(method.getName());
  102. }
  103. rpcInterfaces.put(interfaceName, methodNames);
  104. }
  105. }
  106. }