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/27 20:00:21 UTC

[1/9] incubator-tinkerpop git commit: Moved around CompilerCustomizerProvider instances.

Repository: incubator-tinkerpop
Updated Branches:
  refs/heads/master 509aca771 -> 7286a909e


Moved around CompilerCustomizerProvider instances.


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

Branch: refs/heads/master
Commit: 1466d21917c4b750119e800f96bc51d85b7cee9e
Parents: 6503394
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Fri Jul 24 11:41:03 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Fri Jul 24 11:41:03 2015 -0400

----------------------------------------------------------------------
 .../groovy/CompileStaticCustomizerProvider.java | 60 -------------
 .../ThreadInterruptCustomizerProvider.java      | 35 --------
 .../TimedInterruptCustomizerProvider.java       | 61 -------------
 .../groovy/TypeCheckedCustomizerProvider.java   | 64 --------------
 .../VariableIdentificationCustomizer.java       | 91 --------------------
 .../CompileStaticCustomizerProvider.java        | 61 +++++++++++++
 .../ThreadInterruptCustomizerProvider.java      | 36 ++++++++
 .../TimedInterruptCustomizerProvider.java       | 62 +++++++++++++
 .../TypeCheckedCustomizerProvider.java          | 65 ++++++++++++++
 .../VariableIdentificationCustomizer.java       | 91 ++++++++++++++++++++
 .../groovy/engine/GremlinExecutorTest.java      |  4 +-
 ...mlinGroovyScriptEngineCompileStaticTest.java |  2 +-
 ...inGroovyScriptEngineThreadInterruptTest.java |  2 +-
 ...linGroovyScriptEngineTimedInterruptTest.java |  2 +-
 ...remlinGroovyScriptEngineTypeCheckedTest.java |  4 +-
 15 files changed, 322 insertions(+), 318 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1466d219/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/CompileStaticCustomizerProvider.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/CompileStaticCustomizerProvider.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/CompileStaticCustomizerProvider.java
