[
https://issues.apache.org/jira/browse/NIFI-210?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15117436#comment-15117436
]
ASF GitHub Bot commented on NIFI-210:
-------------------------------------
Github user markap14 commented on a diff in the pull request:
https://github.com/apache/nifi/pull/185#discussion_r50852926
--- Diff:
nifi-nar-bundles/nifi-scripting-bundle/nifi-scripting-processors/src/main/java/org/apache/nifi/processors/script/impl/ScalaScriptEngineConfigurator.java
---
@@ -0,0 +1,132 @@
+/*
+ * 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.nifi.processors.script.impl;
+
+import org.apache.commons.io.input.ReaderInputStream;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.nifi.processors.script.ScriptEngineConfigurator;
+import org.apache.nifi.stream.io.ByteArrayInputStream;
+import scala.tools.nsc.Settings;
+import scala.tools.nsc.interpreter.IMain;
+import scala.tools.nsc.settings.MutableSettings;
+
+import javax.script.Compilable;
+import javax.script.CompiledScript;
+import javax.script.ScriptEngine;
+import javax.script.ScriptException;
+import java.io.File;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.Reader;
+import java.io.SequenceInputStream;
+import java.io.StringReader;
+import java.io.UnsupportedEncodingException;
+
+public class ScalaScriptEngineConfigurator implements
ScriptEngineConfigurator, Compilable {
+
+ private static final String PRELOADS =
+ "import org.apache.nifi.components._\n"
+ + "import org.apache.nifi.flowfile.FlowFile\n"
+ + "import org.apache.nifi.processor._\n"
+ + "import org.apache.nifi.processor.exception._\n"
+ + "import org.apache.nifi.processor.io._\n"
+ + "import org.apache.nifi.processor.util._\n"
+ + "import org.apache.nifi.processors.script._\n"
+ + "import
org.apache.nifi.processors.script.ExecuteScript._\n"
+ + "import org.apache.nifi.logging.ProcessorLog\n";
+
+ private ScriptEngine scriptEngine;
+
+ @Override
+ public String getScriptEngineName() {
+ return "Scala";
+ }
+
+ @Override
+ public Object init(ScriptEngine engine, String modulePath) throws
ScriptException {
+
+ if (modulePath != null) {
+ final Settings settings = ((IMain) engine).settings();
+
+ // We need to find our NAR's working lib/ directory, that's
where the Scala JARs are
+ String myPath =
this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
+
+ String parent = new File(myPath).getParent();
+ String scalaLib = StringUtils.replace(parent, "\\", "/")+"/*";
+
+ // The usual way to set up the scala compiler is to set
java.class.path and then do something like this:
+ // final MutableSettings.BooleanSetting useJavaClasspath =
(MutableSettings.BooleanSetting) settings.usejavacp();
+ // useJavaClasspath.tryToSetFromPropertyValue("true");
+ //
+ // However it appears if you set it up to use the java
classpath, it won't find the rest of the stuff we
+ // need (namely the NAR's dependencies. So instead we leave
the above setting "false" and build the
+ // classpath ourselves, prepending the NAR's dependency folder
to the value of java.class.path
+ final MutableSettings.PathSetting classpath =
settings.classpath();
+ classpath.prepend(System.getProperty("java.class.path"));
+ classpath.prepend(scalaLib);
+
+
+ }
+ scriptEngine = engine;
+ return scriptEngine;
+ }
+
+ @Override
+ public Object eval(ScriptEngine engine, InputStream scriptStream,
String modulePath) throws ScriptException {
+ try {
+ scriptEngine = engine;
+ ByteArrayInputStream bais = new
ByteArrayInputStream(PRELOADS.getBytes("UTF-8"));
+ SequenceInputStream fullInputStream = new
SequenceInputStream(bais, scriptStream);
+ return engine.eval(new InputStreamReader(fullInputStream));
+ } catch (UnsupportedEncodingException e) {
+ e.printStackTrace();
--- End diff --
I think this one slipped through the cracks - should this throw
ScriptException?
> Provide an ExecuteScript processor
> ----------------------------------
>
> Key: NIFI-210
> URL: https://issues.apache.org/jira/browse/NIFI-210
> Project: Apache NiFi
> Issue Type: Improvement
> Components: Core Framework
> Affects Versions: 0.0.1
> Reporter: A. Steven Anderson
> Assignee: Matt Burgess
> Labels: processor, scala
> Fix For: 0.5.0
>
> Attachments:
> 0001-NIFI-210-few-tweaks-to-drop-static-reference-and-fix.patch
>
>
> Add latest Scala version support for ExcecuteScript processor.
> Should also support Clojure as per discussion and request on mailing list
> http://mail-archives.apache.org/mod_mbox/nifi-dev/201506.mbox/%3CCAMpSqch4GK1gnw6M1u8tH6AN8e_miXZN5SNkAeMjBujXYGqJiw%40mail.gmail.com%3E
> UPDATE: The ScriptEngine for Clojure is not being maintained and is not
> currently available via Maven Central or a public repository. Recommend
> adding Clojure as a separate Improvement Jira case.
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)