use of org.apache.cayenne.remote.ClientMessage in project cayenne by apache.
the class LocalConnection method doSendMessage.
/**
* Dispatches a message to an internal handler.
*/
@Override
protected Object doSendMessage(ClientMessage message) throws CayenneRuntimeException {
ClientMessage processedMessage;
try {
switch(serializationPolicy) {
case HESSIAN_SERIALIZATION:
processedMessage = (ClientMessage) HessianUtil.cloneViaClientServerSerialization(message, channel.getEntityResolver());
break;
case JAVA_SERIALIZATION:
processedMessage = Util.cloneViaSerialization(message);
break;
default:
processedMessage = message;
}
} catch (Exception ex) {
throw new CayenneRuntimeException("Error serializing message", ex);
}
Serializable result = (Serializable) DispatchHelper.dispatch(channel, processedMessage);
try {
switch(serializationPolicy) {
case HESSIAN_SERIALIZATION:
return HessianUtil.cloneViaServerClientSerialization(result, channel.getEntityResolver());
case JAVA_SERIALIZATION:
return Util.cloneViaSerialization(result);
default:
return result;
}
} catch (Exception ex) {
throw new CayenneRuntimeException("Error deserializing result", ex);
}
}
use of org.apache.cayenne.remote.ClientMessage in project cayenne by apache.
the class CayenneContextIT method testBeforeHollowDeleteShouldChangeStateToCommited.
@Test
public void testBeforeHollowDeleteShouldChangeStateToCommited() {
ObjectId gid = new ObjectId("MtTable1", "a", "b");
final ClientMtTable1 inflated = new ClientMtTable1();
inflated.setPersistenceState(PersistenceState.COMMITTED);
inflated.setObjectId(gid);
inflated.setGlobalAttribute1("abc");
ClientConnection connection = mock(ClientConnection.class);
when(connection.sendMessage((ClientMessage) any())).thenAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) {
ClientMessage arg = (ClientMessage) invocation.getArguments()[0];
if (arg instanceof BootstrapMessage) {
return new EntityResolver();
} else {
return new GenericResponse(Arrays.asList(inflated));
}
}
});
ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false);
CayenneContext context = new CayenneContext(channel);
context.setEntityResolver(serverContext.getEntityResolver().getClientEntityResolver());
ClientMtTable1 hollow = context.localObject(inflated);
assertEquals(PersistenceState.HOLLOW, hollow.getPersistenceState());
// testing this...
context.deleteObjects(hollow);
assertSame(hollow, context.getGraphManager().getNode(gid));
assertEquals(inflated.getGlobalAttribute1Direct(), hollow.getGlobalAttribute1Direct());
assertEquals(PersistenceState.DELETED, hollow.getPersistenceState());
}
use of org.apache.cayenne.remote.ClientMessage in project cayenne by apache.
the class CayenneContextIT method testBeforePropertyReadShouldInflateHollow.
@Test
public void testBeforePropertyReadShouldInflateHollow() {
ObjectId gid = new ObjectId("MtTable1", "a", "b");
final ClientMtTable1 inflated = new ClientMtTable1();
inflated.setPersistenceState(PersistenceState.COMMITTED);
inflated.setObjectId(gid);
inflated.setGlobalAttribute1("abc");
ClientConnection connection = mock(ClientConnection.class);
when(connection.sendMessage((ClientMessage) any())).thenAnswer(new Answer<Object>() {
public Object answer(InvocationOnMock invocation) {
ClientMessage arg = (ClientMessage) invocation.getArguments()[0];
if (arg instanceof BootstrapMessage) {
return new EntityResolver();
} else {
return new GenericResponse(Arrays.asList(inflated));
}
}
});
ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false);
// check that a HOLLOW object is infalted on "beforePropertyRead"
ClientMtTable1 hollow = new ClientMtTable1();
hollow.setPersistenceState(PersistenceState.HOLLOW);
hollow.setObjectId(gid);
final boolean[] selectExecuted = new boolean[1];
CayenneContext context = new CayenneContext(channel) {
@Override
public List<?> performQuery(Query query) {
selectExecuted[0] = true;
return super.performQuery(query);
}
};
context.setEntityResolver(serverContext.getEntityResolver().getClientEntityResolver());
context.graphManager.registerNode(hollow.getObjectId(), hollow);
// testing this...
context.prepareForAccess(hollow, ClientMtTable1.GLOBAL_ATTRIBUTE1_PROPERTY, false);
assertTrue(selectExecuted[0]);
assertSame(hollow, context.getGraphManager().getNode(gid));
assertEquals(inflated.getGlobalAttribute1Direct(), hollow.getGlobalAttribute1Direct());
assertEquals(PersistenceState.COMMITTED, hollow.getPersistenceState());
}
use of org.apache.cayenne.remote.ClientMessage in project cayenne by apache.
the class ProtostuffLocalConnection method doSendMessage.
@Override
protected Object doSendMessage(ClientMessage message) throws CayenneRuntimeException {
try {
ClientMessage processedMessage = (ClientMessage) cloneViaSerializationService(message);
Object result = DispatchHelper.dispatch(channel, processedMessage);
return cloneViaSerializationService(result);
} catch (Exception ex) {
throw new CayenneRuntimeException("Error deserializing result", ex);
}
}
Aggregations