tinkerpop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From spmalle...@apache.org
Subject [1/2] tinkerpop git commit: Some minor fixes to plugin infrastructure
Date Tue, 24 Jan 2017 16:10:39 GMT
Repository: tinkerpop
Updated Branches:
  refs/heads/master fc7f909a1 -> df3f924b6


Some minor fixes to plugin infrastructure

Graph plugins needed instance() methods to be properly instantiated at runtime. Hooked up
ScriptCustomizer in the GremlinScriptEngineManager. Added BindingsGremlinPlugin to expose
the various "bindings" customizers. None of these changes are breaking and really aren't meant
for usage alongs the 3.2.x line. CTR


Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/e3889bf2
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/e3889bf2
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/e3889bf2

Branch: refs/heads/master
Commit: e3889bf2401b42c3afbc85eabc2fbcebf2588974
Parents: 9d88304
Author: Stephen Mallette <spmva@genoprime.com>
Authored: Tue Jan 24 11:05:13 2017 -0500
Committer: Stephen Mallette <spmva@genoprime.com>
Committed: Tue Jan 24 11:09:12 2017 -0500

----------------------------------------------------------------------
 .../gremlin/jsr223/BindingsGremlinPlugin.java   | 63 ++++++++++++++++++++
 .../DefaultGremlinScriptEngineManager.java      | 20 +++++++
 .../gremlin/jsr223/ScriptCustomizer.java        |  4 ++
 .../gremlin/groovy/engine/GremlinExecutor.java  | 11 +++-
 .../neo4j/jsr223/Neo4jGremlinPlugin.java        |  6 ++
 .../spark/jsr223/SparkGremlinPlugin.java        |  6 ++
 .../jsr223/TinkerGraphGremlinPlugin.java        | 42 +++++++------
 7 files changed, 132 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e3889bf2/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/BindingsGremlinPlugin.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/BindingsGremlinPlugin.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/BindingsGremlinPlugin.java
new file mode 100644
index 0000000..feb501d
--- /dev/null
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/BindingsGremlinPlugin.java
@@ -0,0 +1,63 @@
+/*
+ * 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.tinkerpop.gremlin.jsr223;
+
+import javax.script.Bindings;
+import javax.script.SimpleBindings;
+import java.util.Map;
+import java.util.function.Supplier;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class BindingsGremlinPlugin extends AbstractGremlinPlugin {
+    private static final String NAME = "tinkerpop.bindings";
+
+    private BindingsGremlinPlugin(final Builder builder) {
+        this(builder.bindings);
+    }
+
+    public BindingsGremlinPlugin(final Bindings bindings) {
+        super(NAME, new DefaultBindingsCustomizer(bindings));
+    }
+
+    public BindingsGremlinPlugin(final Supplier<Bindings> bindingsSupplier) {
+        super(NAME, new LazyBindingsCustomizer(bindingsSupplier));
+    }
+
+    public static BindingsGremlinPlugin.Builder build() {
+        return new Builder();
+    }
+
+    public static final class Builder {
+
+        private Bindings bindings = new SimpleBindings();
+
+        private Builder() {}
+
+        public Builder bindings(final Map<String, Object> bindings) {
+            this.bindings = new SimpleBindings(bindings);
+            return this;
+        }
+
+        public BindingsGremlinPlugin create() {
+            return new BindingsGremlinPlugin(this);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e3889bf2/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/DefaultGremlinScriptEngineManager.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/DefaultGremlinScriptEngineManager.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/DefaultGremlinScriptEngineManager.java
index 86b72f2..436deac 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/DefaultGremlinScriptEngineManager.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/DefaultGremlinScriptEngineManager.java
@@ -23,11 +23,13 @@ import javax.script.ScriptContext;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
 import java.util.List;
+import java.util.Map;
 import java.util.Optional;
 import java.util.ServiceConfigurationError;
 import java.util.ServiceLoader;
@@ -441,6 +443,24 @@ public class DefaultGremlinScriptEngineManager implements GremlinScriptEngineMan
     private GremlinScriptEngine createGremlinScriptEngine(final GremlinScriptEngineFactory
spi) {
         final GremlinScriptEngine engine = spi.getScriptEngine();
         engine.setBindings(getBindings(), ScriptContext.GLOBAL_SCOPE);
+
+        final List<ScriptCustomizer> scriptCustomizers = getCustomizers(spi.getEngineName()).stream()
+                .filter(p -> p instanceof ScriptCustomizer)
+                .map(p -> ((ScriptCustomizer) p))
+                .collect(Collectors.toList());
+
+        // since the bindings aren't added until after the ScriptEngine is constructed, running
init scripts that
+        // require bindings creates a problem. as a result, init scripts are applied here
+        scriptCustomizers.stream().flatMap(sc -> sc.getScripts().stream()).
+                map(l -> String.join(System.lineSeparator(), l)).forEach(initScript ->
{
+            try {
+                final Object initializedBindings = engine.eval(initScript);
+                if (initializedBindings != null && initializedBindings instanceof
Map)
+                    ((Map<String,Object>) initializedBindings).forEach((k,v) ->
put(k,v));
+            } catch (Exception ex) {
+                throw new IllegalStateException(ex);
+            }
+        });
         return engine;
     }
 }

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e3889bf2/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/ScriptCustomizer.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/ScriptCustomizer.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/ScriptCustomizer.java
index eb2f8bc..5390776 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/ScriptCustomizer.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/jsr223/ScriptCustomizer.java
@@ -18,11 +18,15 @@
  */
 package org.apache.tinkerpop.gremlin.jsr223;
 
