-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add support for Kotlin JSR223 scripts #2898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
* Copyright 2019 the original author or authors. | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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.springframework.integration.scripting.jsr223; | ||
|
||
import javax.script.Bindings; | ||
import javax.script.ScriptEngine; | ||
|
||
import org.jetbrains.kotlin.script.jsr223.KotlinJsr223JvmLocalScriptEngineFactory; | ||
|
||
|
||
/** | ||
* @author Artem Bilan | ||
* | ||
* @since 5.2 | ||
*/ | ||
public class KotlinScriptExecutor extends AbstractScriptExecutor { | ||
|
||
static { | ||
System.setProperty("idea.use.native.fs.for.win", "false"); | ||
} | ||
|
||
public KotlinScriptExecutor() { | ||
super(new KotlinJsr223JvmLocalScriptEngineFactory().getScriptEngine()); | ||
} | ||
|
||
@Override | ||
protected Object postProcess(Object result, ScriptEngine scriptEngine, String script, Bindings bindings) { | ||
return result; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,13 +16,21 @@ | |
|
||
package org.springframework.integration.scripting.jsr223; | ||
|
||
import javax.script.ScriptEngine; | ||
import javax.script.ScriptEngineManager; | ||
|
||
import org.springframework.integration.scripting.ScriptExecutor; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* The scripting configuration utilities. | ||
* | ||
* @author David Turanski | ||
* @author Artem Bilan | ||
* | ||
* @since 2.1 | ||
*/ | ||
public abstract class ScriptExecutorFactory { | ||
public final class ScriptExecutorFactory { | ||
|
||
public static ScriptExecutor getScriptExecutor(String language) { | ||
if (language.equalsIgnoreCase("python") || language.equalsIgnoreCase("jython")) { | ||
|
@@ -31,7 +39,32 @@ public static ScriptExecutor getScriptExecutor(String language) { | |
else if (language.equalsIgnoreCase("ruby") || language.equalsIgnoreCase("jruby")) { | ||
return new RubyScriptExecutor(); | ||
} | ||
else if (language.equalsIgnoreCase("kotlin")) { | ||
return new KotlinScriptExecutor(); | ||
} | ||
return new DefaultScriptExecutor(language); | ||
} | ||
|
||
/** | ||
* Derive a scripting language from the provided script file name. | ||
* @param scriptLocation the script file to consult for extension. | ||
* @return the language name for the {@link ScriptExecutor}. | ||
* @since 5.2 | ||
*/ | ||
public static String deriveLanguageFromFileExtension(String scriptLocation) { | ||
int index = scriptLocation.lastIndexOf(".") + 1; | ||
Assert.state(index > 0, () -> "Unable to determine language for script '" + scriptLocation + "'"); | ||
String extension = scriptLocation.substring(index); | ||
if (extension.equals("kts")) { | ||
return "kotlin"; | ||
} | ||
ScriptEngineManager engineManager = new ScriptEngineManager(); | ||
ScriptEngine engine = engineManager.getEngineByExtension(extension); | ||
Assert.state(engine != null, () -> "No suitable scripting engine found for extension '" + extension + "'"); | ||
return engine.getFactory().getLanguageName(); | ||
} | ||
|
||
private ScriptExecutorFactory() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Doesn't this work? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry; missed that 😦 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But it needs There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK. Will add. Can we tweak Sonar do not complain on the matter? Or do I miss something else ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding super is a no-op - the compiler generates one anyway. I don't think we want to disable empty block detection. |
||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needs a
private
CTOR.