You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by sp...@apache.org on 2015/07/28 13:56:27 UTC
incubator-tinkerpop git commit: Add a way to generate IoRegistry
instance in GryoPool from static instance.
Repository: incubator-tinkerpop
Updated Branches:
refs/heads/tp30 bcdd45be5 -> 46e514428
Add a way to generate IoRegistry instance in GryoPool from static instance.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/46e51442
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/46e51442
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/46e51442
Branch: refs/heads/tp30
Commit: 46e51442873f994159614995dbb0ffaefb91b416
Parents: bcdd45b
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Tue Jul 28 07:53:07 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Tue Jul 28 07:53:07 2015 -0400
----------------------------------------------------------------------
.../gremlin/structure/io/IoRegistry.java | 5 ++--
.../gremlin/structure/io/gryo/GryoPool.java | 14 +++++++---
.../gremlin/structure/io/gryo/GryoPoolTest.java | 28 +++++++++++++++++---
3 files changed, 38 insertions(+), 9 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/46e51442/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/IoRegistry.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/IoRegistry.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/IoRegistry.java
index fee2811..e73b01e 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/IoRegistry.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/IoRegistry.java
@@ -35,8 +35,9 @@ import java.util.stream.Collectors;
* A generalized custom serializer registry for vendors implementing a {@link Graph}. Vendors should provide an
* implementation of this interface if their implementation requires custom serialization of identifiers or other
* such content housed in their graph. Consider extending from {@link AbstractIoRegistry} and ensure that the
- * implementation has a zero-arg constructor, as implementations may need to be constructed from reflection
- * given different parts of the TinkerPop stack.
+ * implementation has a zero-arg constructor or a static "getInstance" method that returns an {@code IoRegistry}
+ * instance, as implementations may need to be constructed from reflection given different parts of the TinkerPop
+ * stack.
* <p/>
* The serializers to register depend on the {@link Io} implementations that are expected to be supported. There
* are currently two core implementations in {@link GryoIo} and {@link GraphSONIo}. Both of these should be supported
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/46e51442/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoPool.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoPool.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoPool.java
index 2641e56..d27694e 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoPool.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoPool.java
@@ -20,8 +20,8 @@ package org.apache.tinkerpop.gremlin.structure.io.gryo;
import org.apache.commons.configuration.Configuration;
import org.apache.tinkerpop.gremlin.structure.io.IoRegistry;
-import org.apache.tinkerpop.gremlin.structure.io.Mapper;
+import java.lang.reflect.Method;
import java.util.Optional;
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
@@ -127,8 +127,16 @@ public final class GryoPool {
if (className.isEmpty()) return Optional.empty();
try {
- final Class clazz = Class.forName(className);
- return Optional.of((IoRegistry) clazz.newInstance());
+ final Class<?> clazz = Class.forName(className);
+ try {
+ final Method instanceMethod = clazz.getDeclaredMethod("getInstance");
+ if (IoRegistry.class.isAssignableFrom(instanceMethod.getReturnType()))
+ return Optional.of((IoRegistry) instanceMethod.invoke(null));
+ else
+ throw new Exception();
+ } catch (Exception methodex) {
+ return Optional.of((IoRegistry) clazz.newInstance());
+ }
} catch (Exception ex) {
throw new IllegalStateException(ex);
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/46e51442/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoPoolTest.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoPoolTest.java b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoPoolTest.java
index ddc1aeb..8a033c6 100644
--- a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoPoolTest.java
+++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/gryo/GryoPoolTest.java
@@ -81,9 +81,17 @@ public class GryoPoolTest {
}
@Test
- public void shouldConfigPoolOnConstructionWithCustomIoRegistry() throws Exception {
+ public void shouldConfigPoolOnConstructionWithCustomIoRegistryConstructor() throws Exception {
final Configuration conf = new BaseConfiguration();
- conf.setProperty(GryoPool.CONFIG_IO_REGISTRY, CustomIoRegistry.class.getName());
+ conf.setProperty(GryoPool.CONFIG_IO_REGISTRY, CustomIoRegistryConstructorBased.class.getName());
+ final GryoPool pool = new GryoPool(conf);
+ assertReaderWriter(pool.takeWriter(), pool.takeReader(), new Junk("test"), Junk.class);
+ }
+
+ @Test
+ public void shouldConfigPoolOnConstructionWithCustomIoRegistryInstance() throws Exception {
+ final Configuration conf = new BaseConfiguration();
+ conf.setProperty(GryoPool.CONFIG_IO_REGISTRY, CustomIoRegistryInstanceBased.class.getName());
final GryoPool pool = new GryoPool(conf);
assertReaderWriter(pool.takeWriter(), pool.takeReader(), new Junk("test"), Junk.class);
}
@@ -139,9 +147,21 @@ public class GryoPoolTest {
}
}
- public static class CustomIoRegistry extends AbstractIoRegistry {
- public CustomIoRegistry() {
+ public static class CustomIoRegistryConstructorBased extends AbstractIoRegistry {
+ public CustomIoRegistryConstructorBased() {
register(GryoIo.class, Junk.class, null);
}
}
+
+ public static class CustomIoRegistryInstanceBased extends AbstractIoRegistry {
+ private static CustomIoRegistryInstanceBased INSTANCE = new CustomIoRegistryInstanceBased();
+
+ private CustomIoRegistryInstanceBased() {
+ register(GryoIo.class, Junk.class, null);
+ }
+
+ public static CustomIoRegistryInstanceBased getInstance() {
+ return INSTANCE;
+ }
+ }
}