/** * Get-or-create method for synthetic ids. * * @see #syntheticIds */ private <Q extends BaseProxy> SimpleProxyId<Q> allocateSyntheticId(String typeToken, int syntheticId) { @SuppressWarnings("unchecked") SimpleProxyId<Q> toReturn = (SimpleProxyId<Q>) state.syntheticIds.get(syntheticId); if (toReturn == null) { toReturn = state.requestFactory.allocateId(state.requestFactory.<Q> getTypeFromToken(typeToken)); state.syntheticIds.put(syntheticId, toReturn); } return toReturn; }
@Override SimpleProxyId<BaseProxy> getId(IdMessage op) { if (Strength.SYNTHETIC.equals(op.getStrength())) { return getRequestFactory().allocateSyntheticId( getRequestFactory().getTypeFromToken(op.getTypeToken()), op.getSyntheticId()); } return super.getId(op); }
@Override protected AutoBeanFactory getAutoBeanFactory() { return AbstractRequestFactory.this.getAutoBeanFactory(); } };
if (operations != null && state.requestFactory.isEntityType(id.getProxyClass())) { for (WriteOperation writeOperation : operations) { if (writeOperation.equals(WriteOperation.UPDATE) && !state.requestFactory.hasVersionChanged(id, op.getVersion())) { state.requestFactory.getEventBus().fireEventFromSource( new EntityProxyChange<EntityProxy>((EntityProxy) proxy, writeOperation), id.getProxyClass());
public String getHistoryToken(EntityProxyId<?> proxy) { return getHistoryToken((SimpleProxyId<?>) proxy); }
/** * Create a new object, with an ephemeral id. */ public <T extends BaseProxy> T create(Class<T> clazz) { checkLocked(); SimpleProxyId<T> id = state.requestFactory.allocateId(clazz); AutoBean<T> created = createProxy(clazz, id, false); return takeOwnership(created); }
/** * Resolves an IdMessage into an SimpleProxyId. */ SimpleProxyId<BaseProxy> getId(IdMessage op) { if (Strength.SYNTHETIC.equals(op.getStrength())) { return allocateSyntheticId(op.getTypeToken(), op.getSyntheticId()); } return state.requestFactory.getId(op.getTypeToken(), op.getServerId(), op.getClientId()); }
@SuppressWarnings("unchecked") public <T extends EntityProxy> EntityProxyId<T> getProxyId(String historyToken) { return (EntityProxyId<T>) getBaseProxyId(historyToken); }
public Class<? extends EntityProxy> getProxyClass(String historyToken) { String typeToken = IdUtil.getTypeToken(historyToken); if (typeToken != null) { return getTypeFromToken(typeToken); } return getTypeFromToken(historyToken); }
/** * Convert any non-persistent ids into store-local synthetic ids. */ private <T extends BaseProxy> SimpleProxyId<T> serializedId(SimpleProxyId<T> stableId) { assert !stableId.isSynthetic(); if (stableId.isEphemeral()) { @SuppressWarnings("unchecked") SimpleProxyId<T> syntheticId = (SimpleProxyId<T>) syntheticIds.get(stableId); if (syntheticId == null) { int nextId = store.nextId(); assert nextId >= 0 : "ProxyStore.nextId() returned a negative number " + nextId; syntheticId = getRequestFactory().allocateSyntheticId(stableId.getProxyClass(), nextId + 1); syntheticIds.put(stableId, syntheticId); } return syntheticId; } return stableId; }
/** * Assemble all of the state that has been accumulated in this context. This includes: * <ul> * <li>Diffs accumulated on objects passed to {@link #edit}. * <li>Invocations accumulated as Request subtypes passed to {@link #addInvocation}. * </ul> */ public String makePayload() { // Get the factory from the runtime-specific holder. MessageFactory f = MessageFactoryHolder.FACTORY; List<OperationMessage> operations = makePayloadOperations(); List<InvocationMessage> invocationMessages = makePayloadInvocations(); // Create the outer envelope message AutoBean<RequestMessage> bean = f.request(); RequestMessage requestMessage = bean.as(); requestMessage.setRequestFactory(getRequestFactory().getFactoryTypeToken()); if (!invocationMessages.isEmpty()) { requestMessage.setInvocations(invocationMessages); } if (!operations.isEmpty()) { requestMessage.setOperations(operations); } return AutoBeanCodex.encode(bean).getPayload(); }
state.requestFactory.getRequestTransport().send(payload, new TransportReceiver() { public void onTransportFailure(ServerFailure failure) { fail(finalReceiver, failure);
if (operations != null && state.requestFactory.isEntityType(id.getProxyClass())) { for (WriteOperation writeOperation : operations) { if (writeOperation.equals(WriteOperation.UPDATE) && !state.requestFactory.hasVersionChanged(id, op.getVersion())) { state.requestFactory.getEventBus().fireEventFromSource( new EntityProxyChange<EntityProxy>((EntityProxy) proxy, writeOperation), id.getProxyClass());
/** * Used by {@link AbstractRequestContext} to quiesce update events for objects * that haven't truly changed. */ protected boolean hasVersionChanged(SimpleProxyId<?> id, String observedVersion) { assert id != null : "id"; assert observedVersion != null : "observedVersion"; String key = getHistoryToken(id); String existingVersion = version.get(key); // Return true if we haven't seen this before or the versions differ boolean toReturn = existingVersion == null || !existingVersion.equals(observedVersion); if (toReturn) { version.put(key, observedVersion); } return toReturn; } }
public void processPayload(Receiver<Void> receiver, String payload) { Splittable raw = StringQuoter.split(payload); @SuppressWarnings("unchecked") Receiver<Object> callback = (Receiver<Object>) state.invocations.get(0).getReceiver(); if (!raw.isNull("error")) { Splittable error = raw.get("error"); ServerFailure failure = new ServerFailure(error.get("message").asString(), error.get("code").asString(), payload, true); fail(receiver, failure); return; } Splittable result = raw.get("result"); @SuppressWarnings("unchecked") Class<BaseProxy> target = (Class<BaseProxy>) state.invocations.get(0).getRequestData().getReturnType(); SimpleProxyId<BaseProxy> id = getRequestFactory().allocateId(target); AutoBean<BaseProxy> bean = createProxy(target, id, true); // XXX expose this as a proper API ((AbstractAutoBean<?>) bean).setData(result); // AutoBeanCodex.decodeInto(result, bean); if (callback != null) { callback.onSuccess(bean.as()); } if (receiver != null) { receiver.onSuccess(null); } }
/** * Resolves an IdMessage into an SimpleProxyId. */ SimpleProxyId<BaseProxy> getId(IdMessage op) { if (Strength.SYNTHETIC.equals(op.getStrength())) { return allocateSyntheticId(op.getTypeToken(), op.getSyntheticId()); } return state.requestFactory.getId(op.getTypeToken(), op.getServerId(), op.getClientId()); }
@SuppressWarnings("unchecked") public <T extends EntityProxy> EntityProxyId<T> getProxyId(String historyToken) { return (EntityProxyId<T>) getBaseProxyId(historyToken); }
public Class<? extends EntityProxy> getProxyClass(String historyToken) { String typeToken = IdUtil.getTypeToken(historyToken); if (typeToken != null) { return getTypeFromToken(typeToken); } return getTypeFromToken(historyToken); }
/** * Convert any non-persistent ids into store-local synthetic ids. */ private <T extends BaseProxy> SimpleProxyId<T> serializedId(SimpleProxyId<T> stableId) { assert !stableId.isSynthetic(); if (stableId.isEphemeral()) { @SuppressWarnings("unchecked") SimpleProxyId<T> syntheticId = (SimpleProxyId<T>) syntheticIds.get(stableId); if (syntheticId == null) { int nextId = store.nextId(); assert nextId >= 0 : "ProxyStore.nextId() returned a negative number " + nextId; syntheticId = getRequestFactory().allocateSyntheticId(stableId.getProxyClass(), nextId + 1); syntheticIds.put(stableId, syntheticId); } return syntheticId; } return stableId; }
/** * Assemble all of the state that has been accumulated in this context. This includes: * <ul> * <li>Diffs accumulated on objects passed to {@link #edit}. * <li>Invocations accumulated as Request subtypes passed to {@link #addInvocation}. * </ul> */ public String makePayload() { // Get the factory from the runtime-specific holder. MessageFactory f = MessageFactoryHolder.FACTORY; List<OperationMessage> operations = makePayloadOperations(); List<InvocationMessage> invocationMessages = makePayloadInvocations(); // Create the outer envelope message AutoBean<RequestMessage> bean = f.request(); RequestMessage requestMessage = bean.as(); requestMessage.setRequestFactory(getRequestFactory().getFactoryTypeToken()); if (!invocationMessages.isEmpty()) { requestMessage.setInvocations(invocationMessages); } if (!operations.isEmpty()) { requestMessage.setOperations(operations); } return AutoBeanCodex.encode(bean).getPayload(); }