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.jruby;
25 import org.jruby.embed.EmbedEvalUnit;
26 import org.jruby.embed.LocalContextScope;
27 import org.jruby.embed.LocalVariableBehavior;
28 import org.jruby.embed.ScriptingContainer;
29 import org.jruby.runtime.builtin.IRubyObject;
30 import org.onap.policy.apex.context.ContextException;
31 import org.onap.policy.apex.core.engine.executor.TaskExecutor;
32 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
33 import org.slf4j.ext.XLogger;
34 import org.slf4j.ext.XLoggerFactory;
37 * The Class JrubyTaskExecutor is the task executor for task logic written in JRuby.
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public class JrubyTaskExecutor extends TaskExecutor {
42 // Logger for this class
43 private static final XLogger LOGGER = XLoggerFactory.getXLogger(JrubyTaskExecutor.class);
46 private ScriptingContainer container =
47 new ScriptingContainer(LocalContextScope.CONCURRENT, LocalVariableBehavior.TRANSIENT, true);
48 private EmbedEvalUnit parsedjruby = null;
51 * Prepares the task for processing.
53 * @throws StateMachineException thrown when a state machine execution error occurs
56 public void prepare() throws StateMachineException {
57 // Call generic prepare logic
60 // Set up the JRuby engine
61 container = (container == null)
62 ? new ScriptingContainer(LocalContextScope.CONCURRENT, LocalVariableBehavior.TRANSIENT, true)
64 container.setError(System.err);
65 container.setOutput(System.out);
66 container.put("executor", getExecutionContext()); // needed for the compile
67 parsedjruby = container.parse(getSubject().getTaskLogic().getLogic());
71 * Executes the executor for the task in a sequential manner.
73 * @param executionId the execution ID for the current APEX policy execution
74 * @param incomingFields the incoming fields
75 * @return The outgoing fields
76 * @throws StateMachineException on an execution error
77 * @throws ContextException on context errors
80 public Map<String, Object> execute(final long executionId, final Map<String, Object> incomingFields)
81 throws StateMachineException, ContextException {
82 // Do execution pre work
83 executePre(executionId, incomingFields);
85 // Check and execute the JRuby logic
86 container.put("executor", getExecutionContext());
88 /* Precompiled version */
89 boolean returnValue = false;
90 final IRubyObject ret = parsedjruby.run();
92 final Boolean retbool = ret.toJava(java.lang.Boolean.class);
93 if (retbool != null) {
98 // Do the execution post work
99 executePost(returnValue);
101 // Send back the return event
103 return getOutgoing();
110 * Cleans up the task after processing.
112 * @throws StateMachineException thrown when a state machine execution error occurs
115 public void cleanUp() throws StateMachineException {
116 LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getTaskLogic().getLogicFlavour()
117 + "," + getSubject().getTaskLogic().getLogic());
118 container.terminate();