Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Map;
- import com.google.web.bindery.autobean.shared.AutoBean;
- import com.google.web.bindery.autobean.shared.AutoBeanCodex;
- import com.google.web.bindery.autobean.shared.Splittable;
- import com.google.web.bindery.requestfactory.shared.ProxyStore;
- import com.google.web.bindery.requestfactory.shared.impl.MessageFactoryHolder;
- import com.google.web.bindery.requestfactory.shared.messages.OperationMessage;
- /*
- * Patched version of DefaultProxyStore that
- * always returns a unique id.
- */
- public class MyProxyStore implements ProxyStore {
- /**
- * Provide a little bit of future-proofing to at least detect an encoded
- * payload that can't be decoded.
- */
- private static final String EXPECTED_VERSION = "211";
- private final AutoBean<OperationMessage> messageBean;
- private final Map<String, Splittable> map;
- private int curId = 0;
- /**
- * Construct an empty DefaultProxyStore.
- */
- public MyProxyStore() {
- messageBean = MessageFactoryHolder.FACTORY.operation();
- map = new HashMap<String, Splittable>();
- OperationMessage message = messageBean.as();
- message.setPropertyMap(map);
- message.setVersion(EXPECTED_VERSION);
- }
- /**
- * Construct a DefaultProxyStore using the a value returned from
- * {@link #encode()}.
- *
- * @param payload a String previously returned from {@link #encode()}
- * @throws IllegalArgumentException if the payload cannot be parsed
- */
- public MyProxyStore(String payload) throws IllegalArgumentException {
- messageBean =
- AutoBeanCodex.decode(MessageFactoryHolder.FACTORY, OperationMessage.class, payload);
- OperationMessage message = messageBean.as();
- if (!EXPECTED_VERSION.equals(message.getVersion())) {
- throw new IllegalArgumentException("Unexpected version string in payload "
- + message.getVersion());
- }
- map = message.getPropertyMap();
- }
- /**
- * Return a JSON object literal with the contents of the store.
- */
- public String encode() {
- return AutoBeanCodex.encode(messageBean).getPayload();
- }
- public Splittable get(String key) {
- return map.get(key);
- }
- public int nextId() {
- return curId++;
- }
- public void put(String key, Splittable value) {
- map.put(key, value);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment