Repository: geode Updated Branches: refs/heads/feature/GEODE-2995 336b0e0a5 -> e3af5a42c
GEODE-2995: Adding OperationHandlerRegistry package renaming and restructuring Project: http://git-wip-us.apache.org/repos/asf/geode/repo Commit: http://git-wip-us.apache.org/repos/asf/geode/commit/e3af5a42 Tree: http://git-wip-us.apache.org/repos/asf/geode/tree/e3af5a42 Diff: http://git-wip-us.apache.org/repos/asf/geode/diff/e3af5a42 Branch: refs/heads/feature/GEODE-2995 Commit: e3af5a42cbac90e86c0427325cf495c9aabf6d0f Parents: 336b0e0 Author: Udo Kohlmeyer <[email protected]> Authored: Tue Jun 13 12:22:28 2017 -0700 Committer: Udo Kohlmeyer <[email protected]> Committed: Tue Jun 13 12:22:28 2017 -0700 ---------------------------------------------------------------------- .../InvalidProtocolMessageException.java | 27 ------- .../protocol/handler/ProtocolHandler.java | 28 -------- .../protobuf/ProtobufProtocolHandler.java | 43 ------------ .../InvalidProtocolMessageException.java | 27 +++++++ .../geode/protocol/handler/ProtocolHandler.java | 28 ++++++++ .../protocol/operations/OperationHandler.java | 20 ++++++ .../registry/OperationsHandlerRegistry.java | 50 +++++++++++++ ...rationHandlerAlreadyRegisteredException.java | 23 ++++++ .../OperationHandlerNotRegisteredException.java | 26 +++++++ ...eode.client.protocol.handler.ProtocolHandler | 1 - ...pache.geode.protocol.handler.ProtocolHandler | 1 + .../OperationsHandlerRegistryJUnitTest.java | 74 ++++++++++++++++++++ 12 files changed, 249 insertions(+), 99 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/geode/blob/e3af5a42/geode-protobuf/src/main/java/org/apache/geode/client/protocol/exception/InvalidProtocolMessageException.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/client/protocol/exception/InvalidProtocolMessageException.java b/geode-protobuf/src/main/java/org/apache/geode/client/protocol/exception/InvalidProtocolMessageException.java deleted file mode 100644 index 9f80205..0000000 --- a/geode-protobuf/src/main/java/org/apache/geode/client/protocol/exception/InvalidProtocolMessageException.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.client.protocol.exception; - -public class InvalidProtocolMessageException extends Exception { - public InvalidProtocolMessageException(String message) { - super(message); - } - - public InvalidProtocolMessageException(String message, Throwable cause) { - super(message, cause); - } -} http://git-wip-us.apache.org/repos/asf/geode/blob/e3af5a42/geode-protobuf/src/main/java/org/apache/geode/client/protocol/handler/ProtocolHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/client/protocol/handler/ProtocolHandler.java b/geode-protobuf/src/main/java/org/apache/geode/client/protocol/handler/ProtocolHandler.java deleted file mode 100644 index c0e6697..0000000 --- a/geode-protobuf/src/main/java/org/apache/geode/client/protocol/handler/ProtocolHandler.java +++ /dev/null @@ -1,28 +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.handler; - -import org.apache.geode.client.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/e3af5a42/geode-protobuf/src/main/java/org/apache/geode/client/protocol/handler/protobuf/ProtobufProtocolHandler.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/java/org/apache/geode/client/protocol/handler/protobuf/ProtobufProtocolHandler.java b/geode-protobuf/src/main/java/org/apache/geode/client/protocol/handler/protobuf/ProtobufProtocolHandler.java deleted file mode 100644 index 9b87b31..0000000 --- a/geode-protobuf/src/main/java/org/apache/geode/client/protocol/handler/protobuf/ProtobufProtocolHandler.java +++ /dev/null @@ -1,43 +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.handler.protobuf; - -import org.apache.geode.client.protocol.exception.InvalidProtocolMessageException; -import org.apache.geode.client.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/e3af5a42/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 new file mode 100644 index 0000000..ef3d7bb --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/exception/InvalidProtocolMessageException.java @@ -0,0 +1,27 @@ +/* + * 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.exception; + +public class InvalidProtocolMessageException extends Exception { + public InvalidProtocolMessageException(String message) { + super(message); + } + + public InvalidProtocolMessageException(String message, Throwable cause) { + super(message, cause); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/e3af5a42/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 new file mode 100644 index 0000000..525f232 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/handler/ProtocolHandler.java @@ -0,0 +1,28 @@ +/* + * 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/e3af5a42/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 new file mode 100644 index 0000000..793fcb6 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/OperationHandler.java @@ -0,0 +1,20 @@ +/* + * 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; + +public interface OperationHandler<T> { +} http://git-wip-us.apache.org/repos/asf/geode/blob/e3af5a42/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 new file mode 100644 index 0000000..0a39b5e --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistry.java @@ -0,0 +1,50 @@ +/* + * 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.registry; + +import org.apache.geode.protocol.operations.OperationHandler; +import org.apache.geode.protocol.operations.registry.exception.OperationHandlerAlreadyRegisteredException; +import org.apache.geode.protocol.operations.registry.exception.OperationHandlerNotRegisteredException; + +import java.util.HashMap; + +public class OperationsHandlerRegistry { + private HashMap<Integer, OperationHandler> registeredOperations = new HashMap<>(); + + public OperationHandler getOperationHandlerForOperationId(int operationCode) { + OperationHandler operationHandler = registeredOperations.get(operationCode); + if (operationHandler == null) { + throw new OperationHandlerNotRegisteredException( + "There is no operation handler registered for operation code: " + operationCode); + } + return operationHandler; + } + + public synchronized void registerOperationHandlerForOperationId(int operationCode, + OperationHandler operationHandler) { + if (registeredOperations.containsKey(operationCode)) { + throw new OperationHandlerAlreadyRegisteredException( + "An operation handler for operationCode: " + operationCode + + " has already been registered!"); + } + registeredOperations.put(operationCode, operationHandler); + } + + public int getRegisteredOperationHandlersCount() { + return registeredOperations.size(); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/e3af5a42/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 new file mode 100644 index 0000000..7066ee3 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/exception/OperationHandlerAlreadyRegisteredException.java @@ -0,0 +1,23 @@ +/* + * 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.registry.exception; + +public class OperationHandlerAlreadyRegisteredException extends RuntimeException { + public OperationHandlerAlreadyRegisteredException(String message) { + super(message); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/e3af5a42/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 new file mode 100644 index 0000000..d86cac8 --- /dev/null +++ b/geode-protobuf/src/main/java/org/apache/geode/protocol/operations/registry/exception/OperationHandlerNotRegisteredException.java @@ -0,0 +1,26 @@ +/* + * 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.registry.exception; + +/** + * Created by ukohlmeyer on 6/13/17. + */ +public class OperationHandlerNotRegisteredException extends RuntimeException { + public OperationHandlerNotRegisteredException(String message) { + super(message); + } +} http://git-wip-us.apache.org/repos/asf/geode/blob/e3af5a42/geode-protobuf/src/main/resources/META-INF/services/org.apache.geode.client.protocol.handler.ProtocolHandler ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/main/resources/META-INF/services/org.apache.geode.client.protocol.handler.ProtocolHandler b/geode-protobuf/src/main/resources/META-INF/services/org.apache.geode.client.protocol.handler.ProtocolHandler deleted file mode 100644 index 60279ae..0000000 --- a/geode-protobuf/src/main/resources/META-INF/services/org.apache.geode.client.protocol.handler.ProtocolHandler +++ /dev/null @@ -1 +0,0 @@ -org.apache.geode.client.protocol.handler.protobuf.ProtobufProtocolHandler \ No newline at end of file http://git-wip-us.apache.org/repos/asf/geode/blob/e3af5a42/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 new file mode 100644 index 0000000..9af7c5d --- /dev/null +++ b/geode-protobuf/src/main/resources/META-INF/services/org.apache.geode.protocol.handler.ProtocolHandler @@ -0,0 +1 @@ +org.apache.geode.protocol.handler.protobuf.ProtobufProtocolHandler \ No newline at end of file http://git-wip-us.apache.org/repos/asf/geode/blob/e3af5a42/geode-protobuf/src/test/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistryJUnitTest.java ---------------------------------------------------------------------- diff --git a/geode-protobuf/src/test/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistryJUnitTest.java b/geode-protobuf/src/test/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistryJUnitTest.java new file mode 100644 index 0000000..66ee8d1 --- /dev/null +++ b/geode-protobuf/src/test/java/org/apache/geode/protocol/operations/registry/OperationsHandlerRegistryJUnitTest.java @@ -0,0 +1,74 @@ +package org.apache.geode.protocol.operations.registry; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; + +import org.apache.geode.protocol.operations.OperationHandler; +import org.apache.geode.protocol.operations.registry.exception.OperationHandlerAlreadyRegisteredException; +import org.apache.geode.protocol.operations.registry.exception.OperationHandlerNotRegisteredException; +import org.apache.geode.test.junit.categories.UnitTest; +import org.junit.Before; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category(UnitTest.class) +public class OperationsHandlerRegistryJUnitTest { + private OperationsHandlerRegistry operationsHandlerRegistry; + + @Before + public void setup() { + operationsHandlerRegistry = new OperationsHandlerRegistry(); + } + + @Test + public void testAddOperationsHandlerForOperationType() { + operationsHandlerRegistry + .registerOperationHandlerForOperationId(5, new DummyOperationHandler()); + assertEquals(1, operationsHandlerRegistry.getRegisteredOperationHandlersCount()); + } + + @Test + public void testAddingDuplicateOperationsHandlerForOperationType_ThrowsException() { + DummyOperationHandler expectedOperationHandler = new DummyOperationHandler(); + operationsHandlerRegistry.registerOperationHandlerForOperationId(5, expectedOperationHandler); + assertEquals(1, operationsHandlerRegistry.getRegisteredOperationHandlersCount()); + boolean exceptionCaught = false; + try { + operationsHandlerRegistry + .registerOperationHandlerForOperationId(5, new DummyOperationHandler()); + } catch (OperationHandlerAlreadyRegisteredException e) { + exceptionCaught = true; + } + assertTrue(exceptionCaught); + assertEquals(1, operationsHandlerRegistry.getRegisteredOperationHandlersCount()); + assertSame(expectedOperationHandler, + operationsHandlerRegistry.getOperationHandlerForOperationId(5)); + } + + @Test + public void testGetOperationsHandlerForOperationType() { + DummyOperationHandler expectedOperationHandler = new DummyOperationHandler(); + operationsHandlerRegistry.registerOperationHandlerForOperationId(5, expectedOperationHandler); + OperationHandler operationHandler = + operationsHandlerRegistry.getOperationHandlerForOperationId(5); + assertSame(expectedOperationHandler, operationHandler); + } + + @Test + public void testGetOperationsHandlerForMissingOperationType_ThrowsException() { + boolean exceptionCaught = false; + try { + operationsHandlerRegistry.getOperationHandlerForOperationId(5); + } catch (OperationHandlerNotRegisteredException e) { + exceptionCaught = true; + } + assertTrue(exceptionCaught); + } + + private class DummyOperationHandler implements OperationHandler { + + } + + +} \ No newline at end of file
