2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.plugins.executor.javascript;
24 import javax.script.Compilable;
25 import javax.script.CompiledScript;
26 import javax.script.ScriptEngine;
27 import javax.script.ScriptEngineManager;
28 import javax.script.ScriptException;
29 import org.onap.policy.apex.context.ContextException;
30 import org.onap.policy.apex.core.engine.executor.TaskExecutor;
31 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
36 * The Class JavascriptTaskExecutor is the task executor for task logic written in Javascript It is
37 * unlikely that this is thread safe.
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public class JavascriptTaskExecutor extends TaskExecutor {
42 // Logger for this class
43 private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavascriptTaskExecutor.class);
46 private ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
47 private CompiledScript compiled = null;
50 * Prepares the task for processing.
52 * @throws StateMachineException thrown when a state machine execution error occurs
55 public void prepare() throws StateMachineException {
56 // Call generic prepare logic
59 compiled = ((Compilable) engine).compile(getSubject().getTaskLogic().getLogic());
60 } catch (final ScriptException e) {
61 LOGGER.error("execute: task logic failed to compile for task \"" + getSubject().getKey().getId() + "\"");
62 throw new StateMachineException(
63 "task logic failed to compile for task \"" + getSubject().getKey().getId() + "\"", e);
68 * Executes the executor for the task in a sequential manner.
70 * @param executionId the execution ID for the current APEX policy execution
71 * @param incomingFields the incoming fields
72 * @return The outgoing fields
73 * @throws StateMachineException on an execution error
74 * @throws ContextException on context errors
77 public Map<String, Object> execute(final long executionId, final Map<String, Object> incomingFields)
78 throws StateMachineException, ContextException {
79 // Do execution pre work
80 executePre(executionId, incomingFields);
82 // Set up the Javascript engine
83 engine.put("executor", getExecutionContext());
85 // Check and execute the Javascript logic
86 boolean returnValue = false;
88 if (compiled == null) {
89 engine.eval(getSubject().getTaskLogic().getLogic());
91 compiled.eval(engine.getContext());
93 } catch (final ScriptException e) {
94 LOGGER.error("execute: task logic failed to run for task \"" + getSubject().getKey().getId() + "\"");
95 throw new StateMachineException(
96 "task logic failed to run for task \"" + getSubject().getKey().getId() + "\"", e);
99 final Object ret = engine.get("returnValue");
101 LOGGER.error("execute: task logic failed to set a return value for task \"" + getSubject().getKey().getId()
103 throw new StateMachineException("execute: task logic failed to set a return value for task \""
104 + getSubject().getKey().getId() + "\"");
107 // Do the execution post work
108 executePost((Boolean) ret);
110 return getOutgoing();
114 * Cleans up the task after processing.
116 * @throws StateMachineException thrown when a state machine execution error occurs
119 public void cleanUp() throws StateMachineException {
120 LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getTaskLogic().getLogicFlavour()
121 + "," + getSubject().getTaskLogic().getLogic());