2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.plugins.executor.jruby;
24 import java.util.Properties;
26 import java.util.TreeSet;
27 import org.jruby.embed.EmbedEvalUnit;
28 import org.jruby.embed.LocalContextScope;
29 import org.jruby.embed.LocalVariableBehavior;
30 import org.jruby.embed.ScriptingContainer;
31 import org.jruby.runtime.builtin.IRubyObject;
32 import org.onap.policy.apex.context.ContextException;
33 import org.onap.policy.apex.core.engine.event.EnEvent;
34 import org.onap.policy.apex.core.engine.executor.TaskSelectExecutor;
35 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
37 import org.slf4j.ext.XLogger;
38 import org.slf4j.ext.XLoggerFactory;
41 * The Class JrubyTaskSelectExecutor is the task selection executor for task selection logic written in JRuby.
43 * @author Liam Fallon (liam.fallon@ericsson.com)
45 public class JrubyTaskSelectExecutor extends TaskSelectExecutor {
46 // Logger for this class
47 private static final XLogger LOGGER = XLoggerFactory.getXLogger(JrubyTaskSelectExecutor.class);
50 private ScriptingContainer container = new ScriptingContainer(LocalContextScope.CONCURRENT,
51 LocalVariableBehavior.TRANSIENT, true);
52 private EmbedEvalUnit parsedjruby = null;
55 * Prepares the task for processing.
57 * @throws StateMachineException thrown when a state machine execution error occurs
60 public void prepare() throws StateMachineException {
61 // Call generic prepare logic
64 // Set up the JRuby engine
65 container = (container == null)
66 ? new ScriptingContainer(LocalContextScope.CONCURRENT, LocalVariableBehavior.TRANSIENT, true)
69 // Use the container.setError(System.err) and container.setOutput(System.out) method calls to redirect output
70 // and error to standard output and error for debugging
72 container.put("executor", getExecutionContext()); // needed for compile as a placeholder
73 parsedjruby = container.parse(getSubject().getTaskSelectionLogic().getLogic());
77 * Executes the executor for the task in a sequential manner.
79 * @param executionId the execution ID for the current APEX policy execution
80 * @param executionProperties properties for the current APEX policy execution
81 * @param incomingEvent the incoming event
82 * @return The outgoing event
83 * @throws StateMachineException on an execution error
84 * @throws ContextException on context errors
87 public AxArtifactKey execute(final long executionId, final Properties executionProperties,
88 final EnEvent incomingEvent) throws StateMachineException, ContextException {
89 // Do execution pre work
90 executePre(executionId, executionProperties, incomingEvent);
92 // Check and execute the JRuby logic
93 container.put("executor", getExecutionContext());
95 /* Precompiled version */
96 boolean returnValue = false;
97 final IRubyObject ret = parsedjruby.run();
99 final Boolean retbool = ret.toJava(java.lang.Boolean.class);
100 if (retbool != null) {
105 // Do the execution post work
106 executePost(returnValue);
108 // Send back the return event
109 return getOutgoing();
113 * Cleans up the task after processing.
115 * @throws StateMachineException thrown when a state machine execution error occurs
118 public void cleanUp() throws StateMachineException {
119 LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + ","
120 + getSubject().getTaskSelectionLogic().getLogicFlavour() + ","
121 + getSubject().getTaskSelectionLogic().getLogic());
122 container.terminate();
127 * Gets the output event set.
129 * @return the output event set
131 public Set<AxArtifactKey> getOutputEventSet() {
132 return new TreeSet<>();