+import javax.script.ScriptEngineManager;
 import java.util.Collection;
 import java.util.List;
 
 /**
  * A {@link Customizer} that executes scripts in a {@link GremlinScriptEngine} instance for
purpose of initialization.
+ * Implementors of a {@link GremlinScriptEngine} do not need to be concerned with supporting
this {@link Customizer}.
+ * This is work for the {@link ScriptEngineManager} implementation since scripts typically
require access to global
+ * bindings and those are not applied to the {@link GremlinScriptEngine} until after construction.
  *
  * @author Stephen Mallette (http://stephen.genoprime.com)
  */

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e3889bf2/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java
b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java
index 486f19e..567a248 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutor.java
@@ -22,6 +22,7 @@ import org.apache.commons.lang.exception.ExceptionUtils;
 import org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine;
 import org.apache.commons.lang3.concurrent.BasicThreadFactory;
 import org.apache.tinkerpop.gremlin.jsr223.CachedGremlinScriptEngineManager;
+import org.apache.tinkerpop.gremlin.jsr223.DefaultBindingsCustomizer;
 import org.apache.tinkerpop.gremlin.jsr223.GremlinPlugin;
 import org.apache.tinkerpop.gremlin.jsr223.GremlinScriptEngineManager;
 import org.apache.tinkerpop.gremlin.process.traversal.Bytecode;
@@ -121,13 +122,15 @@ public class GremlinExecutor implements AutoCloseable {
         this.gremlinScriptEngineManager = new CachedGremlinScriptEngineManager();
         initializeGremlinScriptEngineManager();
 
-        // this is temporary so that we can have backward compatibilty to the old plugin
system and ScriptEngines
+        // this is temporary so that we can have backward compatibility to the old plugin
system and ScriptEngines
         // approach to configuring Gremlin Server and GremlinExecutor. This code/check should
be removed with the
         // deprecated code around this is removed.
         if (!useGremlinScriptEngineManager)
             this.scriptEngines = createScriptEngines();
-        else
+        else {
             this.scriptEngines = null;
+            gremlinScriptEngineManager.getEngineByName("gremlin-groovy");
+        }
 
         this.suppliedExecutor = suppliedExecutor;
         this.suppliedScheduledExecutor = suppliedScheduledExecutor;
@@ -490,6 +493,10 @@ public class GremlinExecutor implements AutoCloseable {
                 }
             }
         }
+
+        if (this.useGremlinScriptEngineManager) {
+            gremlinScriptEngineManager.setBindings(globalBindings);
+        }
     }
 
     private ScriptEngines createScriptEngines() {

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e3889bf2/neo4j-gremlin/src/main/java/org/apache/tinkerpop/gremlin/neo4j/jsr223/Neo4jGremlinPlugin.java
----------------------------------------------------------------------
diff --git a/neo4j-gremlin/src/main/java/org/apache/tinkerpop/gremlin/neo4j/jsr223/Neo4jGremlinPlugin.java
b/neo4j-gremlin/src/main/java/org/apache/tinkerpop/gremlin/neo4j/jsr223/Neo4jGremlinPlugin.java
index 82a8d18..839a1d1 100644
--- a/neo4j-gremlin/src/main/java/org/apache/tinkerpop/gremlin/neo4j/jsr223/Neo4jGremlinPlugin.java
+++ b/neo4j-gremlin/src/main/java/org/apache/tinkerpop/gremlin/neo4j/jsr223/Neo4jGremlinPlugin.java
@@ -60,10 +60,16 @@ public final class Neo4jGremlinPlugin extends AbstractGremlinPlugin {
         }
     }
 
