|
| 1 | +/* |
| 2 | + * Copyright 2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.integration.scripting; |
| 18 | + |
| 19 | +import java.util.Map; |
| 20 | + |
| 21 | +import org.graalvm.polyglot.Context; |
| 22 | +import org.graalvm.polyglot.Value; |
| 23 | + |
| 24 | +import org.springframework.lang.Nullable; |
| 25 | +import org.springframework.scripting.ScriptSource; |
| 26 | +import org.springframework.util.Assert; |
| 27 | + |
| 28 | +/** |
| 29 | + * GraalVM Polyglot {@link ScriptExecutor} implementation. |
| 30 | + * |
| 31 | + * @author Artem Bilan |
| 32 | + * |
| 33 | + * @since 6.0 |
| 34 | + */ |
| 35 | +public class PolyglotScriptExecutor implements ScriptExecutor { |
| 36 | + |
| 37 | + private final String language; |
| 38 | + |
| 39 | + private Context.Builder contextBuilder; |
| 40 | + |
| 41 | + /** |
| 42 | + * Construct an executor based on the provided language id. |
| 43 | + * @param language the supported by GraalVM language id. |
| 44 | + */ |
| 45 | + public PolyglotScriptExecutor(String language) { |
| 46 | + this(language, Context.newBuilder().allowAllAccess(true)); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Construct an executor based on the provided language id. |
| 51 | + * @param language the supported by GraalVM language id. |
| 52 | + */ |
| 53 | + public PolyglotScriptExecutor(String language, Context.Builder contextBuilder) { |
| 54 | + Assert.hasText(language, "'language' must not be empty"); |
| 55 | + Assert.notNull(contextBuilder, "'contextBuilder' must not be null"); |
| 56 | + this.contextBuilder = contextBuilder; |
| 57 | + this.language = language; |
| 58 | + try (Context context = this.contextBuilder.build()) { |
| 59 | + context.initialize(language); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public Object executeScript(ScriptSource scriptSource, @Nullable Map<String, Object> variables) { |
| 65 | + try (Context context = this.contextBuilder.build()) { |
| 66 | + if (variables != null) { |
| 67 | + Value bindings = context.getBindings(this.language); |
| 68 | + variables.forEach(bindings::putMember); |
| 69 | + } |
| 70 | + return context.eval(this.language, scriptSource.getScriptAsString()).as(Object.class); |
| 71 | + } |
| 72 | + catch (Exception ex) { |
| 73 | + throw new ScriptingException(ex.getMessage(), ex); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | +} |
0 commit comments