/thirdparty/breakpad/third_party/protobuf/protobuf/java/src/main/java/com/google/protobuf/RpcUtil.java

http://github.com/tomahawk-player/tomahawk · Java · 135 lines · 56 code · 10 blank · 69 comment · 1 complexity · 25ee19f03ac22a00a670f324cb267725 MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. package com.google.protobuf;
  31. /**
  32. * Grab-bag of utility functions useful when dealing with RPCs.
  33. *
  34. * @author kenton@google.com Kenton Varda
  35. */
  36. public final class RpcUtil {
  37. private RpcUtil() {}
  38. /**
  39. * Take an {@code RpcCallback<Message>} and convert it to an
  40. * {@code RpcCallback} accepting a specific message type. This is always
  41. * type-safe (parameter type contravariance).
  42. */
  43. @SuppressWarnings("unchecked")
  44. public static <Type extends Message> RpcCallback<Type>
  45. specializeCallback(final RpcCallback<Message> originalCallback) {
  46. return (RpcCallback<Type>)originalCallback;
  47. // The above cast works, but only due to technical details of the Java
  48. // implementation. A more theoretically correct -- but less efficient --
  49. // implementation would be as follows:
  50. // return new RpcCallback<Type>() {
  51. // public void run(Type parameter) {
  52. // originalCallback.run(parameter);
  53. // }
  54. // };
  55. }
  56. /**
  57. * Take an {@code RpcCallback} accepting a specific message type and convert
  58. * it to an {@code RpcCallback<Message>}. The generalized callback will
  59. * accept any message object which has the same descriptor, and will convert
  60. * it to the correct class before calling the original callback. However,
  61. * if the generalized callback is given a message with a different descriptor,
  62. * an exception will be thrown.
  63. */
  64. public static <Type extends Message>
  65. RpcCallback<Message> generalizeCallback(
  66. final RpcCallback<Type> originalCallback,
  67. final Class<Type> originalClass,
  68. final Type defaultInstance) {
  69. return new RpcCallback<Message>() {
  70. public void run(final Message parameter) {
  71. Type typedParameter;
  72. try {
  73. typedParameter = originalClass.cast(parameter);
  74. } catch (ClassCastException ignored) {
  75. typedParameter = copyAsType(defaultInstance, parameter);
  76. }
  77. originalCallback.run(typedParameter);
  78. }
  79. };
  80. }
  81. /**
  82. * Creates a new message of type "Type" which is a copy of "source". "source"
  83. * must have the same descriptor but may be a different class (e.g.
  84. * DynamicMessage).
  85. */
  86. @SuppressWarnings("unchecked")
  87. private static <Type extends Message> Type copyAsType(
  88. final Type typeDefaultInstance, final Message source) {
  89. return (Type)typeDefaultInstance.newBuilderForType()
  90. .mergeFrom(source)
  91. .build();
  92. }
  93. /**
  94. * Creates a callback which can only be called once. This may be useful for
  95. * security, when passing a callback to untrusted code: most callbacks do
  96. * not expect to be called more than once, so doing so may expose bugs if it
  97. * is not prevented.
  98. */
  99. public static <ParameterType>
  100. RpcCallback<ParameterType> newOneTimeCallback(
  101. final RpcCallback<ParameterType> originalCallback) {
  102. return new RpcCallback<ParameterType>() {
  103. private boolean alreadyCalled = false;
  104. public void run(final ParameterType parameter) {
  105. synchronized(this) {
  106. if (alreadyCalled) {
  107. throw new AlreadyCalledException();
  108. }
  109. alreadyCalled = true;
  110. }
  111. originalCallback.run(parameter);
  112. }
  113. };
  114. }
  115. /**
  116. * Exception thrown when a one-time callback is called more than once.
  117. */
  118. public static final class AlreadyCalledException extends RuntimeException {
  119. private static final long serialVersionUID = 5469741279507848266L;
  120. public AlreadyCalledException() {
  121. super("This RpcCallback was already called and cannot be called " +
  122. "multiple times.");
  123. }
  124. }
  125. }