+    private static final Neo4jGremlinPlugin instance = new Neo4jGremlinPlugin();
+
     public Neo4jGremlinPlugin() {
         super(NAME, imports);
     }
 
+    public static Neo4jGremlinPlugin instance() {
+        return instance;
+    }
+
     @Override
     public boolean requireRestart() {
         return true;

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e3889bf2/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/jsr223/SparkGremlinPlugin.java
----------------------------------------------------------------------
diff --git a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/jsr223/SparkGremlinPlugin.java
b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/jsr223/SparkGremlinPlugin.java
index 9403fa4..f864a37 100644
--- a/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/jsr223/SparkGremlinPlugin.java
+++ b/spark-gremlin/src/main/java/org/apache/tinkerpop/gremlin/spark/jsr223/SparkGremlinPlugin.java
@@ -81,10 +81,16 @@ public final class SparkGremlinPlugin extends AbstractGremlinPlugin {
         return bindings;
     });
 
+    private static final SparkGremlinPlugin instance = new SparkGremlinPlugin();
+
     public SparkGremlinPlugin() {
         super(NAME, imports, bindings);
     }
 
+    public static SparkGremlinPlugin instance() {
+        return instance;
+    }
+
     @Override
     public boolean requireRestart() {
         return true;

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e3889bf2/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/jsr223/TinkerGraphGremlinPlugin.java
----------------------------------------------------------------------
diff --git a/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/jsr223/TinkerGraphGremlinPlugin.java
b/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/jsr223/TinkerGraphGremlinPlugin.java
index 19188d2..55e9a4d 100644
--- a/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/jsr223/TinkerGraphGremlinPlugin.java
+++ b/tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/jsr223/TinkerGraphGremlinPlugin.java
@@ -48,25 +48,31 @@ public final class TinkerGraphGremlinPlugin extends AbstractGremlinPlugin
{
 
     private static final ImportCustomizer imports = DefaultImportCustomizer.build()
             .addClassImports(TinkerEdge.class,
-                             TinkerElement.class,
-                             TinkerFactory.class,
-                             TinkerGraph.class,
-                             TinkerGraphVariables.class,
-                             TinkerHelper.class,
-                             TinkerIoRegistry.class,
-                             TinkerIoRegistryV2d0.class,
-                             TinkerProperty.class,
-                             TinkerVertex.class,
-                             TinkerVertexProperty.class,
-                             TinkerGraphComputer.class,
-                             TinkerGraphComputerView.class,
-                             TinkerMapEmitter.class,
-                             TinkerMemory.class,
-                             TinkerMessenger.class,
-                             TinkerReduceEmitter.class,
-                             TinkerWorkerPool.class).create();
+                    TinkerElement.class,
+                    TinkerFactory.class,
+                    TinkerGraph.class,
+                    TinkerGraphVariables.class,
+                    TinkerHelper.class,
+                    TinkerIoRegistry.class,
+                    TinkerIoRegistryV2d0.class,
+                    TinkerProperty.class,
+                    TinkerVertex.class,
+                    TinkerVertexProperty.class,
+                    TinkerGraphComputer.class,
+                    TinkerGraphComputerView.class,
+                    TinkerMapEmitter.class,
+                    TinkerMemory.class,
+                    TinkerMessenger.class,
+                    TinkerReduceEmitter.class,
+                    TinkerWorkerPool.class).create();
+
+    private static final TinkerGraphGremlinPlugin instance = new TinkerGraphGremlinPlugin();
 
     public TinkerGraphGremlinPlugin() {
         super(NAME, imports);
     }
-}
+
+    public static TinkerGraphGremlinPlugin instance() {
+        return instance;
+    }
+}
\ No newline at end of file


Mime
View raw message