throws IOException,
ClassNotFoundException
{
byte[] data;
boolean unknownMessage = false;
final KeyValueMessage kvMessage = (KeyValueMessage) message.specification;
CompletionToken token = null;
String key;
final String messagePrefix = "KeyValueStub - Received request for ";
switch (kvMessage) {
case ACCESS :
KeyValueStub.logger.trace ("Received initiation message");
final KeyValuePayloads.InitRequest initRequest = (InitRequest) message.payload;
token = initRequest.getToken ();
final String bucket = initRequest.getBucket ();
driver.registerClient (token.getClientId (), bucket);
break;
case ABORTED :
KeyValueStub.logger.trace ("Received termination message");
final IdlCommon.AbortRequest abortRequest = (AbortRequest) message.payload;
token = abortRequest.getToken ();
driver.unregisterClient (token.getClientId ());
break;
case SET_REQUEST :
final KeyValuePayloads.SetRequest setRequest = (SetRequest) message.payload;
token = setRequest.getToken ();
key = setRequest.getKey ();
data = setRequest.getValue ().toByteArray ();
final eu.mosaic_cloud.platform.interop.common.kv.KeyValueMessage messageData = new eu.mosaic_cloud.platform.interop.common.kv.KeyValueMessage (key, data, setRequest.getEnvelope ().getContentEncoding (), setRequest.getEnvelope ().getContentType ());
KeyValueStub.logger.trace (messagePrefix + kvMessage.toString () + " key: " + key + " - request id: " + token.getMessageId () + " client id: " + token.getClientId ());
// NOTE: execute operation
final DriverOperationFinishedHandler setCallback = new DriverOperationFinishedHandler (token, session, driver.getClass (), transmitterClass);
final IResult<Boolean> resultSet = driver.invokeSetOperation (token.getClientId (), messageData, setCallback);
setCallback.setDetails (KeyValueOperations.SET, resultSet);
break;
case GET_REQUEST :
final KeyValuePayloads.GetRequest getRequest = (GetRequest) message.payload;
token = getRequest.getToken ();
final DriverOperationFinishedHandler getCallback = new DriverOperationFinishedHandler (token, session, driver.getClass (), transmitterClass);
if (getRequest.getKeyCount () != 1) {
// NOTE: error - the simple driver can handle only single-key get
KeyValueStub.logger.error ("Basic driver can handle only single-key GET.");
driver.handleUnsupportedOperationError (kvMessage.toString (), getCallback);
break;
}
key = getRequest.getKey (0);
KeyValueStub.logger.trace (messagePrefix + kvMessage.toString () + " key: " + key + " - request id: " + token.getMessageId () + " client id: " + token.getClientId ());
final EncodingMetadata expectedEncoding = new EncodingMetadata (getRequest.getEnvelope ().getContentType (), getRequest.getEnvelope ().getContentEncoding ());
final IResult<eu.mosaic_cloud.platform.interop.common.kv.KeyValueMessage> resultGet = driver.invokeGetOperation (token.getClientId (), key, expectedEncoding, getCallback);
getCallback.setDetails (KeyValueOperations.GET, resultGet);
break;
case DELETE_REQUEST :
final KeyValuePayloads.DeleteRequest delRequest = (DeleteRequest) message.payload;
token = delRequest.getToken ();
key = delRequest.getKey ();
KeyValueStub.logger.trace (messagePrefix + kvMessage.toString () + " key: " + key + " - request id: " + token.getMessageId () + " client id: " + token.getClientId ());
final DriverOperationFinishedHandler delCallback = new DriverOperationFinishedHandler (token, session, driver.getClass (), transmitterClass);
final IResult<Boolean> resultDelete = driver.invokeDeleteOperation (token.getClientId (), key, delCallback);
delCallback.setDetails (KeyValueOperations.DELETE, resultDelete);
break;
case LIST_REQUEST :
final KeyValuePayloads.ListRequest listRequest = (ListRequest) message.payload;
token = listRequest.getToken ();
KeyValueStub.logger.trace (messagePrefix + kvMessage.toString () + " - request id: " + token.getMessageId () + " client id: " + token.getClientId ());
final DriverOperationFinishedHandler listCallback = new DriverOperationFinishedHandler (token, session, driver.getClass (), transmitterClass);
final IResult<List<String>> resultList = driver.invokeListOperation (token.getClientId (), listCallback);
listCallback.setDetails (KeyValueOperations.LIST, resultList);
break;
case ERROR :
token = ((IdlCommon.Error) message.payload).getToken ();
unknownMessage = true;
break;
case OK :
token = ((IdlCommon.Ok) message.payload).getToken ();
unknownMessage = true;
break;
case GET_REPLY :
token = ((KeyValuePayloads.GetReply) message.payload).getToken ();
unknownMessage = true;
break;
case LIST_REPLY :
token = ((KeyValuePayloads.ListReply) message.payload).getToken ();
unknownMessage = true;
break;
default:
break;
}
if (unknownMessage) {
this.handleUnknownMessage (session, driver, kvMessage.toString (), token, transmitterClass);
}
}