Guest User

MyProxyStore

a guest
Dec 29th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Map;
  3.  
  4. import com.google.web.bindery.autobean.shared.AutoBean;
  5. import com.google.web.bindery.autobean.shared.AutoBeanCodex;
  6. import com.google.web.bindery.autobean.shared.Splittable;
  7. import com.google.web.bindery.requestfactory.shared.ProxyStore;
  8. import com.google.web.bindery.requestfactory.shared.impl.MessageFactoryHolder;
  9. import com.google.web.bindery.requestfactory.shared.messages.OperationMessage;
  10.  
  11.  
  12. /*
  13. * Patched version of DefaultProxyStore that
  14. * always returns a unique id.
  15. */
  16. public class MyProxyStore implements ProxyStore {
  17. /**
  18. * Provide a little bit of future-proofing to at least detect an encoded
  19. * payload that can't be decoded.
  20. */
  21. private static final String EXPECTED_VERSION = "211";
  22. private final AutoBean<OperationMessage> messageBean;
  23. private final Map<String, Splittable> map;
  24. private int curId = 0;
  25.  
  26. /**
  27. * Construct an empty DefaultProxyStore.
  28. */
  29. public MyProxyStore() {
  30. messageBean = MessageFactoryHolder.FACTORY.operation();
  31. map = new HashMap<String, Splittable>();
  32.  
  33. OperationMessage message = messageBean.as();
  34. message.setPropertyMap(map);
  35. message.setVersion(EXPECTED_VERSION);
  36. }
  37.  
  38. /**
  39. * Construct a DefaultProxyStore using the a value returned from
  40. * {@link #encode()}.
  41. *
  42. * @param payload a String previously returned from {@link #encode()}
  43. * @throws IllegalArgumentException if the payload cannot be parsed
  44. */
  45. public MyProxyStore(String payload) throws IllegalArgumentException {
  46. messageBean =
  47. AutoBeanCodex.decode(MessageFactoryHolder.FACTORY, OperationMessage.class, payload);
  48.  
  49. OperationMessage message = messageBean.as();
  50. if (!EXPECTED_VERSION.equals(message.getVersion())) {
  51. throw new IllegalArgumentException("Unexpected version string in payload "
  52. + message.getVersion());
  53. }
  54. map = message.getPropertyMap();
  55. }
  56.  
  57. /**
  58. * Return a JSON object literal with the contents of the store.
  59. */
  60. public String encode() {
  61. return AutoBeanCodex.encode(messageBean).getPayload();
  62. }
  63.  
  64. public Splittable get(String key) {
  65. return map.get(key);
  66. }
  67.  
  68. public int nextId() {
  69. return curId++;
  70. }
  71.  
  72. public void put(String key, Splittable value) {
  73. map.put(key, value);
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment