GEODE-2995: Implementing review feedback Numerous tweaks and fixes Created protobuf package under org.apache.geode.protocol and moved several classes Added javadoc comments
Signed-off-by: Galen O'Sullivan <gosulli...@pivotal.io> Project: http://git-wip-us.apache.org/repos/asf/geode/repo Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/31f4de06 Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/31f4de06 Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/31f4de06 Branch: refs/heads/feature/GEODE-3109 Commit: 31f4de065bd32b5801017427a14cec142c5897fc Parents: c71c28d Author: Brian Rowe <br...@pivotal.io> Authored: Tue Jun 20 15:49:11 2017 -0700 Committer: Hitesh Khamesra <hkhame...@pivotal.io> Committed: Mon Jun 26 09:26:22 2017 -0700 ---------------------------------------------------------------------- geode-protobuf/build.gradle | 2 - .../org/apache/geode/ProtobufUtilities.java | 34 -------- .../org/apache/geode/protocol/OpsProcessor.java | 53 ------------ .../InvalidProtocolMessageException.java | 3 + .../handler/ProtobufStreamProcessor.java | 61 -------------- .../geode/protocol/handler/ProtocolHandler.java | 27 ------ .../protobuf/ProtobufProtocolHandler.java | 41 --------- .../protocol/operations/OperationHandler.java | 10 +++ .../ProtobufRequestOperationParser.java | 33 -------- .../protobuf/GetRequestOperationHandler.java | 59 ------------- .../registry/OperationsHandlerRegistry.java | 3 + ...rationHandlerAlreadyRegisteredException.java | 3 + .../OperationHandlerNotRegisteredException.java | 3 + .../protobuf/EncodingTypeTranslator.java | 86 +++++++++++++++++++ .../protocol/protobuf/ProtobufOpsProcessor.java | 66 +++++++++++++++ .../protobuf/ProtobufSerializationService.java | 51 ++++++++++++ .../protobuf/ProtobufStreamProcessor.java | 60 +++++++++++++ .../protocol/protobuf/ProtobufUtilities.java | 34 ++++++++ .../operations/GetRequestOperationHandler.java | 58 +++++++++++++ .../serializer/ProtobufProtocolSerializer.java | 41 +++++++++ .../protocol/serializer/ProtocolSerializer.java | 31 +++++++ .../ProtobufSerializationService.java | 50 ----------- .../UnsupportedEncodingTypeException.java | 3 + .../translation/EncodingTypeTranslator.java | 83 ------------------ .../UnsupportedEncodingTypeException.java | 21 ----- .../CodecAlreadyRegisteredForTypeException.java | 3 + .../CodecNotRegisteredForTypeException.java | 3 + ...pache.geode.protocol.handler.ProtocolHandler | 1 - ...e.geode.protocol.operations.OperationHandler | 2 +- ...geode.protocol.serializer.ProtocolSerializer | 1 + .../geode/client/protocol/IntegrationTest.java | 32 +++---- .../geode/client/protocol/OpsProcessorTest.java | 64 -------------- .../ProtobufProtocolHandlerJUnitTest.java | 88 -------------------- .../GetRequestOperationHandlerTest.java | 4 +- .../protobuf/ProtobufOpsProcessorTest.java | 62 ++++++++++++++ .../ProtobufProtocolSerializerJUnitTest.java | 81 ++++++++++++++++++ .../ProtobufSerializationServiceImplTest.java | 5 +- ...eToSerializationTypeTranslatorJUnitTest.java | 1 + .../registry/CodecRegistryJUnitTest.java | 16 ++-- 39 files changed, 632 insertions(+), 647 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/build.gradle ---------------------------------------------------------------------- diff --git a/geode-protobuf/build.gradle b/geode-protobuf/build.gradle index 2102f6a..f069267 100644 --- a/geode-protobuf/build.gradle +++ b/geode-protobuf/build.gradle @@ -44,13 +44,11 @@ protobuf { // The artifact spec for the Protobuf Compiler artifact = 'com.google.protobuf:protoc:3.0.0' } - // this allows our spotless rule to skip this directory (hopefully rat too) generatedFilesBaseDir = "$buildDir/generated-src/proto" } sourceSets { main { - java { srcDir 'build/generated-src/proto/main/java' } http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/ProtobufUtilities.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/ProtobufUtilities.java b/geode-protobuf/src/main/java/org/apache/geode/ProtobufUtilities.java deleted file mode 100644 index c25fc14..0000000 --- a/geode-protobuf/src/main/java/org/apache/geode/ProtobufUtilities.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. You may obtain a - * copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ -package org.apache.geode; - -import com.google.protobuf.ByteString; - -import org.apache.geode.protocol.protobuf.BasicTypes; -import org.apache.geode.protocol.protobuf.ClientProtocol; - -public abstract class ProtobufUtilities { - public static BasicTypes.EncodedValue getEncodedValue(BasicTypes.EncodingType resultEncodingType, - byte[] resultEncodedValue) { - return BasicTypes.EncodedValue.newBuilder().setEncodingType(resultEncodingType) - .setValue(ByteString.copyFrom(resultEncodedValue)).build(); - } - - public static ClientProtocol.Message wrapResponseWithDefaultHeader( - ClientProtocol.Response response) { - return ClientProtocol.Message.newBuilder() - .setMessageHeader(ClientProtocol.MessageHeader.newBuilder()).setResponse(response).build(); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/protocol/OpsProcessor.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/OpsProcessor.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/OpsProcessor.java deleted file mode 100644 index 999fdf0..0000000 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/OpsProcessor.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. You may obtain a - * copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ -package org.apache.geode.protocol; - -import org.apache.geode.cache.Cache; -import org.apache.geode.protocol.operations.OperationHandler; -import org.apache.geode.protocol.operations.ProtobufRequestOperationParser; -import org.apache.geode.protocol.operations.registry.OperationsHandlerRegistry; -import org.apache.geode.protocol.operations.registry.exception.OperationHandlerNotRegisteredException; -import org.apache.geode.protocol.protobuf.ClientProtocol; -import org.apache.geode.protocol.protobuf.RegionAPI; -import org.apache.geode.serialization.SerializationService; -import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; -import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; - -public class OpsProcessor { - private final OperationsHandlerRegistry opsHandlerRegistry; - private final SerializationService serializationService; - - public OpsProcessor(OperationsHandlerRegistry opsHandlerRegistry, - SerializationService serializationService) { - this.opsHandlerRegistry = opsHandlerRegistry; - this.serializationService = serializationService; - } - - public ClientProtocol.Response process(ClientProtocol.Request request, Cache cache) - throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { - OperationHandler opsHandler = null; - try { - opsHandler = opsHandlerRegistry - .getOperationHandlerForOperationId(request.getRequestAPICase().getNumber()); - } catch (OperationHandlerNotRegisteredException e) { - e.printStackTrace(); - } - - Object responseMessage = opsHandler.process(serializationService, - ProtobufRequestOperationParser.getRequestForOperationTypeID(request), cache); - return ClientProtocol.Response.newBuilder() - .setGetResponse((RegionAPI.GetResponse) responseMessage).build(); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/protocol/exception/InvalidProtocolMessageException.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/exception/InvalidProtocolMessageException.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/exception/InvalidProtocolMessageException.java index 8f73f57..dae03a1 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/exception/InvalidProtocolMessageException.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/exception/InvalidProtocolMessageException.java @@ -14,6 +14,9 @@ */ package org.apache.geode.protocol.exception; +/** + * Indicates that a message didn't properly follow it's protocol specification. + */ public class InvalidProtocolMessageException extends Exception { public InvalidProtocolMessageException(String message) { super(message); http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/protocol/handler/ProtobufStreamProcessor.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/handler/ProtobufStreamProcessor.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/handler/ProtobufStreamProcessor.java deleted file mode 100644 index e459f76..0000000 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/handler/ProtobufStreamProcessor.java +++ /dev/null @@ -1,61 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. You may obtain a - * copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ -package org.apache.geode.protocol.handler; - -import org.apache.geode.ProtobufUtilities; -import org.apache.geode.cache.Cache; -import org.apache.geode.protocol.OpsProcessor; -import org.apache.geode.protocol.exception.InvalidProtocolMessageException; -import org.apache.geode.protocol.handler.protobuf.ProtobufProtocolHandler; -import org.apache.geode.protocol.operations.registry.OperationsHandlerRegistry; -import org.apache.geode.protocol.operations.registry.exception.OperationHandlerAlreadyRegisteredException; -import org.apache.geode.protocol.operations.registry.exception.OperationHandlerNotRegisteredException; -import org.apache.geode.protocol.protobuf.ClientProtocol; -import org.apache.geode.serialization.ProtobufSerializationService; -import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; -import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredForTypeException; -import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -public class ProtobufStreamProcessor { - ProtobufProtocolHandler protobufProtocolHandler; - OperationsHandlerRegistry registry; - ProtobufSerializationService protobufSerializationService; - OpsProcessor opsProcessor; - - public ProtobufStreamProcessor() - throws OperationHandlerAlreadyRegisteredException, CodecAlreadyRegisteredForTypeException { - protobufProtocolHandler = new ProtobufProtocolHandler(); - registry = new OperationsHandlerRegistry(); - protobufSerializationService = new ProtobufSerializationService(); - opsProcessor = new OpsProcessor(registry, protobufSerializationService); - } - - public void processOneMessage(InputStream inputStream, OutputStream outputStream, Cache cache) - throws InvalidProtocolMessageException, OperationHandlerNotRegisteredException, - UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException, IOException { - ClientProtocol.Message message = protobufProtocolHandler.deserialize(inputStream); - - ClientProtocol.Request request = message.getRequest(); - ClientProtocol.Response response = opsProcessor.process(request, cache); - - ClientProtocol.Message responseMessage = - ProtobufUtilities.wrapResponseWithDefaultHeader(response); - protobufProtocolHandler.serialize(responseMessage, outputStream); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/protocol/handler/ProtocolHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/handler/ProtocolHandler.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/handler/ProtocolHandler.java deleted file mode 100644 index 4947a0a..0000000 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/handler/ProtocolHandler.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. You may obtain a - * copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ -package org.apache.geode.protocol.handler; - -import org.apache.geode.protocol.exception.InvalidProtocolMessageException; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -public interface ProtocolHandler<T> { - T deserialize(InputStream inputStream) throws InvalidProtocolMessageException; - - void serialize(T inputMessage, OutputStream outputStream) throws IOException; -} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/protocol/handler/protobuf/ProtobufProtocolHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/handler/protobuf/ProtobufProtocolHandler.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/handler/protobuf/ProtobufProtocolHandler.java deleted file mode 100644 index f8c2abd..0000000 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/handler/protobuf/ProtobufProtocolHandler.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. You may obtain a - * copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ -package org.apache.geode.protocol.handler.protobuf; - -import org.apache.geode.protocol.exception.InvalidProtocolMessageException; -import org.apache.geode.protocol.handler.ProtocolHandler; -import org.apache.geode.protocol.protobuf.ClientProtocol; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; - -public class ProtobufProtocolHandler implements ProtocolHandler<ClientProtocol.Message> { - @Override - public ClientProtocol.Message deserialize(InputStream inputStream) - throws InvalidProtocolMessageException { - try { - return ClientProtocol.Message.parseDelimitedFrom(inputStream); - } catch (IOException e) { - throw new InvalidProtocolMessageException("Failed to parse Protobuf Message", e); - } - } - - @Override - public void serialize(ClientProtocol.Message inputMessage, OutputStream outputStream) - throws IOException { - inputMessage.writeDelimitedTo(outputStream); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/OperationHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/OperationHandler.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/OperationHandler.java index 020c27b..8eab146 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/OperationHandler.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/OperationHandler.java @@ -15,13 +15,23 @@ package org.apache.geode.protocol.operations; import org.apache.geode.cache.Cache; +import org.apache.geode.protocol.protobuf.ProtobufOpsProcessor; +import org.apache.geode.protocol.operations.registry.OperationsHandlerRegistry; import org.apache.geode.serialization.SerializationService; import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; +/** + * This interface is implemented by a object capable of handling request types 'Req' and returning an a response of type 'Resp' + * + * See {@link ProtobufOpsProcessor} + */ public interface OperationHandler<Req, Resp> { Resp process(SerializationService serializationService, Req request, Cache cache) throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException; + /** + * @return the magic number used for registering the operation type with the {@link OperationsHandlerRegistry}. + */ int getOperationCode(); } http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/ProtobufRequestOperationParser.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/ProtobufRequestOperationParser.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/ProtobufRequestOperationParser.java deleted file mode 100644 index 7fb0eab..0000000 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/ProtobufRequestOperationParser.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. You may obtain a - * copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ -package org.apache.geode.protocol.operations; - -import org.apache.geode.protocol.protobuf.ClientProtocol; - -public class ProtobufRequestOperationParser { - public static Object getRequestForOperationTypeID(ClientProtocol.Request request) { - switch (request.getRequestAPICase()) { - case PUTREQUEST: - return request.getPutRequest(); - case GETREQUEST: - return request.getGetRequest(); - case PUTALLREQUEST: - return request.getPutAllRequest(); - default: - throw new RuntimeException( - "Unknown request type: " + request.getRequestAPICase().getNumber()); - } - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/protobuf/GetRequestOperationHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/protobuf/GetRequestOperationHandler.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/protobuf/GetRequestOperationHandler.java deleted file mode 100644 index dafacc0..0000000 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/protobuf/GetRequestOperationHandler.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. You may obtain a - * copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ -package org.apache.geode.protocol.operations.protobuf; - -import org.apache.geode.ProtobufUtilities; -import org.apache.geode.cache.Cache; -import org.apache.geode.cache.Region; -import org.apache.geode.cache.RegionService; -import org.apache.geode.protocol.operations.OperationHandler; -import org.apache.geode.protocol.protobuf.BasicTypes; -import org.apache.geode.protocol.protobuf.ClientProtocol; -import org.apache.geode.protocol.protobuf.RegionAPI; -import org.apache.geode.serialization.SerializationService; -import org.apache.geode.serialization.protobuf.translation.EncodingTypeTranslator; -import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; -import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; - -public class GetRequestOperationHandler - implements OperationHandler<RegionAPI.GetRequest, RegionAPI.GetResponse> { - - @Override - public RegionAPI.GetResponse process(SerializationService serializationService, - RegionAPI.GetRequest request, Cache cache) - throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { - String regionName = request.getRegionName(); - BasicTypes.EncodedValue key = request.getKey(); - BasicTypes.EncodingType encodingType = key.getEncodingType(); - byte[] value = key.getValue().toByteArray(); - Object decodedValue = serializationService.decode(encodingType, value); - - Region region = cache.getRegion(regionName); - Object resultValue = region.get(decodedValue); - - BasicTypes.EncodingType resultEncodingType = - EncodingTypeTranslator.getEncodingTypeForObject(resultValue); - byte[] resultEncodedValue = serializationService.encode(resultEncodingType, resultValue); - - return RegionAPI.GetResponse.newBuilder() - .setResult(ProtobufUtilities.getEncodedValue(resultEncodingType, resultEncodedValue)) - .build(); - } - - @Override - public int getOperationCode() { - return ClientProtocol.Request.RequestAPICase.GETREQUEST.getNumber(); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistry.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistry.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistry.java index 80e0459..8e3a331 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistry.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistry.java @@ -21,6 +21,9 @@ import org.apache.geode.protocol.operations.registry.exception.OperationHandlerN import java.util.HashMap; import java.util.ServiceLoader; +/** + * This class tracks which operation handlers are expected to handle which types of operations. + */ public class OperationsHandlerRegistry { private HashMap<Integer, OperationHandler> registeredOperations = new HashMap<>(); http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/exception/OperationHandlerAlreadyRegisteredException.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/exception/OperationHandlerAlreadyRegisteredException.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/exception/OperationHandlerAlreadyRegisteredException.java index 1f42c10..519698d 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/exception/OperationHandlerAlreadyRegisteredException.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/exception/OperationHandlerAlreadyRegisteredException.java @@ -14,6 +14,9 @@ */ package org.apache.geode.protocol.operations.registry.exception; +/** + * Indicates that an operation handler is attempting to register for an already handled operation type. + */ public class OperationHandlerAlreadyRegisteredException extends Exception { public OperationHandlerAlreadyRegisteredException(String message) { super(message); http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/exception/OperationHandlerNotRegisteredException.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/exception/OperationHandlerNotRegisteredException.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/exception/OperationHandlerNotRegisteredException.java index f83e1b3..6772e56 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/exception/OperationHandlerNotRegisteredException.java +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/exception/OperationHandlerNotRegisteredException.java @@ -14,6 +14,9 @@ */ package org.apache.geode.protocol.operations.registry.exception; +/** + * Indicates that we're trying to handle an operation for which no operation handler has registered. + */ public class OperationHandlerNotRegisteredException extends Exception { public OperationHandlerNotRegisteredException(String message) { super(message); http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/EncodingTypeTranslator.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/EncodingTypeTranslator.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/EncodingTypeTranslator.java new file mode 100644 index 0000000..650ac81 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/EncodingTypeTranslator.java @@ -0,0 +1,86 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode.protocol.protobuf; + +import org.apache.geode.pdx.JSONFormatter; +import org.apache.geode.pdx.PdxInstance; +import org.apache.geode.protocol.protobuf.BasicTypes; +import org.apache.geode.serialization.SerializationType; +import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; + +/** + * This class maps protobuf specific encoding types and the corresponding serialization types. + */ +public abstract class EncodingTypeTranslator { + public static SerializationType getSerializationTypeForEncodingType( + BasicTypes.EncodingType encodingType) throws UnsupportedEncodingTypeException { + switch (encodingType) { + case INT: + return SerializationType.INT; + case BYTE: + return SerializationType.BYTE; + case JSON: + return SerializationType.JSON; + case LONG: + return SerializationType.LONG; + case FLOAT: + return SerializationType.FLOAT; + case SHORT: + return SerializationType.SHORT; + case BINARY: + return SerializationType.BINARY; + case DOUBLE: + return SerializationType.DOUBLE; + case STRING: + return SerializationType.STRING; + case BOOLEAN: + return SerializationType.BOOLEAN; + default: + throw new UnsupportedEncodingTypeException( + "No serialization type found for protobuf encoding type: " + encodingType); + } + } + + public static BasicTypes.EncodingType getEncodingTypeForObject(Object resultValue) + throws UnsupportedEncodingTypeException { + if (resultValue instanceof Integer) { + return BasicTypes.EncodingType.INT; + } else if (resultValue instanceof Byte) { + return BasicTypes.EncodingType.BYTE; + } else if (resultValue instanceof PdxInstance) { + String pdxClassName = ((PdxInstance) resultValue).getClassName(); + if (pdxClassName.equals(JSONFormatter.JSON_CLASSNAME)) { + return BasicTypes.EncodingType.JSON; + } + } else if (resultValue instanceof Long) { + return BasicTypes.EncodingType.LONG; + } else if (resultValue instanceof Float) { + return BasicTypes.EncodingType.FLOAT; + } else if (resultValue instanceof Short) { + return BasicTypes.EncodingType.SHORT; + } else if (resultValue instanceof byte[]) { + return BasicTypes.EncodingType.BINARY; + } else if (resultValue instanceof Double) { + return BasicTypes.EncodingType.DOUBLE; + } else if (resultValue instanceof String) { + return BasicTypes.EncodingType.STRING; + } else if (resultValue instanceof Boolean) { + return BasicTypes.EncodingType.BOOLEAN; + } + + throw new UnsupportedEncodingTypeException( + "We cannot translate: " + resultValue.getClass() + " into a specific Protobuf Encoding"); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessor.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessor.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessor.java new file mode 100644 index 0000000..c572d80 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessor.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode.protocol.protobuf; + +import org.apache.geode.cache.Cache; +import org.apache.geode.protocol.exception.InvalidProtocolMessageException; +import org.apache.geode.protocol.operations.OperationHandler; +import org.apache.geode.protocol.operations.registry.OperationsHandlerRegistry; +import org.apache.geode.protocol.operations.registry.exception.OperationHandlerNotRegisteredException; +import org.apache.geode.serialization.SerializationService; +import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; +import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; + +/** + * This handles protobuf requests by determining the operation type of the request and dispatching it to the appropriate handler. + */ +public class ProtobufOpsProcessor { + private final OperationsHandlerRegistry opsHandlerRegistry; + private final SerializationService serializationService; + + public ProtobufOpsProcessor(OperationsHandlerRegistry opsHandlerRegistry, + SerializationService serializationService) { + this.opsHandlerRegistry = opsHandlerRegistry; + this.serializationService = serializationService; + } + + public ClientProtocol.Response process(ClientProtocol.Request request, Cache cache) + throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException, + OperationHandlerNotRegisteredException, InvalidProtocolMessageException { + OperationHandler opsHandler = opsHandlerRegistry + .getOperationHandlerForOperationId(request.getRequestAPICase().getNumber()); + + Object responseMessage = opsHandler.process(serializationService, + getRequestForOperationTypeID(request), cache); + return ClientProtocol.Response.newBuilder() + .setGetResponse((RegionAPI.GetResponse) responseMessage).build(); + } + + // package visibility for testing + static Object getRequestForOperationTypeID(ClientProtocol.Request request) + throws InvalidProtocolMessageException { + switch (request.getRequestAPICase()) { + case PUTREQUEST: + return request.getPutRequest(); + case GETREQUEST: + return request.getGetRequest(); + case PUTALLREQUEST: + return request.getPutAllRequest(); + default: + throw new InvalidProtocolMessageException( + "Unknown request type: " + request.getRequestAPICase().getNumber()); + } + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufSerializationService.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufSerializationService.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufSerializationService.java new file mode 100644 index 0000000..fc76d5e --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufSerializationService.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode.protocol.protobuf; + +import org.apache.geode.serialization.SerializationService; +import org.apache.geode.serialization.SerializationType; +import org.apache.geode.serialization.TypeCodec; +import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; +import org.apache.geode.serialization.registry.SerializationCodecRegistry; +import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredForTypeException; +import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; + +public class ProtobufSerializationService implements SerializationService<BasicTypes.EncodingType> { + private SerializationCodecRegistry serializationCodecRegistry = new SerializationCodecRegistry(); + + public ProtobufSerializationService() throws CodecAlreadyRegisteredForTypeException {} + + @Override + public byte[] encode(BasicTypes.EncodingType encodingTypeValue, Object value) + throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { + TypeCodec codecForType = getTypeCodecForProtobufType(encodingTypeValue); + return codecForType.encode(value); + } + + @Override + public Object decode(BasicTypes.EncodingType encodingTypeValue, byte[] value) + throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { + TypeCodec codecForType = getTypeCodecForProtobufType(encodingTypeValue); + return codecForType.decode(value); + } + + private TypeCodec getTypeCodecForProtobufType(BasicTypes.EncodingType encodingTypeValue) + throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { + SerializationType serializationTypeForEncodingType = + EncodingTypeTranslator.getSerializationTypeForEncodingType(encodingTypeValue); + + return serializationCodecRegistry.getCodecForType(serializationTypeForEncodingType); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufStreamProcessor.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufStreamProcessor.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufStreamProcessor.java new file mode 100644 index 0000000..41a3c40 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufStreamProcessor.java @@ -0,0 +1,60 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode.protocol.protobuf; + +import org.apache.geode.cache.Cache; +import org.apache.geode.protocol.exception.InvalidProtocolMessageException; +import org.apache.geode.protocol.protobuf.serializer.ProtobufProtocolSerializer; +import org.apache.geode.protocol.operations.registry.OperationsHandlerRegistry; +import org.apache.geode.protocol.operations.registry.exception.OperationHandlerAlreadyRegisteredException; +import org.apache.geode.protocol.operations.registry.exception.OperationHandlerNotRegisteredException; +import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; +import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredForTypeException; +import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * This object handles an incoming stream containing protobuf messages. It parses the protobuf messages, hands the requests to an appropriate handler, wraps the response in a protobuf message, and then pushes it to the output stream. + */ +public class ProtobufStreamProcessor { + ProtobufProtocolSerializer protobufProtocolSerializer; + OperationsHandlerRegistry registry; + ProtobufSerializationService protobufSerializationService; + ProtobufOpsProcessor protobufOpsProcessor; + + public ProtobufStreamProcessor() + throws OperationHandlerAlreadyRegisteredException, CodecAlreadyRegisteredForTypeException { + protobufProtocolSerializer = new ProtobufProtocolSerializer(); + registry = new OperationsHandlerRegistry(); + protobufSerializationService = new ProtobufSerializationService(); + protobufOpsProcessor = new ProtobufOpsProcessor(registry, protobufSerializationService); + } + + public void processOneMessage(InputStream inputStream, OutputStream outputStream, Cache cache) + throws InvalidProtocolMessageException, OperationHandlerNotRegisteredException, + UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException, IOException { + ClientProtocol.Message message = protobufProtocolSerializer.deserialize(inputStream); + + ClientProtocol.Request request = message.getRequest(); + ClientProtocol.Response response = protobufOpsProcessor.process(request, cache); + + ClientProtocol.Message responseMessage = + ProtobufUtilities.wrapResponseWithDefaultHeader(response); + protobufProtocolSerializer.serialize(responseMessage, outputStream); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufUtilities.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufUtilities.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufUtilities.java new file mode 100644 index 0000000..d763662 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/ProtobufUtilities.java @@ -0,0 +1,34 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode.protocol.protobuf; + +import com.google.protobuf.ByteString; + +import org.apache.geode.protocol.protobuf.BasicTypes; +import org.apache.geode.protocol.protobuf.ClientProtocol; + +public abstract class ProtobufUtilities { + public static BasicTypes.EncodedValue getEncodedValue(BasicTypes.EncodingType resultEncodingType, + byte[] resultEncodedValue) { + return BasicTypes.EncodedValue.newBuilder().setEncodingType(resultEncodingType) + .setValue(ByteString.copyFrom(resultEncodedValue)).build(); + } + + public static ClientProtocol.Message wrapResponseWithDefaultHeader( + ClientProtocol.Response response) { + return ClientProtocol.Message.newBuilder() + .setMessageHeader(ClientProtocol.MessageHeader.newBuilder()).setResponse(response).build(); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRequestOperationHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRequestOperationHandler.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRequestOperationHandler.java new file mode 100644 index 0000000..37d1d32 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/operations/GetRequestOperationHandler.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode.protocol.protobuf.operations; + +import org.apache.geode.protocol.protobuf.ProtobufUtilities; +import org.apache.geode.cache.Cache; +import org.apache.geode.cache.Region; +import org.apache.geode.protocol.operations.OperationHandler; +import org.apache.geode.protocol.protobuf.BasicTypes; +import org.apache.geode.protocol.protobuf.ClientProtocol; +import org.apache.geode.protocol.protobuf.RegionAPI; +import org.apache.geode.serialization.SerializationService; +import org.apache.geode.protocol.protobuf.EncodingTypeTranslator; +import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; +import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; + +public class GetRequestOperationHandler + implements OperationHandler<RegionAPI.GetRequest, RegionAPI.GetResponse> { + + @Override + public RegionAPI.GetResponse process(SerializationService serializationService, + RegionAPI.GetRequest request, Cache cache) + throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { + String regionName = request.getRegionName(); + BasicTypes.EncodedValue key = request.getKey(); + BasicTypes.EncodingType encodingType = key.getEncodingType(); + byte[] value = key.getValue().toByteArray(); + Object decodedValue = serializationService.decode(encodingType, value); + + Region region = cache.getRegion(regionName); + Object resultValue = region.get(decodedValue); + + BasicTypes.EncodingType resultEncodingType = + EncodingTypeTranslator.getEncodingTypeForObject(resultValue); + byte[] resultEncodedValue = serializationService.encode(resultEncodingType, resultValue); + + return RegionAPI.GetResponse.newBuilder() + .setResult(ProtobufUtilities.getEncodedValue(resultEncodingType, resultEncodedValue)) + .build(); + } + + @Override + public int getOperationCode() { + return ClientProtocol.Request.RequestAPICase.GETREQUEST.getNumber(); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/serializer/ProtobufProtocolSerializer.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/serializer/ProtobufProtocolSerializer.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/serializer/ProtobufProtocolSerializer.java new file mode 100644 index 0000000..683e42f --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/protobuf/serializer/ProtobufProtocolSerializer.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode.protocol.protobuf.serializer; + +import org.apache.geode.protocol.exception.InvalidProtocolMessageException; +import org.apache.geode.protocol.serializer.ProtocolSerializer; +import org.apache.geode.protocol.protobuf.ClientProtocol; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class ProtobufProtocolSerializer implements ProtocolSerializer<ClientProtocol.Message> { + @Override + public ClientProtocol.Message deserialize(InputStream inputStream) + throws InvalidProtocolMessageException { + try { + return ClientProtocol.Message.parseDelimitedFrom(inputStream); + } catch (IOException e) { + throw new InvalidProtocolMessageException("Failed to parse Protobuf Message", e); + } + } + + @Override + public void serialize(ClientProtocol.Message inputMessage, OutputStream outputStream) + throws IOException { + inputMessage.writeDelimitedTo(outputStream); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/protocol/serializer/ProtocolSerializer.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/protocol/serializer/ProtocolSerializer.java b/geode-protobuf/src/main/java/org/apache/geode/protocol/serializer/ProtocolSerializer.java new file mode 100644 index 0000000..7191d87 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/serializer/ProtocolSerializer.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode.protocol.serializer; + +import org.apache.geode.protocol.exception.InvalidProtocolMessageException; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * This interface is used to translate between binary data and protocol specific messages. + * @param <T> The message type of the protocol. + */ +public interface ProtocolSerializer<T> { + T deserialize(InputStream inputStream) throws InvalidProtocolMessageException; + + void serialize(T inputMessage, OutputStream outputStream) throws IOException; +} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/serialization/ProtobufSerializationService.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/ProtobufSerializationService.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/ProtobufSerializationService.java deleted file mode 100644 index 9d78245..0000000 --- a/geode-protobuf/src/main/java/org/apache/geode/serialization/ProtobufSerializationService.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. You may obtain a - * copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ -package org.apache.geode.serialization; - -import org.apache.geode.protocol.protobuf.BasicTypes; -import org.apache.geode.serialization.protobuf.translation.EncodingTypeTranslator; -import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; -import org.apache.geode.serialization.registry.SerializationCodecRegistry; -import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredForTypeException; -import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; - -public class ProtobufSerializationService implements SerializationService<BasicTypes.EncodingType> { - private SerializationCodecRegistry serializationCodecRegistry = new SerializationCodecRegistry(); - - public ProtobufSerializationService() throws CodecAlreadyRegisteredForTypeException {} - - @Override - public byte[] encode(BasicTypes.EncodingType encodingTypeValue, Object value) - throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { - TypeCodec codecForType = getTypeCodecForProtobufType(encodingTypeValue); - return codecForType.encode(value); - } - - @Override - public Object decode(BasicTypes.EncodingType encodingTypeValue, byte[] value) - throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { - TypeCodec codecForType = getTypeCodecForProtobufType(encodingTypeValue); - return codecForType.decode(value); - } - - private TypeCodec getTypeCodecForProtobufType(BasicTypes.EncodingType encodingTypeValue) - throws UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException { - SerializationType serializationTypeForEncodingType = - EncodingTypeTranslator.getSerializationTypeForEncodingType(encodingTypeValue); - - return serializationCodecRegistry.getCodecForType(serializationTypeForEncodingType); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/serialization/exception/UnsupportedEncodingTypeException.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/exception/UnsupportedEncodingTypeException.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/exception/UnsupportedEncodingTypeException.java index 9d75f56..6d2a032 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/serialization/exception/UnsupportedEncodingTypeException.java +++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/exception/UnsupportedEncodingTypeException.java @@ -14,6 +14,9 @@ */ package org.apache.geode.serialization.exception; +/** + * This indicates an encoding type that we don't know how to handle. + */ public class UnsupportedEncodingTypeException extends Exception { public UnsupportedEncodingTypeException(String message) { super(message); http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/serialization/protobuf/translation/EncodingTypeTranslator.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/protobuf/translation/EncodingTypeTranslator.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/protobuf/translation/EncodingTypeTranslator.java deleted file mode 100644 index 667996d..0000000 --- a/geode-protobuf/src/main/java/org/apache/geode/serialization/protobuf/translation/EncodingTypeTranslator.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. You may obtain a - * copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ -package org.apache.geode.serialization.protobuf.translation; - -import org.apache.geode.pdx.JSONFormatter; -import org.apache.geode.pdx.PdxInstance; -import org.apache.geode.protocol.protobuf.BasicTypes; -import org.apache.geode.serialization.SerializationType; -import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; - -public abstract class EncodingTypeTranslator { - public static SerializationType getSerializationTypeForEncodingType( - BasicTypes.EncodingType encodingType) throws UnsupportedEncodingTypeException { - switch (encodingType) { - case INT: - return SerializationType.INT; - case BYTE: - return SerializationType.BYTE; - case JSON: - return SerializationType.JSON; - case LONG: - return SerializationType.LONG; - case FLOAT: - return SerializationType.FLOAT; - case SHORT: - return SerializationType.SHORT; - case BINARY: - return SerializationType.BINARY; - case DOUBLE: - return SerializationType.DOUBLE; - case STRING: - return SerializationType.STRING; - case BOOLEAN: - return SerializationType.BOOLEAN; - default: - throw new UnsupportedEncodingTypeException( - "No serialization type found for protobuf encoding type: " + encodingType); - } - } - - public static BasicTypes.EncodingType getEncodingTypeForObject(Object resultValue) - throws UnsupportedEncodingTypeException { - if (resultValue instanceof Integer) { - return BasicTypes.EncodingType.INT; - } else if (resultValue instanceof Byte) { - return BasicTypes.EncodingType.BYTE; - } else if (resultValue instanceof PdxInstance) { - String pdxClassName = ((PdxInstance) resultValue).getClassName(); - if (pdxClassName.equals(JSONFormatter.JSON_CLASSNAME)) { - return BasicTypes.EncodingType.JSON; - } - } else if (resultValue instanceof Long) { - return BasicTypes.EncodingType.LONG; - } else if (resultValue instanceof Float) { - return BasicTypes.EncodingType.FLOAT; - } else if (resultValue instanceof Short) { - return BasicTypes.EncodingType.SHORT; - } else if (resultValue instanceof byte[]) { - return BasicTypes.EncodingType.BINARY; - } else if (resultValue instanceof Double) { - return BasicTypes.EncodingType.DOUBLE; - } else if (resultValue instanceof String) { - return BasicTypes.EncodingType.STRING; - } else if (resultValue instanceof Boolean) { - return BasicTypes.EncodingType.BOOLEAN; - } - - throw new UnsupportedEncodingTypeException( - "We cannot translate: " + resultValue.getClass() + " into a specific Protobuf Encoding"); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/serialization/protobuf/translation/exception/UnsupportedEncodingTypeException.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/protobuf/translation/exception/UnsupportedEncodingTypeException.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/protobuf/translation/exception/UnsupportedEncodingTypeException.java deleted file mode 100644 index ad01eac..0000000 --- a/geode-protobuf/src/main/java/org/apache/geode/serialization/protobuf/translation/exception/UnsupportedEncodingTypeException.java +++ /dev/null @@ -1,21 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. You may obtain a - * copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ -package org.apache.geode.serialization.protobuf.translation.exception; - -public class UnsupportedEncodingTypeException extends Exception { - public UnsupportedEncodingTypeException(String message) { - super(message); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/exception/CodecAlreadyRegisteredForTypeException.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/exception/CodecAlreadyRegisteredForTypeException.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/exception/CodecAlreadyRegisteredForTypeException.java index 678d374..7e73ba9 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/exception/CodecAlreadyRegisteredForTypeException.java +++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/exception/CodecAlreadyRegisteredForTypeException.java @@ -14,6 +14,9 @@ */ package org.apache.geode.serialization.registry.exception; +/** + * This indicates that we're attempting to register a codec for a type which we already have a handler for. + */ public class CodecAlreadyRegisteredForTypeException extends Exception { public CodecAlreadyRegisteredForTypeException(String message) { super(message); http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/exception/CodecNotRegisteredForTypeException.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/exception/CodecNotRegisteredForTypeException.java b/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/exception/CodecNotRegisteredForTypeException.java index a1a6408..58cb691 100644 --- a/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/exception/CodecNotRegisteredForTypeException.java +++ b/geode-protobuf/src/main/java/org/apache/geode/serialization/registry/exception/CodecNotRegisteredForTypeException.java @@ -14,6 +14,9 @@ */ package org.apache.geode.serialization.registry.exception; +/** + * This indicates we're attempting to handle a type for which we don't have a registered codec. + */ public class CodecNotRegisteredForTypeException extends Exception { public CodecNotRegisteredForTypeException(String message) { super(message); http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/resources/META-INF/services/org.apache.geode.protocol.handler.ProtocolHandler ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/resources/META-INF/services/org.apache.geode.protocol.handler.ProtocolHandler b/geode-protobuf/src/main/resources/META-INF/services/org.apache.geode.protocol.handler.ProtocolHandler deleted file mode 100644 index 9af7c5d..0000000 --- a/geode-protobuf/src/main/resources/META-INF/services/org.apache.geode.protocol.handler.ProtocolHandler +++ /dev/null @@ -1 +0,0 @@ -org.apache.geode.protocol.handler.protobuf.ProtobufProtocolHandler \ No newline at end of file http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/resources/META-INF/services/org.apache.geode.protocol.operations.OperationHandler ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/resources/META-INF/services/org.apache.geode.protocol.operations.OperationHandler b/geode-protobuf/src/main/resources/META-INF/services/org.apache.geode.protocol.operations.OperationHandler index fa7c3c6..b6ec564 100644 --- a/geode-protobuf/src/main/resources/META-INF/services/org.apache.geode.protocol.operations.OperationHandler +++ b/geode-protobuf/src/main/resources/META-INF/services/org.apache.geode.protocol.operations.OperationHandler @@ -1 +1 @@ -org.apache.geode.protocol.operations.protobuf.GetRequestOperationHandler \ No newline at end of file +org.apache.geode.protocol.protobuf.operations.GetRequestOperationHandler \ No newline at end of file http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/main/resources/META-INF/services/org.apache.geode.protocol.serializer.ProtocolSerializer ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/resources/META-INF/services/org.apache.geode.protocol.serializer.ProtocolSerializer b/geode-protobuf/src/main/resources/META-INF/services/org.apache.geode.protocol.serializer.ProtocolSerializer new file mode 100644 index 0000000..2a9af4e --- /dev/null +++ b/geode-protobuf/src/main/resources/META-INF/services/org.apache.geode.protocol.serializer.ProtocolSerializer @@ -0,0 +1 @@ +org.apache.geode.protocol.protobuf.serializer.ProtobufProtocolSerializer \ No newline at end of file http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/test/java/org/apache/geode/client/protocol/IntegrationTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/IntegrationTest.java b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/IntegrationTest.java index e86e908..e32c711 100644 --- a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/IntegrationTest.java +++ b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/IntegrationTest.java @@ -18,15 +18,12 @@ import com.google.protobuf.ByteString; import org.apache.geode.cache.Cache; import org.apache.geode.cache.Region; -import org.apache.geode.protocol.exception.InvalidProtocolMessageException; -import org.apache.geode.protocol.handler.ProtobufStreamProcessor; -import org.apache.geode.protocol.operations.registry.exception.OperationHandlerAlreadyRegisteredException; -import org.apache.geode.protocol.operations.registry.exception.OperationHandlerNotRegisteredException; +import org.apache.geode.protocol.protobuf.ProtobufStreamProcessor; import org.apache.geode.protocol.protobuf.BasicTypes; import org.apache.geode.protocol.protobuf.ClientProtocol; import org.apache.geode.protocol.protobuf.RegionAPI; import org.apache.geode.serialization.codec.StringCodec; -import org.apache.geode.serialization.protobuf.translation.EncodingTypeTranslator; +import org.apache.geode.protocol.protobuf.EncodingTypeTranslator; import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; import org.apache.geode.serialization.registry.SerializationCodecRegistry; import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredForTypeException; @@ -37,7 +34,10 @@ import org.junit.Before; import org.junit.Test; import org.junit.experimental.categories.Category; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import java.io.ByteArrayInputStream; @@ -52,6 +52,7 @@ public class IntegrationTest { public static final String TEST_REGION = "test region"; private StringCodec stringCodec; private Cache cacheStub; + private Region regionStub; @Before public void setup() throws CodecAlreadyRegisteredForTypeException, @@ -59,15 +60,13 @@ public class IntegrationTest { SerializationCodecRegistry serializationCodecRegistry = new SerializationCodecRegistry(); stringCodec = (StringCodec) serializationCodecRegistry.getCodecForType( EncodingTypeTranslator.getSerializationTypeForEncodingType(BasicTypes.EncodingType.STRING)); - cacheStub = getCacheStub(); + regionStub = getRegionStub(); + cacheStub = getCacheStub(regionStub); } @Test - public void testFullRequestToCache() - throws OperationHandlerAlreadyRegisteredException, CodecAlreadyRegisteredForTypeException, - UnsupportedEncodingTypeException, CodecNotRegisteredForTypeException, - OperationHandlerNotRegisteredException, IOException, InvalidProtocolMessageException { - + public void testGetRequestProcessed() + throws Exception { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ProtobufStreamProcessor streamProcessor = new ProtobufStreamProcessor(); @@ -80,14 +79,19 @@ public class IntegrationTest { Assert.assertEquals(BasicTypes.EncodingType.STRING, getResponse.getResult().getEncodingType()); String actualValue = stringCodec.decode(getResponse.getResult().getValue().toByteArray()); Assert.assertEquals(TEST_VALUE, actualValue); + verify(regionStub, times(1)).get(TEST_KEY); + verify(regionStub, times(1)).get(anyString()); } - private Cache getCacheStub() { - Region regionStub = mock(Region.class); + private Region getRegionStub() { + regionStub = mock(Region.class); when(regionStub.get(TEST_KEY)).thenReturn(TEST_VALUE); + return regionStub; + } + private Cache getCacheStub(Region region) { Cache cacheStub = mock(Cache.class); - when(cacheStub.getRegion(TEST_REGION)).thenReturn(regionStub); + when(cacheStub.getRegion(TEST_REGION)).thenReturn(region); return cacheStub; } http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsProcessorTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsProcessorTest.java b/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsProcessorTest.java deleted file mode 100644 index 1ec7a02..0000000 --- a/geode-protobuf/src/test/java/org/apache/geode/client/protocol/OpsProcessorTest.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. You may obtain a - * copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ -package org.apache.geode.client.protocol; - - -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import org.apache.geode.cache.Cache; -import org.apache.geode.cache.RegionService; -import org.apache.geode.protocol.OpsProcessor; -import org.apache.geode.protocol.operations.OperationHandler; -import org.apache.geode.protocol.operations.ProtobufRequestOperationParser; -import org.apache.geode.protocol.operations.registry.OperationsHandlerRegistry; -import org.apache.geode.protocol.operations.registry.exception.OperationHandlerNotRegisteredException; -import org.apache.geode.protocol.protobuf.ClientProtocol; -import org.apache.geode.protocol.protobuf.RegionAPI; -import org.apache.geode.serialization.SerializationService; -import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; -import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; -import org.apache.geode.test.junit.categories.UnitTest; -import org.junit.Assert; -import org.junit.Test; -import org.junit.experimental.categories.Category; - -@Category(UnitTest.class) -public class OpsProcessorTest { - @Test - public void testOpsProcessor() throws CodecNotRegisteredForTypeException, - OperationHandlerNotRegisteredException, UnsupportedEncodingTypeException { - OperationsHandlerRegistry opsHandlerRegistryStub = mock(OperationsHandlerRegistry.class); - OperationHandler operationHandlerStub = mock(OperationHandler.class); - SerializationService serializationServiceStub = mock(SerializationService.class); - Cache dummyCache = mock(Cache.class); - int operationID = ClientProtocol.Request.RequestAPICase.GETREQUEST.getNumber(); - - ClientProtocol.Request messageRequest = ClientProtocol.Request.newBuilder() - .setGetRequest(RegionAPI.GetRequest.newBuilder()).build(); - - RegionAPI.GetResponse expectedResponse = RegionAPI.GetResponse.newBuilder().build(); - - when(opsHandlerRegistryStub.getOperationHandlerForOperationId(operationID)) - .thenReturn(operationHandlerStub); - when(operationHandlerStub.process(serializationServiceStub, - ProtobufRequestOperationParser.getRequestForOperationTypeID(messageRequest), dummyCache)) - .thenReturn(expectedResponse); - - OpsProcessor processor = new OpsProcessor(opsHandlerRegistryStub, serializationServiceStub); - ClientProtocol.Response response = processor.process(messageRequest, dummyCache); - Assert.assertEquals(expectedResponse, response.getGetResponse()); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/test/java/org/apache/geode/protocol/handler/ProtobufProtocolHandlerJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/handler/ProtobufProtocolHandlerJUnitTest.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/handler/ProtobufProtocolHandlerJUnitTest.java deleted file mode 100644 index 21c3107..0000000 --- a/geode-protobuf/src/test/java/org/apache/geode/protocol/handler/ProtobufProtocolHandlerJUnitTest.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more contributor license - * agreements. See the NOTICE file distributed with this work for additional information regarding - * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the License. You may obtain a - * copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License - * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express - * or implied. See the License for the specific language governing permissions and limitations under - * the License. - */ -package org.apache.geode.protocol.handler; - -import org.apache.geode.client.protocol.MessageUtil; -import org.apache.geode.protocol.exception.InvalidProtocolMessageException; -import org.apache.geode.protocol.handler.ProtocolHandler; -import org.apache.geode.protocol.handler.protobuf.ProtobufProtocolHandler; -import org.apache.geode.protocol.protobuf.ClientProtocol; -import org.apache.geode.test.junit.categories.UnitTest; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.junit.experimental.categories.Category; - -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.ServiceLoader; - -@Category(UnitTest.class) -public class ProtobufProtocolHandlerJUnitTest { - private ProtocolHandler<ClientProtocol.Message> protocolHandler; - - @Before - public void startup() { - ServiceLoader<ProtocolHandler> serviceLoader = ServiceLoader.load(ProtocolHandler.class); - for (ProtocolHandler protocolHandler : serviceLoader) { - if (protocolHandler instanceof ProtobufProtocolHandler) { - this.protocolHandler = protocolHandler; - } - } - } - - @Test - public void testDeserializeByteArrayToMessage() - throws IOException, InvalidProtocolMessageException { - ClientProtocol.Message expectedRequestMessage = MessageUtil.createGetRequestMessage(); - - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - - expectedRequestMessage.writeDelimitedTo(byteArrayOutputStream); - InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); - - ClientProtocol.Message actualMessage = protocolHandler.deserialize(inputStream); - Assert.assertEquals(expectedRequestMessage, actualMessage); - } - - @Test - public void testDeserializeInvalidByteThrowsException() throws IOException { - ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); - byteArrayOutputStream.write("Some incorrect byte array".getBytes()); - InputStream inputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); - - boolean caughtException = false; - try { - protocolHandler.deserialize(inputStream); - } catch (InvalidProtocolMessageException e) { - caughtException = true; - } - Assert.assertTrue(caughtException); - } - - @Test - public void testSerializeMessageToByteArray() throws IOException { - ClientProtocol.Message message = MessageUtil.createGetRequestMessage(); - ByteArrayOutputStream expectedByteArrayOutputStream = new ByteArrayOutputStream(); - message.writeDelimitedTo(expectedByteArrayOutputStream); - byte[] expectedByteArray = expectedByteArrayOutputStream.toByteArray(); - - ByteArrayOutputStream actualByteArrayOutputStream = new ByteArrayOutputStream(); - protocolHandler.serialize(message, actualByteArrayOutputStream); - Assert.assertArrayEquals(expectedByteArray, actualByteArrayOutputStream.toByteArray()); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/test/java/org/apache/geode/protocol/operations/protobuf/GetRequestOperationHandlerTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/operations/protobuf/GetRequestOperationHandlerTest.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/operations/protobuf/GetRequestOperationHandlerTest.java index 359db7d..cb98eb7 100644 --- a/geode-protobuf/src/test/java/org/apache/geode/protocol/operations/protobuf/GetRequestOperationHandlerTest.java +++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/operations/protobuf/GetRequestOperationHandlerTest.java @@ -18,12 +18,12 @@ import com.google.protobuf.ByteString; import org.apache.geode.cache.Cache; import org.apache.geode.cache.Region; -import org.apache.geode.cache.RegionService; import org.apache.geode.protocol.protobuf.BasicTypes; import org.apache.geode.protocol.protobuf.RegionAPI; +import org.apache.geode.protocol.protobuf.operations.GetRequestOperationHandler; import org.apache.geode.serialization.SerializationService; import org.apache.geode.serialization.codec.StringCodec; -import org.apache.geode.serialization.protobuf.translation.EncodingTypeTranslator; +import org.apache.geode.protocol.protobuf.EncodingTypeTranslator; import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; import org.apache.geode.serialization.registry.SerializationCodecRegistry; import org.apache.geode.serialization.registry.exception.CodecAlreadyRegisteredForTypeException; http://git-wip-us.apache.org/repos/asf/geode/blob/31f4de06/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessorTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessorTest.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessorTest.java new file mode 100644 index 0000000..9298026 --- /dev/null +++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/protobuf/ProtobufOpsProcessorTest.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more contributor license + * agreements. See the NOTICE file distributed with this work for additional information regarding + * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the License. You may obtain a + * copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express + * or implied. See the License for the specific language governing permissions and limitations under + * the License. + */ +package org.apache.geode.protocol.protobuf; + + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.apache.geode.cache.Cache; +import org.apache.geode.protocol.exception.InvalidProtocolMessageException; +import org.apache.geode.protocol.operations.OperationHandler; +import org.apache.geode.protocol.operations.registry.OperationsHandlerRegistry; +import org.apache.geode.protocol.operations.registry.exception.OperationHandlerNotRegisteredException; +import org.apache.geode.serialization.SerializationService; +import org.apache.geode.serialization.exception.UnsupportedEncodingTypeException; +import org.apache.geode.serialization.registry.exception.CodecNotRegisteredForTypeException; +import org.apache.geode.test.junit.categories.UnitTest; +import org.junit.Assert; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category(UnitTest.class) +public class ProtobufOpsProcessorTest { + @Test + public void testOpsProcessor() throws CodecNotRegisteredForTypeException, + OperationHandlerNotRegisteredException, UnsupportedEncodingTypeException, + InvalidProtocolMessageException { + OperationsHandlerRegistry opsHandlerRegistryStub = mock(OperationsHandlerRegistry.class); + OperationHandler operationHandlerStub = mock(OperationHandler.class); + SerializationService serializationServiceStub = mock(SerializationService.class); + Cache dummyCache = mock(Cache.class); + int operationID = ClientProtocol.Request.RequestAPICase.GETREQUEST.getNumber(); + + ClientProtocol.Request messageRequest = ClientProtocol.Request.newBuilder() + .setGetRequest(RegionAPI.GetRequest.newBuilder()).build(); + + RegionAPI.GetResponse expectedResponse = RegionAPI.GetResponse.newBuilder().build(); + + when(opsHandlerRegistryStub.getOperationHandlerForOperationId(operationID)) + .thenReturn(operationHandlerStub); + when(operationHandlerStub.process(serializationServiceStub, + ProtobufOpsProcessor.getRequestForOperationTypeID(messageRequest), dummyCache)) + .thenReturn(expectedResponse); + + ProtobufOpsProcessor + processor = new ProtobufOpsProcessor(opsHandlerRegistryStub, serializationServiceStub); + ClientProtocol.Response response = processor.process(messageRequest, dummyCache); + Assert.assertEquals(expectedResponse, response.getGetResponse()); + } +}