deleted file mode 100644
index 928f6cd..0000000
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/CompileStaticCustomizerProvider.java
+++ /dev/null
@@ -1,60 +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.tinkerpop.gremlin.groovy;
-
-import groovy.transform.CompileStatic;
-import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
-import org.codehaus.groovy.control.customizers.CompilationCustomizer;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
-/**
- * Injects the {@code CompileStatic} transformer to enable type validation on script execution.
- *
- * @author Stephen Mallette (http://stephen.genoprime.com)
- */
-public class CompileStaticCustomizerProvider implements CompilerCustomizerProvider {
-
-    private final String extensions;
-
-    public CompileStaticCustomizerProvider() {
-        this(null);
-    }
-
-    public CompileStaticCustomizerProvider(final String extensions) {
-        this.extensions = extensions;
-    }
-
-    @Override
-    public CompilationCustomizer create() {
-        final Map<String, Object> annotationParams = new HashMap<>();
-        if (extensions != null && !extensions.isEmpty()) {
-            if (extensions.contains(","))
-                annotationParams.put("extensions", Stream.of(extensions.split(",")).collect(Collectors.toList()));
-            else
-                annotationParams.put("extensions", Collections.singletonList(extensions));
-        }
-
-        return new ASTTransformationCustomizer(annotationParams, CompileStatic.class);
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1466d219/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/ThreadInterruptCustomizerProvider.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/ThreadInterruptCustomizerProvider.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/ThreadInterruptCustomizerProvider.java
deleted file mode 100644
index f8f46a7..0000000
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/ThreadInterruptCustomizerProvider.java
+++ /dev/null
@@ -1,35 +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.tinkerpop.gremlin.groovy;
-
-import groovy.transform.ThreadInterrupt;
-import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
-import org.codehaus.groovy.control.customizers.CompilationCustomizer;
-
-/**
- * Injects checks for thread interruption into scripts.
- *
- * @author Stephen Mallette (http://stephen.genoprime.com)
- */
-public class ThreadInterruptCustomizerProvider implements CompilerCustomizerProvider {
-    @Override
-    public CompilationCustomizer create() {
-        return new ASTTransformationCustomizer(ThreadInterrupt.class);
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1466d219/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/TimedInterruptCustomizerProvider.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/TimedInterruptCustomizerProvider.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/TimedInterruptCustomizerProvider.java
deleted file mode 100644
index 44dfe21..0000000
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/TimedInterruptCustomizerProvider.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.tinkerpop.gremlin.groovy;
-
-import groovy.transform.TimedInterrupt;
-import org.codehaus.groovy.ast.tools.GeneralUtils;
-import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
-import org.codehaus.groovy.control.customizers.CompilationCustomizer;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.concurrent.TimeUnit;
-
-/**
- * Injects a check in loops and other areas of code to interrupt script execution if the run time exceeds the
- * specified time.
- *
- * @author Stephen Mallette (http://stephen.genoprime.com)
- */
-public class TimedInterruptCustomizerProvider implements CompilerCustomizerProvider {
-    public static final long DEFAULT_INTERRUPTION_TIMEOUT = 60000;
-
-    private final long interruptionTimeout;
-
-    public TimedInterruptCustomizerProvider() {
-        this(DEFAULT_INTERRUPTION_TIMEOUT);
-    }
-
-    public TimedInterruptCustomizerProvider(final Long interruptionTimeout) {
-        this.interruptionTimeout = interruptionTimeout;
-    }
-
-    public TimedInterruptCustomizerProvider(final Integer interruptionTimeout) {
-        this.interruptionTimeout = interruptionTimeout.longValue();
-    }
-
-    @Override
-    public CompilationCustomizer create() {
-        final Map<String, Object> timedInterruptAnnotationParams = new HashMap<>();
-        timedInterruptAnnotationParams.put("value", interruptionTimeout);
-        timedInterruptAnnotationParams.put("unit", GeneralUtils.propX(GeneralUtils.classX(TimeUnit.class), TimeUnit.MILLISECONDS.toString()));
-        timedInterruptAnnotationParams.put("checkOnMethodStart", false);
-        return new ASTTransformationCustomizer(timedInterruptAnnotationParams, TimedInterrupt.class);
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1466d219/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/TypeCheckedCustomizerProvider.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/TypeCheckedCustomizerProvider.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/TypeCheckedCustomizerProvider.java
deleted file mode 100644
index 73f6434..0000000
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/TypeCheckedCustomizerProvider.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.tinkerpop.gremlin.groovy;
-
-import groovy.transform.TypeChecked;
-import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
-import org.codehaus.groovy.control.customizers.CompilationCustomizer;
-
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
-/**
- * Injects the {@code TypeChecked} transformer to enable type validation on script execution.
- *
- * @author Stephen Mallette (http://stephen.genoprime.com)
- */
-public class TypeCheckedCustomizerProvider implements CompilerCustomizerProvider {
-
-    private final String extensions;
-
-    public TypeCheckedCustomizerProvider() {
-        this(null);
-    }
-
-    /**
-     * Configures the {@code TypeChecked} annotation to use optional extensions.  The argument should be one or more
-     * groovy scripts on the classpath or the fully qualified classname of a precompiled extension.  If there are
-     * multiple extensions then extensions should be comma separated.
-     */
-    public TypeCheckedCustomizerProvider(final String extensions) {
-        this.extensions = extensions;
-    }
-
-    @Override
-    public CompilationCustomizer create() {
-        final Map<String, Object> annotationParams = new HashMap<>();
-        if (extensions != null && !extensions.isEmpty()) {
-            if (extensions.contains(","))
-                annotationParams.put("extensions", Stream.of(extensions.split(",")).collect(Collectors.toList()));
-            else
-                annotationParams.put("extensions", Collections.singletonList(extensions));
-        }
-        return new ASTTransformationCustomizer(annotationParams, TypeChecked.class);
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1466d219/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/VariableIdentificationCustomizer.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/VariableIdentificationCustomizer.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/VariableIdentificationCustomizer.java
deleted file mode 100644
index 4606bde..0000000
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/VariableIdentificationCustomizer.java
+++ /dev/null
@@ -1,91 +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.tinkerpop.gremlin.groovy.jsr223;
-
-import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
-import org.codehaus.groovy.ast.ClassNode;
-import org.codehaus.groovy.ast.expr.VariableExpression;
-import org.codehaus.groovy.classgen.GeneratorContext;
-import org.codehaus.groovy.control.CompilationFailedException;
-import org.codehaus.groovy.control.CompilePhase;
-import org.codehaus.groovy.control.SourceUnit;
-import org.codehaus.groovy.control.customizers.CompilationCustomizer;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.Set;
-import java.util.TreeSet;
-
-/**
- * @author Stephen Mallette (http://stephen.genoprime.com)
- */
-public class VariableIdentificationCustomizer extends CompilationCustomizer {
-
-    private static final ThreadLocal<Set<String>> variables = new ThreadLocal<Set<String>>()  {
-        @Override
-        protected Set<String> initialValue() {
-            return new TreeSet<>();
-        }
-    };
-
-    private static final List<String> variablesToIgnore = new ArrayList<>(Arrays.asList("this", "args", "context", "super"));
-
-    public VariableIdentificationCustomizer() {
-        super(CompilePhase.CLASS_GENERATION);
-    }
-
-    public Set<String> getVariables() {
-        return variables.get();
-    }
-
-    public void clearVariables() {
-        variables.get().clear();
-    }
-
-    @Override
-    public void call(final SourceUnit sourceUnit, final GeneratorContext generatorContext,
-                     final ClassNode classNode) throws CompilationFailedException {
-        classNode.visitContents(new Visitor(sourceUnit));
-    }
-
-    class Visitor extends ClassCodeVisitorSupport {
-
-        private SourceUnit sourceUnit;
-
-        public Visitor(final SourceUnit sourceUnit) {
-            this.sourceUnit = sourceUnit;
-        }
-
-        @Override
-        public void visitVariableExpression(final VariableExpression expression) {
-            if (!variablesToIgnore.contains(expression.getName())
-                    && expression.getAccessedVariable().isDynamicTyped()) {
-                variables.get().add(expression.getName());
-            }
-
-            super.visitVariableExpression(expression);
-        }
-
-        @Override
-        protected SourceUnit getSourceUnit() {
-            return sourceUnit;
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1466d219/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/CompileStaticCustomizerProvider.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/CompileStaticCustomizerProvider.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/CompileStaticCustomizerProvider.java
new file mode 100644
index 0000000..65e1224
--- /dev/null
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/CompileStaticCustomizerProvider.java
@@ -0,0 +1,61 @@
+/*
+ * 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.groovy.jsr223.customizer;
+
+import groovy.transform.CompileStatic;
+import org.apache.tinkerpop.gremlin.groovy.CompilerCustomizerProvider;
+import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
+import org.codehaus.groovy.control.customizers.CompilationCustomizer;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ * Injects the {@code CompileStatic} transformer to enable type validation on script execution.
+ *
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class CompileStaticCustomizerProvider implements CompilerCustomizerProvider {
+
+    private final String extensions;
+
+    public CompileStaticCustomizerProvider() {
+        this(null);
+    }
+
+    public CompileStaticCustomizerProvider(final String extensions) {
+        this.extensions = extensions;
+    }
+
+    @Override
+    public CompilationCustomizer create() {
+        final Map<String, Object> annotationParams = new HashMap<>();
+        if (extensions != null && !extensions.isEmpty()) {
+            if (extensions.contains(","))
+                annotationParams.put("extensions", Stream.of(extensions.split(",")).collect(Collectors.toList()));
+            else
+                annotationParams.put("extensions", Collections.singletonList(extensions));
+        }
+
+        return new ASTTransformationCustomizer(annotationParams, CompileStatic.class);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1466d219/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/ThreadInterruptCustomizerProvider.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/ThreadInterruptCustomizerProvider.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/ThreadInterruptCustomizerProvider.java
new file mode 100644
index 0000000..8c942c6
--- /dev/null
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/ThreadInterruptCustomizerProvider.java
@@ -0,0 +1,36 @@
+/*
+ * 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.groovy.jsr223.customizer;
+
+import groovy.transform.ThreadInterrupt;
+import org.apache.tinkerpop.gremlin.groovy.CompilerCustomizerProvider;
+import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
+import org.codehaus.groovy.control.customizers.CompilationCustomizer;
+
+/**
+ * Injects checks for thread interruption into scripts.
+ *
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class ThreadInterruptCustomizerProvider implements CompilerCustomizerProvider {
+    @Override
+    public CompilationCustomizer create() {
+        return new ASTTransformationCustomizer(ThreadInterrupt.class);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1466d219/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/TimedInterruptCustomizerProvider.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/TimedInterruptCustomizerProvider.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/TimedInterruptCustomizerProvider.java
new file mode 100644
index 0000000..1a85b2d
--- /dev/null
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/TimedInterruptCustomizerProvider.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.tinkerpop.gremlin.groovy.jsr223.customizer;
+
+import groovy.transform.TimedInterrupt;
+import org.apache.tinkerpop.gremlin.groovy.CompilerCustomizerProvider;
+import org.codehaus.groovy.ast.tools.GeneralUtils;
+import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
+import org.codehaus.groovy.control.customizers.CompilationCustomizer;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Injects a check in loops and other areas of code to interrupt script execution if the run time exceeds the
+ * specified time.
+ *
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class TimedInterruptCustomizerProvider implements CompilerCustomizerProvider {
+    public static final long DEFAULT_INTERRUPTION_TIMEOUT = 60000;
+
+    private final long interruptionTimeout;
+
+    public TimedInterruptCustomizerProvider() {
+        this(DEFAULT_INTERRUPTION_TIMEOUT);
+    }
+
+    public TimedInterruptCustomizerProvider(final Long interruptionTimeout) {
+        this.interruptionTimeout = interruptionTimeout;
+    }
+
+    public TimedInterruptCustomizerProvider(final Integer interruptionTimeout) {
+        this.interruptionTimeout = interruptionTimeout.longValue();
+    }
+
+    @Override
+    public CompilationCustomizer create() {
+        final Map<String, Object> timedInterruptAnnotationParams = new HashMap<>();
+        timedInterruptAnnotationParams.put("value", interruptionTimeout);
+        timedInterruptAnnotationParams.put("unit", GeneralUtils.propX(GeneralUtils.classX(TimeUnit.class), TimeUnit.MILLISECONDS.toString()));
+        timedInterruptAnnotationParams.put("checkOnMethodStart", false);
+        return new ASTTransformationCustomizer(timedInterruptAnnotationParams, TimedInterrupt.class);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1466d219/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/TypeCheckedCustomizerProvider.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/TypeCheckedCustomizerProvider.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/TypeCheckedCustomizerProvider.java
new file mode 100644
index 0000000..f0bc791
--- /dev/null
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/TypeCheckedCustomizerProvider.java
@@ -0,0 +1,65 @@
+/*
+ * 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.groovy.jsr223.customizer;
+
+import groovy.transform.TypeChecked;
+import org.apache.tinkerpop.gremlin.groovy.CompilerCustomizerProvider;
+import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
+import org.codehaus.groovy.control.customizers.CompilationCustomizer;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ * Injects the {@code TypeChecked} transformer to enable type validation on script execution.
+ *
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class TypeCheckedCustomizerProvider implements CompilerCustomizerProvider {
+
+    private final String extensions;
+
+    public TypeCheckedCustomizerProvider() {
+        this(null);
+    }
+
+    /**
+     * Configures the {@code TypeChecked} annotation to use optional extensions.  The argument should be one or more
+     * groovy scripts on the classpath or the fully qualified classname of a precompiled extension.  If there are
+     * multiple extensions then extensions should be comma separated.
+     */
+    public TypeCheckedCustomizerProvider(final String extensions) {
+        this.extensions = extensions;
+    }
+
+    @Override
+    public CompilationCustomizer create() {
+        final Map<String, Object> annotationParams = new HashMap<>();
+        if (extensions != null && !extensions.isEmpty()) {
+            if (extensions.contains(","))
+                annotationParams.put("extensions", Stream.of(extensions.split(",")).collect(Collectors.toList()));
+            else
+                annotationParams.put("extensions", Collections.singletonList(extensions));
+        }
+        return new ASTTransformationCustomizer(annotationParams, TypeChecked.class);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1466d219/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/VariableIdentificationCustomizer.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/VariableIdentificationCustomizer.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/VariableIdentificationCustomizer.java
new file mode 100644
index 0000000..79df578
--- /dev/null
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/VariableIdentificationCustomizer.java
@@ -0,0 +1,91 @@
+/*
+ * 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.groovy.jsr223.customizer;
+
+import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
+import org.codehaus.groovy.ast.ClassNode;
+import org.codehaus.groovy.ast.expr.VariableExpression;
+import org.codehaus.groovy.classgen.GeneratorContext;
+import org.codehaus.groovy.control.CompilationFailedException;
+import org.codehaus.groovy.control.CompilePhase;
+import org.codehaus.groovy.control.SourceUnit;
+import org.codehaus.groovy.control.customizers.CompilationCustomizer;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class VariableIdentificationCustomizer extends CompilationCustomizer {
+
+    private static final ThreadLocal<Set<String>> variables = new ThreadLocal<Set<String>>()  {
+        @Override
+        protected Set<String> initialValue() {
+            return new TreeSet<>();
+        }
+    };
+
+    private static final List<String> variablesToIgnore = new ArrayList<>(Arrays.asList("this", "args", "context", "super"));
+
+    public VariableIdentificationCustomizer() {
+        super(CompilePhase.CLASS_GENERATION);
+    }
+
+    public Set<String> getVariables() {
+        return variables.get();
+    }
+
+    public void clearVariables() {
+        variables.get().clear();
+    }
+
+    @Override
+    public void call(final SourceUnit sourceUnit, final GeneratorContext generatorContext,
+                     final ClassNode classNode) throws CompilationFailedException {
+        classNode.visitContents(new Visitor(sourceUnit));
+    }
+
+    class Visitor extends ClassCodeVisitorSupport {
+
+        private SourceUnit sourceUnit;
+
+        public Visitor(final SourceUnit sourceUnit) {
+            this.sourceUnit = sourceUnit;
+        }
+
+        @Override
+        public void visitVariableExpression(final VariableExpression expression) {
+            if (!variablesToIgnore.contains(expression.getName())
+                    && expression.getAccessedVariable().isDynamicTyped()) {
+                variables.get().add(expression.getName());
+            }
+
+            super.visitVariableExpression(expression);
+        }
+
+        @Override
+        protected SourceUnit getSourceUnit() {
+            return sourceUnit;
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1466d219/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java
index 85431a6..1268a3e 100644
--- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java
@@ -20,8 +20,8 @@ package org.apache.tinkerpop.gremlin.groovy.engine;
 
 import org.apache.commons.lang3.concurrent.BasicThreadFactory;
 import org.apache.tinkerpop.gremlin.TestHelper;
-import org.apache.tinkerpop.gremlin.groovy.ThreadInterruptCustomizerProvider;
-import org.apache.tinkerpop.gremlin.groovy.TimedInterruptCustomizerProvider;
+import org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.ThreadInterruptCustomizerProvider;
+import org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.TimedInterruptCustomizerProvider;
 import org.junit.Test;
 
 import javax.script.Bindings;

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1466d219/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineCompileStaticTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineCompileStaticTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineCompileStaticTest.java
index 466d223..1f46f58 100644
--- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineCompileStaticTest.java
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineCompileStaticTest.java
@@ -18,7 +18,7 @@
  */
 package org.apache.tinkerpop.gremlin.groovy.jsr223;
 
-import org.apache.tinkerpop.gremlin.groovy.CompileStaticCustomizerProvider;
+import org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.CompileStaticCustomizerProvider;
 import org.codehaus.groovy.control.MultipleCompilationErrorsException;
 import org.junit.Test;
 

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1466d219/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineThreadInterruptTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineThreadInterruptTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineThreadInterruptTest.java
index 939453f..c002939 100644
--- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineThreadInterruptTest.java
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineThreadInterruptTest.java
@@ -18,7 +18,7 @@
  */
 package org.apache.tinkerpop.gremlin.groovy.jsr223;
 
-import org.apache.tinkerpop.gremlin.groovy.ThreadInterruptCustomizerProvider;
+import org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.ThreadInterruptCustomizerProvider;
 import org.junit.Test;
 
 import javax.script.ScriptEngine;

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1466d219/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTimedInterruptTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTimedInterruptTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTimedInterruptTest.java
index 40d0104..25867af 100644
--- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTimedInterruptTest.java
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTimedInterruptTest.java
@@ -19,7 +19,7 @@
 package org.apache.tinkerpop.gremlin.groovy.jsr223;
 
 import org.apache.tinkerpop.gremlin.groovy.DefaultImportCustomizerProvider;
-import org.apache.tinkerpop.gremlin.groovy.TimedInterruptCustomizerProvider;
+import org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.TimedInterruptCustomizerProvider;
 import org.junit.Test;
 
 import javax.script.ScriptEngine;

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1466d219/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTypeCheckedTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTypeCheckedTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTypeCheckedTest.java
index 97367e1..d1a0dca 100644
--- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTypeCheckedTest.java
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTypeCheckedTest.java
@@ -18,8 +18,8 @@
  */
 package org.apache.tinkerpop.gremlin.groovy.jsr223;
 
-import org.apache.tinkerpop.gremlin.groovy.CompileStaticCustomizerProvider;
-import org.apache.tinkerpop.gremlin.groovy.TypeCheckedCustomizerProvider;
+import org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.CompileStaticCustomizerProvider;
+import org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.TypeCheckedCustomizerProvider;
 import org.codehaus.groovy.control.MultipleCompilationErrorsException;
 import org.junit.Test;
 



[5/9] incubator-tinkerpop git commit: The "secure" config now include CompileStaticCompilerCustomizerProvider

Posted by sp...@apache.org.
The "secure" config now include CompileStaticCompilerCustomizerProvider

This meant that there had to be changes to the init script itself which wasn't type safe.  It also meant providing a new way to promote bindings as unresolved variables are not allowed under CompileStatic and TypeChecked.  Kept the old method of doing promotion but added the ability for the init script to pass back a Map of bindings to become global bindings.  That seems to work well.  Will deprecate the old method in 3.1.x.


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

Branch: refs/heads/master
Commit: 8b48e41789e8ffdbd2da9f9f0cc50077b0dbc353
Parents: f0ddc36
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Mon Jul 27 12:24:04 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Mon Jul 27 12:24:04 2015 -0400

----------------------------------------------------------------------
 CHANGELOG.asciidoc                              |  1 +
 .../customizer/SimpleSandboxExtension.groovy    |  3 +-
 .../gremlin/groovy/engine/GremlinExecutor.java  | 29 ++++++++----
 .../groovy/engine/GremlinExecutorInit.groovy    |  3 +-
 gremlin-server/conf/gremlin-server-secure.yaml  | 12 +++--
 .../scripts/empty-sample-secure.groovy          | 48 ++++++++++++++++++++
 .../server/util/ServerGremlinExecutor.java      |  2 +
 7 files changed, 81 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8b48e417/CHANGELOG.asciidoc
----------------------------------------------------------------------
diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index 6f1e236..84f408f 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -25,6 +25,7 @@ image::http://www.tinkerpop.com/docs/current/images/gremlin-hindu.png[width=225]
 TinkerPop 3.0.1 (NOT OFFICIALLY RELEASED YET)
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
+* Gremlin Server initialization scripts can now return a `Map` of values that will become global bindings for the server.
 * Introduced the `--dryRun` option to the document generation process which ignores actual script execution in the Gremlin Console.
 * Fixed bug in `EventStrategy` around property changed events when calling `property` without cardinality or meta-property values.
 * Improved support for the `Accept` header for REST-based requests in Gremlin Server.

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8b48e417/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/SimpleSandboxExtension.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/SimpleSandboxExtension.groovy b/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/SimpleSandboxExtension.groovy
index 111e363..f316dc6 100644
--- a/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/SimpleSandboxExtension.groovy
+++ b/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/SimpleSandboxExtension.groovy
@@ -33,7 +33,8 @@ class SimpleSandboxExtension extends SandboxExtension {
         graphIsAlwaysGraphInstance = false
 
         methodFilter = (BiPredicate<String, MethodNode>) { descriptor, method ->
-            return method.declaringClass.name != 'java.lang.System'
+            // declaring class is null when it refers to a script eval (it seems)
+            return null == method.declaringClass || method.declaringClass.name != 'java.lang.System'
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8b48e417/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 4d9df08..bd549ea 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
@@ -232,7 +232,7 @@ public class GremlinExecutor implements AutoCloseable {
         logger.debug("Preparing to evaluate script - {} - in thread [{}]", script, Thread.currentThread().getName());
 
         final Bindings bindings = new SimpleBindings();
-        bindings.putAll(this.globalBindings);
+        bindings.putAll(globalBindings);
         bindings.putAll(boundVars);
         beforeEval.accept(bindings);
 
@@ -417,19 +417,28 @@ public class GremlinExecutor implements AutoCloseable {
                 }).filter(p -> p.getValue1().isPresent()).map(p -> Pair.with(p.getValue0(), p.getValue1().get())).forEachOrdered(p -> {
                     try {
                         final Bindings bindings = new SimpleBindings();
-                        bindings.putAll(this.globalBindings);
+                        bindings.putAll(globalBindings);
 
                         // evaluate init scripts with hard reference so as to ensure it doesn't get garbage collected
                         bindings.put(GremlinGroovyScriptEngine.KEY_REFERENCE_TYPE, GremlinGroovyScriptEngine.REFERENCE_TYPE_HARD);
 
-                        se.eval(p.getValue1(), bindings, language);
-
-                        // re-assign graph bindings back to global bindings and grab TraversalSource creations.
-                        // prevent assignment of non-graph implementations just in case someone tries to overwrite
-                        // them in the init
-                        bindings.entrySet().stream()
-                                .filter(promoteBinding)
-                                .forEach(kv -> this.globalBindings.put(kv.getKey(), kv.getValue()));
+                        // todo: drop the "old method" in 3.1.0
+                        // the returned object should be a Map of initialized global bindings - this is in contrast
+                        // to the older method of scanning bindings for certain types/keys to promote.  the reason
+                        // for this change has to do with CompileStatic/TypeChecked "secure" configurations that
+                        // require the defined variables to be typed.  Under that scenario they are not "unresolved"
+                        // and thus don't get set to bindings automatically by the InvokerHelper.
+                        final Object initializedBindings = se.eval(p.getValue1(), bindings, language);
+                        if (initializedBindings != null && initializedBindings instanceof Map) {
+                            globalBindings.putAll((Map) initializedBindings);
+                        } else {
+                            // re-assign graph bindings back to global bindings and grab TraversalSource creations.
+                            // prevent assignment of non-graph implementations just in case someone tries to overwrite
+                            // them in the init
+                            bindings.entrySet().stream()
+                                    .filter(promoteBinding)
+                                    .forEach(kv -> globalBindings.put(kv.getKey(), kv.getValue()));
+                        }
 
                         logger.info("Initialized {} ScriptEngine with {}", language, p.getValue0());
                     } catch (ScriptException sx) {

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8b48e417/gremlin-groovy/src/test/resources/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorInit.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/resources/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorInit.groovy b/gremlin-groovy/src/test/resources/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorInit.groovy
index 513ccd4..db046b0 100644
--- a/gremlin-groovy/src/test/resources/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorInit.groovy
+++ b/gremlin-groovy/src/test/resources/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorInit.groovy
@@ -19,4 +19,5 @@
 def add(x, y) { x + y }
 
 someSet = ["test"] as Set
-someMap = [name:"stephen"]
\ No newline at end of file
+someMap = [name:"stephen"]
+""
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8b48e417/gremlin-server/conf/gremlin-server-secure.yaml
----------------------------------------------------------------------
diff --git a/gremlin-server/conf/gremlin-server-secure.yaml b/gremlin-server/conf/gremlin-server-secure.yaml
index 530b6cb..7fce9f7 100644
--- a/gremlin-server/conf/gremlin-server-secure.yaml
+++ b/gremlin-server/conf/gremlin-server-secure.yaml
@@ -20,7 +20,7 @@
 # secure configuration of Gremlin Server.  Keep in mind that
 # this configuration uses a generated self-signed certificate
 # for SSL and a not so secure TinkerGraph for the credential
-# store - not suitable for production
+# store - not suitable for production.
 ##############################################################
 
 host: localhost
@@ -38,10 +38,12 @@ scriptEngines: {
   gremlin-groovy: {
     imports: [java.lang.Math],
     staticImports: [java.lang.Math.PI],
-    scripts: [scripts/empty-sample.groovy]},
-  nashorn: {
-      imports: [java.lang.Math],
-      staticImports: [java.lang.Math.PI]}}
+    scripts: [scripts/empty-sample-secure.groovy],
+    config: {
+      compilerCustomizerProviders: {
+              "org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.ThreadInterruptCustomizerProvider":[],
+              "org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.TimedInterruptCustomizerProvider":[10000],
+              "org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.CompileStaticCustomizerProvider":["org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.SimpleSandboxExtension"]}}}}
 serializers:
   - { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0 }                                             # application/vnd.gremlin-v1.0+gryo
   - { className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0, config: { serializeResultToString: true }}   # application/vnd.gremlin-v1.0+gryo-stringd

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8b48e417/gremlin-server/scripts/empty-sample-secure.groovy
----------------------------------------------------------------------
diff --git a/gremlin-server/scripts/empty-sample-secure.groovy b/gremlin-server/scripts/empty-sample-secure.groovy
new file mode 100644
index 0000000..614e81f
--- /dev/null
+++ b/gremlin-server/scripts/empty-sample-secure.groovy
@@ -0,0 +1,48 @@
+/*
+ * 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.
+ */
+
+/*******************************************************************
+ * This script is meant to be executed with the
+ * conf/gremlin-server-secure.yaml configuration file as an example
+ * of how an init script running with CompileStaticCustomizerProvider
+ * or TypeCheckedCustomizerProvider must be written to properly
+ * execute.
+ *******************************************************************/
+
+// An example of an initialization script that can be configured to run in Gremlin Server.
+// Functions defined here will go into global cache and will not be removed from there
+// unless there is a reset of the ScriptEngine.
+def addItUp(int x, int y) { x + y }
+
+// an init script that returns a Map allows explicit setting of global bindings.
+def globals = [:]
+
+// defines a sample LifeCycleHook that prints some output to the Gremlin Server console.
+// note that the name of the key in the "global" map is unimportant.
+globals << [hook : [
+  onStartUp: { LifeCycleHook.Context ctx ->
+    ctx.logger.info("Executed once at startup of Gremlin Server.")
+  },
+  onShutDown: { LifeCycleHook.Context ctx ->
+    ctx.logger.info("Executed once at shutdown of Gremlin Server.")
+  }
+] as LifeCycleHook]
+
+// define the default TraversalSource to bind queries to - this one will be named "g".
+globals << [g : graph.traversal()]
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8b48e417/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/ServerGremlinExecutor.java
----------------------------------------------------------------------
diff --git a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/ServerGremlinExecutor.java b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/ServerGremlinExecutor.java
index 714db3e..02a763a 100644
--- a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/ServerGremlinExecutor.java
+++ b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/ServerGremlinExecutor.java
@@ -95,6 +95,7 @@ public class ServerGremlinExecutor<T extends ScheduledExecutorService> {
 
         logger.info("Initialized Gremlin thread pool.  Threads in pool named with pattern gremlin-*");
 
+        // todo: deprecate promoteBindings in 3.1.0
         final GremlinExecutor.Builder gremlinExecutorBuilder = GremlinExecutor.build()
                 .scriptEvaluationTimeout(settings.scriptEvaluationTimeout)
                 .afterFailure((b, e) -> graphManager.rollbackAll())
@@ -111,6 +112,7 @@ public class ServerGremlinExecutor<T extends ScheduledExecutorService> {
         settings.scriptEngines.forEach((k, v) -> {
             // make sure that server related classes are available at init
             v.imports.add(LifeCycleHook.class.getCanonicalName());
+            v.imports.add(LifeCycleHook.Context.class.getCanonicalName());
             gremlinExecutorBuilder.addEngineSettings(k, v.imports, v.staticImports, v.scripts, v.config);
         });
 


[3/9] incubator-tinkerpop git commit: Added a basic implemenation of the SandboxExtension to ship with Gremlin Server.

Posted by sp...@apache.org.
Added a basic implemenation of the SandboxExtension to ship with Gremlin Server.

Affords some basic out-of-the-box protection (but not much).


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

Branch: refs/heads/master
Commit: 0c0aa358ad0495b4b6261279ef22e605adbb3ff5
Parents: c362669
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Fri Jul 24 12:54:59 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Fri Jul 24 12:54:59 2015 -0400

----------------------------------------------------------------------
 .../customizer/SimpleSandboxExtension.groovy    | 39 ++++++++++++++++++++
 1 file changed, 39 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/0c0aa358/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/SimpleSandboxExtension.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/SimpleSandboxExtension.groovy b/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/SimpleSandboxExtension.groovy
new file mode 100644
index 0000000..111e363
--- /dev/null
+++ b/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/SimpleSandboxExtension.groovy
@@ -0,0 +1,39 @@
+/*
+ * 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.groovy.jsr223.customizer
+
+import org.codehaus.groovy.ast.MethodNode
+
+import java.util.function.BiPredicate
+
+/**
+ * Blacklists the {@code System} class to ensure that one can't call {@code System.exit()}.
+ *
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+class SimpleSandboxExtension extends SandboxExtension {
+    SimpleSandboxExtension() {
+        gIsAlwaysGraphTraversalSource = false
+        graphIsAlwaysGraphInstance = false
+
+        methodFilter = (BiPredicate<String, MethodNode>) { descriptor, method ->
+            return method.declaringClass.name != 'java.lang.System'
+        }
+    }
+}


[8/9] incubator-tinkerpop git commit: Merge remote-tracking branch 'origin/tp30'

Posted by sp...@apache.org.
Merge remote-tracking branch 'origin/tp30'


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

Branch: refs/heads/master
Commit: 59735dbfe5ec9fcace46e52ac45494e5fcf61b06
Parents: 509aca7 a05a444
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Mon Jul 27 13:15:02 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Mon Jul 27 13:15:02 2015 -0400

----------------------------------------------------------------------
 CHANGELOG.asciidoc                              |   1 +
 docs/src/gremlin-applications.asciidoc          |  89 +++++++++-
 .../jsr223/AllowColorSandboxExtension.groovy    |   1 +
 .../BlockSomeVariablesSandboxExtension.groovy   |   1 +
 ...ebindAllVariableTypesSandboxExtension.groovy |   2 +
 .../GremlinGroovyScriptEngineOverGraphTest.java |   4 -
 ...mlinGroovyScriptEngineSandboxCustomTest.java |   3 +-
 ...GroovyScriptEngineSandboxedStandardTest.java |   5 +-
 ...nGroovyScriptEngineTinkerPopSandboxTest.java |   5 +-
 .../groovy/jsr223/SandboxExtension.groovy       | 176 ------------------
 .../jsr223/TinkerPopSandboxExtension.groovy     |  47 -----
 .../jsr223/customizer/SandboxExtension.groovy   | 177 +++++++++++++++++++
 .../customizer/SimpleSandboxExtension.groovy    |  40 +++++
 .../customizer/TinkerPopSandboxExtension.groovy |  47 +++++
 .../groovy/CompileStaticCustomizerProvider.java |  60 -------
 .../ThreadInterruptCustomizerProvider.java      |  35 ----
 .../TimedInterruptCustomizerProvider.java       |  61 -------
 .../groovy/TypeCheckedCustomizerProvider.java   |  64 -------
 .../gremlin/groovy/engine/GremlinExecutor.java  |  29 +--
 .../VariableIdentificationCustomizer.java       |  91 ----------
 .../CompileStaticCustomizerProvider.java        |  61 +++++++
 .../ThreadInterruptCustomizerProvider.java      |  36 ++++
 .../TimedInterruptCustomizerProvider.java       |  62 +++++++
 .../TypeCheckedCustomizerProvider.java          |  65 +++++++
 .../VariableIdentificationCustomizer.java       |  91 ++++++++++
 .../groovy/engine/GremlinExecutorTest.java      |   4 +-
 ...mlinGroovyScriptEngineCompileStaticTest.java |   2 +-
 ...inGroovyScriptEngineThreadInterruptTest.java |   2 +-
 ...linGroovyScriptEngineTimedInterruptTest.java |   2 +-
 ...remlinGroovyScriptEngineTypeCheckedTest.java |   4 +-
 .../groovy/engine/GremlinExecutorInit.groovy    |   3 +-
 gremlin-server/conf/gremlin-server-secure.yaml  |  12 +-
 .../scripts/empty-sample-secure.groovy          |  48 +++++
 gremlin-server/scripts/empty-sample.groovy      |  24 +--
 gremlin-server/scripts/generate-classic.groovy  |  18 +-
 .../scripts/generate-modern-readonly.groovy     |  26 +--
 gremlin-server/scripts/generate-modern.groovy   |  20 +--
 gremlin-server/scripts/load-sample.groovy       |  26 +--
 .../server/util/ServerGremlinExecutor.java      |   2 +
 ...-shouldRebindTraversalSourceVariables.groovy |   8 +-
 40 files changed, 823 insertions(+), 631 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/59735dbf/CHANGELOG.asciidoc
----------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/59735dbf/docs/src/gremlin-applications.asciidoc
----------------------------------------------------------------------


[9/9] incubator-tinkerpop git commit: Removed bindings promotion from GremlinExecutor.

Posted by sp...@apache.org.
Removed bindings promotion from GremlinExecutor.

The revised approach that simply takes values from a Map returned from the init script simplifies the initialization of global bindings and introduces new flexibility over the old more limited method of binding promotion.  As a result, binding promotion no longer needs to be supported as it really should not be the recommended way to deal with this issue.


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

Branch: refs/heads/master
Commit: 7286a909e5fc2be9c3444f6c51fe5e0a4d4b66b3
Parents: 59735db
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Mon Jul 27 13:48:07 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Mon Jul 27 13:48:07 2015 -0400

----------------------------------------------------------------------
 .../gremlin/groovy/engine/GremlinExecutor.java  | 31 +++-----------------
 .../groovy/engine/GremlinExecutorTest.java      | 11 +++----
 .../groovy/engine/GremlinExecutorInit.groovy    |  3 +-
 .../gremlin/server/op/session/Session.java      |  2 --
 .../server/util/ServerGremlinExecutor.java      |  4 ---
 5 files changed, 9 insertions(+), 42 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/7286a909/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 bd549ea..99bf36f 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
@@ -82,7 +82,6 @@ public class GremlinExecutor implements AutoCloseable {
     private final Map<String, EngineSettings> settings;
     private final long scriptEvaluationTimeout;
     private final Bindings globalBindings;
-    private final Predicate<Map.Entry<String,Object>> promoteBinding;
     private final List<List<String>> use;
     private final ExecutorService executorService;
     private final ScheduledExecutorService scheduledExecutorService;
@@ -107,7 +106,6 @@ public class GremlinExecutor implements AutoCloseable {
         this.settings = builder.settings;
         this.scriptEvaluationTimeout = builder.scriptEvaluationTimeout;
         this.globalBindings = builder.globalBindings;
-        this.promoteBinding = builder.promoteBinding;
         this.enabledPlugins = builder.enabledPlugins;
         this.scriptEngines = createScriptEngines();
         this.suppliedExecutor = suppliedExecutor;
@@ -422,23 +420,12 @@ public class GremlinExecutor implements AutoCloseable {
                         // evaluate init scripts with hard reference so as to ensure it doesn't get garbage collected
                         bindings.put(GremlinGroovyScriptEngine.KEY_REFERENCE_TYPE, GremlinGroovyScriptEngine.REFERENCE_TYPE_HARD);
 
-                        // todo: drop the "old method" in 3.1.0
-                        // the returned object should be a Map of initialized global bindings - this is in contrast
-                        // to the older method of scanning bindings for certain types/keys to promote.  the reason
-                        // for this change has to do with CompileStatic/TypeChecked "secure" configurations that
-                        // require the defined variables to be typed.  Under that scenario they are not "unresolved"
-                        // and thus don't get set to bindings automatically by the InvokerHelper.
+                        // the returned object should be a Map of initialized global bindings
                         final Object initializedBindings = se.eval(p.getValue1(), bindings, language);
-                        if (initializedBindings != null && initializedBindings instanceof Map) {
+                        if (initializedBindings != null && initializedBindings instanceof Map)
                             globalBindings.putAll((Map) initializedBindings);
-                        } else {
-                            // re-assign graph bindings back to global bindings and grab TraversalSource creations.
-                            // prevent assignment of non-graph implementations just in case someone tries to overwrite
-                            // them in the init
-                            bindings.entrySet().stream()
-                                    .filter(promoteBinding)
-                                    .forEach(kv -> globalBindings.put(kv.getKey(), kv.getValue()));
-                        }
+                        else
+                            logger.warn("Initialization script {} did not return a Map - no global bindings specified", p.getValue0());
 
                         logger.info("Initialized {} ScriptEngine with {}", language, p.getValue0());
                     } catch (ScriptException sx) {
@@ -482,7 +469,6 @@ public class GremlinExecutor implements AutoCloseable {
         };
         private List<List<String>> use = new ArrayList<>();
         private Bindings globalBindings = new SimpleBindings();
-        private Predicate<Map.Entry<String,Object>> promoteBinding = kv -> false;
 
         private Builder() {
         }
@@ -509,15 +495,6 @@ public class GremlinExecutor implements AutoCloseable {
         }
 
         /**
-         * A predicate applied to the binding list to determine if it should be promoted to a "global" binding
-         * that should be tied to every script.
-         */
-        public Builder promoteBindings(final Predicate<Map.Entry<String,Object>> promoteBinding) {
-            this.promoteBinding = promoteBinding;
-            return this;
-        }
-
-        /**
          * Bindings to apply to every script evaluated.
          */
         public Builder globalBindings(final Bindings bindings) {

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/7286a909/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java
index 1268a3e..1c164da 100644
--- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java
@@ -412,23 +412,20 @@ public class GremlinExecutorTest {
     }
 
     @Test
-    public void shouldInitializeWithScriptAndPromoteBinding() throws Exception {
+    public void shouldInitializeWithScriptAndMakeGlobalBinding() throws Exception {
         final GremlinExecutor gremlinExecutor = GremlinExecutor.build()
                 .addEngineSettings("gremlin-groovy",
                         Collections.emptyList(),
                         Collections.emptyList(),
                         Arrays.asList(PATHS.get("GremlinExecutorInit.groovy")),
                         Collections.emptyMap())
-                .promoteBindings(kv -> kv.getValue() instanceof Set)
                 .create();
 
         assertEquals(2, gremlinExecutor.eval("add(1,1)").get());
-        assertThat(gremlinExecutor.getGlobalBindings().keySet(), contains("someSet"));
-        assertThat(gremlinExecutor.getGlobalBindings().keySet(), not(contains("someMap")));
+        assertThat(gremlinExecutor.getGlobalBindings().keySet(), not(contains("someSet")));
+        assertThat(gremlinExecutor.getGlobalBindings().keySet(), contains("name"));
 
-        final Set<String> s = (Set<String>) gremlinExecutor.getGlobalBindings().get("someSet");
-        assertThat(s, contains("test"));
-        assertEquals(1, s.size());
+        assertEquals("stephen", gremlinExecutor.getGlobalBindings().get("name"));
 
         gremlinExecutor.close();
     }

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/7286a909/gremlin-groovy/src/test/resources/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorInit.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/resources/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorInit.groovy b/gremlin-groovy/src/test/resources/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorInit.groovy
index db046b0..6f92355 100644
--- a/gremlin-groovy/src/test/resources/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorInit.groovy
+++ b/gremlin-groovy/src/test/resources/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorInit.groovy
@@ -19,5 +19,4 @@
 def add(x, y) { x + y }
 
 someSet = ["test"] as Set
-someMap = [name:"stephen"]
-""
\ No newline at end of file
+[name:"stephen"]

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/7286a909/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/session/Session.java
----------------------------------------------------------------------
diff --git a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/session/Session.java b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/session/Session.java
index 7c5bc54..0c2c1c0 100644
--- a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/session/Session.java
+++ b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/op/session/Session.java
@@ -135,8 +135,6 @@ public class Session {
                 })
                 .enabledPlugins(new HashSet<>(settings.plugins))
                 .globalBindings(graphManager.getAsBindings())
-                .promoteBindings(kv -> kv.getValue() instanceof Graph
-                        || kv.getValue() instanceof TraversalSource)
                 .executorService(executor)
                 .scheduledExecutorService(scheduledExecutorService);
 

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/7286a909/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/ServerGremlinExecutor.java
----------------------------------------------------------------------
diff --git a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/ServerGremlinExecutor.java b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/ServerGremlinExecutor.java
index 02a763a..a3a2175 100644
--- a/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/ServerGremlinExecutor.java
+++ b/gremlin-server/src/main/java/org/apache/tinkerpop/gremlin/server/util/ServerGremlinExecutor.java
@@ -95,7 +95,6 @@ public class ServerGremlinExecutor<T extends ScheduledExecutorService> {
 
         logger.info("Initialized Gremlin thread pool.  Threads in pool named with pattern gremlin-*");
 
-        // todo: deprecate promoteBindings in 3.1.0
         final GremlinExecutor.Builder gremlinExecutorBuilder = GremlinExecutor.build()
                 .scriptEvaluationTimeout(settings.scriptEvaluationTimeout)
                 .afterFailure((b, e) -> graphManager.rollbackAll())
@@ -103,9 +102,6 @@ public class ServerGremlinExecutor<T extends ScheduledExecutorService> {
                 .afterTimeout(b -> graphManager.rollbackAll())
                 .enabledPlugins(new HashSet<>(settings.plugins))
                 .globalBindings(graphManager.getAsBindings())
-                .promoteBindings(kv -> kv.getValue() instanceof Graph
-                        || kv.getValue() instanceof TraversalSource
-                        || kv.getValue() instanceof LifeCycleHook)
                 .executorService(this.gremlinExecutorService)
                 .scheduledExecutorService(this.scheduledExecutorService);
 


[2/9] incubator-tinkerpop git commit: Moved the sandbox classes into new packaging to be consistent with CompilerCustomizerProvider

Posted by sp...@apache.org.
Moved the sandbox classes into new packaging to be consistent with CompilerCustomizerProvider


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

Branch: refs/heads/master
Commit: c36266906c419007e4179d37085ac20185d6d470
Parents: 1466d21
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Fri Jul 24 11:52:34 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Fri Jul 24 11:52:34 2015 -0400

----------------------------------------------------------------------
 .../jsr223/AllowColorSandboxExtension.groovy    |   1 +
 .../BlockSomeVariablesSandboxExtension.groovy   |   1 +
 ...ebindAllVariableTypesSandboxExtension.groovy |   2 +
 .../GremlinGroovyScriptEngineOverGraphTest.java |   4 -
 ...mlinGroovyScriptEngineSandboxCustomTest.java |   3 +-
 ...GroovyScriptEngineSandboxedStandardTest.java |   5 +-
 ...nGroovyScriptEngineTinkerPopSandboxTest.java |   5 +-
 .../groovy/jsr223/SandboxExtension.groovy       | 176 ------------------
 .../jsr223/TinkerPopSandboxExtension.groovy     |  47 -----
 .../jsr223/customizer/SandboxExtension.groovy   | 177 +++++++++++++++++++
 .../customizer/TinkerPopSandboxExtension.groovy |  47 +++++
 11 files changed, 235 insertions(+), 233 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c3626690/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/AllowColorSandboxExtension.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/AllowColorSandboxExtension.groovy b/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/AllowColorSandboxExtension.groovy
index 7a1911f..a00fb26 100644
--- a/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/AllowColorSandboxExtension.groovy
+++ b/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/AllowColorSandboxExtension.groovy
@@ -18,6 +18,7 @@
  */
 package org.apache.tinkerpop.gremlin.groovy.jsr223
 
+import org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.SandboxExtension
 import org.codehaus.groovy.ast.MethodNode
 
 import java.util.function.BiPredicate

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c3626690/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/BlockSomeVariablesSandboxExtension.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/BlockSomeVariablesSandboxExtension.groovy b/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/BlockSomeVariablesSandboxExtension.groovy
index fcbbc51..e5d9a3b 100644
--- a/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/BlockSomeVariablesSandboxExtension.groovy
+++ b/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/BlockSomeVariablesSandboxExtension.groovy
@@ -18,6 +18,7 @@
  */
 package org.apache.tinkerpop.gremlin.groovy.jsr223
 
+import org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.SandboxExtension
 import org.apache.tinkerpop.gremlin.structure.Graph
 import org.codehaus.groovy.ast.ClassNode
 import org.codehaus.groovy.ast.expr.VariableExpression

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c3626690/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/RebindAllVariableTypesSandboxExtension.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/RebindAllVariableTypesSandboxExtension.groovy b/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/RebindAllVariableTypesSandboxExtension.groovy
index c475016..d0d20ba 100644
--- a/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/RebindAllVariableTypesSandboxExtension.groovy
+++ b/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/RebindAllVariableTypesSandboxExtension.groovy
@@ -18,6 +18,8 @@
  */
 package org.apache.tinkerpop.gremlin.groovy.jsr223
 
+import org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.SandboxExtension
+
 /**
  * @author Stephen Mallette (http://stephen.genoprime.com)
  */

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c3626690/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineOverGraphTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineOverGraphTest.java b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineOverGraphTest.java
index a8899c7..7de26db 100644
--- a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineOverGraphTest.java
+++ b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineOverGraphTest.java
@@ -23,15 +23,12 @@ import org.apache.commons.lang.exception.ExceptionUtils;
 import org.apache.tinkerpop.gremlin.AbstractGremlinTest;
 import org.apache.tinkerpop.gremlin.FeatureRequirementSet;
 import org.apache.tinkerpop.gremlin.LoadGraphWith;
-import org.apache.tinkerpop.gremlin.groovy.CompileStaticCustomizerProvider;
 import org.apache.tinkerpop.gremlin.groovy.DefaultImportCustomizerProvider;
 import org.apache.tinkerpop.gremlin.groovy.NoImportCustomizerProvider;
-import org.apache.tinkerpop.gremlin.groovy.TypeCheckedCustomizerProvider;
 import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
 import org.apache.tinkerpop.gremlin.structure.Direction;
 import org.apache.tinkerpop.gremlin.structure.Vertex;
 import org.apache.tinkerpop.gremlin.util.config.YamlConfiguration;
-import org.codehaus.groovy.control.MultipleCompilationErrorsException;
 import org.junit.Assert;
 import org.junit.Test;
 
@@ -46,7 +43,6 @@ import java.util.List;
 import java.util.Random;
 import java.util.concurrent.CountDownLatch;
 
-import static org.hamcrest.Matchers.containsString;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c3626690/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineSandboxCustomTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineSandboxCustomTest.java b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineSandboxCustomTest.java
index 648e688..f4e35ba 100644
--- a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineSandboxCustomTest.java
+++ b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineSandboxCustomTest.java
@@ -20,8 +20,9 @@ package org.apache.tinkerpop.gremlin.groovy.jsr223;
 
 import org.apache.tinkerpop.gremlin.AbstractGremlinTest;
 import org.apache.tinkerpop.gremlin.LoadGraphWith;
-import org.apache.tinkerpop.gremlin.groovy.CompileStaticCustomizerProvider;
 import org.apache.tinkerpop.gremlin.groovy.CompilerCustomizerProvider;
+import org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.CompileStaticCustomizerProvider;
+import org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.SandboxExtension;
 import org.codehaus.groovy.control.MultipleCompilationErrorsException;
 import org.junit.Test;
 

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c3626690/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineSandboxedStandardTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineSandboxedStandardTest.java b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineSandboxedStandardTest.java
index 7f55e78..be8ec00 100644
--- a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineSandboxedStandardTest.java
+++ b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineSandboxedStandardTest.java
@@ -20,9 +20,10 @@ package org.apache.tinkerpop.gremlin.groovy.jsr223;
 
 import org.apache.tinkerpop.gremlin.AbstractGremlinTest;
 import org.apache.tinkerpop.gremlin.LoadGraphWith;
-import org.apache.tinkerpop.gremlin.groovy.CompileStaticCustomizerProvider;
 import org.apache.tinkerpop.gremlin.groovy.CompilerCustomizerProvider;
-import org.apache.tinkerpop.gremlin.groovy.TypeCheckedCustomizerProvider;
+import org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.CompileStaticCustomizerProvider;
+import org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.SandboxExtension;
+import org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.TypeCheckedCustomizerProvider;
 import org.codehaus.groovy.control.MultipleCompilationErrorsException;
 import org.junit.Test;
 import org.junit.runner.RunWith;

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c3626690/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTinkerPopSandboxTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTinkerPopSandboxTest.java b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTinkerPopSandboxTest.java
index 5299857..34fbd38 100644
--- a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTinkerPopSandboxTest.java
+++ b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTinkerPopSandboxTest.java
@@ -20,14 +20,13 @@ package org.apache.tinkerpop.gremlin.groovy.jsr223;
 
 import org.apache.tinkerpop.gremlin.AbstractGremlinTest;
 import org.apache.tinkerpop.gremlin.LoadGraphWith;
-import org.apache.tinkerpop.gremlin.groovy.CompileStaticCustomizerProvider;
 import org.apache.tinkerpop.gremlin.groovy.CompilerCustomizerProvider;
-import org.apache.tinkerpop.gremlin.structure.Graph;
+import org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.CompileStaticCustomizerProvider;
+import org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.TinkerPopSandboxExtension;
 import org.codehaus.groovy.control.MultipleCompilationErrorsException;
 import org.junit.Test;
 
 import javax.script.Bindings;
-import javax.script.SimpleBindings;
 
 import static org.hamcrest.Matchers.containsString;
 import static org.junit.Assert.assertEquals;

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c3626690/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/SandboxExtension.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/SandboxExtension.groovy b/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/SandboxExtension.groovy
deleted file mode 100644
index 505f921..0000000
--- a/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/SandboxExtension.groovy
+++ /dev/null
@@ -1,176 +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.tinkerpop.gremlin.groovy.jsr223
-
-import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource
-import org.apache.tinkerpop.gremlin.structure.Graph
-import org.codehaus.groovy.ast.ClassCodeVisitorSupport
-import org.codehaus.groovy.ast.ClassHelper
-import org.codehaus.groovy.ast.ClassNode
-import org.codehaus.groovy.ast.MethodNode
-import org.codehaus.groovy.ast.Parameter
-import org.codehaus.groovy.ast.expr.PropertyExpression
-import org.codehaus.groovy.ast.expr.VariableExpression
-import org.codehaus.groovy.control.SourceUnit
-import org.codehaus.groovy.transform.sc.StaticCompilationMetadataKeys
-import org.codehaus.groovy.transform.stc.ExtensionMethodNode
-import org.codehaus.groovy.transform.stc.GroovyTypeCheckingExtensionSupport
-import org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport
-
-import java.util.function.BiPredicate
-
-/**
- * A sandbox for the {@link GremlinGroovyScriptEngine} that provides base functionality for securing evaluated scripts.
- * By default, this implementation ensures that the variable "graph" is always a {@link Graph} instance and the
- * variable "g" is always a {@link GraphTraversalSource}.
- * <p/>
- * Users should extend this class to modify features as it has some helper methods to make developing a
- * sandbox extension a bit easier than starting from a base groovy type checking extension.
- *
- * @author Stephen Mallette (http://stephen.genoprime.com)
- */
-class SandboxExtension extends GroovyTypeCheckingExtensionSupport.TypeCheckingDSL {
-
-    /**
-     * When assigned to the {@code #variableFilter} all variables are allowed.
-     */
-    public static final BiPredicate<VariableExpression, Map<String,ClassNode>> VARIABLES_ALLOW_ALL = { var, types -> true }
-
-    /**
-     * When assigned to the {@code methodFilter} all methods are allowed.
-     */
-    public static final BiPredicate<String, MethodNode> METHODS_ALLOW_ALL = { exp, method -> true }
-
-    /**
-     * Forces any variable named "graph" to be of type {@link Graph}.
-     */
-    protected boolean graphIsAlwaysGraphInstance = true
-
-    /**
-     * Forces any variable named "g" to be of type {@link GraphTraversalSource}.
-     */
-    protected boolean gIsAlwaysGraphTraversalSource = true
-
-    protected BiPredicate<VariableExpression, Map<String,ClassNode>> variableFilter = VARIABLES_ALLOW_ALL
-    protected BiPredicate<String, MethodNode> methodFilter = METHODS_ALLOW_ALL
-
-    @Override
-    Object run() {
-        unresolvedVariable { var ->
-            if (var.name == "graph" && graphIsAlwaysGraphInstance) {
-                storeType(var, classNodeFor(Graph))
-                handled = true
-                return
-            }
-
-            if (var.name == "g" && gIsAlwaysGraphTraversalSource) {
-                storeType(var, classNodeFor(GraphTraversalSource))
-                handled = true
-                return
-            }
-
-            final Map<String,ClassNode> varTypes = (Map<String,ClassNode>) GremlinGroovyScriptEngine.COMPILE_OPTIONS.get()
-                    .get(GremlinGroovyScriptEngine.COMPILE_OPTIONS_VAR_TYPES)
-
-            // use the types of the bound variables.  filter as necessary and provide special treatment for
-            // "g" and "graph" as they are potentially handled above already and don't need to be bound
-            // implicitly by the binding variables
-            if (varTypes.containsKey(var.name) && variableFilter.test(var, varTypes))  {
-                if (!(var.name in ["graph",  "g"]) || (var.name == "graph" && !graphIsAlwaysGraphInstance
-                         || var.name == "g" && !gIsAlwaysGraphTraversalSource)) {
-                    storeType(var, varTypes.get(var.name))
-                    handled = true
-                    return
-                }
-            }
-        }
-
-        onMethodSelection { expr, MethodNode methodNode ->
-            def descriptor = toMethodDescriptor(methodNode)
-            if (!methodFilter.test(descriptor,methodNode))
-                addStaticTypeError("Not authorized to call this method: $descriptor", expr)
-        }
-
-        // handles calls to properties
-        afterVisitMethod { methodNode ->
-            def visitor = new PropertyExpressionEvaluator(context.source)
-            visitor.visitMethod(methodNode)
-        }
-    }
-
-    /**
-     * Helper method for those extending the sandbox and useful in turning methods into regex matchable strings.
-     */
-    static String toMethodDescriptor(final MethodNode node) {
-        if (node instanceof ExtensionMethodNode)
-            return toMethodDescriptor(node.extensionMethodNode)
-
-        def sb = new StringBuilder()
-        sb.append(node.declaringClass.toString(false))
-        sb.append("#")
-        sb.append(node.name)
-        sb.append('(')
-        sb.append(node.parameters.collect { Parameter it ->
-            prettyPrint(it.originType)
-        }.join(','))
-        sb.append(')')
-        sb
-    }
-
-    /**
-     * Helper method for those extending the sandbox.
-     */
-    static String prettyPrint(final ClassNode node) {
-        node.isArray() ? "${prettyPrint(node.componentType)}[]" : node.toString(false)
-    }
-
-    /**
-     * Evaluates methods selected by groovy property magic (i.e. {@code Person.name} -> {@code Person.getName()})
-     */
-    class PropertyExpressionEvaluator extends ClassCodeVisitorSupport {
-        private final SourceUnit unit
-        private final List<String> whiteList
-
-        public PropertyExpressionEvaluator(final SourceUnit unit) {
-            this.unit = unit
-            this.whiteList = whiteList
-        }
-
-        @Override
-        protected SourceUnit getSourceUnit() {
-            unit
-        }
-
-        @Override
-        void visitPropertyExpression(final PropertyExpression expression) {
-            super.visitPropertyExpression(expression)
-
-            ClassNode owner = expression.objectExpression.getNodeMetaData(StaticCompilationMetadataKeys.PROPERTY_OWNER)
-            if (owner) {
-                if (expression.spreadSafe && StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(owner, classNodeFor(Collection)))
-                    owner = typeCheckingVisitor.inferComponentType(owner, ClassHelper.int_TYPE)
-
-                def descriptor = "${prettyPrint(owner)}#${expression.propertyAsString}"
-                if (!methodFilter.test(descriptor, expression))
-                    addStaticTypeError("Not authorized to call this method: $descriptor", expression)
-            }
-        }
-    }
-
-}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c3626690/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/TinkerPopSandboxExtension.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/TinkerPopSandboxExtension.groovy b/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/TinkerPopSandboxExtension.groovy
deleted file mode 100644
index 940101c..0000000
--- a/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/TinkerPopSandboxExtension.groovy
+++ /dev/null
@@ -1,47 +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.tinkerpop.gremlin.groovy.jsr223
-
-import org.codehaus.groovy.ast.MethodNode
-
-import java.util.function.BiPredicate
-
-/**
- * @author Stephen Mallette (http://stephen.genoprime.com)
- */
-class TinkerPopSandboxExtension extends SandboxExtension {
-
-    TinkerPopSandboxExtension() {
-        gIsAlwaysGraphTraversalSource = false
-        graphIsAlwaysGraphInstance = false
-
-        def gremlinRoot = "org\\.apache\\.tinkerpop\\.gremlin"
-        def gremlinRootStructure = "$gremlinRoot\\.structure"
-        def gremlinRootProcess = "$gremlinRoot\\.process"
-        def methodWhiteList = ["java\\.util\\..*",
-                               "org\\.codehaus\\.groovy\\.runtime\\.DefaultGroovyMethods",
-                               "$gremlinRootStructure\\..*",
-                               "$gremlinRootProcess\\..*",
-                               "$gremlinRootProcess\\.traversal\\.dsl\\.graph\\..*"]
-
-        methodFilter = (BiPredicate<String, MethodNode>) { descriptor, method ->
-            methodWhiteList.any { descriptor =~ it }
-        }
-    }
-}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c3626690/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/SandboxExtension.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/SandboxExtension.groovy b/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/SandboxExtension.groovy
new file mode 100644
index 0000000..14fcd99
--- /dev/null
+++ b/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/SandboxExtension.groovy
@@ -0,0 +1,177 @@
+/*
+ * 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.groovy.jsr223.customizer
+
+import org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine
+import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource
+import org.apache.tinkerpop.gremlin.structure.Graph
+import org.codehaus.groovy.ast.ClassCodeVisitorSupport
+import org.codehaus.groovy.ast.ClassHelper
+import org.codehaus.groovy.ast.ClassNode
+import org.codehaus.groovy.ast.MethodNode
+import org.codehaus.groovy.ast.Parameter
+import org.codehaus.groovy.ast.expr.PropertyExpression
+import org.codehaus.groovy.ast.expr.VariableExpression
+import org.codehaus.groovy.control.SourceUnit
+import org.codehaus.groovy.transform.sc.StaticCompilationMetadataKeys
+import org.codehaus.groovy.transform.stc.ExtensionMethodNode
+import org.codehaus.groovy.transform.stc.GroovyTypeCheckingExtensionSupport
+import org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport
+
+import java.util.function.BiPredicate
+
+/**
+ * A sandbox for the {@link GremlinGroovyScriptEngine} that provides base functionality for securing evaluated scripts.
+ * By default, this implementation ensures that the variable "graph" is always a {@link Graph} instance and the
+ * variable "g" is always a {@link GraphTraversalSource}.
+ * <p/>
+ * Users should extend this class to modify features as it has some helper methods to make developing a
+ * sandbox extension a bit easier than starting from a base groovy type checking extension.
+ *
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+class SandboxExtension extends GroovyTypeCheckingExtensionSupport.TypeCheckingDSL {
+
+    /**
+     * When assigned to the {@code #variableFilter} all variables are allowed.
+     */
+    public static final BiPredicate<VariableExpression, Map<String,ClassNode>> VARIABLES_ALLOW_ALL = { var, types -> true }
+
+    /**
+     * When assigned to the {@code methodFilter} all methods are allowed.
+     */
+    public static final BiPredicate<String, MethodNode> METHODS_ALLOW_ALL = { exp, method -> true }
+
+    /**
+     * Forces any variable named "graph" to be of type {@link Graph}.
+     */
+    protected boolean graphIsAlwaysGraphInstance = true
+
+    /**
+     * Forces any variable named "g" to be of type {@link GraphTraversalSource}.
+     */
+    protected boolean gIsAlwaysGraphTraversalSource = true
+
+    protected BiPredicate<VariableExpression, Map<String,ClassNode>> variableFilter = VARIABLES_ALLOW_ALL
+    protected BiPredicate<String, MethodNode> methodFilter = METHODS_ALLOW_ALL
+
+    @Override
+    Object run() {
+        unresolvedVariable { var ->
+            if (var.name == "graph" && graphIsAlwaysGraphInstance) {
+                storeType(var, classNodeFor(Graph))
+                handled = true
+                return
+            }
+
+            if (var.name == "g" && gIsAlwaysGraphTraversalSource) {
+                storeType(var, classNodeFor(GraphTraversalSource))
+                handled = true
+                return
+            }
+
+            final Map<String,ClassNode> varTypes = (Map<String,ClassNode>) GremlinGroovyScriptEngine.COMPILE_OPTIONS.get()
+                    .get(GremlinGroovyScriptEngine.COMPILE_OPTIONS_VAR_TYPES)
+
+            // use the types of the bound variables.  filter as necessary and provide special treatment for
+            // "g" and "graph" as they are potentially handled above already and don't need to be bound
+            // implicitly by the binding variables
+            if (varTypes.containsKey(var.name) && variableFilter.test(var, varTypes))  {
+                if (!(var.name in ["graph",  "g"]) || (var.name == "graph" && !graphIsAlwaysGraphInstance
+                         || var.name == "g" && !gIsAlwaysGraphTraversalSource)) {
+                    storeType(var, varTypes.get(var.name))
+                    handled = true
+                    return
+                }
+            }
+        }
+
+        onMethodSelection { expr, MethodNode methodNode ->
+            def descriptor = toMethodDescriptor(methodNode)
+            if (!methodFilter.test(descriptor,methodNode))
+                addStaticTypeError("Not authorized to call this method: $descriptor", expr)
+        }
+
+        // handles calls to properties
+        afterVisitMethod { methodNode ->
+            def visitor = new PropertyExpressionEvaluator(context.source)
+            visitor.visitMethod(methodNode)
+        }
+    }
+
+    /**
+     * Helper method for those extending the sandbox and useful in turning methods into regex matchable strings.
+     */
+    static String toMethodDescriptor(final MethodNode node) {
+        if (node instanceof ExtensionMethodNode)
+            return toMethodDescriptor(node.extensionMethodNode)
+
+        def sb = new StringBuilder()
+        sb.append(node.declaringClass.toString(false))
+        sb.append("#")
+        sb.append(node.name)
+        sb.append('(')
+        sb.append(node.parameters.collect { Parameter it ->
+            prettyPrint(it.originType)
+        }.join(','))
+        sb.append(')')
+        sb
+    }
+
+    /**
+     * Helper method for those extending the sandbox.
+     */
+    static String prettyPrint(final ClassNode node) {
+        node.isArray() ? "${prettyPrint(node.componentType)}[]" : node.toString(false)
+    }
+
+    /**
+     * Evaluates methods selected by groovy property magic (i.e. {@code Person.name} -> {@code Person.getName()})
+     */
+    class PropertyExpressionEvaluator extends ClassCodeVisitorSupport {
+        private final SourceUnit unit
+        private final List<String> whiteList
+
+        public PropertyExpressionEvaluator(final SourceUnit unit) {
+            this.unit = unit
+            this.whiteList = whiteList
+        }
+
+        @Override
+        protected SourceUnit getSourceUnit() {
+            unit
+        }
+
+        @Override
+        void visitPropertyExpression(final PropertyExpression expression) {
+            super.visitPropertyExpression(expression)
+
+            ClassNode owner = expression.objectExpression.getNodeMetaData(StaticCompilationMetadataKeys.PROPERTY_OWNER)
+            if (owner) {
+                if (expression.spreadSafe && StaticTypeCheckingSupport.implementsInterfaceOrIsSubclassOf(owner, classNodeFor(Collection)))
+                    owner = typeCheckingVisitor.inferComponentType(owner, ClassHelper.int_TYPE)
+
+                def descriptor = "${prettyPrint(owner)}#${expression.propertyAsString}"
+                if (!methodFilter.test(descriptor, expression))
+                    addStaticTypeError("Not authorized to call this method: $descriptor", expression)
+            }
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c3626690/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/TinkerPopSandboxExtension.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/TinkerPopSandboxExtension.groovy b/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/TinkerPopSandboxExtension.groovy
new file mode 100644
index 0000000..2af7584
--- /dev/null
+++ b/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/customizer/TinkerPopSandboxExtension.groovy
@@ -0,0 +1,47 @@
+/*
+ * 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.groovy.jsr223.customizer
+
+import org.codehaus.groovy.ast.MethodNode
+
+import java.util.function.BiPredicate
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+class TinkerPopSandboxExtension extends SandboxExtension {
+
+    TinkerPopSandboxExtension() {
+        gIsAlwaysGraphTraversalSource = false
+        graphIsAlwaysGraphInstance = false
+
+        def gremlinRoot = "org\\.apache\\.tinkerpop\\.gremlin"
+        def gremlinRootStructure = "$gremlinRoot\\.structure"
+        def gremlinRootProcess = "$gremlinRoot\\.process"
+        def methodWhiteList = ["java\\.util\\..*",
+                               "org\\.codehaus\\.groovy\\.runtime\\.DefaultGroovyMethods",
+                               "$gremlinRootStructure\\..*",
+                               "$gremlinRootProcess\\..*",
+                               "$gremlinRootProcess\\.traversal\\.dsl\\.graph\\..*"]
+
+        methodFilter = (BiPredicate<String, MethodNode>) { descriptor, method ->
+            methodWhiteList.any { descriptor =~ it }
+        }
+    }
+}


[7/9] incubator-tinkerpop git commit: Update docs for new method of handling global bindings in init scripts.

Posted by sp...@apache.org.
Update docs for new method of handling global bindings in init scripts.


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

Branch: refs/heads/master
Commit: a05a4443a7198cf6293c553f55ec7d0310798b2f
Parents: 97a9d00
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Mon Jul 27 13:08:16 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Mon Jul 27 13:08:16 2015 -0400

----------------------------------------------------------------------
 docs/src/gremlin-applications.asciidoc        | 4 +++-
 gremlin-server/scripts/generate-modern.groovy | 8 ++++----
 2 files changed, 7 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/a05a4443/docs/src/gremlin-applications.asciidoc
----------------------------------------------------------------------
diff --git a/docs/src/gremlin-applications.asciidoc b/docs/src/gremlin-applications.asciidoc
index c4de1b0..c4ea18e 100644
--- a/docs/src/gremlin-applications.asciidoc
+++ b/docs/src/gremlin-applications.asciidoc
@@ -245,7 +245,9 @@ The log messages that printed above show a number of things, but most importantl
 include::{basedir}/gremlin-server/scripts/generate-modern.groovy[]
 ----
 
-There are two important aspects to the above script.  First, it defines a `LifeCycleHook` for Gremlin Server.  The "hook" provides a way to tie script code into the Gremlin Server startup and shutdown sequences.  The `LifeCycleHook` has two methods that can be implemented `onStartUp` and `onShutDown`.  These events are called once at Gremlin Server start and once at Gremlin Server stop.  Code outside of the "hook" is executed for each `ScriptEngine` creation (multiple may be created when "sessions" are enabled). In this case, the startup hook loads the "modern" graph into the empty TinkerGraph instance, preparing it for use.  Outside of the "hook", the script then creates a `TraversalSource` variable `g` from `graph`. This variable `g` will be made available on future remote script executions. Note that `Graph` and `TraversalSource` objects that are created or modified in the initialization script will become globally bound to the server. It is not possible to bind variables of othe
 r types. Any functions that are defined will be cached for future use.
+The script above initializes a `Map` and assigns two key/values to it.  The first, assigned to "hook", defines a `LifeCycleHook` for Gremlin Server.  The "hook" provides a way to tie script code into the Gremlin Server startup and shutdown sequences.  The `LifeCycleHook` has two methods that can be implemented: `onStartUp` and `onShutDown`.  These events are called once at Gremlin Server start and once at Gremlin Server stop.  This is an important point because code outside of the "hook" is executed for each `ScriptEngine` creation (multiple may be created when "sessions" are enabled) and therefore the `LifeCycleHook` provides a way to ensure that a script is only executed a single time. In this case, the startup hook loads the "modern" graph into the empty TinkerGraph instance, preparing it for use.  The second key/value pair assigned to the `Map`, named "g", defines a `TraversalSource` from the `Graph` bound to the "graph" variable in the YAML configuration file.  This variable `g
 `, as well as any other variable assigned to the `Map`, will be made available as variables for future remote script executions.  In more general terms, any key/value pairs assigned to a `Map` returned from the init script will become variables that are global to all requests. In addition, any functions that are defined will be cached for future use.
+
+IMPORTANT: The approach to global bindings described above was introduced with 3.0.1.  In 3.0.0, initialization scripts that simply created `Graph` and/or `TraversalSource` objects became globally bound to the server and it was not possible to bind variables of other types. This less flexible feature is expected to be removed from Gremlin Server in 3.1.0 but remains present in the 3.0.x line of releases for backward compatibility with existing scripts.
 
 With Gremlin Server running it is now possible to issue some scripts to it for processing.  Start Gremlin Console as follows:
 

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/a05a4443/gremlin-server/scripts/generate-modern.groovy
----------------------------------------------------------------------
diff --git a/gremlin-server/scripts/generate-modern.groovy b/gremlin-server/scripts/generate-modern.groovy
index d0f5489..8f39860 100644
--- a/gremlin-server/scripts/generate-modern.groovy
+++ b/gremlin-server/scripts/generate-modern.groovy
@@ -23,10 +23,10 @@ def globals = [:]
 // Generates the modern graph into an "empty" TinkerGraph via LifeCycleHook.
 // Note that the name of the key in the "global" map is unimportant.
 globals << [hook : [
-        onStartUp: { ctx ->
-          ctx.logger.info("Loading 'modern' graph data.")
-          TinkerFactory.generateClassic(graph)
-        }
+  onStartUp: { ctx ->
+    ctx.logger.info("Loading 'modern' graph data.")
+    TinkerFactory.generateClassic(graph)
+  }
 ] as LifeCycleHook]
 
 // define the default TraversalSource to bind queries to - this one will be named "g".


[6/9] incubator-tinkerpop git commit: Update all the sample init scripts to return Map.

Posted by sp...@apache.org.
Update all the sample init scripts to return Map.

Returning Map represents the revised model for promoting bindings.


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

Branch: refs/heads/master
Commit: 97a9d00a88d82f72d64e9d7e42df0b2d33a1e5df
Parents: 8b48e41
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Mon Jul 27 12:50:59 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Mon Jul 27 12:50:59 2015 -0400

----------------------------------------------------------------------
 gremlin-server/scripts/empty-sample.groovy      | 24 +++++++++---------
 gremlin-server/scripts/generate-classic.groovy  | 18 +++++++-------
 .../scripts/generate-modern-readonly.groovy     | 26 ++++++++++----------
 gremlin-server/scripts/generate-modern.groovy   | 26 ++++++++++----------
 gremlin-server/scripts/load-sample.groovy       | 26 ++++++++++----------
 ...-shouldRebindTraversalSourceVariables.groovy |  8 +++---
 6 files changed, 64 insertions(+), 64 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/97a9d00a/gremlin-server/scripts/empty-sample.groovy
----------------------------------------------------------------------
diff --git a/gremlin-server/scripts/empty-sample.groovy b/gremlin-server/scripts/empty-sample.groovy
index 7e3b905..3465577 100644
--- a/gremlin-server/scripts/empty-sample.groovy
+++ b/gremlin-server/scripts/empty-sample.groovy
@@ -17,22 +17,24 @@
  * under the License.
  */
 
+// An example of an initialization script that can be configured to run in Gremlin Server.
+// Functions defined here will go into global cache and will not be removed from there
+// unless there is a reset of the ScriptEngine.
+def addItUp(x, y) { x + y }
+
+// an init script that returns a Map allows explicit setting of global bindings.
+def globals = [:]
+
 // defines a sample LifeCycleHook that prints some output to the Gremlin Server console.
-// it is important that the hook be assigned to a variable (in this case "hook").
-// the exact name of this variable is unimportant.
-hook = [
+// note that the name of the key in the "global" map is unimportant.
+globals << [hook : [
   onStartUp: { ctx ->
     ctx.logger.info("Executed once at startup of Gremlin Server.")
   },
   onShutDown: { ctx ->
     ctx.logger.info("Executed once at shutdown of Gremlin Server.")
   }
-] as LifeCycleHook
+] as LifeCycleHook]
 
-// define the default TraversalSource to bind queries to.
-g = graph.traversal()
-
-// An example of an initialization script that can be configured to run in Gremlin Server.
-// Functions defined here will go into global cache and will not be removed from there
-// unless there is a reset of the ScriptEngine.
-def addItUp(x, y) { x + y }
\ No newline at end of file
+// define the default TraversalSource to bind queries to - this one will be named "g".
+globals << [g : graph.traversal()]
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/97a9d00a/gremlin-server/scripts/generate-classic.groovy
----------------------------------------------------------------------
diff --git a/gremlin-server/scripts/generate-classic.groovy b/gremlin-server/scripts/generate-classic.groovy
index e6d3fb1..4c73e74 100644
--- a/gremlin-server/scripts/generate-classic.groovy
+++ b/gremlin-server/scripts/generate-classic.groovy
@@ -17,17 +17,17 @@
  * under the License.
  */
 
-// Generates the classic graph into an "empty" TinkerGraph via LifeCycleHook
-// it is important that the hook be assigned to a variable (in this case "hook").
-// the exact name of this variable is unimportant.
-hook = [
+// an init script that returns a Map allows explicit setting of global bindings.
+def globals = [:]
+
+// Generates the classic graph into an "empty" TinkerGraph via LifeCycleHook.
+// Note that the name of the key in the "global" map is unimportant.
+globals << [hook : [
   onStartUp: { ctx ->
     ctx.logger.info("Loading 'classic' graph data.")
     TinkerFactory.generateClassic(graph)
   }
-] as LifeCycleHook
+] as LifeCycleHook]
 
-// Define the default TraversalSource to bind queries to. Code outside of the "hook"
-// will execute for each instantiated ScriptEngine instance. Use this part of the
-// script to initialize functions that are meant to be re-usable.
-g = graph.traversal()
\ No newline at end of file
+// define the default TraversalSource to bind queries to - this one will be named "g".
+globals << [g : graph.traversal()]
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/97a9d00a/gremlin-server/scripts/generate-modern-readonly.groovy
----------------------------------------------------------------------
diff --git a/gremlin-server/scripts/generate-modern-readonly.groovy b/gremlin-server/scripts/generate-modern-readonly.groovy
index 6e4dc89..1cef3aa 100644
--- a/gremlin-server/scripts/generate-modern-readonly.groovy
+++ b/gremlin-server/scripts/generate-modern-readonly.groovy
@@ -17,17 +17,17 @@
  * under the License.
  */
 
-// Generates the modern graph into an "empty" TinkerGraph via LifeCycleHook
-// it is important that the hook be assigned to a variable (in this case "hook").
-// the exact name of this variable is unimportant.
-hook = [
-  onStartUp: { ctx ->
-    ctx.logger.info("Loading 'modern' graph data.")
-    TinkerFactory.generateModern(graph)
-  }
-] as LifeCycleHook
+// an init script that returns a Map allows explicit setting of global bindings.
+def globals = [:]
 
-// Define the default TraversalSource to bind queries to. Code outside of the "hook"
-// will execute for each instantiated ScriptEngine instance. Use this part of the
-// script to initialize functions that are meant to be re-usable.
-g = graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance()))
\ No newline at end of file
+// Generates the modern graph into an "empty" TinkerGraph via LifeCycleHook.
+// Note that the name of the key in the "global" map is unimportant.
+globals << [hook : [
+        onStartUp: { ctx ->
+          ctx.logger.info("Loading 'modern' graph data.")
+          TinkerFactory.generateClassic(graph)
+        }
+] as LifeCycleHook]
+
+// define the default TraversalSource to bind queries to - this one will be named "g".
+globals << [g : graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance()))]

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/97a9d00a/gremlin-server/scripts/generate-modern.groovy
----------------------------------------------------------------------
diff --git a/gremlin-server/scripts/generate-modern.groovy b/gremlin-server/scripts/generate-modern.groovy
index 2d32848..d0f5489 100644
--- a/gremlin-server/scripts/generate-modern.groovy
+++ b/gremlin-server/scripts/generate-modern.groovy
@@ -17,17 +17,17 @@
  * under the License.
  */
 
-// Generates the modern graph into an "empty" TinkerGraph via LifeCycleHook
-// it is important that the hook be assigned to a variable (in this case "hook").
-// the exact name of this variable is unimportant.
-hook = [
-  onStartUp: { ctx ->
-    ctx.logger.info("Loading 'modern' graph data.")
-    TinkerFactory.generateModern(graph)
-  }
-] as LifeCycleHook
+// an init script that returns a Map allows explicit setting of global bindings.
+def globals = [:]
 
-// Define the default TraversalSource to bind queries to. Code outside of the "hook"
-// will execute for each instantiated ScriptEngine instance. Use this part of the
-// script to initialize functions that are meant to be re-usable.
-g = graph.traversal()
\ No newline at end of file
+// Generates the modern graph into an "empty" TinkerGraph via LifeCycleHook.
+// Note that the name of the key in the "global" map is unimportant.
+globals << [hook : [
+        onStartUp: { ctx ->
+          ctx.logger.info("Loading 'modern' graph data.")
+          TinkerFactory.generateClassic(graph)
+        }
+] as LifeCycleHook]
+
+// define the default TraversalSource to bind queries to - this one will be named "g".
+globals << [g : graph.traversal()]
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/97a9d00a/gremlin-server/scripts/load-sample.groovy
----------------------------------------------------------------------
diff --git a/gremlin-server/scripts/load-sample.groovy b/gremlin-server/scripts/load-sample.groovy
index da36168..81241a0 100644
--- a/gremlin-server/scripts/load-sample.groovy
+++ b/gremlin-server/scripts/load-sample.groovy
@@ -17,23 +17,23 @@
  * under the License.
  */
 
-// Generates the modern graph into an "empty" TinkerGraph via LifeCycleHook
-// it is important that the hook be assigned to a variable (in this case "hook").
-// the exact name of this variable is unimportant.
-hook = [
+// Functions defined here will go into global cache and will not be removed from there
+// unless there is a reset of the ScriptEngine.
+def addItUp(x, y) { x + y }
+
+// an init script that returns a Map allows explicit setting of global bindings.
+def globals = [:]
+
+// defines a sample LifeCycleHook that prints some output to the Gremlin Server console.
+// note that the name of the key in the "global" map is unimportant.
+globals << [hook : [
   onStartUp: { ctx ->
     ctx.logger.info("Loading graph data from data/sample.kryo.")
 
     // An example of an initialization script that can be configured to run in Gremlin Server.
     graph.io(GryoIo.build()).readGraph('data/sample.kryo')
   }
-] as LifeCycleHook
+] as LifeCycleHook]
 
-// define the default TraversalSource to bind queries to. Code outside of the "hook"
-// will execute for each instantiated ScriptEngine instance. Use this part of the
-// script to initialize functions that are meant to be re-usable.
-g = graph.traversal()
-
-// Functions defined here will go into global cache and will not be removed from there
-// unless there is a reset of the ScriptEngine.
-def addItUp(x, y) { x + y }
\ No newline at end of file
+// define the default TraversalSource to bind queries to - this one will be named "g".
+globals << [g : graph.traversal()]
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/97a9d00a/gremlin-server/src/test/resources/org/apache/tinkerpop/gremlin/server/generate-shouldRebindTraversalSourceVariables.groovy
----------------------------------------------------------------------
diff --git a/gremlin-server/src/test/resources/org/apache/tinkerpop/gremlin/server/generate-shouldRebindTraversalSourceVariables.groovy b/gremlin-server/src/test/resources/org/apache/tinkerpop/gremlin/server/generate-shouldRebindTraversalSourceVariables.groovy
index 668c440..909cd69 100644
--- a/gremlin-server/src/test/resources/org/apache/tinkerpop/gremlin/server/generate-shouldRebindTraversalSourceVariables.groovy
+++ b/gremlin-server/src/test/resources/org/apache/tinkerpop/gremlin/server/generate-shouldRebindTraversalSourceVariables.groovy
@@ -17,8 +17,6 @@
  * under the License.
  */
 
-// Define the default TraversalSource to bind queries to. Code outside of the "hook"
-// will execute for each instantiated ScriptEngine instance. Use this part of the
-// script to initialize functions that are meant to be re-usable.
-g = graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance()))
-g1 = graph.traversal()
\ No newline at end of file
+def globals = [:]
+globals << [g : graph.traversal(GraphTraversalSource.build().with(ReadOnlyStrategy.instance()))]
+globals << [g1 : graph.traversal()]
\ No newline at end of file


[4/9] incubator-tinkerpop git commit: Docs on Script Execution security in Gremlin Server.

Posted by sp...@apache.org.
Docs on Script Execution security in Gremlin Server.


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

Branch: refs/heads/master
Commit: f0ddc3622c8efe65573b44764c51102eb72fa906
Parents: 0c0aa35
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Fri Jul 24 12:55:45 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Fri Jul 24 12:55:45 2015 -0400

----------------------------------------------------------------------
 docs/src/gremlin-applications.asciidoc | 85 ++++++++++++++++++++++++++++-
 1 file changed, 83 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/f0ddc362/docs/src/gremlin-applications.asciidoc
----------------------------------------------------------------------
diff --git a/docs/src/gremlin-applications.asciidoc b/docs/src/gremlin-applications.asciidoc
index bab23e9..c4de1b0 100644
--- a/docs/src/gremlin-applications.asciidoc
+++ b/docs/src/gremlin-applications.asciidoc
@@ -536,7 +536,7 @@ By default, Gremlin Server is configured to allow all requests to be processed (
 
 [source,yaml]
 authentication: {
-  className: org.apache.tinkerpop.gremlin.server.auth.SimpleAuthenticator
+  className: org.apache.tinkerpop.gremlin.server.auth.SimpleAuthenticator,
   config: {
     credentialsDb: conf/credential-graph.properties}}
 
@@ -547,7 +547,7 @@ A quick way to get started with the `SimpleAuthenticator` is to use TinkerGraph
 
 [source,yaml]
 authentication: {
-  className: org.apache.tinkerpop.gremlin.server.auth.SimpleAuthenticator
+  className: org.apache.tinkerpop.gremlin.server.auth.SimpleAuthenticator,
   config: {
     credentialsDb: conf/tinkergraph-empty.properties,
     credentialsDbLocation: data/credentials.kryo}}
@@ -610,6 +610,87 @@ credentials.removeUser("daniel")
 credentials.countUsers()
 ----
 
+[[script-execution]]
+Script Execution
+++++++++++++++++
+
+It is important to remember that Gremlin Server exposes a `ScriptEngine` instance that allows for remote execution of arbitrary code on the server.  Obviously, this situation can represent a security risk or, more minimally, provide ways for "bad" scripts to be inadvertently executed. A simple example of a "valid" Gremlin script that would cause some problems would be, `while(true) {}`, which would consume a thread in the Gremlin pool indefinitely, thus preventing it from serving other requests.  Sending enough of these kinds of scripts would eventually consume all available threads and Gremlin Server would stop responding.
+
+Gremlin Server (more specifically the `GremlinGroovyScriptEngine`) provides methods to protect itself from these kinds of troublesome scripts.  A user can configure the script engine with different `CompilerCustomizerProvider` implementations.  Consider the basic configuration from the Gremlin Server YAML file:
+
+[source,yaml]
+scriptEngines: {
+  gremlin-groovy: {
+    imports: [java.lang.Math],
+    staticImports: [java.lang.Math.PI],
+    scripts: [scripts/empty-sample.groovy]}}
+
+This configuration can be extended to include a `config` key as follows:
+
+[source,yaml]
+scriptEngines: {
+  gremlin-groovy: {
+    imports: [java.lang.Math],
+    staticImports: [java.lang.Math.PI],
+    scripts: [scripts/empty-sample.groovy],
+    config: {
+      compilerCustomizerProviders: {
+        "org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.TimedInterruptCustomizerProvider":[10000] }}}
+
+This configuration sets up the script engine with a `CompilerCustomizerProvider` implementation.  The `TimedInterruptCustomizerProvider` injects checks that ensure that loops (like `while`) can only execute for `10000` milliseconds.  With this configuration in place, a remote execution as follows, now times out rather than consuming the thread continuously:
+
+[source,groovy]
+gremlin> :remote connect tinkerpop.server conf/remote.yaml
+==>Connected - localhost/127.0.0.1:8182
+gremlin> :> while(true) { }
+Execution timed out after 10000 units. Start time: Fri Jul 24 11:04:52 EDT 2015
+
+There are a number of pre-packaged `CustomizerProvider` implementations:
+
+[width="100%",cols="3,10a",options="header"]
+|=========================================================
+|Customizer |Description
+|`CompileStaticCustomizerProvider` |Applies `CompileStatic` annotations to incoming scripts thus removing dynamic dispatch. More information about static compilation can be found in the link:http://docs.groovy-lang.org/latest/html/documentation/#_static_compilation[Groovy Documentation].  It is possible to configure this `CustomizerProvider` by specifying a comma separated list of link:http://docs.groovy-lang.org/latest/html/documentation/#Typecheckingextensions-Workingwithextensions[type checking extensions] that can have the effect of securing calls to various methods.
+|`ThreadInterruptCustomizerProvider` |Injects checks for thread interruption, thus allowing the thread to potentially respect calls to `Thread.interrupt()`
+|`TimedInterruptCustomizerProvider` |Injects checks into loops to interrupt them if they exceed the configured timeout in milliseconds.
+|`TypeCheckedCustomizerProvider` |Similar to the above mentioned, `CompileStaticCustomizerProvider`, the `TypeCheckedCustomizerProvider` injects `TypeChecked` annotations to incoming scripts.  More information on the nature of this annotation can be found in the link:http://docs.groovy-lang.org/latest/html/documentation/#_the_code_typechecked_code_annotation[Groovy Documentation].  It too takes a comma separated list of link:http://docs.groovy-lang.org/latest/html/documentation/#Typecheckingextensions-Workingwithextensions[type checking extensions].
+|=========================================================
+
+To provide some basic out-of-the-box protections against troublesome scripts, the following configuration can be used:
+
+[source,yaml]
+scriptEngines: {
+  gremlin-groovy: {
+    imports: [java.lang.Math],
+    staticImports: [java.lang.Math.PI],
+    scripts: [scripts/empty-sample.groovy],
+    config: {
+      compilerCustomizerProviders: {
+        "org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.ThreadInterruptCustomizerProvider":[],
+        "org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.TimedInterruptCustomizerProvider":[10000],
+        "org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.CompileStaticCustomizerProvider":["org.apache.tinkerpop.gremlin.groovy.jsr223.customizer.SimpleSandboxExtension"]}}}}
+
+NOTE: The above configuration could also use the `TypeCheckedCustomizerProvider` in place of the `CompileStaticCustomizerProvider`.  The differences between `TypeChecked` and `CompileStatic` are beyond the scope of this documentation.  Consult the latest link:http://docs.groovy-lang.org/latest/html/documentation/#_typing[Groovy Documentation' for information on the differences. It is important to understand the impact that these configuration will have on submitted scripts before enabling this feature.
+
+This configuration uses the `SimpleSandboxExtension`, which blacklists calls to methods on the `System` class, thereby preventing someone from remotely killing the server:
+
+[source,groovy]
+----
+gremlin> :> System.exit(0)
+Script8.groovy: 1: [Static type checking] - Not authorized to call this method: java.lang.System#exit(int)
+ @ line 1, column 1.
+   System.exit(0)
+   ^
+
+1 error
+----
+
+The `SimpleSandboxExtension` is by no means a "complete" implementation protecting against all manner of nefarious scripts, but it does provide an example for how such a capability might be implemented.  A full implementation would likely represent domain specific white-listing of methods (and possibly variables) available for execution in the script engine.
+
+A final thought on the topic of `CompilerCustomizerProvider` implementations is that they are not just for "security" (though they are demonstrated in that capacity here).  They can be used for a variety of features that can fine tune the Groovy compilation process.  Read more about compilation customization in the link:http://docs.groovy-lang.org/latest/html/documentation/#compilation-customizers[Groovy Documentation].
+
+NOTE: The import of classes to the script engine is handled by the `ImportCustomizerProvider`.  As the concept of "imports" is a first-class citizen (i.e. has its own configuration options), it is not recommended that the `ImportCustomizerProvider` be used as a configuration option to `compilerCustomizerProviders`.
+
 Serialization
 ^^^^^^^^^^^^^