#ignite-965: code style IgniteScriptProcessor.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/0a257f18 Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/0a257f18 Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/0a257f18 Branch: refs/heads/ignite-965 Commit: 0a257f1809cc8e1dd3710558582478859325da9b Parents: d481ea8 Author: ivasilinets <ivasilin...@gridgain.com> Authored: Tue Jun 23 18:27:42 2015 +0300 Committer: ivasilinets <ivasilin...@gridgain.com> Committed: Tue Jun 23 18:27:42 2015 +0300 ---------------------------------------------------------------------- .../compute/IgniteComputeCommandHandler.java | 14 +--- .../IgniteComputeTaskCommandHandler.java | 84 +++++++++----------- .../internal/processors/scripting/IgniteJS.java | 27 +++++++ .../scripting/IgniteScriptProcessor.java | 74 +++++++++-------- .../internal/IgniteScriptManagerSelfTest.java | 40 ---------- 5 files changed, 101 insertions(+), 138 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0a257f18/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeCommandHandler.java index 345a898..acb4ab6 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeCommandHandler.java @@ -69,12 +69,7 @@ public class IgniteComputeCommandHandler extends GridRestCommandHandlerAdapter { private Ignite ignite; @Override public void run() { - try { - ((IgniteKernal) ignite).context().scripting().runJS(req0.function()); - } - catch (ScriptException e) { - throw new IgniteException(e); - } + ((IgniteKernal) ignite).context().scripting().runJSFunction(req0.function()); } }); @@ -86,12 +81,7 @@ public class IgniteComputeCommandHandler extends GridRestCommandHandlerAdapter { private Ignite ignite; @Override public Object call() { - try { - return ((IgniteKernal) ignite).context().scripting().runJS(req0.function()); - } - catch (ScriptException e) { - throw new IgniteException(e); - } + return ((IgniteKernal) ignite).context().scripting().runJSFunction(req0.function()); } }); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0a257f18/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeTaskCommandHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeTaskCommandHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeTaskCommandHandler.java index 7329a5b..8de073c 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeTaskCommandHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/handlers/compute/IgniteComputeTaskCommandHandler.java @@ -99,61 +99,54 @@ public class IgniteComputeTaskCommandHandler extends GridRestCommandHandlerAdapt nodesIds = nodesIds.substring(0, nodesIds.length() - 1) + "]"; - try { - String newMap = new String("function () {\n" + - " var res = [];\n" + - " var resCont = function(f, args, nodeId) {\n" + - " res.push([f.toString(), args, nodeId])\n" + - " }\n" + - " var locF = " + mapFunc + "; \n locF(" + - nodesIds + ", " + - "\"" + this.arg + "\"" + - ", resCont.bind(null)" + ");\n" + - " return res;\n" + - "}"); + String newMap = new String("function () {\n" + + " var res = [];\n" + + " var resCont = function(f, args, nodeId) {\n" + + " res.push([f.toString(), args, nodeId])\n" + + " }\n" + + " var locF = " + mapFunc + "; \n locF(" + + nodesIds + ", " + + "\"" + this.arg + "\"" + + ", resCont.bind(null)" + ");\n" + + " return res;\n" + + "}"); - List mapRes = (List)ctx.scripting().runJS(newMap); + List mapRes = (List)ctx.scripting().runJSFunction(newMap); - for (Object arr : mapRes) { - Object[] nodeTask = ((List)arr).toArray(); + for (Object arr : mapRes) { + Object[] nodeTask = ((List)arr).toArray(); - final String func = (String)nodeTask[0]; + final String func = (String)nodeTask[0]; - final List argv = (List) nodeTask[1]; + final List argv = (List) nodeTask[1]; - String nodeIdStr = (String) nodeTask[2]; + String nodeIdStr = (String) nodeTask[2]; - UUID nodeId = UUID.fromString(nodeIdStr); + UUID nodeId = UUID.fromString(nodeIdStr); - ClusterNode node = ctx.grid().cluster().node(nodeId); + ClusterNode node = ctx.grid().cluster().node(nodeId); - map.put(new ComputeJobAdapter() { - /** Ignite. */ - @IgniteInstanceResource - private Ignite ignite; + map.put(new ComputeJobAdapter() { + /** Ignite. */ + @IgniteInstanceResource + private Ignite ignite; - @Override public Object execute() throws IgniteException { - System.out.println("Compute job on node " + ignite.cluster().localNode().id()); - try { - String[] argv1 = new String[argv.size()]; + @Override public Object execute() throws IgniteException { + System.out.println("Compute job on node " + ignite.cluster().localNode().id()); + try { + String[] argv1 = new String[argv.size()]; - for (int i = 0; i < argv1.length; ++i) - argv1[i] = "\"" + argv.get(i).toString() + "\""; + for (int i = 0; i < argv1.length; ++i) + argv1[i] = "\"" + argv.get(i).toString() + "\""; - return ctx.scripting().runJS(func, argv1); - } - catch (Exception e) { - throw new IgniteException(e); - } + return ctx.scripting().runJSFunction(func, argv1); } - }, node); + catch (Exception e) { + throw new IgniteException(e); + } + } + }, node); - } - } - catch (ScriptException e) { - throw new IgniteException(e); - } - finally { } return map; @@ -166,12 +159,7 @@ public class IgniteComputeTaskCommandHandler extends GridRestCommandHandlerAdapt for (ComputeJobResult res : results) data.add(res.getData()); - try { - return ctx.scripting().runJS(reduceFunc, new String[] {data.toString()}); - } - catch (ScriptException e) { - throw new IgniteException(e); - } + return ctx.scripting().runJSFunction(reduceFunc, new String[]{data.toString()}); } } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0a257f18/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteJS.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteJS.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteJS.java new file mode 100644 index 0000000..6ffe1b1 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteJS.java @@ -0,0 +1,27 @@ +/* + * 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.ignite.internal.processors.scripting; + +/** + * Ignite JS binding. + */ +public class IgniteJS { + public void hello() { + System.out.println("HELLO HAPPY WORLD!!!"); + } +} http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0a257f18/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptProcessor.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptProcessor.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptProcessor.java index 99c206e..094373c 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptProcessor.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/scripting/IgniteScriptProcessor.java @@ -20,20 +20,20 @@ package org.apache.ignite.internal.processors.scripting; import org.apache.ignite.*; import org.apache.ignite.internal.*; import org.apache.ignite.internal.processors.*; -import org.apache.ignite.internal.util.typedef.*; import javax.script.*; -import java.util.*; + +import static javax.script.ScriptContext.*; /** - * Ignite scripting manager. + * Ignite scripting processor. */ public class IgniteScriptProcessor extends GridProcessorAdapter { /** Javascript engine name. */ public static final String JAVA_SCRIPT_ENGINE_NAME = "JavaScript"; - /** Script factory **/ - private final ScriptEngineManager factory = new ScriptEngineManager(); + /** Javascript engine. */ + private ScriptEngine jsEngine; /** * @param ctx Kernal context. @@ -42,16 +42,17 @@ public class IgniteScriptProcessor extends GridProcessorAdapter { super(ctx); } - /** - * @param engName Engine name. - * @param script Script. - * @throws ScriptException If script failed. - */ - public Object run(String engName, String script) throws ScriptException { - if (!engName.equals(JAVA_SCRIPT_ENGINE_NAME)) - throw new IgniteException("Engine is not supported. [engName=" + engName + "]"); + /** {@inheritDoc} */ + @Override public void start() throws IgniteCheckedException { + ScriptEngineManager factory = new ScriptEngineManager(); + + jsEngine = factory.getEngineByName(JAVA_SCRIPT_ENGINE_NAME); - return runJS(script); + Bindings bind = jsEngine.createBindings(); + + bind.put("ignite", new IgniteJS()); + + jsEngine.setBindings(bind, ENGINE_SCOPE); } /** @@ -60,23 +61,13 @@ public class IgniteScriptProcessor extends GridProcessorAdapter { * @return Script result. * @throws ScriptException If script failed. */ - public Object runJS(String script, String[] args) throws ScriptException { - ScriptEngine engine = factory.getEngineByName("JavaScript"); - - Bindings b = engine.createBindings(); - - b.put("ignite", new Ignite()); - - engine.setBindings(b, ScriptContext.ENGINE_SCOPE); - - script = "(" + script + ")(" ; - - for (int i = 0; i < args.length; ++i) - script += args[i] + (i < args.length - 1 ? "," : ""); - - script += ");"; - - return engine.eval(script); + public Object runJSFunction(String script, String[] args) throws IgniteException { + try { + return jsEngine.eval(callJsFunction(script, args)); + } + catch (ScriptException e) { + throw new IgniteException("Cannot evaluate javascript function + " + script, e); + } } /** @@ -84,16 +75,23 @@ public class IgniteScriptProcessor extends GridProcessorAdapter { * @return Script result. * @throws ScriptException If script failed. */ - public Object runJS(String script) throws ScriptException { - return runJS(script, new String[]{""}); + public Object runJSFunction(String script) throws IgniteException { + return runJSFunction(script, new String[]{""}); } /** - * Ignite JS binding. + * @param script JS function script. + * @param args Arguments. + * @return Script that calls function. */ - public static class Ignite { - public void hello() { - System.out.println("HELLO HAPPY WORLD!!!"); - } + private String callJsFunction(String script, String[] args) { + String callFuncScript = "(" + script + ")("; + + for (int i = 0; i < args.length; ++i) + callFuncScript += args[i] + (i < args.length - 1 ? "," : ""); + + callFuncScript += ");"; + + return callFuncScript; } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0a257f18/modules/nodejs/src/test/java/org/apache/ignite/internal/IgniteScriptManagerSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/nodejs/src/test/java/org/apache/ignite/internal/IgniteScriptManagerSelfTest.java b/modules/nodejs/src/test/java/org/apache/ignite/internal/IgniteScriptManagerSelfTest.java deleted file mode 100644 index e61fd26..0000000 --- a/modules/nodejs/src/test/java/org/apache/ignite/internal/IgniteScriptManagerSelfTest.java +++ /dev/null @@ -1,40 +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.ignite.internal; - -import org.apache.ignite.internal.processors.scripting.*; -import org.apache.ignite.testframework.junits.common.*; - -/** - * Test {@link IgniteScriptProcessor} - */ -public class IgniteScriptManagerSelfTest extends GridCommonAbstractTest { - /** - * @throws Exception If failed. - */ - public void testRunScript() throws Exception { - IgniteScriptProcessor mng = startGrid(0).context().scripting(); - - System.out.println("Result = " + mng.runJS("5 + 5;")); - System.out.println("Result = " + mng.runJS("function () {return (5+5)}")); - - System.out.println("Result = " + mng.runJS("function sum() {return (5+5)}; sum(); ")); - - stopAllGrids(); - } -}