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/15 23:00:16 UTC
[01/31] incubator-tinkerpop git commit: Refactoring
GremlinGroovyScriptEngine constructor.
Repository: incubator-tinkerpop
Updated Branches:
refs/heads/tp30 99e89be35 -> 2cf9f5e3e
Refactoring GremlinGroovyScriptEngine constructor.
Allows multiple CompilationCustomizerProviders to be supplied.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/fc825931
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/fc825931
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/fc825931
Branch: refs/heads/tp30
Commit: fc82593105e97ba97c6f6410ba0347a4fddc9cac
Parents: 6ea202c
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Jul 8 10:31:16 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Jul 8 10:31:16 2015 -0400
----------------------------------------------------------------------
.../GremlinGroovyScriptEngineOverGraphTest.java | 2 +-
.../groovy/NoImportCustomizerProvider.java | 7 +-
.../jsr223/GremlinGroovyScriptEngine.java | 30 ++++---
.../VariableIdentificationCustomizer.java | 91 ++++++++++++++++++++
.../jsr223/GremlinGroovyScriptEngineTest.java | 14 +--
.../structure/io/script/ScriptRecordReader.java | 2 +-
.../structure/io/script/ScriptRecordWriter.java | 2 +-
7 files changed, 126 insertions(+), 22 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/fc825931/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 7eb892b..babcf1b 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
@@ -70,7 +70,7 @@ public class GremlinGroovyScriptEngineOverGraphTest extends AbstractGremlinTest
@Test
@LoadGraphWith(LoadGraphWith.GraphData.MODERN)
public void shouldLoadImports() throws Exception {
- final ScriptEngine engineNoImports = new GremlinGroovyScriptEngine(new NoImportCustomizerProvider());
+ final ScriptEngine engineNoImports = new GremlinGroovyScriptEngine(NoImportCustomizerProvider.INSTANCE);
try {
engineNoImports.eval("Vertex.class.getName()");
fail("Should have thrown an exception because no imports were supplied");
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/fc825931/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/NoImportCustomizerProvider.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/NoImportCustomizerProvider.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/NoImportCustomizerProvider.java
index 91816b8..343c8a6 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/NoImportCustomizerProvider.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/NoImportCustomizerProvider.java
@@ -29,7 +29,12 @@ import java.util.Set;
*
* @author Stephen Mallette (http://stephen.genoprime.com)
*/
-public class NoImportCustomizerProvider implements ImportCustomizerProvider {
+public final class NoImportCustomizerProvider implements ImportCustomizerProvider {
+
+ public static final NoImportCustomizerProvider INSTANCE = new NoImportCustomizerProvider();
+
+ private NoImportCustomizerProvider() {}
+
@Override
public CompilationCustomizer getCompilationCustomizer() {
return new ImportCustomizer();
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/fc825931/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
index be9e436..f86498e 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
@@ -19,9 +19,11 @@
package org.apache.tinkerpop.gremlin.groovy.jsr223;
import groovy.transform.ThreadInterrupt;
+import org.apache.tinkerpop.gremlin.groovy.CompilerCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.DefaultImportCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.EmptyImportCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.ImportCustomizerProvider;
+import org.apache.tinkerpop.gremlin.groovy.NoImportCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.SecurityCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.loaders.GremlinLoader;
import org.apache.tinkerpop.gremlin.groovy.plugin.Artifact;
@@ -62,6 +64,7 @@ import java.io.Writer;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
+import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
@@ -138,21 +141,26 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
this(new DefaultImportCustomizerProvider());
}
- public GremlinGroovyScriptEngine(final ImportCustomizerProvider importCustomizerProvider) {
- this(importCustomizerProvider, null);
+ public GremlinGroovyScriptEngine(final CompilerCustomizerProvider... compilerCustomizerProviders) {
+ this(DEFAULT_SCRIPT_EVALUATION_TIMEOUT, compilerCustomizerProviders);
}
- public GremlinGroovyScriptEngine(final ImportCustomizerProvider importCustomizerProvider,
- final SecurityCustomizerProvider securityCustomizerProvider) {
- this(importCustomizerProvider, securityCustomizerProvider, DEFAULT_SCRIPT_EVALUATION_TIMEOUT);
- }
+ public GremlinGroovyScriptEngine(final long scriptEvaluationTimeout,
+ final CompilerCustomizerProvider... compilerCustomizerProviders) {
+ final List<CompilerCustomizerProvider> providers = Arrays.asList(compilerCustomizerProviders);
- public GremlinGroovyScriptEngine(final ImportCustomizerProvider importCustomizerProvider,
- final SecurityCustomizerProvider securityCustomizerProvider,
- final long scriptEvaluationTimeout) {
GremlinLoader.load();
- this.importCustomizerProvider = importCustomizerProvider;
- this.securityProvider = Optional.ofNullable(securityCustomizerProvider);
+
+ this.importCustomizerProvider = providers.stream()
+ .filter(p -> p instanceof ImportCustomizerProvider)
+ .map(p -> (ImportCustomizerProvider) p)
+ .findFirst().orElse(NoImportCustomizerProvider.INSTANCE);
+
+ this.securityProvider = providers.stream()
+ .filter(p -> p instanceof SecurityCustomizerProvider)
+ .map(p -> (SecurityCustomizerProvider) p)
+ .findFirst();
+
this.scriptEvaluationTimeout = scriptEvaluationTimeout;
createClassLoader();
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/fc825931/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
new file mode 100644
index 0000000..4606bde
--- /dev/null
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/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;
+
+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/fc825931/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
index c88a2ec..d60154d 100644
--- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
@@ -87,7 +87,7 @@ public class GremlinGroovyScriptEngineTest {
@Test
public void shouldLoadImportsViaDependencyManagerInterface() throws Exception {
- final GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(new NoImportCustomizerProvider());
+ final GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(NoImportCustomizerProvider.INSTANCE);
try {
engine.eval("Vertex.class.getName()");
fail("Should have thrown an exception because no imports were supplied");
@@ -101,7 +101,7 @@ public class GremlinGroovyScriptEngineTest {
@Test
public void shouldLoadImportsViaDependencyManagerInterfaceAdditively() throws Exception {
- final GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(new NoImportCustomizerProvider());
+ final GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(NoImportCustomizerProvider.INSTANCE);
try {
engine.eval("Vertex.class.getName()");
fail("Should have thrown an exception because no imports were supplied");
@@ -133,7 +133,7 @@ public class GremlinGroovyScriptEngineTest {
@Test
public void shouldLoadImportsViaDependencyManagerFromDependencyGatheredByUse() throws Exception {
- final GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(new NoImportCustomizerProvider());
+ final GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(NoImportCustomizerProvider.INSTANCE);
try {
engine.eval("org.apache.commons.math3.util.FastMath.abs(-1235)");
fail("Should have thrown an exception because no imports were supplied");
@@ -148,7 +148,7 @@ public class GremlinGroovyScriptEngineTest {
@Test
public void shouldAllowsUseToBeExecutedAfterImport() throws Exception {
- final GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(new NoImportCustomizerProvider());
+ final GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(NoImportCustomizerProvider.INSTANCE);
try {
engine.eval("org.apache.commons.math3.util.FastMath.abs(-1235)");
fail("Should have thrown an exception because no imports were supplied");
@@ -310,7 +310,7 @@ public class GremlinGroovyScriptEngineTest {
@Test
public void shouldTimeoutScriptOnTimedWhile() throws Exception {
- final ScriptEngine engine = new GremlinGroovyScriptEngine(new DefaultImportCustomizerProvider(), null, 3000);
+ final ScriptEngine engine = new GremlinGroovyScriptEngine(3000, new DefaultImportCustomizerProvider());
try {
engine.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 10000) {}");
fail("This should have timed out");
@@ -321,7 +321,7 @@ public class GremlinGroovyScriptEngineTest {
@Test
public void shouldTimeoutScriptOnTimedWhileOnceEngineHasBeenAliveForLongerThanTimeout() throws Exception {
- final ScriptEngine engine = new GremlinGroovyScriptEngine(new DefaultImportCustomizerProvider(), null, 3000);
+ final ScriptEngine engine = new GremlinGroovyScriptEngine(3000, new DefaultImportCustomizerProvider());
Thread.sleep(4000);
try {
engine.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 10000) {}");
@@ -336,7 +336,7 @@ public class GremlinGroovyScriptEngineTest {
@Test
public void shouldNotTimeoutStandaloneFunction() throws Exception {
// use a super fast timeout which should not prevent the call of a cached function
- final ScriptEngine engine = new GremlinGroovyScriptEngine(new DefaultImportCustomizerProvider(), null, 1);
+ final ScriptEngine engine = new GremlinGroovyScriptEngine(1, new DefaultImportCustomizerProvider());
engine.eval("def addItUp(x,y) { x + y }");
assertEquals(3, engine.eval("addItUp(1,2)"));
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/fc825931/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordReader.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordReader.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordReader.java
index 191c249..aec6aff 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordReader.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordReader.java
@@ -64,7 +64,7 @@ public final class ScriptRecordReader extends RecordReader<NullWritable, VertexW
public void initialize(final InputSplit genericSplit, final TaskAttemptContext context) throws IOException {
this.lineRecordReader.initialize(genericSplit, context);
final Configuration configuration = context.getConfiguration();
- this.engine = new GremlinGroovyScriptEngine(new DefaultImportCustomizerProvider(), null, Integer.MAX_VALUE);
+ this.engine = new GremlinGroovyScriptEngine(Integer.MAX_VALUE, new DefaultImportCustomizerProvider());
//this.engine = ScriptEngineCache.get(configuration.get(SCRIPT_ENGINE, ScriptEngineCache.DEFAULT_SCRIPT_ENGINE));
final FileSystem fs = FileSystem.get(configuration);
try {
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/fc825931/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordWriter.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordWriter.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordWriter.java
index e0a9ece..b9bd068 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordWriter.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordWriter.java
@@ -61,7 +61,7 @@ public final class ScriptRecordWriter extends RecordWriter<NullWritable, VertexW
public ScriptRecordWriter(final DataOutputStream out, final TaskAttemptContext context) throws IOException {
this.out = out;
final Configuration configuration = context.getConfiguration();
- this.engine = new GremlinGroovyScriptEngine(new DefaultImportCustomizerProvider(), null, Integer.MAX_VALUE);
+ this.engine = new GremlinGroovyScriptEngine(Integer.MAX_VALUE, new DefaultImportCustomizerProvider());
//this.engine = ScriptEngineCache.get(configuration.get(SCRIPT_ENGINE, ScriptEngineCache.DEFAULT_SCRIPT_ENGINE));
final FileSystem fs = FileSystem.get(configuration);
try {
[14/31] incubator-tinkerpop git commit: Add a
TypeCheckedCompilerCustomizerProvider.
Posted by sp...@apache.org.
Add a TypeCheckedCompilerCustomizerProvider.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/44d8f377
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/44d8f377
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/44d8f377
Branch: refs/heads/tp30
Commit: 44d8f3772f18ff1613613579cd0d1ef5b28d20ef
Parents: 3779682
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Thu Jul 9 08:24:08 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Thu Jul 9 08:24:08 2015 -0400
----------------------------------------------------------------------
.../groovy/TypeCheckedCustomizerProvider.java | 51 +++++++++++++++
...remlinGroovyScriptEngineTypeCheckedTest.java | 66 ++++++++++++++++++++
.../resources/PreventColorUsageExtension.groovy | 23 +++++++
3 files changed, 140 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/44d8f377/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
new file mode 100644
index 0000000..14ff7ca
--- /dev/null
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/TypeCheckedCustomizerProvider.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tinkerpop.gremlin.groovy;
+
+import groovy.transform.TypeChecked;
+import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
+import org.codehaus.groovy.control.customizers.CompilationCustomizer;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 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);
+ }
+
+ public TypeCheckedCustomizerProvider(final String extensions) {
+ this.extensions = extensions;
+ }
+
+ @Override
+ public CompilationCustomizer create() {
+ final Map<String, Object> timedInterruptAnnotationParams = new HashMap<>();
+ if (extensions != null && !extensions.isEmpty()) timedInterruptAnnotationParams.put("extensions", extensions);
+ return new ASTTransformationCustomizer(timedInterruptAnnotationParams, TypeChecked.class);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/44d8f377/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
new file mode 100644
index 0000000..0a7639e
--- /dev/null
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTypeCheckedTest.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tinkerpop.gremlin.groovy.jsr223;
+
+import org.apache.tinkerpop.gremlin.groovy.TypeCheckedCustomizerProvider;
+import org.codehaus.groovy.control.MultipleCompilationErrorsException;
+import org.junit.Test;
+
+import javax.script.ScriptException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class GremlinGroovyScriptEngineTypeCheckedTest {
+ @Test
+ public void shouldTypeCheck() throws Exception {
+ // with no type checking this should pass
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine()) {
+ assertEquals(255, scriptEngine.eval("((Object) new java.awt.Color(255, 255, 255)).red"));
+ }
+
+ final TypeCheckedCustomizerProvider provider = new TypeCheckedCustomizerProvider();
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(provider)) {
+ scriptEngine.eval("((Object) new java.awt.Color(255, 255, 255)).red");
+ fail("Should have failed type checking");
+ } catch (ScriptException se) {
+ assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
+ }
+ }
+
+ @Test
+ public void shouldTypeCheckWithExtension() throws Exception {
+ // with no type checking extension this should pass
+ final TypeCheckedCustomizerProvider providerNoExtension = new TypeCheckedCustomizerProvider();
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerNoExtension)) {
+ assertEquals(255, scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red"));
+ }
+
+ final TypeCheckedCustomizerProvider providerWithExtension = new TypeCheckedCustomizerProvider("PreventColorUsageExtension.groovy");
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) {
+ scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red");
+ fail("Should have failed type checking");
+ } catch (ScriptException se) {
+ assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/44d8f377/gremlin-groovy/src/test/resources/PreventColorUsageExtension.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/resources/PreventColorUsageExtension.groovy b/gremlin-groovy/src/test/resources/PreventColorUsageExtension.groovy
new file mode 100644
index 0000000..38eac69
--- /dev/null
+++ b/gremlin-groovy/src/test/resources/PreventColorUsageExtension.groovy
@@ -0,0 +1,23 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+onMethodSelection { expr, methodNode ->
+ if (methodNode.declaringClass.name=='java.awt.Color') {
+ addStaticTypeError("Method call is not allowed!", expr)
+ }
+}
\ No newline at end of file
[12/31] incubator-tinkerpop git commit: Minor code cleanup - optimize
imports.
Posted by sp...@apache.org.
Minor code cleanup - optimize imports.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/17ed3cee
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/17ed3cee
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/17ed3cee
Branch: refs/heads/tp30
Commit: 17ed3ceec1003f06821d4ebc314c64bea9a516e6
Parents: 2a319f0
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Jul 8 14:45:33 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Jul 8 14:45:33 2015 -0400
----------------------------------------------------------------------
.../gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java | 8 ++------
1 file changed, 2 insertions(+), 6 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/17ed3cee/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
index 59a0530..9042e6d 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
@@ -18,7 +18,6 @@
*/
package org.apache.tinkerpop.gremlin.groovy.jsr223;
-import groovy.transform.ThreadInterrupt;
import org.apache.tinkerpop.gremlin.groovy.CompilerCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.DefaultImportCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.EmptyImportCustomizerProvider;
@@ -40,7 +39,6 @@ import groovy.lang.Script;
import groovy.lang.Tuple;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.control.CompilerConfiguration;
-import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
import org.codehaus.groovy.jsr223.GroovyCompiledScript;
import org.codehaus.groovy.jsr223.GroovyScriptEngineImpl;
import org.codehaus.groovy.runtime.InvokerHelper;
@@ -322,10 +320,8 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
return new GroovyCompiledScript(this, getScriptClass(scriptSource));
} catch (SyntaxException e) {
throw new ScriptException(e.getMessage(), e.getSourceLocator(), e.getLine());
- } catch (IOException e) {
+ } catch (IOException | CompilationFailedException e) {
throw new ScriptException(e);
- } catch (CompilationFailedException ee) {
- throw new ScriptException(ee);
}
}
@@ -526,7 +522,7 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
if (c == groovy.lang.Script.class) {
return ctxtLoader;
}
- } catch (ClassNotFoundException e) {
+ } catch (ClassNotFoundException ignored) {
}
return groovy.lang.Script.class.getClassLoader();
}
[17/31] incubator-tinkerpop git commit: Add CompileStatic compiler
customizer provider.
Posted by sp...@apache.org.
Add CompileStatic compiler customizer provider.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/8665a0fc
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/8665a0fc
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/8665a0fc
Branch: refs/heads/tp30
Commit: 8665a0fcd5cb74d38d59c37560ccf0ac7623b0dd
Parents: 9149c20
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Thu Jul 9 09:49:39 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Thu Jul 9 09:49:39 2015 -0400
----------------------------------------------------------------------
.../groovy/CompileStaticCustomizerProvider.java | 40 +++++++++
...mlinGroovyScriptEngineCompileStaticTest.java | 92 ++++++++++++++++++++
.../PreventCountDownLatchUsageExtension.groovy | 23 +++++
3 files changed, 155 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8665a0fc/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
new file mode 100644
index 0000000..c7cdbeb
--- /dev/null
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/CompileStaticCustomizerProvider.java
@@ -0,0 +1,40 @@
+package org.apache.tinkerpop.gremlin.groovy;
+
+import groovy.transform.CompileStatic;
+import groovy.transform.TypeChecked;
+import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
+import org.codehaus.groovy.control.customizers.CompilationCustomizer;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * 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> compileStaticAnnotationParams = new HashMap<>();
+ if (extensions != null && !extensions.isEmpty()) {
+ if (extensions.contains(","))
+ compileStaticAnnotationParams.put("extensions", extensions.split(","));
+ else
+ compileStaticAnnotationParams.put("extensions", extensions);
+ }
+
+ return new ASTTransformationCustomizer(compileStaticAnnotationParams, CompileStatic.class);
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8665a0fc/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
new file mode 100644
index 0000000..d5ebc09
--- /dev/null
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineCompileStaticTest.java
@@ -0,0 +1,92 @@
+/*
+ * 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.groovy.CompileStaticCustomizerProvider;
+import org.codehaus.groovy.control.MultipleCompilationErrorsException;
+import org.junit.Test;
+
+import javax.script.ScriptException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class GremlinGroovyScriptEngineCompileStaticTest {
+ @Test
+ public void shouldCompileStatic() throws Exception {
+ // with no type checking this should pass
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine()) {
+ assertEquals(255, scriptEngine.eval("((Object) new java.awt.Color(255, 255, 255)).red"));
+ }
+
+ final CompileStaticCustomizerProvider provider = new CompileStaticCustomizerProvider();
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(provider)) {
+ scriptEngine.eval("((Object) new java.awt.Color(255, 255, 255)).red");
+ fail("Should have failed type checking");
+ } catch (ScriptException se) {
+ assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
+ }
+ }
+
+ @Test
+ public void shouldCompileStaticWithExtension() throws Exception {
+ // with no type checking extension this should pass
+ final CompileStaticCustomizerProvider providerNoExtension = new CompileStaticCustomizerProvider();
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerNoExtension)) {
+ assertEquals(255, scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red"));
+ }
+
+ final CompileStaticCustomizerProvider providerWithExtension = new CompileStaticCustomizerProvider("PreventColorUsageExtension.groovy");
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) {
+ scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red");
+ fail("Should have failed type checking");
+ } catch (ScriptException se) {
+ assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
+ }
+ }
+
+ @Test
+ public void shouldCompileStaticWithMultipleExtension() throws Exception {
+ // with no type checking extension this should pass
+ final CompileStaticCustomizerProvider providerNoExtension = new CompileStaticCustomizerProvider();
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerNoExtension)) {
+ assertEquals(255, scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red"));
+ assertEquals(1l, scriptEngine.eval("def c = new java.util.concurrent.CountDownLatch(1); c.count"));
+ }
+
+ final CompileStaticCustomizerProvider providerWithExtension = new CompileStaticCustomizerProvider(
+ "PreventColorUsageExtension.groovy,PreventCountDownLatchUsageExtension.groovy");
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) {
+ scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red");
+ fail("Should have failed type checking");
+ } catch (ScriptException se) {
+ assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
+ }
+
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) {
+ scriptEngine.eval("def c = new java.util.concurrent.CountDownLatch(1); c.count");
+ fail("Should have failed type checking");
+ } catch (ScriptException se) {
+ assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8665a0fc/gremlin-groovy/src/test/resources/PreventCountDownLatchUsageExtension.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/resources/PreventCountDownLatchUsageExtension.groovy b/gremlin-groovy/src/test/resources/PreventCountDownLatchUsageExtension.groovy
new file mode 100644
index 0000000..74af460
--- /dev/null
+++ b/gremlin-groovy/src/test/resources/PreventCountDownLatchUsageExtension.groovy
@@ -0,0 +1,23 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+onMethodSelection { expr, methodNode ->
+ if (methodNode.declaringClass.name=='java.util.concurrent.CountDownLatch') {
+ addStaticTypeError("Method call is not allowed!", expr)
+ }
+}
\ No newline at end of file
[20/31] incubator-tinkerpop git commit: Add a SandboxExtension.
Posted by sp...@apache.org.
Add a SandboxExtension.
Provide a way to use the ASTTransformer to validate incoming variables and types.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/c47426dd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/c47426dd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/c47426dd
Branch: refs/heads/tp30
Commit: c47426dd7c9c18b8aed2d2565cf046ad47ea9a43
Parents: 6252cfa
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Thu Jul 9 15:18:13 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Thu Jul 9 15:18:13 2015 -0400
----------------------------------------------------------------------
.../GremlinGroovyScriptEngineOverGraphTest.java | 143 ++++++++++++++++++-
.../groovy/jsr223/SandboxExtension.groovy | 66 +++++++++
.../jsr223/GremlinGroovyScriptEngine.java | 28 ++++
3 files changed, 236 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c47426dd/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 babcf1b..c72bfd5 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,12 +23,15 @@ 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;
@@ -43,10 +46,12 @@ 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;
import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@@ -145,7 +150,7 @@ public class GremlinGroovyScriptEngineOverGraphTest extends AbstractGremlinTest
public void shouldClearBindingsBetweenEvals() throws Exception {
final ScriptEngine engine = new GremlinGroovyScriptEngine();
engine.put("g", g);
- Assert.assertEquals(g.V(convertToVertexId("marko")).next(), engine.eval("g.V(" + convertToVertexId("marko") + ").next()"));
+ assertEquals(g.V(convertToVertexId("marko")).next(), engine.eval("g.V(" + convertToVertexId("marko") + ").next()"));
final Bindings bindings = engine.createBindings();
bindings.put("g", g);
@@ -234,6 +239,142 @@ public class GremlinGroovyScriptEngineOverGraphTest extends AbstractGremlinTest
@Test
@LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+ public void shouldEvalGraphTraversalSourceWithTypeChecked() throws Exception {
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine()) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("g", g);
+ assertEquals(g.V(convertToVertexId("marko")).next(), engine.eval("g.V(" + convertToVertexId("marko") + ").next()", bindings));
+ }
+
+ final TypeCheckedCustomizerProvider providerNoExtension = new TypeCheckedCustomizerProvider();
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(providerNoExtension)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("g", g);
+ engine.eval("g.V(" + convertToVertexId("marko") + ").next()", bindings);
+ fail("Type checking should have forced an error as 'g' is not defined");
+ } catch (Exception ex) {
+ assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
+ assertThat(ex.getMessage(), containsString("The variable [g] is undeclared."));
+ }
+
+ final TypeCheckedCustomizerProvider providerWithExtension = new TypeCheckedCustomizerProvider(
+ SandboxExtension.class.getName());
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(providerWithExtension)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("g", g);
+ assertEquals(g.V(convertToVertexId("marko")).next(), engine.eval("g.V(" + convertToVertexId("marko") + ").next()", bindings));
+ assertEquals(g.V(convertToVertexId("marko")).out("created").count().next(), engine.eval("g.V(" + convertToVertexId("marko") + ").out(\"created\").count().next()", bindings));
+ }
+ }
+
+ @Test
+ @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+ public void shouldEvalGraphWithTypeChecked() throws Exception {
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine()) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("graph", graph);
+ assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(" + convertToVertexId("marko") + ").next()", bindings));
+ }
+
+ final TypeCheckedCustomizerProvider providerNoExtension = new TypeCheckedCustomizerProvider();
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(providerNoExtension)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("graph", graph);
+ assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(" + convertToVertexId("marko") + ").next()", bindings));
+ fail("Type checking should have forced an error as 'graph' is not defined");
+ } catch (Exception ex) {
+ assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
+ assertThat(ex.getMessage(), containsString("The variable [graph] is undeclared."));
+ }
+
+ final TypeCheckedCustomizerProvider providerWithExtension = new TypeCheckedCustomizerProvider(
+ SandboxExtension.class.getName());
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(providerWithExtension)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("graph", graph);
+ assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(" + convertToVertexId("marko") + ").next()", bindings));
+ }
+ }
+
+ @Test
+ @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+ public void shouldEvalGraphTraversalSourceWithCompileStatic() throws Exception {
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine()) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("g", g);
+ assertEquals(g.V(convertToVertexId("marko")).next(), engine.eval("g.V(" + convertToVertexId("marko") + ").next()", bindings));
+ }
+
+ final CompileStaticCustomizerProvider providerNoExtension = new CompileStaticCustomizerProvider();
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(providerNoExtension)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("g", g);
+ engine.eval("g.V(" + convertToVertexId("marko") + ").next()", bindings);
+ fail("Type checking should have forced an error as 'g' is not defined");
+ } catch (Exception ex) {
+ assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
+ assertThat(ex.getMessage(), containsString("The variable [g] is undeclared."));
+ }
+
+ final CompileStaticCustomizerProvider providerWithExtension = new CompileStaticCustomizerProvider(
+ SandboxExtension.class.getName());
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(providerWithExtension)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("g", g);
+ assertEquals(g.V(convertToVertexId("marko")).next(), engine.eval("g.V(" + convertToVertexId("marko") + ").next()", bindings));
+ assertEquals(g.V(convertToVertexId("marko")).out("created").count().next(), engine.eval("g.V(" + convertToVertexId("marko") + ").out(\"created\").count().next()", bindings));
+ }
+ }
+
+ @Test
+ @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+ public void shouldEvalGraphWithCompileStatic() throws Exception {
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine()) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("graph", graph);
+ assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(" + convertToVertexId("marko") + ").next()", bindings));
+ }
+
+ final CompileStaticCustomizerProvider providerNoExtension = new CompileStaticCustomizerProvider();
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(providerNoExtension)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("graph", graph);
+ assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(" + convertToVertexId("marko") + ").next()", bindings));
+ fail("Type checking should have forced an error as 'graph' is not defined");
+ } catch (Exception ex) {
+ assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
+ assertThat(ex.getMessage(), containsString("The variable [graph] is undeclared."));
+ }
+
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(providerNoExtension)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("graph", graph);
+ bindings.put("x", convertToVertexId("marko"));
+ assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(x).next()", bindings));
+ fail("Type checking should have forced an error as 'graph' is not defined");
+ } catch (Exception ex) {
+ assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
+ assertThat(ex.getMessage(), containsString("The variable [graph] is undeclared."));
+ }
+
+ final CompileStaticCustomizerProvider providerWithExtension = new CompileStaticCustomizerProvider(
+ SandboxExtension.class.getName());
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(providerWithExtension)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("graph", graph);
+ assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(" + convertToVertexId("marko") + ").next()", bindings));
+ }
+
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(providerWithExtension)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("graph", graph);
+ bindings.put("x", convertToVertexId("marko"));
+ assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(x).next()", bindings));
+ }
+ }
+
+ @Test
+ @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
public void shouldEvalGlobalClosuresEvenAfterEvictionOfClass() throws ScriptException {
final GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine();
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c47426dd/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
new file mode 100644
index 0000000..4888f8c
--- /dev/null
+++ b/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/SandboxExtension.groovy
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.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.ClassNode
+import org.codehaus.groovy.ast.expr.VariableExpression
+import org.codehaus.groovy.transform.stc.GroovyTypeCheckingExtensionSupport
+
+import java.util.function.BiPredicate
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+class SandboxExtension extends GroovyTypeCheckingExtensionSupport.TypeCheckingDSL {
+
+ protected boolean graphIsAlwaysGraphInstance = true
+ protected boolean gIsAlwaysGraphTraversalSource = true
+ protected BiPredicate<VariableExpression, Map<String,ClassNode>> filter = USE_ALL
+
+ public static final BiPredicate<VariableExpression, Map<String,ClassNode>> USE_ALL = { var, types -> true }
+
+ @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)
+ if (varTypes.containsKey(var.name) && filter.test(var, varTypes)
+ || (var.name == "graph" && !graphIsAlwaysGraphInstance
+ || var.name == "g" && !gIsAlwaysGraphTraversalSource)) {
+ storeType(var, varTypes.get(var.name))
+ handled = true
+ return
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c47426dd/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
index 9042e6d..09af47f 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
@@ -37,6 +37,8 @@ import groovy.lang.MissingMethodException;
import groovy.lang.MissingPropertyException;
import groovy.lang.Script;
import groovy.lang.Tuple;
+import org.codehaus.groovy.ast.ClassHelper;
+import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.jsr223.GroovyCompiledScript;
@@ -81,6 +83,7 @@ import java.util.stream.Collectors;
*/
public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements DependencyManager, AutoCloseable {
+ public static final String COMPILE_OPTIONS_VAR_TYPES = "sandbox.bindings";
public static final String KEY_REFERENCE_TYPE = "#jsr223.groovy.engine.keep.globals";
public static final String REFERENCE_TYPE_PHANTOM = "phantom";
public static final String REFERENCE_TYPE_WEAK = "weak";
@@ -96,6 +99,13 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
}
};
+ public static final ThreadLocal<Map<String, Object>> COMPILE_OPTIONS = new ThreadLocal<Map<String, Object>>(){
+ @Override
+ protected Map<String, Object> initialValue() {
+ return new HashMap<>();
+ }
+ };
+
/**
* Script to generated Class map.
*/
@@ -287,6 +297,7 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
} catch (ClassCastException cce) { /*ignore.*/ }
try {
+ registerBindingTypes(context);
final Class clazz = getScriptClass(script);
if (null == clazz) throw new ScriptException("Script class is null");
return eval(clazz, context);
@@ -447,6 +458,23 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
}
}
+ private void registerBindingTypes(final ScriptContext context) {
+ final Map<String,ClassNode> variableTypes = new HashMap<>();
+ clearVarTypes();
+ context.getBindings(ScriptContext.ENGINE_SCOPE).forEach((k, v) -> {
+ final Class clazz = v.getClass();
+ variableTypes.put(k, ClassHelper.make(clazz));
+ });
+
+ COMPILE_OPTIONS.get().put(COMPILE_OPTIONS_VAR_TYPES, variableTypes);
+ }
+
+ private static void clearVarTypes() {
+ final Map<String,Object> m = COMPILE_OPTIONS.get();
+ if (m.containsKey(COMPILE_OPTIONS_VAR_TYPES))
+ ((Map<String,ClassNode>) m.get(COMPILE_OPTIONS_VAR_TYPES)).clear();
+ }
+
private void ensureSandbox() {
if (securityProvider.isPresent() && !this.registeredSandbox.get()) {
this.securityProvider.get().registerInterceptors();
[06/31] incubator-tinkerpop git commit: Update javadoc.
Posted by sp...@apache.org.
Update javadoc.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/8b4f3df0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/8b4f3df0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/8b4f3df0
Branch: refs/heads/tp30
Commit: 8b4f3df05defd73a5300a45196eede6a346e2544
Parents: e41b52c
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Jul 8 12:45:45 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Jul 8 12:45:45 2015 -0400
----------------------------------------------------------------------
.../gremlin/groovy/jsr223/DependencyManager.java | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/8b4f3df0/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/DependencyManager.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/DependencyManager.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/DependencyManager.java
index e182ac8..5c1b8fe 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/DependencyManager.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/DependencyManager.java
@@ -18,8 +18,10 @@
*/
package org.apache.tinkerpop.gremlin.groovy.jsr223;
+import org.apache.tinkerpop.gremlin.groovy.ImportCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.plugin.GremlinPlugin;
import org.apache.tinkerpop.gremlin.groovy.plugin.GremlinPluginException;
+import org.apache.tinkerpop.gremlin.groovy.plugin.PluginAcceptor;
import java.util.List;
import java.util.Map;
@@ -36,16 +38,16 @@ public interface DependencyManager {
/**
* Take maven coordinates and load the classes into the classloader used by the ScriptEngine. Those ScriptEngines
* that can support script engine plugins should check if there are any new {@link GremlinPlugin}
- * implementations in the classloader. The {@link org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine}
+ * implementations in the classloader. The {@link GremlinGroovyScriptEngine}
* implementation uses ServiceLoader to figure out if there are such classes to return.
* <p/>
* It is up to the caller to execute the
* {@link GremlinPlugin#pluginTo(org.apache.tinkerpop.gremlin.groovy.plugin.PluginAcceptor)} method. The reason for
* this has to do with conflicts that can occur with mapper imports that are added via the
- * {@link org.apache.tinkerpop.gremlin.groovy.ImportCustomizerProvider} and scripts executed through the
- * {@link org.apache.tinkerpop.gremlin.groovy.plugin.PluginAcceptor}. Generally speaking, all calls to this "use" method
+ * {@link ImportCustomizerProvider} and scripts executed through the
+ * {@link PluginAcceptor}. Generally speaking, all calls to this "use" method
* should be complete prior to calling
- * {@link GremlinPlugin#pluginTo(org.apache.tinkerpop.gremlin.groovy.plugin.PluginAcceptor)}.
+ * {@link GremlinPlugin#pluginTo(PluginAcceptor)}.
*/
List<GremlinPlugin> use(final String group, final String artifact, final String version);
[02/31] incubator-tinkerpop git commit: Merge remote-tracking branch
'origin/master' into variables-id
Posted by sp...@apache.org.
Merge remote-tracking branch 'origin/master' into variables-id
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/0e1c86bd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/0e1c86bd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/0e1c86bd
Branch: refs/heads/tp30
Commit: 0e1c86bd627e25708a26c6fe23a2f7be4e0b47e3
Parents: fc82593 46fc989
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Jul 8 11:21:37 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Jul 8 11:21:37 2015 -0400
----------------------------------------------------------------------
CHANGELOG.asciidoc | 1 +
.../gremlin/groovy/engine/ScriptEngines.java | 9 ++++--
.../groovy/engine/GremlinExecutorTest.java | 31 ++++++++++++++++++++
.../jsr223/GremlinGroovyScriptEngineTest.java | 20 +++++++++++++
4 files changed, 59 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/0e1c86bd/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
----------------------------------------------------------------------
diff --cc gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
index d60154d,6c6ade0..2bdbda3
--- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
@@@ -334,9 -334,29 +334,29 @@@ public class GremlinGroovyScriptEngineT
}
@Test
+ public void shouldContinueToEvalScriptsEvenWithTimedInterrupt() throws Exception {
+ final ScriptEngine engine = new GremlinGroovyScriptEngine(new DefaultImportCustomizerProvider(), null, 50);
+
+ for (int ix = 0; ix < 10; ix++) {
+ try {
+ // this script takes 10 ms longer than the interruptionTimeout
+ engine.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 60) {}");
+ fail("This should have timed out");
+ } catch (ScriptException se) {
+ assertEquals(TimeoutException.class, se.getCause().getCause().getClass());
+ }
+
+ // this script takes 10 ms less than the interruptionTimeout
+ assertEquals("test", engine.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 40) {};'test'"));
+ }
+
+ assertEquals(2, engine.eval("1+1"));
+ }
+
+ @Test
public void shouldNotTimeoutStandaloneFunction() throws Exception {
// use a super fast timeout which should not prevent the call of a cached function
- final ScriptEngine engine = new GremlinGroovyScriptEngine(new DefaultImportCustomizerProvider(), null, 1);
+ final ScriptEngine engine = new GremlinGroovyScriptEngine(1, new DefaultImportCustomizerProvider());
engine.eval("def addItUp(x,y) { x + y }");
assertEquals(3, engine.eval("addItUp(1,2)"));
[05/31] incubator-tinkerpop git commit: Change scope of
ManagedConcurrentValueMap.
Posted by sp...@apache.org.
Change scope of ManagedConcurrentValueMap.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/e41b52c1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/e41b52c1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/e41b52c1
Branch: refs/heads/tp30
Commit: e41b52c1b6cf9c4d3becc504de9e3a5c4217da84
Parents: ce9d090
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Jul 8 12:45:10 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Jul 8 12:45:10 2015 -0400
----------------------------------------------------------------------
.../gremlin/groovy/jsr223/ManagedConcurrentValueMap.java | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e41b52c1/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/ManagedConcurrentValueMap.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/ManagedConcurrentValueMap.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/ManagedConcurrentValueMap.java
index 8f38604..dce056b 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/ManagedConcurrentValueMap.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/ManagedConcurrentValueMap.java
@@ -28,13 +28,13 @@ import java.util.concurrent.ConcurrentHashMap;
*
* @author Stephen Mallette (http://stephen.genoprime.com)
*/
-public class ManagedConcurrentValueMap<K, V> {
+class ManagedConcurrentValueMap<K, V> {
private final ConcurrentHashMap<K, ManagedReference<V>> internalMap;
private ReferenceBundle bundle;
public ManagedConcurrentValueMap(final ReferenceBundle bundle) {
this.bundle = bundle;
- internalMap = new ConcurrentHashMap<K, ManagedReference<V>>();
+ internalMap = new ConcurrentHashMap<>();
}
/**
@@ -76,6 +76,9 @@ public class ManagedConcurrentValueMap<K, V> {
internalMap.put(key, ref);
}
+ /**
+ * Clear the map.
+ */
public void clear() {
internalMap.clear();
}
[27/31] incubator-tinkerpop git commit: Add sandboxing checks for
groovy properties.
Posted by sp...@apache.org.
Add sandboxing checks for groovy properties.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/69e39ee7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/69e39ee7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/69e39ee7
Branch: refs/heads/tp30
Commit: 69e39ee7a0595530110119f4e215c6f1aa1f092d
Parents: 3173d9e
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Fri Jul 10 15:18:18 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Fri Jul 10 15:18:18 2015 -0400
----------------------------------------------------------------------
.../jsr223/AllowColorSandboxExtension.groovy | 35 +++++++++++++
...nGroovyScriptEngineTinkerPopSandboxTest.java | 34 +++++++++++++
.../groovy/jsr223/SandboxExtension.groovy | 53 +++++++++++++++++---
.../jsr223/TinkerPopSandboxExtension.groovy | 5 +-
4 files changed, 118 insertions(+), 9 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/69e39ee7/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
new file mode 100644
index 0000000..7a1911f
--- /dev/null
+++ b/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/AllowColorSandboxExtension.groovy
@@ -0,0 +1,35 @@
+/*
+ * 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 AllowColorSandboxExtension extends SandboxExtension {
+ AllowColorSandboxExtension() {
+ def methodWhiteList = ["java\\.awt\\.Color#<init>"]
+ methodFilter = (BiPredicate<String, MethodNode>) { descriptor, method ->
+ methodWhiteList.any { descriptor =~ it }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/69e39ee7/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 6908078..5299857 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
@@ -61,4 +61,38 @@ public class GremlinGroovyScriptEngineTinkerPopSandboxTest extends AbstractGreml
assertEquals(g.V(convertToVertexId("marko")).out("created").count().next(), engine.eval("g.V(" + convertToVertexId("marko") + ").out(\"created\").count().next()", bindings));
}
}
+
+ @Test
+ @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+ public void shouldNotEvalColorWhenCallingMethods() throws Exception {
+ final CompilerCustomizerProvider standardSandbox = new CompileStaticCustomizerProvider(AllowColorSandboxExtension.class.getName());
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(standardSandbox)) {
+ assertEquals(new java.awt.Color(255,255,255), engine.eval("new java.awt.Color(255,255,255)"));
+ }
+
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(standardSandbox)) {
+ engine.eval("new java.awt.Color(255,255,255).getRed()");
+ fail("Type checking should have forced an error as 'getRed()' is not authorized - just Color construction");
+ } catch (Exception ex) {
+ assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
+ assertThat(ex.getMessage(), containsString("Not authorized to call this method"));
+ }
+ }
+
+ @Test
+ @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+ public void shouldNotEvalColorWhenCallingProperties() throws Exception {
+ final CompilerCustomizerProvider standardSandbox = new CompileStaticCustomizerProvider(AllowColorSandboxExtension.class.getName());
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(standardSandbox)) {
+ assertEquals(new java.awt.Color(255,255,255), engine.eval("new java.awt.Color(255,255,255)"));
+ }
+
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(standardSandbox)) {
+ engine.eval("new java.awt.Color(255,255,255).red");
+ fail("Type checking should have forced an error as 'red' is not authorized - just Color construction");
+ } catch (Exception ex) {
+ assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
+ assertThat(ex.getMessage(), containsString("Not authorized to call this method"));
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/69e39ee7/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
index a2960f0..f2027f4 100644
--- 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
@@ -20,13 +20,18 @@ 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.Expression
+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
@@ -43,12 +48,12 @@ class SandboxExtension extends GroovyTypeCheckingExtensionSupport.TypeCheckingDS
public static final BiPredicate<VariableExpression, Map<String,ClassNode>> VARIABLES_ALLOW_ALL = { var, types -> true }
- public static final BiPredicate<VariableExpression, Map<String,ClassNode>> METHODS_ALLOW_ALL = { exp, method -> true }
+ public static final BiPredicate<String, MethodNode> METHODS_ALLOW_ALL = { exp, method -> true }
protected boolean graphIsAlwaysGraphInstance = true
protected boolean gIsAlwaysGraphTraversalSource = true
protected BiPredicate<VariableExpression, Map<String,ClassNode>> variableFilter = VARIABLES_ALLOW_ALL
- protected BiPredicate<Expression, MethodNode> methodFilter = METHODS_ALLOW_ALL
+ protected BiPredicate<String, MethodNode> methodFilter = METHODS_ALLOW_ALL
@Override
Object run() {
@@ -82,10 +87,15 @@ class SandboxExtension extends GroovyTypeCheckingExtensionSupport.TypeCheckingDS
}
onMethodSelection { expr, MethodNode methodNode ->
- if (!methodFilter.test(expr, methodNode)) {
- def descriptor = toMethodDescriptor(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)
}
}
@@ -114,4 +124,35 @@ class SandboxExtension extends GroovyTypeCheckingExtensionSupport.TypeCheckingDS
static String prettyPrint(final ClassNode node) {
node.isArray()?"${prettyPrint(node.componentType)}[]":node.toString(false)
}
+
+ 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/69e39ee7/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
index 3b60e1d..940101c 100644
--- 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
@@ -19,7 +19,6 @@
package org.apache.tinkerpop.gremlin.groovy.jsr223
import org.codehaus.groovy.ast.MethodNode
-import org.codehaus.groovy.ast.expr.Expression
import java.util.function.BiPredicate
@@ -36,12 +35,12 @@ class TinkerPopSandboxExtension extends SandboxExtension {
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<Expression, MethodNode>) { exp, method ->
- def descriptor = toMethodDescriptor(method)
+ methodFilter = (BiPredicate<String, MethodNode>) { descriptor, method ->
methodWhiteList.any { descriptor =~ it }
}
}
[04/31] incubator-tinkerpop git commit: Update javadoc.
Posted by sp...@apache.org.
Update javadoc.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/ce9d090f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/ce9d090f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/ce9d090f
Branch: refs/heads/tp30
Commit: ce9d090f9f8a79fc51f247b73a3315be37b776c8
Parents: 569ef03
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Jul 8 12:02:27 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Jul 8 12:02:27 2015 -0400
----------------------------------------------------------------------
.../tinkerpop/gremlin/groovy/CompilerCustomizerProvider.java | 8 ++++++++
.../gremlin/groovy/EmptyImportCustomizerProvider.java | 3 +--
2 files changed, 9 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ce9d090f/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/CompilerCustomizerProvider.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/CompilerCustomizerProvider.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/CompilerCustomizerProvider.java
index b0b6f29..a633ff2 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/CompilerCustomizerProvider.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/CompilerCustomizerProvider.java
@@ -18,11 +18,19 @@
*/
package org.apache.tinkerpop.gremlin.groovy;
+import org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine;
import org.codehaus.groovy.control.customizers.CompilationCustomizer;
/**
+ * Provides a way to plugin Groovy {@code CompilationCustomizer} implementations to the
+ * {@link GremlinGroovyScriptEngine}.
+ *
* @author Stephen Mallette (http://stephen.genoprime.com)
*/
public interface CompilerCustomizerProvider {
+
+ /**
+ * Create a new instance of a {@code CompilationCustomizer} to add to the {@link GremlinGroovyScriptEngine}.
+ */
public CompilationCustomizer getCompilationCustomizer();
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/ce9d090f/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/EmptyImportCustomizerProvider.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/EmptyImportCustomizerProvider.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/EmptyImportCustomizerProvider.java
index 4400e8f..03e86c9 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/EmptyImportCustomizerProvider.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/EmptyImportCustomizerProvider.java
@@ -26,8 +26,7 @@ import java.util.HashSet;
import java.util.Set;
/**
- * This {@link org.apache.tinkerpop.gremlin.groovy.ImportCustomizerProvider} is empty and comes with no pre-defined imports
- * at all.
+ * This {@link ImportCustomizerProvider} is empty and comes with no pre-defined imports at all.
*
* @author Stephen Mallette (http://stephen.genoprime.com)
*/
[25/31] incubator-tinkerpop git commit: Implement a basic TinkerPop
sandbox implementation and add tests.
Posted by sp...@apache.org.
Implement a basic TinkerPop sandbox implementation and add tests.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/84701fa0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/84701fa0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/84701fa0
Branch: refs/heads/tp30
Commit: 84701fa0fbc7c052ac50b192bba4ff968db66a12
Parents: 2af2c3d
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Fri Jul 10 10:53:26 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Fri Jul 10 10:53:26 2015 -0400
----------------------------------------------------------------------
.../gremlin/groovy/GroovyEnvironmentSuite.java | 2 +
...nGroovyScriptEngineTinkerPopSandboxTest.java | 64 ++++++++++++++++++++
.../groovy/jsr223/SandboxExtension.groovy | 32 ++++++----
.../jsr223/TinkerPopSandboxExtension.groovy | 48 +++++++++++++++
4 files changed, 133 insertions(+), 13 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/84701fa0/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/GroovyEnvironmentSuite.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/GroovyEnvironmentSuite.java b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/GroovyEnvironmentSuite.java
index 67c6ff6..2fb9e8e 100644
--- a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/GroovyEnvironmentSuite.java
+++ b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/GroovyEnvironmentSuite.java
@@ -25,6 +25,7 @@ import org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutorOverGraphTest;
import org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineOverGraphTest;
import org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineSandboxCustomTest;
import org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineSandboxedStandardTest;
+import org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineTinkerPopSandboxTest;
import org.apache.tinkerpop.gremlin.groovy.loaders.SugarLoader;
import org.apache.tinkerpop.gremlin.groovy.loaders.SugarLoaderTest;
import org.apache.tinkerpop.gremlin.groovy.util.SugarTestHelper;
@@ -54,6 +55,7 @@ public class GroovyEnvironmentSuite extends AbstractGremlinSuite {
GremlinGroovyScriptEngineOverGraphTest.class,
GremlinGroovyScriptEngineSandboxCustomTest.class,
GremlinGroovyScriptEngineSandboxedStandardTest.class,
+ GremlinGroovyScriptEngineTinkerPopSandboxTest.class,
GremlinExecutorOverGraphTest.class,
SugarLoaderTest.class,
};
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/84701fa0/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
new file mode 100644
index 0000000..6908078
--- /dev/null
+++ b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTinkerPopSandboxTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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.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.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;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class GremlinGroovyScriptEngineTinkerPopSandboxTest extends AbstractGremlinTest {
+ @Test
+ public void shouldNotEvalAsTheMethodIsNotWhiteListed() throws Exception {
+ final CompilerCustomizerProvider standardSandbox = new CompileStaticCustomizerProvider(TinkerPopSandboxExtension.class.getName());
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(standardSandbox)) {
+ engine.eval("java.lang.Math.abs(123)");
+ fail("Should have a compile error because class/method is not white listed");
+ } catch (Exception ex) {
+ assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
+ assertThat(ex.getCause().getMessage(), containsString("Not authorized to call this method"));
+ }
+ }
+
+ @Test
+ @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+ public void shouldEvalOnGAsTheMethodIsWhiteListed() throws Exception {
+ final CompilerCustomizerProvider standardSandbox = new CompileStaticCustomizerProvider(TinkerPopSandboxExtension.class.getName());
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(standardSandbox)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("g", g);
+ assertEquals(g.V(convertToVertexId("marko")).next(), engine.eval("g.V(" + convertToVertexId("marko") + ").next()", bindings));
+ assertEquals(g.V(convertToVertexId("marko")).out("created").count().next(), engine.eval("g.V(" + convertToVertexId("marko") + ").out(\"created\").count().next()", bindings));
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/84701fa0/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
index be0cbca..a2960f0 100644
--- 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
@@ -44,17 +44,11 @@ class SandboxExtension extends GroovyTypeCheckingExtensionSupport.TypeCheckingDS
public static final BiPredicate<VariableExpression, Map<String,ClassNode>> VARIABLES_ALLOW_ALL = { var, types -> true }
public static final BiPredicate<VariableExpression, Map<String,ClassNode>> METHODS_ALLOW_ALL = { exp, method -> true }
- public static final BiPredicate<Expression, MethodNode> METHODS_REGEX_WHITELIST = { exp, method ->
- def descriptor = toMethodDescriptor(method)
- !methodList.any { descriptor =~ it }
- }
- public static final BiPredicate<Expression, MethodNode> METHODS_REGEX_BLACKLIST = { exp, method -> !METHODS_REGEX_WHITELIST.test(exp, method) }
protected boolean graphIsAlwaysGraphInstance = true
protected boolean gIsAlwaysGraphTraversalSource = true
protected BiPredicate<VariableExpression, Map<String,ClassNode>> variableFilter = VARIABLES_ALLOW_ALL
protected BiPredicate<Expression, MethodNode> methodFilter = METHODS_ALLOW_ALL
- protected List<String> methodList = new ArrayList<String>();
@Override
Object run() {
@@ -73,6 +67,10 @@ class SandboxExtension extends GroovyTypeCheckingExtensionSupport.TypeCheckingDS
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)) {
@@ -84,16 +82,17 @@ class SandboxExtension extends GroovyTypeCheckingExtensionSupport.TypeCheckingDS
}
onMethodSelection { expr, MethodNode methodNode ->
- if (!methodFilter.test(expr, methodNode))
- addStaticTypeError("Not authorized to call this method: $descr", expr)
+ if (!methodFilter.test(expr, methodNode)) {
+ def descriptor = toMethodDescriptor(methodNode)
+ addStaticTypeError("Not authorized to call this method: $descriptor", expr)
+ }
}
}
- private static String prettyPrint(ClassNode node) {
- node.isArray()?"${prettyPrint(node.componentType)}[]":node.toString(false)
- }
-
- private static String toMethodDescriptor(final MethodNode node) {
+ /**
+ * 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)
@@ -108,4 +107,11 @@ class SandboxExtension extends GroovyTypeCheckingExtensionSupport.TypeCheckingDS
sb.append(')')
sb
}
+
+ /**
+ * Helper method for those extending the sandbox.
+ */
+ static String prettyPrint(final ClassNode node) {
+ node.isArray()?"${prettyPrint(node.componentType)}[]":node.toString(false)
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/84701fa0/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
new file mode 100644
index 0000000..3b60e1d
--- /dev/null
+++ b/gremlin-groovy/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/TinkerPopSandboxExtension.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.
+ */
+package org.apache.tinkerpop.gremlin.groovy.jsr223
+
+import org.codehaus.groovy.ast.MethodNode
+import org.codehaus.groovy.ast.expr.Expression
+
+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\\..*",
+ "$gremlinRootStructure\\..*",
+ "$gremlinRootProcess\\..*",
+ "$gremlinRootProcess\\.traversal\\.dsl\\.graph\\..*"]
+
+ methodFilter = (BiPredicate<Expression, MethodNode>) { exp, method ->
+ def descriptor = toMethodDescriptor(method)
+ methodWhiteList.any { descriptor =~ it }
+ }
+ }
+}
[24/31] incubator-tinkerpop git commit: Add more tests for variable
filtering in GremlinGroovyScriptEngine sandbox.
Posted by sp...@apache.org.
Add more tests for variable filtering in GremlinGroovyScriptEngine sandbox.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/2af2c3dd
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/2af2c3dd
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/2af2c3dd
Branch: refs/heads/tp30
Commit: 2af2c3ddecd188bbf400acf910c2f73fcf21da84
Parents: 6af00c6
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Fri Jul 10 09:23:00 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Fri Jul 10 09:23:12 2015 -0400
----------------------------------------------------------------------
.../BlockSomeVariablesSandboxExtension.groovy | 41 +++++++++++++++++
...mlinGroovyScriptEngineSandboxCustomTest.java | 46 +++++++++++++++++++-
.../groovy/jsr223/SandboxExtension.groovy | 13 +++---
3 files changed, 92 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/2af2c3dd/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
new file mode 100644
index 0000000..fcbbc51
--- /dev/null
+++ b/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/BlockSomeVariablesSandboxExtension.groovy
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tinkerpop.gremlin.groovy.jsr223
+
+import org.apache.tinkerpop.gremlin.structure.Graph
+import org.codehaus.groovy.ast.ClassNode
+import org.codehaus.groovy.ast.expr.VariableExpression
+
+import java.util.function.BiPredicate
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+class BlockSomeVariablesSandboxExtension extends SandboxExtension {
+ BlockSomeVariablesSandboxExtension() {
+ gIsAlwaysGraphTraversalSource = false
+ graphIsAlwaysGraphInstance = false
+
+ // variable names must have a length of 3 and they can't be Graph instances
+ variableFilter = (BiPredicate<VariableExpression, Map<String,ClassNode>>) { v, m ->
+ def varType = m[v.name].getTypeClass()
+ v.name.length() > 3 && !Graph.isAssignableFrom(varType)
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/2af2c3dd/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 b9f8fcf..648e688 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
@@ -37,7 +37,6 @@ import static org.junit.Assert.fail;
*/
public class GremlinGroovyScriptEngineSandboxCustomTest extends AbstractGremlinTest {
@Test
- @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
public void shouldEvalGVariableAsSomethingOtherThanGraphTraversalSource() throws Exception {
final CompilerCustomizerProvider standardSandbox = new CompileStaticCustomizerProvider(SandboxExtension.class.getName());
try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(standardSandbox)) {
@@ -60,7 +59,6 @@ public class GremlinGroovyScriptEngineSandboxCustomTest extends AbstractGremlinT
}
@Test
- @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
public void shouldEvalGraphVariableAsSomethingOtherThanGraph() throws Exception {
final CompilerCustomizerProvider standardSandbox = new CompileStaticCustomizerProvider(SandboxExtension.class.getName());
try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(standardSandbox)) {
@@ -81,4 +79,48 @@ public class GremlinGroovyScriptEngineSandboxCustomTest extends AbstractGremlinT
assertEquals(2, engine.eval("graph+1", bindings));
}
}
+
+ @Test
+ @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+ public void shouldNotEvalBecauseSandboxIsConfiguredToNotAcceptGraphInstances() throws Exception {
+ final CompilerCustomizerProvider customSandbox = new CompileStaticCustomizerProvider(
+ BlockSomeVariablesSandboxExtension.class.getName());
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(customSandbox)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("graph", graph);
+ engine.eval("graph.vertices()", bindings);
+ fail("Should have a compile error because sandbox does not allow Graph");
+ } catch (Exception ex) {
+ assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
+ assertThat(ex.getMessage(), containsString("The variable [graph] is undeclared."));
+ }
+ }
+
+ @Test
+ @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+ public void shouldNotEvalBecauseSandboxIsConfiguredToNotAcceptShortVarNames() throws Exception {
+ final CompilerCustomizerProvider customSandbox = new CompileStaticCustomizerProvider(
+ BlockSomeVariablesSandboxExtension.class.getName());
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(customSandbox)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("g", 1);
+ engine.eval("g+1", bindings);
+ fail("Should have a compile error because sandbox wants names > 3");
+ } catch (Exception ex) {
+ assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
+ assertThat(ex.getMessage(), containsString("The variable [g] is undeclared."));
+ }
+ }
+
+ @Test
+ @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+ public void shouldEvalAsVariableRequirementsAreInRangeOfSandbox() throws Exception {
+ final CompilerCustomizerProvider customSandbox = new CompileStaticCustomizerProvider(
+ BlockSomeVariablesSandboxExtension.class.getName());
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(customSandbox)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("graph", 1);
+ assertEquals(2, engine.eval("graph+1", bindings));
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/2af2c3dd/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
index d62ccb3..be0cbca 100644
--- 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
@@ -73,12 +73,13 @@ class SandboxExtension extends GroovyTypeCheckingExtensionSupport.TypeCheckingDS
final Map<String,ClassNode> varTypes = (Map<String,ClassNode>) GremlinGroovyScriptEngine.COMPILE_OPTIONS.get()
.get(GremlinGroovyScriptEngine.COMPILE_OPTIONS_VAR_TYPES)
- if (varTypes.containsKey(var.name) && variableFilter.test(var, varTypes)
- || (var.name == "graph" && !graphIsAlwaysGraphInstance
- || var.name == "g" && !gIsAlwaysGraphTraversalSource)) {
- storeType(var, varTypes.get(var.name))
- handled = true
- return
+ 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
+ }
}
}
[29/31] incubator-tinkerpop git commit: Merge remote-tracking branch
'origin/compiler-customizer' into tp30-582
Posted by sp...@apache.org.
Merge remote-tracking branch 'origin/compiler-customizer' into tp30-582
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/12eb58bf
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/12eb58bf
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/12eb58bf
Branch: refs/heads/tp30
Commit: 12eb58bf3742a549c9cacda11204dac1d21b264d
Parents: 3f5405a 1a3a32d
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Tue Jul 14 08:38:50 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Tue Jul 14 08:38:50 2015 -0400
----------------------------------------------------------------------
.../jsr223/AllowColorSandboxExtension.groovy | 35 ++++
.../BlockSomeVariablesSandboxExtension.groovy | 41 +++++
...ebindAllVariableTypesSandboxExtension.groovy | 29 +++
.../gremlin/groovy/GroovyEnvironmentSuite.java | 11 +-
.../GremlinGroovyScriptEngineOverGraphTest.java | 9 +-
...mlinGroovyScriptEngineSandboxCustomTest.java | 126 +++++++++++++
...GroovyScriptEngineSandboxedStandardTest.java | 131 ++++++++++++++
...nGroovyScriptEngineTinkerPopSandboxTest.java | 98 +++++++++++
gremlin-groovy/pom.xml | 11 --
.../groovy/jsr223/SandboxExtension.groovy | 176 +++++++++++++++++++
.../jsr223/TinkerPopSandboxExtension.groovy | 47 +++++
.../AbstractImportCustomizerProvider.java | 2 +-
.../groovy/CompileStaticCustomizerProvider.java | 60 +++++++
.../groovy/CompilerCustomizerProvider.java | 10 +-
.../groovy/EmptyImportCustomizerProvider.java | 5 +-
.../groovy/NoImportCustomizerProvider.java | 9 +-
.../groovy/SecurityCustomizerProvider.java | 66 -------
.../ThreadInterruptCustomizerProvider.java | 35 ++++
.../TimedInterruptCustomizerProvider.java | 61 +++++++
.../groovy/TypeCheckedCustomizerProvider.java | 64 +++++++
.../gremlin/groovy/engine/ScriptEngines.java | 50 ++++--
.../groovy/jsr223/DependencyManager.java | 10 +-
.../jsr223/GremlinGroovyScriptEngine.java | 122 ++++++-------
.../jsr223/ManagedConcurrentValueMap.java | 7 +-
.../VariableIdentificationCustomizer.java | 91 ++++++++++
.../groovy/engine/GremlinExecutorTest.java | 74 ++++----
...mlinGroovyScriptEngineCompileStaticTest.java | 99 +++++++++++
.../jsr223/GremlinGroovyScriptEngineTest.java | 132 +-------------
...inGroovyScriptEngineThreadInterruptTest.java | 78 ++++++++
...linGroovyScriptEngineTimedInterruptTest.java | 94 ++++++++++
...remlinGroovyScriptEngineTypeCheckedTest.java | 100 +++++++++++
.../groovy/jsr223/PrecompiledExtensions.java | 58 ++++++
.../structure/io/script/ScriptRecordReader.java | 2 +-
.../structure/io/script/ScriptRecordWriter.java | 2 +-
34 files changed, 1587 insertions(+), 358 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/12eb58bf/gremlin-groovy/pom.xml
----------------------------------------------------------------------
[19/31] incubator-tinkerpop git commit: Fixed a bug in how typecheck
extensions were registering.
Posted by sp...@apache.org.
Fixed a bug in how typecheck extensions were registering.
Was fooled into thinking this was working before because of a faulty test.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/6252cfa9
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/6252cfa9
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/6252cfa9
Branch: refs/heads/tp30
Commit: 6252cfa96fb90d4a255cf4b5af9f51f57b7eab33
Parents: f933308
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Thu Jul 9 11:16:57 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Thu Jul 9 11:16:57 2015 -0400
----------------------------------------------------------------------
.../groovy/CompileStaticCustomizerProvider.java | 30 ++++++++--
.../groovy/TypeCheckedCustomizerProvider.java | 19 ++++++-
...mlinGroovyScriptEngineCompileStaticTest.java | 11 +++-
...remlinGroovyScriptEngineTypeCheckedTest.java | 12 +++-
.../groovy/jsr223/PrecompiledExtensions.java | 58 ++++++++++++++++++++
.../resources/PreventColorUsageExtension.groovy | 23 --------
.../PreventCountDownLatchUsageExtension.groovy | 23 --------
7 files changed, 118 insertions(+), 58 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/6252cfa9/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
index c7cdbeb..928f6cd 100644
--- 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
@@ -1,12 +1,32 @@
+/*
+ * 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 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 CompileStatic} transformer to enable type validation on script execution.
@@ -27,14 +47,14 @@ public class CompileStaticCustomizerProvider implements CompilerCustomizerProvid
@Override
public CompilationCustomizer create() {
- final Map<String, Object> compileStaticAnnotationParams = new HashMap<>();
+ final Map<String, Object> annotationParams = new HashMap<>();
if (extensions != null && !extensions.isEmpty()) {
if (extensions.contains(","))
- compileStaticAnnotationParams.put("extensions", extensions.split(","));
+ annotationParams.put("extensions", Stream.of(extensions.split(",")).collect(Collectors.toList()));
else
- compileStaticAnnotationParams.put("extensions", extensions);
+ annotationParams.put("extensions", Collections.singletonList(extensions));
}
- return new ASTTransformationCustomizer(compileStaticAnnotationParams, CompileStatic.class);
+ return new ASTTransformationCustomizer(annotationParams, CompileStatic.class);
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/6252cfa9/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
index 14ff7ca..73f6434 100644
--- 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
@@ -22,8 +22,11 @@ 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.
@@ -38,14 +41,24 @@ public class TypeCheckedCustomizerProvider implements CompilerCustomizerProvider
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> timedInterruptAnnotationParams = new HashMap<>();
- if (extensions != null && !extensions.isEmpty()) timedInterruptAnnotationParams.put("extensions", extensions);
- return new ASTTransformationCustomizer(timedInterruptAnnotationParams, TypeChecked.class);
+ 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/6252cfa9/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 d5ebc09..466d223 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
@@ -24,7 +24,9 @@ import org.junit.Test;
import javax.script.ScriptException;
+import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
/**
@@ -55,12 +57,14 @@ public class GremlinGroovyScriptEngineCompileStaticTest {
assertEquals(255, scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red"));
}
- final CompileStaticCustomizerProvider providerWithExtension = new CompileStaticCustomizerProvider("PreventColorUsageExtension.groovy");
+ final CompileStaticCustomizerProvider providerWithExtension = new CompileStaticCustomizerProvider(
+ PrecompiledExtensions.PreventColorUsageExtension.class.getName());
try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) {
scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red");
fail("Should have failed type checking");
} catch (ScriptException se) {
assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
+ assertThat(se.getMessage(), containsString("Method call is not allowed!"));
}
}
@@ -74,12 +78,14 @@ public class GremlinGroovyScriptEngineCompileStaticTest {
}
final CompileStaticCustomizerProvider providerWithExtension = new CompileStaticCustomizerProvider(
- "PreventColorUsageExtension.groovy,PreventCountDownLatchUsageExtension.groovy");
+ PrecompiledExtensions.PreventColorUsageExtension.class.getName() +
+ "," + PrecompiledExtensions.PreventCountDownLatchUsageExtension.class.getName());
try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) {
scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red");
fail("Should have failed type checking");
} catch (ScriptException se) {
assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
+ assertThat(se.getMessage(), containsString("Method call is not allowed!"));
}
try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) {
@@ -87,6 +93,7 @@ public class GremlinGroovyScriptEngineCompileStaticTest {
fail("Should have failed type checking");
} catch (ScriptException se) {
assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
+ assertThat(se.getMessage(), containsString("Method call is not allowed!"));
}
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/6252cfa9/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 b298750..97367e1 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,13 +18,16 @@
*/
package org.apache.tinkerpop.gremlin.groovy.jsr223;
+import org.apache.tinkerpop.gremlin.groovy.CompileStaticCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.TypeCheckedCustomizerProvider;
import org.codehaus.groovy.control.MultipleCompilationErrorsException;
import org.junit.Test;
import javax.script.ScriptException;
+import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
/**
@@ -55,12 +58,14 @@ public class GremlinGroovyScriptEngineTypeCheckedTest {
assertEquals(255, scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red"));
}
- final TypeCheckedCustomizerProvider providerWithExtension = new TypeCheckedCustomizerProvider("PreventColorUsageExtension.groovy");
+ final CompileStaticCustomizerProvider providerWithExtension = new CompileStaticCustomizerProvider(
+ PrecompiledExtensions.PreventColorUsageExtension.class.getName());
try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) {
scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red");
fail("Should have failed type checking");
} catch (ScriptException se) {
assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
+ assertThat(se.getMessage(), containsString("Method call is not allowed!"));
}
}
@@ -74,12 +79,14 @@ public class GremlinGroovyScriptEngineTypeCheckedTest {
}
final TypeCheckedCustomizerProvider providerWithExtension = new TypeCheckedCustomizerProvider(
- "PreventColorUsageExtension.groovy,PreventCountDownLatchUsageExtension.groovy");
+ PrecompiledExtensions.PreventColorUsageExtension.class.getName() +
+ "," + PrecompiledExtensions.PreventCountDownLatchUsageExtension.class.getName());
try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) {
scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red");
fail("Should have failed type checking");
} catch (ScriptException se) {
assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
+ assertThat(se.getMessage(), containsString("Method call is not allowed!"));
}
try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) {
@@ -87,6 +94,7 @@ public class GremlinGroovyScriptEngineTypeCheckedTest {
fail("Should have failed type checking");
} catch (ScriptException se) {
assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
+ assertThat(se.getMessage(), containsString("Method call is not allowed!"));
}
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/6252cfa9/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/PrecompiledExtensions.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/PrecompiledExtensions.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/PrecompiledExtensions.java
new file mode 100644
index 0000000..d9e6a5c
--- /dev/null
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/PrecompiledExtensions.java
@@ -0,0 +1,58 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tinkerpop.gremlin.groovy.jsr223;
+
+import org.codehaus.groovy.ast.MethodNode;
+import org.codehaus.groovy.ast.expr.Expression;
+import org.codehaus.groovy.transform.stc.AbstractTypeCheckingExtension;
+import org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class PrecompiledExtensions {
+ public static class PreventColorUsageExtension extends AbstractTypeCheckingExtension {
+
+ public PreventColorUsageExtension(final StaticTypeCheckingVisitor typeCheckingVisitor) {
+ super(typeCheckingVisitor);
+ }
+
+ @Override
+ public void onMethodSelection(final Expression expression, final MethodNode target) {
+ if (target.getDeclaringClass().getName().equals("java.awt.Color")) {
+ addStaticTypeError("Method call is not allowed!", expression);
+ }
+ }
+ }
+
+ public static class PreventCountDownLatchUsageExtension extends AbstractTypeCheckingExtension {
+
+ public PreventCountDownLatchUsageExtension(final StaticTypeCheckingVisitor typeCheckingVisitor) {
+ super(typeCheckingVisitor);
+ }
+
+ @Override
+ public void onMethodSelection(final Expression expression, final MethodNode target) {
+ if (target.getDeclaringClass().getName().equals("java.util.concurrent.CountDownLatch")) {
+ addStaticTypeError("Method call is not allowed!", expression);
+ }
+ }
+
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/6252cfa9/gremlin-groovy/src/test/resources/PreventColorUsageExtension.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/resources/PreventColorUsageExtension.groovy b/gremlin-groovy/src/test/resources/PreventColorUsageExtension.groovy
deleted file mode 100644
index 38eac69..0000000
--- a/gremlin-groovy/src/test/resources/PreventColorUsageExtension.groovy
+++ /dev/null
@@ -1,23 +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.
- */
-onMethodSelection { expr, methodNode ->
- if (methodNode.declaringClass.name=='java.awt.Color') {
- addStaticTypeError("Method call is not allowed!", expr)
- }
-}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/6252cfa9/gremlin-groovy/src/test/resources/PreventCountDownLatchUsageExtension.groovy
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/resources/PreventCountDownLatchUsageExtension.groovy b/gremlin-groovy/src/test/resources/PreventCountDownLatchUsageExtension.groovy
deleted file mode 100644
index 74af460..0000000
--- a/gremlin-groovy/src/test/resources/PreventCountDownLatchUsageExtension.groovy
+++ /dev/null
@@ -1,23 +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.
- */
-onMethodSelection { expr, methodNode ->
- if (methodNode.declaringClass.name=='java.util.concurrent.CountDownLatch') {
- addStaticTypeError("Method call is not allowed!", expr)
- }
-}
\ No newline at end of file
[10/31] incubator-tinkerpop git commit: Created a
TimedInterruptCustomizerProvider.
Posted by sp...@apache.org.
Created a TimedInterruptCustomizerProvider.
Add the TimedInterrupt compilation customizer via the same mechanism as other customizers. No longer treat this as a special case with the timeout in millis passed directly to the GremlinGroovyScriptEngine.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/95b6eb8a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/95b6eb8a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/95b6eb8a
Branch: refs/heads/tp30
Commit: 95b6eb8a954b534aba289803872660faae81ac18
Parents: 3df735b
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Jul 8 13:51:35 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Jul 8 13:51:35 2015 -0400
----------------------------------------------------------------------
.../TimedInterruptCustomizerProvider.java | 57 ++++++++++++++++++++
.../gremlin/groovy/engine/ScriptEngines.java | 16 +++---
.../jsr223/GremlinGroovyScriptEngine.java | 20 -------
.../jsr223/GremlinGroovyScriptEngineTest.java | 15 ++++--
4 files changed, 77 insertions(+), 31 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/95b6eb8a/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
new file mode 100644
index 0000000..a8c8eb1
--- /dev/null
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/TimedInterruptCustomizerProvider.java
@@ -0,0 +1,57 @@
+/*
+ * 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;
+ }
+
+ @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/95b6eb8a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
index 3e884ae..ba310ec 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
@@ -18,8 +18,10 @@
*/
package org.apache.tinkerpop.gremlin.groovy.engine;
+import org.apache.tinkerpop.gremlin.groovy.CompilerCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.DefaultImportCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.SecurityCustomizerProvider;
+import org.apache.tinkerpop.gremlin.groovy.TimedInterruptCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.jsr223.DependencyManager;
import org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine;
import org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineFactory;
@@ -329,24 +331,26 @@ public class ScriptEngines implements AutoCloseable {
// generically with the DependencyManager interface, but going to wait to see how other ScriptEngines
// develop for TinkerPop3 before committing too deeply here to any specific way of doing this.
if (language.equals(gremlinGroovyScriptEngineFactory.getLanguageName())) {
+ final List<CompilerCustomizerProvider> providers = new ArrayList<>();
+ providers.add(new DefaultImportCustomizerProvider(imports, staticImports));
+
final String clazz = (String) config.getOrDefault("sandbox", "");
- SecurityCustomizerProvider securityCustomizerProvider = null;
if (!clazz.isEmpty()) {
try {
final Class providerClass = Class.forName(clazz);
final GroovyInterceptor interceptor = (GroovyInterceptor) providerClass.newInstance();
- securityCustomizerProvider = new SecurityCustomizerProvider(interceptor);
+ providers.add(new SecurityCustomizerProvider(interceptor));
} catch (Exception ex) {
logger.warn("Could not instantiate GroovyInterceptor implementation [%s] for the SecurityCustomizerProvider. It will not be applied.", clazz);
- securityCustomizerProvider = null;
}
}
final long interruptionTimeout = ((Number) config.getOrDefault("interruptionTimeout",
- GremlinGroovyScriptEngine.DEFAULT_SCRIPT_EVALUATION_TIMEOUT)).longValue();
+ TimedInterruptCustomizerProvider.DEFAULT_INTERRUPTION_TIMEOUT)).longValue();
+ if (interruptionTimeout > 0) providers.add(new TimedInterruptCustomizerProvider(interruptionTimeout));
- return Optional.of((ScriptEngine) new GremlinGroovyScriptEngine(interruptionTimeout,
- new DefaultImportCustomizerProvider(imports, staticImports), securityCustomizerProvider));
+ final CompilerCustomizerProvider[] providerArray = new CompilerCustomizerProvider[providers.size()];
+ return Optional.of((ScriptEngine) new GremlinGroovyScriptEngine(providers.toArray(providerArray)));
} else {
return Optional.ofNullable(SCRIPT_ENGINE_MANAGER.getEngineByName(language));
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/95b6eb8a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
index bb8b912..d93fd01 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
@@ -86,7 +86,6 @@ import java.util.stream.Collectors;
*/
public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements DependencyManager, AutoCloseable {
- public static final long DEFAULT_SCRIPT_EVALUATION_TIMEOUT = 60000;
public static final String KEY_REFERENCE_TYPE = "#jsr223.groovy.engine.keep.globals";
public static final String REFERENCE_TYPE_PHANTOM = "phantom";
public static final String REFERENCE_TYPE_WEAK = "weak";
@@ -131,11 +130,6 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
private ImportCustomizerProvider importCustomizerProvider;
private Optional<SecurityCustomizerProvider> securityProvider;
- /**
- * If this value is zero then no timeout is applied.
- */
- private final long scriptEvaluationTimeout;
-
private final Set<Artifact> artifactsToUse = new HashSet<>();
private final List<CompilerCustomizerProvider> customizerProviders;
@@ -145,11 +139,6 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
}
public GremlinGroovyScriptEngine(final CompilerCustomizerProvider... compilerCustomizerProviders) {
- this(DEFAULT_SCRIPT_EVALUATION_TIMEOUT, compilerCustomizerProviders);
- }
-
- public GremlinGroovyScriptEngine(final long scriptEvaluationTimeout,
- final CompilerCustomizerProvider... compilerCustomizerProviders) {
final List<CompilerCustomizerProvider> providers = Arrays.asList(compilerCustomizerProviders);
GremlinLoader.load();
@@ -169,7 +158,6 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
.filter(p -> p != null && !(p instanceof SecurityCustomizerProvider || p instanceof ImportCustomizerProvider))
.collect(Collectors.toList());
- this.scriptEvaluationTimeout = scriptEvaluationTimeout;
createClassLoader();
}
@@ -496,14 +484,6 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
if (this.securityProvider.isPresent())
conf.addCompilationCustomizers(this.securityProvider.get().create());
- if (scriptEvaluationTimeout > 0) {
- final Map<String, Object> timedInterruptAnnotationParams = new HashMap<>();
- timedInterruptAnnotationParams.put("value", scriptEvaluationTimeout);
- timedInterruptAnnotationParams.put("unit", GeneralUtils.propX(GeneralUtils.classX(TimeUnit.class), TimeUnit.MILLISECONDS.toString()));
- timedInterruptAnnotationParams.put("checkOnMethodStart", false);
- conf.addCompilationCustomizers(new ASTTransformationCustomizer(timedInterruptAnnotationParams, TimedInterrupt.class));
- }
-
conf.addCompilationCustomizers(new ASTTransformationCustomizer(ThreadInterrupt.class));
customizerProviders.forEach(p -> conf.addCompilationCustomizers(p.create()));
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/95b6eb8a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
index ee6abe9..15c4cb4 100644
--- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
@@ -23,6 +23,7 @@ import groovy.lang.Script;
import org.apache.tinkerpop.gremlin.groovy.DefaultImportCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.NoImportCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.SecurityCustomizerProvider;
+import org.apache.tinkerpop.gremlin.groovy.TimedInterruptCustomizerProvider;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
import org.junit.Test;
@@ -310,7 +311,8 @@ public class GremlinGroovyScriptEngineTest {
@Test
public void shouldTimeoutScriptOnTimedWhile() throws Exception {
- final ScriptEngine engine = new GremlinGroovyScriptEngine(3000, new DefaultImportCustomizerProvider());
+ final ScriptEngine engine = new GremlinGroovyScriptEngine(
+ new TimedInterruptCustomizerProvider(1000), new DefaultImportCustomizerProvider());
try {
engine.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 10000) {}");
fail("This should have timed out");
@@ -321,8 +323,9 @@ public class GremlinGroovyScriptEngineTest {
@Test
public void shouldTimeoutScriptOnTimedWhileOnceEngineHasBeenAliveForLongerThanTimeout() throws Exception {
- final ScriptEngine engine = new GremlinGroovyScriptEngine(3000, new DefaultImportCustomizerProvider());
- Thread.sleep(4000);
+ final ScriptEngine engine = new GremlinGroovyScriptEngine(
+ new TimedInterruptCustomizerProvider(1000), new DefaultImportCustomizerProvider());
+ Thread.sleep(2000);
try {
engine.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 10000) {}");
fail("This should have timed out");
@@ -335,7 +338,8 @@ public class GremlinGroovyScriptEngineTest {
@Test
public void shouldContinueToEvalScriptsEvenWithTimedInterrupt() throws Exception {
- final ScriptEngine engine = new GremlinGroovyScriptEngine(50, new DefaultImportCustomizerProvider());
+ final ScriptEngine engine = new GremlinGroovyScriptEngine(
+ new TimedInterruptCustomizerProvider(50), new DefaultImportCustomizerProvider());
for (int ix = 0; ix < 10; ix++) {
try {
@@ -356,7 +360,8 @@ public class GremlinGroovyScriptEngineTest {
@Test
public void shouldNotTimeoutStandaloneFunction() throws Exception {
// use a super fast timeout which should not prevent the call of a cached function
- final ScriptEngine engine = new GremlinGroovyScriptEngine(1, new DefaultImportCustomizerProvider());
+ final ScriptEngine engine = new GremlinGroovyScriptEngine(
+ new TimedInterruptCustomizerProvider(1), new DefaultImportCustomizerProvider());
engine.eval("def addItUp(x,y) { x + y }");
assertEquals(3, engine.eval("addItUp(1,2)"));
[16/31] incubator-tinkerpop git commit: Pulled out TimedInterrupt
compiler provider tests to their own case
Posted by sp...@apache.org.
Pulled out TimedInterrupt compiler provider tests to their own case
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/9149c205
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/9149c205
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/9149c205
Branch: refs/heads/tp30
Commit: 9149c2058577c9c7fc5b0c3f703ac8baeb5ab884
Parents: 6b8628b
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Thu Jul 9 08:31:47 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Thu Jul 9 08:31:47 2015 -0400
----------------------------------------------------------------------
.../jsr223/GremlinGroovyScriptEngineTest.java | 58 ------------
...linGroovyScriptEngineTimedInterruptTest.java | 94 ++++++++++++++++++++
2 files changed, 94 insertions(+), 58 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9149c205/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
index 15c4cb4..3fda86b 100644
--- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
@@ -309,64 +309,6 @@ public class GremlinGroovyScriptEngineTest {
assertEquals("轉注", engine.eval("'轉注'"));
}
- @Test
- public void shouldTimeoutScriptOnTimedWhile() throws Exception {
- final ScriptEngine engine = new GremlinGroovyScriptEngine(
- new TimedInterruptCustomizerProvider(1000), new DefaultImportCustomizerProvider());
- try {
- engine.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 10000) {}");
- fail("This should have timed out");
- } catch (ScriptException se) {
- assertEquals(TimeoutException.class, se.getCause().getCause().getClass());
- }
- }
-
- @Test
- public void shouldTimeoutScriptOnTimedWhileOnceEngineHasBeenAliveForLongerThanTimeout() throws Exception {
- final ScriptEngine engine = new GremlinGroovyScriptEngine(
- new TimedInterruptCustomizerProvider(1000), new DefaultImportCustomizerProvider());
- Thread.sleep(2000);
- try {
- engine.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 10000) {}");
- fail("This should have timed out");
- } catch (ScriptException se) {
- assertEquals(TimeoutException.class, se.getCause().getCause().getClass());
- }
-
- assertEquals(2, engine.eval("1+1"));
- }
-
- @Test
- public void shouldContinueToEvalScriptsEvenWithTimedInterrupt() throws Exception {
- final ScriptEngine engine = new GremlinGroovyScriptEngine(
- new TimedInterruptCustomizerProvider(50), new DefaultImportCustomizerProvider());
-
- for (int ix = 0; ix < 10; ix++) {
- try {
- // this script takes 10 ms longer than the interruptionTimeout
- engine.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 60) {}");
- fail("This should have timed out");
- } catch (ScriptException se) {
- assertEquals(TimeoutException.class, se.getCause().getCause().getClass());
- }
-
- // this script takes 10 ms less than the interruptionTimeout
- assertEquals("test", engine.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 40) {};'test'"));
- }
-
- assertEquals(2, engine.eval("1+1"));
- }
-
- @Test
- public void shouldNotTimeoutStandaloneFunction() throws Exception {
- // use a super fast timeout which should not prevent the call of a cached function
- final ScriptEngine engine = new GremlinGroovyScriptEngine(
- new TimedInterruptCustomizerProvider(1), new DefaultImportCustomizerProvider());
- engine.eval("def addItUp(x,y) { x + y }");
-
- assertEquals(3, engine.eval("addItUp(1,2)"));
- }
-
public static class DenyAll extends GroovyValueFilter {
@Override
public Object filter(final Object o) {
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/9149c205/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
new file mode 100644
index 0000000..40d0104
--- /dev/null
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTimedInterruptTest.java
@@ -0,0 +1,94 @@
+/*
+ * 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.groovy.DefaultImportCustomizerProvider;
+import org.apache.tinkerpop.gremlin.groovy.TimedInterruptCustomizerProvider;
+import org.junit.Test;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptException;
+import java.util.concurrent.TimeoutException;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class GremlinGroovyScriptEngineTimedInterruptTest {
+
+ @Test
+ public void shouldTimeoutScriptOnTimedWhile() throws Exception {
+ final ScriptEngine engine = new GremlinGroovyScriptEngine(
+ new TimedInterruptCustomizerProvider(1000), new DefaultImportCustomizerProvider());
+ try {
+ engine.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 10000) {}");
+ fail("This should have timed out");
+ } catch (ScriptException se) {
+ assertEquals(TimeoutException.class, se.getCause().getCause().getClass());
+ }
+ }
+
+ @Test
+ public void shouldTimeoutScriptOnTimedWhileOnceEngineHasBeenAliveForLongerThanTimeout() throws Exception {
+ final ScriptEngine engine = new GremlinGroovyScriptEngine(
+ new TimedInterruptCustomizerProvider(1000), new DefaultImportCustomizerProvider());
+ Thread.sleep(2000);
+ try {
+ engine.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 10000) {}");
+ fail("This should have timed out");
+ } catch (ScriptException se) {
+ assertEquals(TimeoutException.class, se.getCause().getCause().getClass());
+ }
+
+ assertEquals(2, engine.eval("1+1"));
+ }
+
+ @Test
+ public void shouldContinueToEvalScriptsEvenWithTimedInterrupt() throws Exception {
+ final ScriptEngine engine = new GremlinGroovyScriptEngine(
+ new TimedInterruptCustomizerProvider(50), new DefaultImportCustomizerProvider());
+
+ for (int ix = 0; ix < 10; ix++) {
+ try {
+ // this script takes 10 ms longer than the interruptionTimeout
+ engine.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 60) {}");
+ fail("This should have timed out");
+ } catch (ScriptException se) {
+ assertEquals(TimeoutException.class, se.getCause().getCause().getClass());
+ }
+
+ // this script takes 10 ms less than the interruptionTimeout
+ assertEquals("test", engine.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 40) {};'test'"));
+ }
+
+ assertEquals(2, engine.eval("1+1"));
+ }
+
+ @Test
+ public void shouldNotTimeoutStandaloneFunction() throws Exception {
+ // use a super fast timeout which should not prevent the call of a cached function
+ final ScriptEngine engine = new GremlinGroovyScriptEngine(
+ new TimedInterruptCustomizerProvider(1), new DefaultImportCustomizerProvider());
+ engine.eval("def addItUp(x,y) { x + y }");
+
+ assertEquals(3, engine.eval("addItUp(1,2)"));
+ }
+}
[03/31] incubator-tinkerpop git commit: Fix compile errors after
merge.
Posted by sp...@apache.org.
Fix compile errors after merge.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/569ef03a
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/569ef03a
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/569ef03a
Branch: refs/heads/tp30
Commit: 569ef03a0042342a659d9bac19738e6ed46c04a4
Parents: 0e1c86b
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Jul 8 11:56:30 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Jul 8 11:56:30 2015 -0400
----------------------------------------------------------------------
.../apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java | 5 ++---
.../gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java | 2 +-
2 files changed, 3 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/569ef03a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
index 5ff4494..3e884ae 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
@@ -345,9 +345,8 @@ public class ScriptEngines implements AutoCloseable {
final long interruptionTimeout = ((Number) config.getOrDefault("interruptionTimeout",
GremlinGroovyScriptEngine.DEFAULT_SCRIPT_EVALUATION_TIMEOUT)).longValue();
- return Optional.of((ScriptEngine) new GremlinGroovyScriptEngine(
- new DefaultImportCustomizerProvider(imports, staticImports), securityCustomizerProvider,
- interruptionTimeout));
+ return Optional.of((ScriptEngine) new GremlinGroovyScriptEngine(interruptionTimeout,
+ new DefaultImportCustomizerProvider(imports, staticImports), securityCustomizerProvider));
} else {
return Optional.ofNullable(SCRIPT_ENGINE_MANAGER.getEngineByName(language));
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/569ef03a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
index 2bdbda3..ee6abe9 100644
--- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
@@ -335,7 +335,7 @@ public class GremlinGroovyScriptEngineTest {
@Test
public void shouldContinueToEvalScriptsEvenWithTimedInterrupt() throws Exception {
- final ScriptEngine engine = new GremlinGroovyScriptEngine(new DefaultImportCustomizerProvider(), null, 50);
+ final ScriptEngine engine = new GremlinGroovyScriptEngine(50, new DefaultImportCustomizerProvider());
for (int ix = 0; ix < 10; ix++) {
try {
[18/31] incubator-tinkerpop git commit: Add test for multiple
extensions on TypeChecking
Posted by sp...@apache.org.
Add test for multiple extensions on TypeChecking
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/f9333087
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/f9333087
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/f9333087
Branch: refs/heads/tp30
Commit: f93330870fad731c9a4af4eb06e0bb27c78171f3
Parents: 8665a0f
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Thu Jul 9 09:51:22 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Thu Jul 9 09:51:22 2015 -0400
----------------------------------------------------------------------
...remlinGroovyScriptEngineTypeCheckedTest.java | 26 ++++++++++++++++++++
1 file changed, 26 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/f9333087/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 0a7639e..b298750 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
@@ -63,4 +63,30 @@ public class GremlinGroovyScriptEngineTypeCheckedTest {
assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
}
}
+
+ @Test
+ public void shouldTypeCheckWithMultipleExtension() throws Exception {
+ // with no type checking extension this should pass
+ final TypeCheckedCustomizerProvider providerNoExtension = new TypeCheckedCustomizerProvider();
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerNoExtension)) {
+ assertEquals(255, scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red"));
+ assertEquals(1l, scriptEngine.eval("def c = new java.util.concurrent.CountDownLatch(1); c.count"));
+ }
+
+ final TypeCheckedCustomizerProvider providerWithExtension = new TypeCheckedCustomizerProvider(
+ "PreventColorUsageExtension.groovy,PreventCountDownLatchUsageExtension.groovy");
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) {
+ scriptEngine.eval("def c = new java.awt.Color(255, 255, 255); c.red");
+ fail("Should have failed type checking");
+ } catch (ScriptException se) {
+ assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
+ }
+
+ try (GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(providerWithExtension)) {
+ scriptEngine.eval("def c = new java.util.concurrent.CountDownLatch(1); c.count");
+ fail("Should have failed type checking");
+ } catch (ScriptException se) {
+ assertEquals(MultipleCompilationErrorsException.class, se.getCause().getClass());
+ }
+ }
}
[13/31] incubator-tinkerpop git commit: Both TimedInterrupt and
ThreadInterrupt compiler customizers are configurable.
Posted by sp...@apache.org.
Both TimedInterrupt and ThreadInterrupt compiler customizers are configurable.
In other words, they can be configured from yaml/properties into the GremlinExecutor which further unifies this model. This design also allows for arbitrary and custom CompilerCustomizerProviders to be added.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/37796826
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/37796826
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/37796826
Branch: refs/heads/tp30
Commit: 37796826dce643648b3148807fe82d13e9790190
Parents: 17ed3ce
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Jul 8 17:07:34 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Jul 8 17:07:34 2015 -0400
----------------------------------------------------------------------
.../TimedInterruptCustomizerProvider.java | 6 ++-
.../gremlin/groovy/engine/ScriptEngines.java | 36 ++++++++++++----
.../groovy/engine/GremlinExecutorTest.java | 44 +++++++++++++++++++-
.../jsr223/GremlinGroovyScriptEngineTest.java | 2 +
4 files changed, 78 insertions(+), 10 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/37796826/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
index a8c8eb1..44dfe21 100644
--- 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
@@ -42,10 +42,14 @@ public class TimedInterruptCustomizerProvider implements CompilerCustomizerProvi
this(DEFAULT_INTERRUPTION_TIMEOUT);
}
- public TimedInterruptCustomizerProvider(final long interruptionTimeout) {
+ 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<>();
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/37796826/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
index 24b9f58..ba7157e 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
@@ -41,8 +41,10 @@ import javax.script.ScriptException;
import java.io.Closeable;
import java.io.IOException;
import java.io.Reader;
+import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -52,6 +54,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.stream.Collectors;
+import java.util.stream.Stream;
/**
* Holds a batch of the configured {@code ScriptEngine} objects for the server.
@@ -342,16 +345,35 @@ public class ScriptEngines implements AutoCloseable {
final GroovyInterceptor interceptor = (GroovyInterceptor) providerClass.newInstance();
providers.add(new SecurityCustomizerProvider(interceptor));
} catch (Exception ex) {
- logger.warn("Could not instantiate GroovyInterceptor implementation [%s] for the SecurityCustomizerProvider. It will not be applied.", clazz);
+ logger.warn("Could not instantiate GroovyInterceptor implementation [{}] for the SecurityCustomizerProvider. It will not be applied.", clazz);
}
}
- final long interruptionTimeout = ((Number) config.getOrDefault("interruptionTimeout",
- TimedInterruptCustomizerProvider.DEFAULT_INTERRUPTION_TIMEOUT)).longValue();
- if (interruptionTimeout > 0) providers.add(new TimedInterruptCustomizerProvider(interruptionTimeout));
-
- final boolean threadInterrupt = (boolean) config.getOrDefault("enableThreadInterrupt", true);
- if (threadInterrupt) providers.add(new ThreadInterruptCustomizerProvider());
+ // the key to the config of the compilerCustomizerProvider is the fully qualified classname of a
+ // CompilerCustomizerProvider. the value is a list of arguments to pass to an available constructor.
+ // the arguments must match in terms of type, so given that configuration typically comes from yaml
+ // or properties file, it is best to stick to primitive values when possible here for simplicity.
+ final Map<String,Object> compilerCustomizerProviders = (Map<String,Object>) config.getOrDefault(
+ "compilerCustomizerProviders", Collections.emptyMap());
+ compilerCustomizerProviders.forEach((k,v) -> {
+ try {
+ final Class providerClass = Class.forName(k);
+ if (v != null && v instanceof List && ((List) v).size() > 0) {
+ final List<Object> l = (List) v;
+ final Object[] args = new Object[l.size()];
+ l.toArray(args);
+
+ final Class<?>[] argClasses = new Class<?>[args.length];
+ Stream.of(args).map(a -> a.getClass()).collect(Collectors.toList()).toArray(argClasses);
+ final Constructor constructor = providerClass.getConstructor(argClasses);
+ providers.add((CompilerCustomizerProvider) constructor.newInstance(args));
+ } else {
+ providers.add((CompilerCustomizerProvider) providerClass.newInstance());
+ }
+ } catch(Exception ex) {
+ logger.warn("Could not instantiate CompilerCustomizerProvider implementation [{}]. It will not be applied.", clazz);
+ }
+ });
final CompilerCustomizerProvider[] providerArray = new CompilerCustomizerProvider[providers.size()];
return Optional.of((ScriptEngine) new GremlinGroovyScriptEngine(providers.toArray(providerArray)));
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/37796826/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 480c72f..6604b78 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,6 +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.GremlinGroovyScriptEngineTest;
import org.junit.Test;
import org.kohsuke.groovy.sandbox.GroovyInterceptor;
@@ -28,6 +30,7 @@ import javax.script.Bindings;
import javax.script.CompiledScript;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
@@ -435,9 +438,14 @@ public class GremlinExecutorTest {
@Test
public void shouldContinueToEvalScriptsEvenWithTimedInterrupt() throws Exception {
- GroovyInterceptor.getApplicableInterceptors().forEach(GroovyInterceptor::unregister);
+ final Map<String,List<Object>> compilerCustomizerConfig = new HashMap<>();
+ final List<Object> args = new ArrayList<>();
+ args.add(50);
+ compilerCustomizerConfig.put(TimedInterruptCustomizerProvider.class.getName(), args);
+
final Map<String, Object> config = new HashMap<>();
- config.put("interruptionTimeout", 50l);
+ config.put("compilerCustomizerProviders", compilerCustomizerConfig);
+
final GremlinExecutor gremlinExecutor = GremlinExecutor.build()
.addEngineSettings("gremlin-groovy",
Collections.emptyList(),
@@ -462,6 +470,38 @@ public class GremlinExecutorTest {
gremlinExecutor.close();
}
+ @Test
+ public void shouldInterruptWhile() throws Exception {
+ final Map<String,List<Object>> compilerCustomizerConfig = new HashMap<>();
+ compilerCustomizerConfig.put(ThreadInterruptCustomizerProvider.class.getName(), new ArrayList<>());
+
+ final Map<String, Object> config = new HashMap<>();
+ config.put("compilerCustomizerProviders", compilerCustomizerConfig);
+
+ final GremlinExecutor gremlinExecutor = GremlinExecutor.build()
+ .addEngineSettings("gremlin-groovy",
+ Collections.emptyList(),
+ Collections.emptyList(),
+ Arrays.asList(PATHS.get("GremlinExecutorInit.groovy")),
+ config)
+ .create();
+ final AtomicBoolean asserted = new AtomicBoolean(false);
+
+ final Thread t = new Thread(() -> {
+ try {
+ gremlinExecutor.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 10000) {}").get();
+ } catch (Exception se) {
+ asserted.set(se instanceof InterruptedException);
+ }
+ });
+
+ t.start();
+ Thread.sleep(100);
+ t.interrupt();
+ while(t.isAlive()) {}
+
+ assertTrue(asserted.get());
+ }
@Test
public void shouldSecureAll() throws Exception {
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/37796826/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
index 8f71588..19f56d8 100644
--- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
@@ -372,6 +372,7 @@ public class GremlinGroovyScriptEngineTest {
});
t.start();
+ Thread.sleep(100);
t.interrupt();
while(t.isAlive()) {}
@@ -394,6 +395,7 @@ public class GremlinGroovyScriptEngineTest {
});
t.start();
+ Thread.sleep(100);
t.interrupt();
while(t.isAlive()) {}
[22/31] incubator-tinkerpop git commit: More tests for sandboxing the
GremlinGroovyScriptEngine.
Posted by sp...@apache.org.
More tests for sandboxing the GremlinGroovyScriptEngine.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/e5988298
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/e5988298
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/e5988298
Branch: refs/heads/tp30
Commit: e5988298766095b8ef6e4f5a287221d4bd850c75
Parents: 927b5ef
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Fri Jul 10 08:02:21 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Fri Jul 10 08:02:21 2015 -0400
----------------------------------------------------------------------
...ebindAllVariableTypesSandboxExtension.groovy | 29 +++++++
.../gremlin/groovy/GroovyEnvironmentSuite.java | 2 +
...mlinGroovyScriptEngineSandboxCustomTest.java | 84 ++++++++++++++++++++
3 files changed, 115 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e5988298/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
new file mode 100644
index 0000000..c475016
--- /dev/null
+++ b/gremlin-groovy-test/src/main/groovy/org/apache/tinkerpop/gremlin/groovy/jsr223/RebindAllVariableTypesSandboxExtension.groovy
@@ -0,0 +1,29 @@
+/*
+ * 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
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+class RebindAllVariableTypesSandboxExtension extends SandboxExtension {
+ RebindAllVariableTypesSandboxExtension() {
+ gIsAlwaysGraphTraversalSource = false
+ graphIsAlwaysGraphInstance = false
+ }
+}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e5988298/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/GroovyEnvironmentSuite.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/GroovyEnvironmentSuite.java b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/GroovyEnvironmentSuite.java
index c553542..67c6ff6 100644
--- a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/GroovyEnvironmentSuite.java
+++ b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/GroovyEnvironmentSuite.java
@@ -23,6 +23,7 @@ import org.apache.tinkerpop.gremlin.AbstractGremlinTest;
import org.apache.tinkerpop.gremlin.GraphManager;
import org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutorOverGraphTest;
import org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineOverGraphTest;
+import org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineSandboxCustomTest;
import org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineSandboxedStandardTest;
import org.apache.tinkerpop.gremlin.groovy.loaders.SugarLoader;
import org.apache.tinkerpop.gremlin.groovy.loaders.SugarLoaderTest;
@@ -51,6 +52,7 @@ public class GroovyEnvironmentSuite extends AbstractGremlinSuite {
*/
private static final Class<?>[] allTests = new Class<?>[]{
GremlinGroovyScriptEngineOverGraphTest.class,
+ GremlinGroovyScriptEngineSandboxCustomTest.class,
GremlinGroovyScriptEngineSandboxedStandardTest.class,
GremlinExecutorOverGraphTest.class,
SugarLoaderTest.class,
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/e5988298/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
new file mode 100644
index 0000000..b9f8fcf
--- /dev/null
+++ b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineSandboxCustomTest.java
@@ -0,0 +1,84 @@
+/*
+ * 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.AbstractGremlinTest;
+import org.apache.tinkerpop.gremlin.LoadGraphWith;
+import org.apache.tinkerpop.gremlin.groovy.CompileStaticCustomizerProvider;
+import org.apache.tinkerpop.gremlin.groovy.CompilerCustomizerProvider;
+import org.codehaus.groovy.control.MultipleCompilationErrorsException;
+import org.junit.Test;
+
+import javax.script.Bindings;
+
+import static org.hamcrest.Matchers.containsString;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class GremlinGroovyScriptEngineSandboxCustomTest extends AbstractGremlinTest {
+ @Test
+ @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+ public void shouldEvalGVariableAsSomethingOtherThanGraphTraversalSource() throws Exception {
+ final CompilerCustomizerProvider standardSandbox = new CompileStaticCustomizerProvider(SandboxExtension.class.getName());
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(standardSandbox)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("g", 1);
+ engine.eval("g+1", bindings);
+ fail("Should have a compile error because 'g' is expected to be GraphTraversalSource");
+ } catch (Exception ex) {
+ assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
+ assertThat(ex.getCause().getMessage(), containsString("Cannot find matching method"));
+ }
+
+ final CompilerCustomizerProvider customSandbox = new CompileStaticCustomizerProvider(
+ RebindAllVariableTypesSandboxExtension.class.getName());
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(customSandbox)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("g", 1);
+ assertEquals(2, engine.eval("g+1", bindings));
+ }
+ }
+
+ @Test
+ @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+ public void shouldEvalGraphVariableAsSomethingOtherThanGraph() throws Exception {
+ final CompilerCustomizerProvider standardSandbox = new CompileStaticCustomizerProvider(SandboxExtension.class.getName());
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(standardSandbox)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("graph", 1);
+ engine.eval("graph+1", bindings);
+ fail("Should have a compile error because 'graph' is expected to be Graph");
+ } catch (Exception ex) {
+ assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
+ assertThat(ex.getCause().getMessage(), containsString("Cannot find matching method"));
+ }
+
+ final CompilerCustomizerProvider customSandbox = new CompileStaticCustomizerProvider(
+ RebindAllVariableTypesSandboxExtension.class.getName());
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(customSandbox)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("graph", 1);
+ assertEquals(2, engine.eval("graph+1", bindings));
+ }
+ }
+}
[21/31] incubator-tinkerpop git commit: Break out sandboxed
scriptengine tests to their own case.
Posted by sp...@apache.org.
Break out sandboxed scriptengine tests to their own case.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/927b5ef4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/927b5ef4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/927b5ef4
Branch: refs/heads/tp30
Commit: 927b5ef4966bec02e65a8fec6698ce094d05764b
Parents: c47426d
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Fri Jul 10 07:43:05 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Fri Jul 10 07:43:05 2015 -0400
----------------------------------------------------------------------
.../gremlin/groovy/GroovyEnvironmentSuite.java | 7 +-
.../GremlinGroovyScriptEngineOverGraphTest.java | 136 -------------------
...GroovyScriptEngineSandboxedStandardTest.java | 131 ++++++++++++++++++
3 files changed, 133 insertions(+), 141 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/927b5ef4/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/GroovyEnvironmentSuite.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/GroovyEnvironmentSuite.java b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/GroovyEnvironmentSuite.java
index 4a775f4..c553542 100644
--- a/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/GroovyEnvironmentSuite.java
+++ b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/GroovyEnvironmentSuite.java
@@ -23,6 +23,7 @@ import org.apache.tinkerpop.gremlin.AbstractGremlinTest;
import org.apache.tinkerpop.gremlin.GraphManager;
import org.apache.tinkerpop.gremlin.groovy.engine.GremlinExecutorOverGraphTest;
import org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineOverGraphTest;
+import org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineSandboxedStandardTest;
import org.apache.tinkerpop.gremlin.groovy.loaders.SugarLoader;
import org.apache.tinkerpop.gremlin.groovy.loaders.SugarLoaderTest;
import org.apache.tinkerpop.gremlin.groovy.util.SugarTestHelper;
@@ -32,11 +33,6 @@ import org.apache.tinkerpop.gremlin.structure.StructureStandardSuite;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;
-import java.util.Arrays;
-import java.util.List;
-import java.util.stream.Collectors;
-import java.util.stream.Stream;
-
/**
* The {@code GroovyEnvironmentSuite} is a JUnit test runner that executes the Gremlin Test Suite over a
* {@link Graph} implementation. This test suite covers ensures that a vendor implementation is compliant with
@@ -55,6 +51,7 @@ public class GroovyEnvironmentSuite extends AbstractGremlinSuite {
*/
private static final Class<?>[] allTests = new Class<?>[]{
GremlinGroovyScriptEngineOverGraphTest.class,
+ GremlinGroovyScriptEngineSandboxedStandardTest.class,
GremlinExecutorOverGraphTest.class,
SugarLoaderTest.class,
};
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/927b5ef4/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 c72bfd5..a8899c7 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
@@ -239,142 +239,6 @@ public class GremlinGroovyScriptEngineOverGraphTest extends AbstractGremlinTest
@Test
@LoadGraphWith(LoadGraphWith.GraphData.MODERN)
- public void shouldEvalGraphTraversalSourceWithTypeChecked() throws Exception {
- try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine()) {
- final Bindings bindings = engine.createBindings();
- bindings.put("g", g);
- assertEquals(g.V(convertToVertexId("marko")).next(), engine.eval("g.V(" + convertToVertexId("marko") + ").next()", bindings));
- }
-
- final TypeCheckedCustomizerProvider providerNoExtension = new TypeCheckedCustomizerProvider();
- try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(providerNoExtension)) {
- final Bindings bindings = engine.createBindings();
- bindings.put("g", g);
- engine.eval("g.V(" + convertToVertexId("marko") + ").next()", bindings);
- fail("Type checking should have forced an error as 'g' is not defined");
- } catch (Exception ex) {
- assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
- assertThat(ex.getMessage(), containsString("The variable [g] is undeclared."));
- }
-
- final TypeCheckedCustomizerProvider providerWithExtension = new TypeCheckedCustomizerProvider(
- SandboxExtension.class.getName());
- try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(providerWithExtension)) {
- final Bindings bindings = engine.createBindings();
- bindings.put("g", g);
- assertEquals(g.V(convertToVertexId("marko")).next(), engine.eval("g.V(" + convertToVertexId("marko") + ").next()", bindings));
- assertEquals(g.V(convertToVertexId("marko")).out("created").count().next(), engine.eval("g.V(" + convertToVertexId("marko") + ").out(\"created\").count().next()", bindings));
- }
- }
-
- @Test
- @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
- public void shouldEvalGraphWithTypeChecked() throws Exception {
- try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine()) {
- final Bindings bindings = engine.createBindings();
- bindings.put("graph", graph);
- assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(" + convertToVertexId("marko") + ").next()", bindings));
- }
-
- final TypeCheckedCustomizerProvider providerNoExtension = new TypeCheckedCustomizerProvider();
- try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(providerNoExtension)) {
- final Bindings bindings = engine.createBindings();
- bindings.put("graph", graph);
- assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(" + convertToVertexId("marko") + ").next()", bindings));
- fail("Type checking should have forced an error as 'graph' is not defined");
- } catch (Exception ex) {
- assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
- assertThat(ex.getMessage(), containsString("The variable [graph] is undeclared."));
- }
-
- final TypeCheckedCustomizerProvider providerWithExtension = new TypeCheckedCustomizerProvider(
- SandboxExtension.class.getName());
- try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(providerWithExtension)) {
- final Bindings bindings = engine.createBindings();
- bindings.put("graph", graph);
- assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(" + convertToVertexId("marko") + ").next()", bindings));
- }
- }
-
- @Test
- @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
- public void shouldEvalGraphTraversalSourceWithCompileStatic() throws Exception {
- try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine()) {
- final Bindings bindings = engine.createBindings();
- bindings.put("g", g);
- assertEquals(g.V(convertToVertexId("marko")).next(), engine.eval("g.V(" + convertToVertexId("marko") + ").next()", bindings));
- }
-
- final CompileStaticCustomizerProvider providerNoExtension = new CompileStaticCustomizerProvider();
- try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(providerNoExtension)) {
- final Bindings bindings = engine.createBindings();
- bindings.put("g", g);
- engine.eval("g.V(" + convertToVertexId("marko") + ").next()", bindings);
- fail("Type checking should have forced an error as 'g' is not defined");
- } catch (Exception ex) {
- assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
- assertThat(ex.getMessage(), containsString("The variable [g] is undeclared."));
- }
-
- final CompileStaticCustomizerProvider providerWithExtension = new CompileStaticCustomizerProvider(
- SandboxExtension.class.getName());
- try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(providerWithExtension)) {
- final Bindings bindings = engine.createBindings();
- bindings.put("g", g);
- assertEquals(g.V(convertToVertexId("marko")).next(), engine.eval("g.V(" + convertToVertexId("marko") + ").next()", bindings));
- assertEquals(g.V(convertToVertexId("marko")).out("created").count().next(), engine.eval("g.V(" + convertToVertexId("marko") + ").out(\"created\").count().next()", bindings));
- }
- }
-
- @Test
- @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
- public void shouldEvalGraphWithCompileStatic() throws Exception {
- try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine()) {
- final Bindings bindings = engine.createBindings();
- bindings.put("graph", graph);
- assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(" + convertToVertexId("marko") + ").next()", bindings));
- }
-
- final CompileStaticCustomizerProvider providerNoExtension = new CompileStaticCustomizerProvider();
- try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(providerNoExtension)) {
- final Bindings bindings = engine.createBindings();
- bindings.put("graph", graph);
- assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(" + convertToVertexId("marko") + ").next()", bindings));
- fail("Type checking should have forced an error as 'graph' is not defined");
- } catch (Exception ex) {
- assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
- assertThat(ex.getMessage(), containsString("The variable [graph] is undeclared."));
- }
-
- try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(providerNoExtension)) {
- final Bindings bindings = engine.createBindings();
- bindings.put("graph", graph);
- bindings.put("x", convertToVertexId("marko"));
- assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(x).next()", bindings));
- fail("Type checking should have forced an error as 'graph' is not defined");
- } catch (Exception ex) {
- assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
- assertThat(ex.getMessage(), containsString("The variable [graph] is undeclared."));
- }
-
- final CompileStaticCustomizerProvider providerWithExtension = new CompileStaticCustomizerProvider(
- SandboxExtension.class.getName());
- try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(providerWithExtension)) {
- final Bindings bindings = engine.createBindings();
- bindings.put("graph", graph);
- assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(" + convertToVertexId("marko") + ").next()", bindings));
- }
-
- try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(providerWithExtension)) {
- final Bindings bindings = engine.createBindings();
- bindings.put("graph", graph);
- bindings.put("x", convertToVertexId("marko"));
- assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(x).next()", bindings));
- }
- }
-
- @Test
- @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
public void shouldEvalGlobalClosuresEvenAfterEvictionOfClass() throws ScriptException {
final GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine();
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/927b5ef4/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
new file mode 100644
index 0000000..7f55e78
--- /dev/null
+++ b/gremlin-groovy-test/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineSandboxedStandardTest.java
@@ -0,0 +1,131 @@
+/*
+ * 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.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.codehaus.groovy.control.MultipleCompilationErrorsException;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import javax.script.Bindings;
+
+import java.util.Arrays;
+
+import static org.hamcrest.Matchers.containsString;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+@RunWith(Parameterized.class)
+public class GremlinGroovyScriptEngineSandboxedStandardTest extends AbstractGremlinTest {
+ @Parameterized.Parameters(name = "{0}")
+ public static Iterable<Object[]> data() {
+ return Arrays.asList(new Object[][]{
+ {TypeCheckedCustomizerProvider.class.getSimpleName(), new TypeCheckedCustomizerProvider(), new TypeCheckedCustomizerProvider(SandboxExtension.class.getName())},
+ {CompileStaticCustomizerProvider.class.getSimpleName(), new CompileStaticCustomizerProvider(), new CompileStaticCustomizerProvider(SandboxExtension.class.getName())}});
+ }
+
+ @Parameterized.Parameter(value = 0)
+ public String name;
+
+ @Parameterized.Parameter(value = 1)
+ public CompilerCustomizerProvider notSandboxed;
+
+ @Parameterized.Parameter(value = 2)
+ public CompilerCustomizerProvider sandboxed;
+
+ @Test
+ @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+ public void shouldEvalGraphTraversalSource() throws Exception {
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine()) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("g", g);
+ assertEquals(g.V(convertToVertexId("marko")).next(), engine.eval("g.V(" + convertToVertexId("marko") + ").next()", bindings));
+ }
+
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(notSandboxed)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("g", g);
+ engine.eval("g.V(" + convertToVertexId("marko") + ").next()", bindings);
+ fail("Type checking should have forced an error as 'g' is not defined");
+ } catch (Exception ex) {
+ assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
+ assertThat(ex.getMessage(), containsString("The variable [g] is undeclared."));
+ }
+
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(sandboxed)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("g", g);
+ assertEquals(g.V(convertToVertexId("marko")).next(), engine.eval("g.V(" + convertToVertexId("marko") + ").next()", bindings));
+ assertEquals(g.V(convertToVertexId("marko")).out("created").count().next(), engine.eval("g.V(" + convertToVertexId("marko") + ").out(\"created\").count().next()", bindings));
+ }
+ }
+
+ @Test
+ @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+ public void shouldEvalGraph() throws Exception {
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine()) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("graph", graph);
+ assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(" + convertToVertexId("marko") + ").next()", bindings));
+ }
+
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(notSandboxed)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("graph", graph);
+ assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(" + convertToVertexId("marko") + ").next()", bindings));
+ fail("Type checking should have forced an error as 'graph' is not defined");
+ } catch (Exception ex) {
+ assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
+ assertThat(ex.getMessage(), containsString("The variable [graph] is undeclared."));
+ }
+
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(notSandboxed)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("graph", graph);
+ bindings.put("x", convertToVertexId("marko"));
+ assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(x).next()", bindings));
+ fail("Type checking should have forced an error as 'graph' is not defined");
+ } catch (Exception ex) {
+ assertEquals(MultipleCompilationErrorsException.class, ex.getCause().getClass());
+ assertThat(ex.getMessage(), containsString("The variable [graph] is undeclared."));
+ }
+
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(sandboxed)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("graph", graph);
+ assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(" + convertToVertexId("marko") + ").next()", bindings));
+ }
+
+ try (GremlinGroovyScriptEngine engine = new GremlinGroovyScriptEngine(sandboxed)) {
+ final Bindings bindings = engine.createBindings();
+ bindings.put("graph", graph);
+ bindings.put("x", convertToVertexId("marko"));
+ assertEquals(graph.vertices(convertToVertexId("marko")).next(), engine.eval("graph.vertices(x).next()", bindings));
+ }
+ }
+}
[11/31] incubator-tinkerpop git commit: Add a
ThreadInterruptCustomizerProvider.
Posted by sp...@apache.org.
Add a ThreadInterruptCustomizerProvider.
Now all configuration of the compiler is done through CompilerCustomizerProvider implementations.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/2a319f0e
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/2a319f0e
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/2a319f0e
Branch: refs/heads/tp30
Commit: 2a319f0eaa9af73187aad607780b189dd75c5b9a
Parents: 95b6eb8
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Jul 8 14:36:37 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Jul 8 14:36:37 2015 -0400
----------------------------------------------------------------------
.../ThreadInterruptCustomizerProvider.java | 35 ++++++++++++++++
.../gremlin/groovy/engine/ScriptEngines.java | 4 ++
.../jsr223/GremlinGroovyScriptEngine.java | 5 ---
.../jsr223/GremlinGroovyScriptEngineTest.java | 43 ++++++++++++++++++++
.../structure/io/script/ScriptRecordReader.java | 2 +-
.../structure/io/script/ScriptRecordWriter.java | 2 +-
6 files changed, 84 insertions(+), 7 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/2a319f0e/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
new file mode 100644
index 0000000..f8f46a7
--- /dev/null
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/ThreadInterruptCustomizerProvider.java
@@ -0,0 +1,35 @@
+/*
+ * 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/2a319f0e/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
index ba310ec..24b9f58 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
@@ -21,6 +21,7 @@ package org.apache.tinkerpop.gremlin.groovy.engine;
import org.apache.tinkerpop.gremlin.groovy.CompilerCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.DefaultImportCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.SecurityCustomizerProvider;
+import org.apache.tinkerpop.gremlin.groovy.ThreadInterruptCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.TimedInterruptCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.jsr223.DependencyManager;
import org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine;
@@ -349,6 +350,9 @@ public class ScriptEngines implements AutoCloseable {
TimedInterruptCustomizerProvider.DEFAULT_INTERRUPTION_TIMEOUT)).longValue();
if (interruptionTimeout > 0) providers.add(new TimedInterruptCustomizerProvider(interruptionTimeout));
+ final boolean threadInterrupt = (boolean) config.getOrDefault("enableThreadInterrupt", true);
+ if (threadInterrupt) providers.add(new ThreadInterruptCustomizerProvider());
+
final CompilerCustomizerProvider[] providerArray = new CompilerCustomizerProvider[providers.size()];
return Optional.of((ScriptEngine) new GremlinGroovyScriptEngine(providers.toArray(providerArray)));
} else {
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/2a319f0e/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
index d93fd01..59a0530 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
@@ -38,8 +38,6 @@ import groovy.lang.MissingMethodException;
import groovy.lang.MissingPropertyException;
import groovy.lang.Script;
import groovy.lang.Tuple;
-import groovy.transform.TimedInterrupt;
-import org.codehaus.groovy.ast.tools.GeneralUtils;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
@@ -72,7 +70,6 @@ import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;
-import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@@ -484,8 +481,6 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
if (this.securityProvider.isPresent())
conf.addCompilationCustomizers(this.securityProvider.get().create());
- conf.addCompilationCustomizers(new ASTTransformationCustomizer(ThreadInterrupt.class));
-
customizerProviders.forEach(p -> conf.addCompilationCustomizers(p.create()));
this.loader = new GremlinGroovyClassLoader(getParentLoader(), conf);
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/2a319f0e/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
index 15c4cb4..8f71588 100644
--- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
@@ -23,6 +23,7 @@ import groovy.lang.Script;
import org.apache.tinkerpop.gremlin.groovy.DefaultImportCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.NoImportCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.SecurityCustomizerProvider;
+import org.apache.tinkerpop.gremlin.groovy.ThreadInterruptCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.TimedInterruptCustomizerProvider;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
@@ -358,6 +359,48 @@ public class GremlinGroovyScriptEngineTest {
}
@Test
+ public void shouldInterruptWhile() throws Exception {
+ final ScriptEngine engine = new GremlinGroovyScriptEngine(new ThreadInterruptCustomizerProvider());
+ final AtomicBoolean asserted = new AtomicBoolean(false);
+
+ final Thread t = new Thread(() -> {
+ try {
+ engine.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 10000) {}");
+ } catch (ScriptException se) {
+ asserted.set(se.getCause().getCause() instanceof InterruptedException);
+ }
+ });
+
+ t.start();
+ t.interrupt();
+ while(t.isAlive()) {}
+
+ assertTrue(asserted.get());
+ }
+
+ @Test
+ public void shouldNotInterruptWhile() throws Exception {
+ // companion to shouldInterruptWhile t
+ final ScriptEngine engine = new GremlinGroovyScriptEngine();
+ final AtomicBoolean exceptionFired = new AtomicBoolean(true);
+
+ final Thread t = new Thread(() -> {
+ try {
+ engine.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 2000) {};'test'");
+ exceptionFired.set(false);
+ } catch (ScriptException se) {
+ se.printStackTrace();
+ }
+ });
+
+ t.start();
+ t.interrupt();
+ while(t.isAlive()) {}
+
+ assertFalse(exceptionFired.get());
+ }
+
+ @Test
public void shouldNotTimeoutStandaloneFunction() throws Exception {
// use a super fast timeout which should not prevent the call of a cached function
final ScriptEngine engine = new GremlinGroovyScriptEngine(
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/2a319f0e/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordReader.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordReader.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordReader.java
index aec6aff..e56eb1a 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordReader.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordReader.java
@@ -64,7 +64,7 @@ public final class ScriptRecordReader extends RecordReader<NullWritable, VertexW
public void initialize(final InputSplit genericSplit, final TaskAttemptContext context) throws IOException {
this.lineRecordReader.initialize(genericSplit, context);
final Configuration configuration = context.getConfiguration();
- this.engine = new GremlinGroovyScriptEngine(Integer.MAX_VALUE, new DefaultImportCustomizerProvider());
+ this.engine = new GremlinGroovyScriptEngine(new DefaultImportCustomizerProvider());
//this.engine = ScriptEngineCache.get(configuration.get(SCRIPT_ENGINE, ScriptEngineCache.DEFAULT_SCRIPT_ENGINE));
final FileSystem fs = FileSystem.get(configuration);
try {
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/2a319f0e/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordWriter.java
----------------------------------------------------------------------
diff --git a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordWriter.java b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordWriter.java
index b9bd068..5cf6170 100644
--- a/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordWriter.java
+++ b/hadoop-gremlin/src/main/java/org/apache/tinkerpop/gremlin/hadoop/structure/io/script/ScriptRecordWriter.java
@@ -61,7 +61,7 @@ public final class ScriptRecordWriter extends RecordWriter<NullWritable, VertexW
public ScriptRecordWriter(final DataOutputStream out, final TaskAttemptContext context) throws IOException {
this.out = out;
final Configuration configuration = context.getConfiguration();
- this.engine = new GremlinGroovyScriptEngine(Integer.MAX_VALUE, new DefaultImportCustomizerProvider());
+ this.engine = new GremlinGroovyScriptEngine(new DefaultImportCustomizerProvider());
//this.engine = ScriptEngineCache.get(configuration.get(SCRIPT_ENGINE, ScriptEngineCache.DEFAULT_SCRIPT_ENGINE));
final FileSystem fs = FileSystem.get(configuration);
try {
[07/31] incubator-tinkerpop git commit: Ensure that import/security
compilation providers are removed.
Posted by sp...@apache.org.
Ensure that import/security compilation providers are removed.
This way they are not re-applied later as they have implementation specific requirements.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/f19a65f1
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/f19a65f1
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/f19a65f1
Branch: refs/heads/tp30
Commit: f19a65f129b07bd7b11aa10c5bfebb97415a0c33
Parents: 8b4f3df
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Jul 8 12:58:18 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Jul 8 12:58:18 2015 -0400
----------------------------------------------------------------------
.../gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java | 6 ++++++
1 file changed, 6 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/f19a65f1/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
index f86498e..d3a8155 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
@@ -137,6 +137,8 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
private final Set<Artifact> artifactsToUse = new HashSet<>();
+ private final List<CompilerCustomizerProvider> customizerProviders;
+
public GremlinGroovyScriptEngine() {
this(new DefaultImportCustomizerProvider());
}
@@ -161,6 +163,10 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
.map(p -> (SecurityCustomizerProvider) p)
.findFirst();
+ // remove used providers as the rest will be applied directly
+ providers.removeIf(p -> p instanceof SecurityCustomizerProvider || p instanceof ImportCustomizerProvider);
+ customizerProviders = providers;
+
this.scriptEvaluationTimeout = scriptEvaluationTimeout;
createClassLoader();
}
[08/31] incubator-tinkerpop git commit: Rename
CompilationCustomizerProvider method from get to create.
Posted by sp...@apache.org.
Rename CompilationCustomizerProvider method from get to create.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/47c0fa3d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/47c0fa3d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/47c0fa3d
Branch: refs/heads/tp30
Commit: 47c0fa3d1e67fb29ee001b841303e3b732f89161
Parents: f19a65f
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Jul 8 13:06:55 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Jul 8 13:06:55 2015 -0400
----------------------------------------------------------------------
.../gremlin/groovy/AbstractImportCustomizerProvider.java | 2 +-
.../gremlin/groovy/CompilerCustomizerProvider.java | 2 +-
.../gremlin/groovy/EmptyImportCustomizerProvider.java | 2 +-
.../gremlin/groovy/NoImportCustomizerProvider.java | 2 +-
.../gremlin/groovy/SecurityCustomizerProvider.java | 2 +-
.../gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java | 10 ++++++----
6 files changed, 11 insertions(+), 9 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/47c0fa3d/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/AbstractImportCustomizerProvider.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/AbstractImportCustomizerProvider.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/AbstractImportCustomizerProvider.java
index e0c6670..f50395c 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/AbstractImportCustomizerProvider.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/AbstractImportCustomizerProvider.java
@@ -136,7 +136,7 @@ public abstract class AbstractImportCustomizerProvider implements ImportCustomiz
}
@Override
- public CompilationCustomizer getCompilationCustomizer() {
+ public CompilationCustomizer create() {
final ImportCustomizer ic = new ImportCustomizer();
processImports(ic, imports);
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/47c0fa3d/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/CompilerCustomizerProvider.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/CompilerCustomizerProvider.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/CompilerCustomizerProvider.java
index a633ff2..9980911 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/CompilerCustomizerProvider.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/CompilerCustomizerProvider.java
@@ -32,5 +32,5 @@ public interface CompilerCustomizerProvider {
/**
* Create a new instance of a {@code CompilationCustomizer} to add to the {@link GremlinGroovyScriptEngine}.
*/
- public CompilationCustomizer getCompilationCustomizer();
+ public CompilationCustomizer create();
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/47c0fa3d/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/EmptyImportCustomizerProvider.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/EmptyImportCustomizerProvider.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/EmptyImportCustomizerProvider.java
index 03e86c9..36d5f93 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/EmptyImportCustomizerProvider.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/EmptyImportCustomizerProvider.java
@@ -55,7 +55,7 @@ public class EmptyImportCustomizerProvider extends DefaultImportCustomizerProvid
}
@Override
- public CompilationCustomizer getCompilationCustomizer() {
+ public CompilationCustomizer create() {
final ImportCustomizer ic = new ImportCustomizer();
processImports(ic, extraImports);
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/47c0fa3d/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/NoImportCustomizerProvider.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/NoImportCustomizerProvider.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/NoImportCustomizerProvider.java
index 343c8a6..9ded2b4 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/NoImportCustomizerProvider.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/NoImportCustomizerProvider.java
@@ -36,7 +36,7 @@ public final class NoImportCustomizerProvider implements ImportCustomizerProvide
private NoImportCustomizerProvider() {}
@Override
- public CompilationCustomizer getCompilationCustomizer() {
+ public CompilationCustomizer create() {
return new ImportCustomizer();
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/47c0fa3d/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/SecurityCustomizerProvider.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/SecurityCustomizerProvider.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/SecurityCustomizerProvider.java
index 67d0674..bef0c2c 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/SecurityCustomizerProvider.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/SecurityCustomizerProvider.java
@@ -50,7 +50,7 @@ public class SecurityCustomizerProvider implements CompilerCustomizerProvider {
}
@Override
- public CompilationCustomizer getCompilationCustomizer() {
+ public CompilationCustomizer create() {
return new SandboxTransformer();
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/47c0fa3d/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
index d3a8155..68083e7 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
@@ -75,6 +75,7 @@ import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.regex.Pattern;
+import java.util.stream.Collectors;
/**
* This {@code ScriptEngine} implementation is heavily adapted from the {@code GroovyScriptEngineImpl} to include
@@ -164,8 +165,9 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
.findFirst();
// remove used providers as the rest will be applied directly
- providers.removeIf(p -> p instanceof SecurityCustomizerProvider || p instanceof ImportCustomizerProvider);
- customizerProviders = providers;
+ customizerProviders = providers.stream()
+ .filter(p -> !(p instanceof SecurityCustomizerProvider || p instanceof ImportCustomizerProvider))
+ .collect(Collectors.toList());
this.scriptEvaluationTimeout = scriptEvaluationTimeout;
createClassLoader();
@@ -489,10 +491,10 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
private synchronized void createClassLoader() {
final CompilerConfiguration conf = new CompilerConfiguration();
- conf.addCompilationCustomizers(this.importCustomizerProvider.getCompilationCustomizer());
+ conf.addCompilationCustomizers(this.importCustomizerProvider.create());
if (this.securityProvider.isPresent())
- conf.addCompilationCustomizers(this.securityProvider.get().getCompilationCustomizer());
+ conf.addCompilationCustomizers(this.securityProvider.get().create());
if (scriptEvaluationTimeout > 0) {
final Map<String, Object> timedInterruptAnnotationParams = new HashMap<>();
[28/31] incubator-tinkerpop git commit: Add some javadoc to the
SandboxExtension.
Posted by sp...@apache.org.
Add some javadoc to the SandboxExtension.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/1a3a32d8
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/1a3a32d8
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/1a3a32d8
Branch: refs/heads/tp30
Commit: 1a3a32d8454ff8bcd8da6c57fa59e9ecf3b90db6
Parents: 69e39ee
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Mon Jul 13 08:20:44 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Mon Jul 13 08:20:44 2015 -0400
----------------------------------------------------------------------
.../groovy/jsr223/SandboxExtension.groovy | 22 ++++++++++++++++++--
1 file changed, 20 insertions(+), 2 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/1a3a32d8/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
index f2027f4..505f921 100644
--- 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
@@ -40,18 +40,33 @@ import java.util.function.BiPredicate
* 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 typically extend this class to modify features.
+ * 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
@@ -122,9 +137,12 @@ class SandboxExtension extends GroovyTypeCheckingExtensionSupport.TypeCheckingDS
* Helper method for those extending the sandbox.
*/
static String prettyPrint(final ClassNode node) {
- node.isArray()?"${prettyPrint(node.componentType)}[]":node.toString(false)
+ 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
[30/31] incubator-tinkerpop git commit: Merge remote-tracking branch
'origin/tp30-582' into tp30
Posted by sp...@apache.org.
Merge remote-tracking branch 'origin/tp30-582' into 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/cacba0d7
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/cacba0d7
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/cacba0d7
Branch: refs/heads/tp30
Commit: cacba0d78a8e969598e558bd110215210b849986
Parents: 99e89be 12eb58b
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Jul 15 16:46:39 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Jul 15 16:46:39 2015 -0400
----------------------------------------------------------------------
.../jsr223/AllowColorSandboxExtension.groovy | 35 ++++
.../BlockSomeVariablesSandboxExtension.groovy | 41 +++++
...ebindAllVariableTypesSandboxExtension.groovy | 29 +++
.../gremlin/groovy/GroovyEnvironmentSuite.java | 11 +-
.../GremlinGroovyScriptEngineOverGraphTest.java | 9 +-
...mlinGroovyScriptEngineSandboxCustomTest.java | 126 +++++++++++++
...GroovyScriptEngineSandboxedStandardTest.java | 131 ++++++++++++++
...nGroovyScriptEngineTinkerPopSandboxTest.java | 98 +++++++++++
gremlin-groovy/pom.xml | 11 --
.../groovy/jsr223/SandboxExtension.groovy | 176 +++++++++++++++++++
.../jsr223/TinkerPopSandboxExtension.groovy | 47 +++++
.../AbstractImportCustomizerProvider.java | 2 +-
.../groovy/CompileStaticCustomizerProvider.java | 60 +++++++
.../groovy/CompilerCustomizerProvider.java | 10 +-
.../groovy/EmptyImportCustomizerProvider.java | 5 +-
.../groovy/NoImportCustomizerProvider.java | 9 +-
.../groovy/SecurityCustomizerProvider.java | 66 -------
.../ThreadInterruptCustomizerProvider.java | 35 ++++
.../TimedInterruptCustomizerProvider.java | 61 +++++++
.../groovy/TypeCheckedCustomizerProvider.java | 64 +++++++
.../gremlin/groovy/engine/ScriptEngines.java | 50 ++++--
.../groovy/jsr223/DependencyManager.java | 10 +-
.../jsr223/GremlinGroovyScriptEngine.java | 122 ++++++-------
.../jsr223/ManagedConcurrentValueMap.java | 7 +-
.../VariableIdentificationCustomizer.java | 91 ++++++++++
.../groovy/engine/GremlinExecutorTest.java | 74 ++++----
...mlinGroovyScriptEngineCompileStaticTest.java | 99 +++++++++++
.../jsr223/GremlinGroovyScriptEngineTest.java | 132 +-------------
...inGroovyScriptEngineThreadInterruptTest.java | 78 ++++++++
...linGroovyScriptEngineTimedInterruptTest.java | 94 ++++++++++
...remlinGroovyScriptEngineTypeCheckedTest.java | 100 +++++++++++
.../groovy/jsr223/PrecompiledExtensions.java | 58 ++++++
.../structure/io/script/ScriptRecordReader.java | 2 +-
.../structure/io/script/ScriptRecordWriter.java | 2 +-
34 files changed, 1587 insertions(+), 358 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/cacba0d7/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/engine/GremlinExecutorTest.java
----------------------------------------------------------------------
[23/31] incubator-tinkerpop git commit: Lay some foundations for
method whitelisting in the sandbox.
Posted by sp...@apache.org.
Lay some foundations for method whitelisting in the sandbox.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/6af00c69
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/6af00c69
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/6af00c69
Branch: refs/heads/tp30
Commit: 6af00c6984c206e1fea2cb30305a877c54ef1e73
Parents: e598829
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Fri Jul 10 08:50:28 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Fri Jul 10 08:50:28 2015 -0400
----------------------------------------------------------------------
.../groovy/jsr223/SandboxExtension.groovy | 52 ++++++++++++++++++--
.../jsr223/GremlinGroovyScriptEngine.java | 32 ++++++------
2 files changed, 64 insertions(+), 20 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/6af00c69/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
index 4888f8c..d62ccb3 100644
--- 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
@@ -21,21 +21,40 @@ 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.ClassNode
+import org.codehaus.groovy.ast.MethodNode
+import org.codehaus.groovy.ast.Parameter
+import org.codehaus.groovy.ast.expr.Expression
import org.codehaus.groovy.ast.expr.VariableExpression
+import org.codehaus.groovy.transform.stc.ExtensionMethodNode
import org.codehaus.groovy.transform.stc.GroovyTypeCheckingExtensionSupport
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 typically extend this class to modify features.
+ *
* @author Stephen Mallette (http://stephen.genoprime.com)
*/
class SandboxExtension extends GroovyTypeCheckingExtensionSupport.TypeCheckingDSL {
+ public static final BiPredicate<VariableExpression, Map<String,ClassNode>> VARIABLES_ALLOW_ALL = { var, types -> true }
+
+ public static final BiPredicate<VariableExpression, Map<String,ClassNode>> METHODS_ALLOW_ALL = { exp, method -> true }
+ public static final BiPredicate<Expression, MethodNode> METHODS_REGEX_WHITELIST = { exp, method ->
+ def descriptor = toMethodDescriptor(method)
+ !methodList.any { descriptor =~ it }
+ }
+ public static final BiPredicate<Expression, MethodNode> METHODS_REGEX_BLACKLIST = { exp, method -> !METHODS_REGEX_WHITELIST.test(exp, method) }
+
protected boolean graphIsAlwaysGraphInstance = true
protected boolean gIsAlwaysGraphTraversalSource = true
- protected BiPredicate<VariableExpression, Map<String,ClassNode>> filter = USE_ALL
-
- public static final BiPredicate<VariableExpression, Map<String,ClassNode>> USE_ALL = { var, types -> true }
+ protected BiPredicate<VariableExpression, Map<String,ClassNode>> variableFilter = VARIABLES_ALLOW_ALL
+ protected BiPredicate<Expression, MethodNode> methodFilter = METHODS_ALLOW_ALL
+ protected List<String> methodList = new ArrayList<String>();
@Override
Object run() {
@@ -54,7 +73,7 @@ class SandboxExtension extends GroovyTypeCheckingExtensionSupport.TypeCheckingDS
final Map<String,ClassNode> varTypes = (Map<String,ClassNode>) GremlinGroovyScriptEngine.COMPILE_OPTIONS.get()
.get(GremlinGroovyScriptEngine.COMPILE_OPTIONS_VAR_TYPES)
- if (varTypes.containsKey(var.name) && filter.test(var, varTypes)
+ if (varTypes.containsKey(var.name) && variableFilter.test(var, varTypes)
|| (var.name == "graph" && !graphIsAlwaysGraphInstance
|| var.name == "g" && !gIsAlwaysGraphTraversalSource)) {
storeType(var, varTypes.get(var.name))
@@ -62,5 +81,30 @@ class SandboxExtension extends GroovyTypeCheckingExtensionSupport.TypeCheckingDS
return
}
}
+
+ onMethodSelection { expr, MethodNode methodNode ->
+ if (!methodFilter.test(expr, methodNode))
+ addStaticTypeError("Not authorized to call this method: $descr", expr)
+ }
+ }
+
+ private static String prettyPrint(ClassNode node) {
+ node.isArray()?"${prettyPrint(node.componentType)}[]":node.toString(false)
+ }
+
+ private 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
}
}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/6af00c69/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
index 09af47f..d484d4e 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
@@ -84,6 +84,7 @@ import java.util.stream.Collectors;
public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements DependencyManager, AutoCloseable {
public static final String COMPILE_OPTIONS_VAR_TYPES = "sandbox.bindings";
+ public static final String COMPILE_OPTIONS_WHITELIST = "sandbox.whitelist";
public static final String KEY_REFERENCE_TYPE = "#jsr223.groovy.engine.keep.globals";
public static final String REFERENCE_TYPE_PHANTOM = "phantom";
public static final String REFERENCE_TYPE_WEAK = "weak";
@@ -148,12 +149,12 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
GremlinLoader.load();
- this.importCustomizerProvider = providers.stream()
+ importCustomizerProvider = providers.stream()
.filter(p -> p instanceof ImportCustomizerProvider)
.map(p -> (ImportCustomizerProvider) p)
.findFirst().orElse(NoImportCustomizerProvider.INSTANCE);
- this.securityProvider = providers.stream()
+ securityProvider = providers.stream()
.filter(p -> p instanceof SecurityCustomizerProvider)
.map(p -> (SecurityCustomizerProvider) p)
.findFirst();
@@ -238,8 +239,7 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
// use the EmptyImportCustomizer because it doesn't come with static initializers containing
// existing imports.
- this.importCustomizerProvider = new EmptyImportCustomizerProvider(
- this.importCustomizerProvider, imports, staticImports);
+ importCustomizerProvider = new EmptyImportCustomizerProvider(importCustomizerProvider, imports, staticImports);
reset();
}
@@ -249,25 +249,25 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
@Override
public void close() throws Exception {
- this.securityProvider.ifPresent(SecurityCustomizerProvider::unregisterInterceptors);
+ securityProvider.ifPresent(SecurityCustomizerProvider::unregisterInterceptors);
}
@Override
public void reset() {
- this.securityProvider.ifPresent(SecurityCustomizerProvider::unregisterInterceptors);
+ securityProvider.ifPresent(SecurityCustomizerProvider::unregisterInterceptors);
createClassLoader();
// must clear the local cache here because the the classloader has been reset. therefore, classes previously
// referenced before that might not have evaluated might cleanly evaluate now.
- this.classMap.clear();
- this.globalClosures.clear();
+ classMap.clear();
+ globalClosures.clear();
- this.loadedPlugins.clear();
+ loadedPlugins.clear();
final Set<Artifact> toReuse = new HashSet<>(artifactsToUse);
toReuse.forEach(this::use);
- this.getContext().getBindings(ScriptContext.ENGINE_SCOPE).clear();
+ getContext().getBindings(ScriptContext.ENGINE_SCOPE).clear();
}
private void use(final Artifact artifact) {
@@ -315,10 +315,10 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
@Override
public ScriptEngineFactory getFactory() {
- if (this.factory == null) {
+ if (factory == null) {
synchronized (this) {
- if (this.factory == null) {
- this.factory = new GremlinGroovyScriptEngineFactory();
+ if (factory == null) {
+ factory = new GremlinGroovyScriptEngineFactory();
}
}
}
@@ -476,9 +476,9 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
}
private void ensureSandbox() {
- if (securityProvider.isPresent() && !this.registeredSandbox.get()) {
- this.securityProvider.get().registerInterceptors();
- this.registeredSandbox.set(true);
+ if (securityProvider.isPresent() && !registeredSandbox.get()) {
+ securityProvider.get().registerInterceptors();
+ registeredSandbox.set(true);
}
}
[15/31] incubator-tinkerpop git commit: Pull ThreadInterrupt compiler
provider tests to their own case.
Posted by sp...@apache.org.
Pull ThreadInterrupt compiler provider tests to their own case.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/6b8628be
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/6b8628be
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/6b8628be
Branch: refs/heads/tp30
Commit: 6b8628be701558ba1bf91f8713639f6d9fe33694
Parents: 44d8f37
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Thu Jul 9 08:29:07 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Thu Jul 9 08:29:07 2015 -0400
----------------------------------------------------------------------
.../jsr223/GremlinGroovyScriptEngineTest.java | 45 -----------
...inGroovyScriptEngineThreadInterruptTest.java | 78 ++++++++++++++++++++
2 files changed, 78 insertions(+), 45 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/6b8628be/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
index 19f56d8..15c4cb4 100644
--- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
@@ -23,7 +23,6 @@ import groovy.lang.Script;
import org.apache.tinkerpop.gremlin.groovy.DefaultImportCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.NoImportCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.SecurityCustomizerProvider;
-import org.apache.tinkerpop.gremlin.groovy.ThreadInterruptCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.TimedInterruptCustomizerProvider;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
@@ -359,50 +358,6 @@ public class GremlinGroovyScriptEngineTest {
}
@Test
- public void shouldInterruptWhile() throws Exception {
- final ScriptEngine engine = new GremlinGroovyScriptEngine(new ThreadInterruptCustomizerProvider());
- final AtomicBoolean asserted = new AtomicBoolean(false);
-
- final Thread t = new Thread(() -> {
- try {
- engine.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 10000) {}");
- } catch (ScriptException se) {
- asserted.set(se.getCause().getCause() instanceof InterruptedException);
- }
- });
-
- t.start();
- Thread.sleep(100);
- t.interrupt();
- while(t.isAlive()) {}
-
- assertTrue(asserted.get());
- }
-
- @Test
- public void shouldNotInterruptWhile() throws Exception {
- // companion to shouldInterruptWhile t
- final ScriptEngine engine = new GremlinGroovyScriptEngine();
- final AtomicBoolean exceptionFired = new AtomicBoolean(true);
-
- final Thread t = new Thread(() -> {
- try {
- engine.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 2000) {};'test'");
- exceptionFired.set(false);
- } catch (ScriptException se) {
- se.printStackTrace();
- }
- });
-
- t.start();
- Thread.sleep(100);
- t.interrupt();
- while(t.isAlive()) {}
-
- assertFalse(exceptionFired.get());
- }
-
- @Test
public void shouldNotTimeoutStandaloneFunction() throws Exception {
// use a super fast timeout which should not prevent the call of a cached function
final ScriptEngine engine = new GremlinGroovyScriptEngine(
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/6b8628be/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
new file mode 100644
index 0000000..939453f
--- /dev/null
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineThreadInterruptTest.java
@@ -0,0 +1,78 @@
+/*
+ * 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.groovy.ThreadInterruptCustomizerProvider;
+import org.junit.Test;
+
+import javax.script.ScriptEngine;
+import javax.script.ScriptException;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * @author Stephen Mallette (http://stephen.genoprime.com)
+ */
+public class GremlinGroovyScriptEngineThreadInterruptTest {
+ @Test
+ public void shouldInterruptWhile() throws Exception {
+ final ScriptEngine engine = new GremlinGroovyScriptEngine(new ThreadInterruptCustomizerProvider());
+ final AtomicBoolean asserted = new AtomicBoolean(false);
+
+ final Thread t = new Thread(() -> {
+ try {
+ engine.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 10000) {}");
+ } catch (ScriptException se) {
+ asserted.set(se.getCause().getCause() instanceof InterruptedException);
+ }
+ });
+
+ t.start();
+ Thread.sleep(100);
+ t.interrupt();
+ while(t.isAlive()) {}
+
+ assertTrue(asserted.get());
+ }
+
+ @Test
+ public void shouldNotInterruptWhile() throws Exception {
+ // companion to shouldInterruptWhile t
+ final ScriptEngine engine = new GremlinGroovyScriptEngine();
+ final AtomicBoolean exceptionFired = new AtomicBoolean(true);
+
+ final Thread t = new Thread(() -> {
+ try {
+ engine.eval("s = System.currentTimeMillis();\nwhile((System.currentTimeMillis() - s) < 2000) {};'test'");
+ exceptionFired.set(false);
+ } catch (ScriptException se) {
+ se.printStackTrace();
+ }
+ });
+
+ t.start();
+ Thread.sleep(100);
+ t.interrupt();
+ while(t.isAlive()) {}
+
+ assertFalse(exceptionFired.get());
+ }
+}
[09/31] incubator-tinkerpop git commit: Apply remaining custom
CompilationCustomizers to GremlinGroovyScriptEngine
Posted by sp...@apache.org.
Apply remaining custom CompilationCustomizers to GremlinGroovyScriptEngine
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/3df735b3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/3df735b3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/3df735b3
Branch: refs/heads/tp30
Commit: 3df735b371e7c67024e2c6deb989ef1eefc4bb4f
Parents: 47c0fa3
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Jul 8 13:19:31 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Jul 8 13:19:31 2015 -0400
----------------------------------------------------------------------
.../gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/3df735b3/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
index 68083e7..bb8b912 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
@@ -166,7 +166,7 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
// remove used providers as the rest will be applied directly
customizerProviders = providers.stream()
- .filter(p -> !(p instanceof SecurityCustomizerProvider || p instanceof ImportCustomizerProvider))
+ .filter(p -> p != null && !(p instanceof SecurityCustomizerProvider || p instanceof ImportCustomizerProvider))
.collect(Collectors.toList());
this.scriptEvaluationTimeout = scriptEvaluationTimeout;
@@ -506,6 +506,8 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
conf.addCompilationCustomizers(new ASTTransformationCustomizer(ThreadInterrupt.class));
+ customizerProviders.forEach(p -> conf.addCompilationCustomizers(p.create()));
+
this.loader = new GremlinGroovyClassLoader(getParentLoader(), conf);
this.securityProvider.ifPresent(SecurityCustomizerProvider::registerInterceptors);
}
[26/31] incubator-tinkerpop git commit: Removed groovy-sandbox
dependency.
Posted by sp...@apache.org.
Removed groovy-sandbox dependency.
Now that there's a more straightforward way to sandbox using AST transformations, this dependency can go. It was based on older version of groovy and potentially used class/methods that could be deprecated. The new approach with AST more closely fits with groovy's public API.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/3173d9ea
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/3173d9ea
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/3173d9ea
Branch: refs/heads/tp30
Commit: 3173d9ea7f8f4fd94fe2721620f078691dc19364
Parents: 84701fa
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Fri Jul 10 13:53:03 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Fri Jul 10 13:53:03 2015 -0400
----------------------------------------------------------------------
gremlin-groovy/pom.xml | 11 ---
.../groovy/SecurityCustomizerProvider.java | 66 ------------------
.../gremlin/groovy/engine/ScriptEngines.java | 17 +----
.../jsr223/GremlinGroovyScriptEngine.java | 35 +---------
.../groovy/engine/GremlinExecutorTest.java | 54 ---------------
.../jsr223/GremlinGroovyScriptEngineTest.java | 71 --------------------
6 files changed, 2 insertions(+), 252 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/3173d9ea/gremlin-groovy/pom.xml
----------------------------------------------------------------------
diff --git a/gremlin-groovy/pom.xml b/gremlin-groovy/pom.xml
index 2baa511..6d28501 100644
--- a/gremlin-groovy/pom.xml
+++ b/gremlin-groovy/pom.xml
@@ -66,17 +66,6 @@ limitations under the License.
<version>3.3.1</version>
</dependency>
<dependency>
- <groupId>org.kohsuke</groupId>
- <artifactId>groovy-sandbox</artifactId>
- <version>1.6</version>
- <exclusions>
- <exclusion>
- <groupId>org.codehaus.groovy</groupId>
- <artifactId>groovy</artifactId>
- </exclusion>
- </exclusions>
- </dependency>
- <dependency>
<groupId>org.apache.tinkerpop</groupId>
<artifactId>gremlin-test</artifactId>
<version>${project.version}</version>
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/3173d9ea/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/SecurityCustomizerProvider.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/SecurityCustomizerProvider.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/SecurityCustomizerProvider.java
deleted file mode 100644
index bef0c2c..0000000
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/SecurityCustomizerProvider.java
+++ /dev/null
@@ -1,66 +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 org.codehaus.groovy.control.customizers.CompilationCustomizer;
-import org.kohsuke.groovy.sandbox.GroovyInterceptor;
-import org.kohsuke.groovy.sandbox.SandboxTransformer;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-/**
- * @author Stephen Mallette (http://stephen.genoprime.com)
- */
-public class SecurityCustomizerProvider implements CompilerCustomizerProvider {
-
- private final List<GroovyInterceptor> interceptors;
-
- public SecurityCustomizerProvider() {
- this.interceptors = new ArrayList<>();
- }
-
- public SecurityCustomizerProvider(final GroovyInterceptor... interceptors) {
- this.interceptors = Arrays.asList(interceptors);
- }
-
- public void addInterceptor(final GroovyInterceptor interceptor) {
- this.interceptors.add(interceptor);
- }
-
- public void removeInterceptor(final GroovyInterceptor interceptor) {
- this.interceptors.remove(interceptor);
- }
-
- @Override
- public CompilationCustomizer create() {
- return new SandboxTransformer();
- }
-
- public void registerInterceptors() {
- interceptors.forEach(GroovyInterceptor::register);
- }
-
- public void unregisterInterceptors() {
- interceptors.forEach(GroovyInterceptor::unregister);
- }
-
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/3173d9ea/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
index ba7157e..c83a532 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/engine/ScriptEngines.java
@@ -20,15 +20,11 @@ package org.apache.tinkerpop.gremlin.groovy.engine;
import org.apache.tinkerpop.gremlin.groovy.CompilerCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.DefaultImportCustomizerProvider;
-import org.apache.tinkerpop.gremlin.groovy.SecurityCustomizerProvider;
-import org.apache.tinkerpop.gremlin.groovy.ThreadInterruptCustomizerProvider;
-import org.apache.tinkerpop.gremlin.groovy.TimedInterruptCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.jsr223.DependencyManager;
import org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine;
import org.apache.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngineFactory;
import org.apache.tinkerpop.gremlin.groovy.plugin.GremlinPlugin;
import org.apache.tinkerpop.gremlin.groovy.plugin.IllegalEnvironmentException;
-import org.kohsuke.groovy.sandbox.GroovyInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -338,17 +334,6 @@ public class ScriptEngines implements AutoCloseable {
final List<CompilerCustomizerProvider> providers = new ArrayList<>();
providers.add(new DefaultImportCustomizerProvider(imports, staticImports));
- final String clazz = (String) config.getOrDefault("sandbox", "");
- if (!clazz.isEmpty()) {
- try {
- final Class providerClass = Class.forName(clazz);
- final GroovyInterceptor interceptor = (GroovyInterceptor) providerClass.newInstance();
- providers.add(new SecurityCustomizerProvider(interceptor));
- } catch (Exception ex) {
- logger.warn("Could not instantiate GroovyInterceptor implementation [{}] for the SecurityCustomizerProvider. It will not be applied.", clazz);
- }
- }
-
// the key to the config of the compilerCustomizerProvider is the fully qualified classname of a
// CompilerCustomizerProvider. the value is a list of arguments to pass to an available constructor.
// the arguments must match in terms of type, so given that configuration typically comes from yaml
@@ -371,7 +356,7 @@ public class ScriptEngines implements AutoCloseable {
providers.add((CompilerCustomizerProvider) providerClass.newInstance());
}
} catch(Exception ex) {
- logger.warn("Could not instantiate CompilerCustomizerProvider implementation [{}]. It will not be applied.", clazz);
+ logger.warn("Could not instantiate CompilerCustomizerProvider implementation [{}]. It will not be applied.", k);
}
});
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/3173d9ea/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
index d484d4e..dae19e5 100644
--- a/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
+++ b/gremlin-groovy/src/main/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngine.java
@@ -23,7 +23,6 @@ import org.apache.tinkerpop.gremlin.groovy.DefaultImportCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.EmptyImportCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.ImportCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.NoImportCustomizerProvider;
-import org.apache.tinkerpop.gremlin.groovy.SecurityCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.loaders.GremlinLoader;
import org.apache.tinkerpop.gremlin.groovy.plugin.Artifact;
import org.apache.tinkerpop.gremlin.groovy.plugin.GremlinPlugin;
@@ -67,7 +66,6 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
-import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.atomic.AtomicLong;
@@ -84,7 +82,6 @@ import java.util.stream.Collectors;
public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements DependencyManager, AutoCloseable {
public static final String COMPILE_OPTIONS_VAR_TYPES = "sandbox.bindings";
- public static final String COMPILE_OPTIONS_WHITELIST = "sandbox.whitelist";
public static final String KEY_REFERENCE_TYPE = "#jsr223.groovy.engine.keep.globals";
public static final String REFERENCE_TYPE_PHANTOM = "phantom";
public static final String REFERENCE_TYPE_WEAK = "weak";
@@ -93,13 +90,6 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
private static final Pattern patternImportStatic = Pattern.compile("\\Aimport\\sstatic.*");
- private ThreadLocal<Boolean> registeredSandbox = new ThreadLocal<Boolean>() {
- @Override
- protected Boolean initialValue() {
- return false;
- }
- };
-
public static final ThreadLocal<Map<String, Object>> COMPILE_OPTIONS = new ThreadLocal<Map<String, Object>>(){
@Override
protected Map<String, Object> initialValue() {
@@ -134,7 +124,6 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
private static final String GROOVY_LANG_SCRIPT = "groovy.lang.Script";
private ImportCustomizerProvider importCustomizerProvider;
- private Optional<SecurityCustomizerProvider> securityProvider;
private final Set<Artifact> artifactsToUse = new HashSet<>();
@@ -154,14 +143,9 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
.map(p -> (ImportCustomizerProvider) p)
.findFirst().orElse(NoImportCustomizerProvider.INSTANCE);
- securityProvider = providers.stream()
- .filter(p -> p instanceof SecurityCustomizerProvider)
- .map(p -> (SecurityCustomizerProvider) p)
- .findFirst();
-
// remove used providers as the rest will be applied directly
customizerProviders = providers.stream()
- .filter(p -> p != null && !(p instanceof SecurityCustomizerProvider || p instanceof ImportCustomizerProvider))
+ .filter(p -> p != null && !(p instanceof ImportCustomizerProvider))
.collect(Collectors.toList());
createClassLoader();
@@ -249,12 +233,10 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
@Override
public void close() throws Exception {
- securityProvider.ifPresent(SecurityCustomizerProvider::unregisterInterceptors);
}
@Override
public void reset() {
- securityProvider.ifPresent(SecurityCustomizerProvider::unregisterInterceptors);
createClassLoader();
// must clear the local cache here because the the classloader has been reset. therefore, classes previously
@@ -368,8 +350,6 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
}
Class getScriptClass(final String script) throws SyntaxException, CompilationFailedException, IOException {
- ensureSandbox();
-
Class clazz = classMap.get(script);
if (clazz != null) return clazz;
@@ -383,8 +363,6 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
}
Object eval(final Class scriptClass, final ScriptContext context) throws ScriptException {
- ensureSandbox();
-
context.setAttribute("context", context, ScriptContext.ENGINE_SCOPE);
final Writer writer = context.getWriter();
context.setAttribute("out", writer instanceof PrintWriter ? writer : new PrintWriter(writer), ScriptContext.ENGINE_SCOPE);
@@ -475,13 +453,6 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
((Map<String,ClassNode>) m.get(COMPILE_OPTIONS_VAR_TYPES)).clear();
}
- private void ensureSandbox() {
- if (securityProvider.isPresent() && !registeredSandbox.get()) {
- securityProvider.get().registerInterceptors();
- registeredSandbox.set(true);
- }
- }
-
private Object invokeImpl(final Object thiz, final String name, final Object args[]) throws ScriptException, NoSuchMethodException {
if (name == null) {
throw new NullPointerException("Method name can not be null");
@@ -502,13 +473,9 @@ public class GremlinGroovyScriptEngine extends GroovyScriptEngineImpl implements
final CompilerConfiguration conf = new CompilerConfiguration();
conf.addCompilationCustomizers(this.importCustomizerProvider.create());
- if (this.securityProvider.isPresent())
- conf.addCompilationCustomizers(this.securityProvider.get().create());
-
customizerProviders.forEach(p -> conf.addCompilationCustomizers(p.create()));
this.loader = new GremlinGroovyClassLoader(getParentLoader(), conf);
- this.securityProvider.ifPresent(SecurityCustomizerProvider::registerInterceptors);
}
private Object callGlobal(final String name, final Object args[]) {
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/3173d9ea/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 6604b78..6fd3d01 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
@@ -22,13 +22,10 @@ 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.GremlinGroovyScriptEngineTest;
import org.junit.Test;
-import org.kohsuke.groovy.sandbox.GroovyInterceptor;
import javax.script.Bindings;
import javax.script.CompiledScript;
-import javax.script.ScriptException;
import javax.script.SimpleBindings;
import java.util.ArrayList;
import java.util.Arrays;
@@ -504,57 +501,6 @@ public class GremlinExecutorTest {
}
@Test
- public void shouldSecureAll() throws Exception {
- GroovyInterceptor.getApplicableInterceptors().forEach(GroovyInterceptor::unregister);
- final Map<String, Object> config = new HashMap<>();
- config.put("sandbox", GremlinGroovyScriptEngineTest.DenyAll.class.getName());
- final GremlinExecutor gremlinExecutor = GremlinExecutor.build()
- .addEngineSettings("gremlin-groovy",
- Collections.emptyList(),
- Collections.emptyList(),
- Arrays.asList(PATHS.get("GremlinExecutorInit.groovy")),
- config)
- .create();
- try {
- gremlinExecutor.eval("c = new java.awt.Color(255, 255, 255)").get();
- fail("Should have failed security");
- } catch (Exception se) {
- assertEquals(SecurityException.class, se.getCause().getClass());
- } finally {
- gremlinExecutor.close();
- }
- }
-
- @Test
- public void shouldSecureSome() throws Exception {
- GroovyInterceptor.getApplicableInterceptors().forEach(GroovyInterceptor::unregister);
- final Map<String, Object> config = new HashMap<>();
- config.put("sandbox", GremlinGroovyScriptEngineTest.AllowSome.class.getName());
- final GremlinExecutor gremlinExecutor = GremlinExecutor.build()
- .addEngineSettings("gremlin-groovy",
- Collections.emptyList(),
- Collections.emptyList(),
- Arrays.asList(PATHS.get("GremlinExecutorInit.groovy")),
- config)
- .create();
- try {
- gremlinExecutor.eval("c = 'new java.awt.Color(255, 255, 255)'").get();
- fail("Should have failed security");
- } catch (Exception se) {
- assertEquals(SecurityException.class, se.getCause().getClass());
- }
-
- try {
- final java.awt.Color c = (java.awt.Color) gremlinExecutor.eval("g = new java.awt.Color(255, 255, 255)").get();
- assertEquals(java.awt.Color.class, c.getClass());
- } catch (Exception ignored) {
- fail("Should not have tossed an exception");
- } finally {
- gremlinExecutor.close();
- }
- }
-
- @Test
public void shouldInitializeWithScriptAndWorkAfterReset() throws Exception {
final GremlinExecutor gremlinExecutor = GremlinExecutor.build()
.addEngineSettings("gremlin-groovy",
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/3173d9ea/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
----------------------------------------------------------------------
diff --git a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
index 3fda86b..5ca078a 100644
--- a/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
+++ b/gremlin-groovy/src/test/java/org/apache/tinkerpop/gremlin/groovy/jsr223/GremlinGroovyScriptEngineTest.java
@@ -19,16 +19,10 @@
package org.apache.tinkerpop.gremlin.groovy.jsr223;
import groovy.lang.Closure;
-import groovy.lang.Script;
-import org.apache.tinkerpop.gremlin.groovy.DefaultImportCustomizerProvider;
import org.apache.tinkerpop.gremlin.groovy.NoImportCustomizerProvider;
-import org.apache.tinkerpop.gremlin.groovy.SecurityCustomizerProvider;
-import org.apache.tinkerpop.gremlin.groovy.TimedInterruptCustomizerProvider;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.apache.tinkerpop.gremlin.util.iterator.IteratorUtils;
import org.junit.Test;
-import org.kohsuke.groovy.sandbox.GroovyInterceptor;
-import org.kohsuke.groovy.sandbox.GroovyValueFilter;
import javax.script.Bindings;
import javax.script.ScriptContext;
@@ -38,7 +32,6 @@ import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.Assert.assertEquals;
@@ -265,72 +258,8 @@ public class GremlinGroovyScriptEngineTest {
}
@Test
- public void shouldSecureAll() throws Exception {
- GroovyInterceptor.getApplicableInterceptors().forEach(GroovyInterceptor::unregister);
- final SecurityCustomizerProvider provider = new SecurityCustomizerProvider(new DenyAll());
- final GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(
- new DefaultImportCustomizerProvider(), provider);
- try {
- scriptEngine.eval("g = new java.awt.Color(255, 255, 255)");
- fail("Should have failed security");
- } catch (ScriptException se) {
- assertEquals(SecurityException.class, se.getCause().getCause().getClass());
- } finally {
- provider.unregisterInterceptors();
- }
- }
-
- @Test
- public void shouldSecureSome() throws Exception {
- GroovyInterceptor.getApplicableInterceptors().forEach(GroovyInterceptor::unregister);
- final SecurityCustomizerProvider provider = new SecurityCustomizerProvider(new AllowSome());
- final GremlinGroovyScriptEngine scriptEngine = new GremlinGroovyScriptEngine(
- new DefaultImportCustomizerProvider(), provider);
- try {
- scriptEngine.eval("g = 'new java.awt.Color(255, 255, 255)'");
- fail("Should have failed security");
- } catch (ScriptException se) {
- assertEquals(SecurityException.class, se.getCause().getCause().getClass());
- }
-
- try {
- final java.awt.Color c = (java.awt.Color) scriptEngine.eval("c = new java.awt.Color(255, 255, 255)");
- assertEquals(java.awt.Color.class, c.getClass());
- } catch (Exception ex) {
- fail("Should not have tossed an exception");
- } finally {
- provider.unregisterInterceptors();
- }
- }
-
- @Test
public void shouldProcessScriptWithUTF8Characters() throws Exception {
final ScriptEngine engine = new GremlinGroovyScriptEngine();
assertEquals("轉注", engine.eval("'轉注'"));
}
-
- public static class DenyAll extends GroovyValueFilter {
- @Override
- public Object filter(final Object o) {
- throw new SecurityException("Denied!");
- }
- }
-
- public static class AllowSome extends GroovyValueFilter {
-
- public static final Set<Class> ALLOWED_TYPES = new HashSet<Class>() {{
- add(java.awt.Color.class);
- add(Integer.class);
- add(Class.class);
- }};
-
- @Override
- public Object filter(final Object o) {
- if (null == o || ALLOWED_TYPES.contains(o.getClass()))
- return o;
- if (o instanceof Script || o instanceof Closure)
- return o; // access to properties of compiled groovy script
- throw new SecurityException("Unexpected type: " + o.getClass());
- }
- }
}
[31/31] incubator-tinkerpop git commit: Update changelog.
Posted by sp...@apache.org.
Update changelog.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/2cf9f5e3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/2cf9f5e3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/2cf9f5e3
Branch: refs/heads/tp30
Commit: 2cf9f5e3e27991f5e50e49d66480f269287e573d
Parents: cacba0d
Author: Stephen Mallette <sp...@genoprime.com>
Authored: Wed Jul 15 16:59:56 2015 -0400
Committer: Stephen Mallette <sp...@genoprime.com>
Committed: Wed Jul 15 16:59:56 2015 -0400
----------------------------------------------------------------------
CHANGELOG.asciidoc | 4 ++++
1 file changed, 4 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/2cf9f5e3/CHANGELOG.asciidoc
----------------------------------------------------------------------
diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc
index 830144e..cf7304d 100644
--- a/CHANGELOG.asciidoc
+++ b/CHANGELOG.asciidoc
@@ -29,6 +29,10 @@ TinkerPop 3.0.1 (NOT OFFICIALLY RELEASED YET)
* Removed "reserved" graph concept names from tests (e.g. "label", "edge", "value") to support the convention of avoiding these strings for property names.
* Introduced `GraphProvider.Descriptor` which annotates a `GraphProvider` implementation to describe what `GraphComputer` implementation will be used.
* Modified `OptOut` to include a `computers` attribute which allows the `Graph` to opt-out of computer-based tests for specific computation engines.
+* Added a `SandboxExtension` that can be plugged into `TypeCheckedCustomizerProvider` and `CompileStaticCustomizerProvider` to control classes and methods that can be used in the `GremlinGroovyScriptEngine`.
+* Added a number of new `ImportCustomizerProvider` implementations such as, `TimedInterruptCustomizerProvider`, `TypeCheckedCustomizerProvider` and others.
+* Refactored `GremlinGroovyScriptEngine` to make more general use of `ImportCustomizerProvider` implementations.
+* Removed dependency on `groovy-sandbox` from the `gremlin-groovy` module.
TinkerPop 3.0.0 (Release Date: July 9, 2015)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~