Changes for checkstyle 8.32
[policy/apex-pdp.git] / plugins / plugins-executor / plugins-executor-jruby / src / main / java / org / onap / policy / apex / plugins / executor / jruby / JrubyStateFinalizerExecutor.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.executor.jruby;
23
24 import java.util.Map;
25 import java.util.Properties;
26 import org.jruby.embed.EmbedEvalUnit;
27 import org.jruby.embed.LocalContextScope;
28 import org.jruby.embed.LocalVariableBehavior;
29 import org.jruby.embed.ScriptingContainer;
30 import org.jruby.runtime.builtin.IRubyObject;
31 import org.onap.policy.apex.context.ContextException;
32 import org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor;
33 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
34 import org.slf4j.ext.XLogger;
35 import org.slf4j.ext.XLoggerFactory;
36
37 /**
38  * The Class JrubyStateFinalizerExecutor is the state finalizer executor for state finalizer logic written in JRuby.
39  *
40  * @author Liam Fallon (liam.fallon@ericsson.com)
41  */
42 public class JrubyStateFinalizerExecutor extends StateFinalizerExecutor {
43     // Logger for this class
44     private static final XLogger LOGGER = XLoggerFactory.getXLogger(JrubyStateFinalizerExecutor.class);
45
46     // Jruby container
47     private ScriptingContainer container = new ScriptingContainer(LocalContextScope.CONCURRENT,
48                     LocalVariableBehavior.TRANSIENT, true);
49     private EmbedEvalUnit parsedjruby = null;
50
51     /**
52      * Prepares the state finalizer for processing.
53      *
54      * @throws StateMachineException thrown when a state machine execution error occurs
55      */
56     @Override
57     public void prepare() throws StateMachineException {
58         // Call generic prepare logic
59         super.prepare();
60
61         // Set up the JRuby engine
62         container = (container == null)
63                         ? new ScriptingContainer(LocalContextScope.CONCURRENT, LocalVariableBehavior.TRANSIENT, true)
64                         : container;
65
66         // Use the container.setError(System.err) and container.setOutput(System.out) method calls to redirect output
67         // and error to standard output and error for debugging
68         container.put("executor", getExecutionContext()); // needed for the compile
69         parsedjruby = container.parse(getSubject().getLogic());
70     }
71
72     /**
73      * Executes the executor for the state finalizer logic in a sequential manner.
74      *
75      * @param executionId the execution ID for the current APEX policy execution
76      * @param executionProperties properties for the current APEX policy execution
77      * @param incomingFields the incoming fields for finalisation
78      * @return The state output for the state
79      * @throws StateMachineException on an execution error
80      * @throws ContextException on context errors
81      */
82     @Override
83     public String execute(final long executionId, final Properties executionProperties,
84                     final Map<String, Object> incomingFields) throws StateMachineException, ContextException {
85         // Do execution pre work
86         executePre(executionId, executionProperties, incomingFields);
87
88         // Check and execute the JRuby logic
89         container.put("executor", getExecutionContext());
90
91         /* Precompiled version */
92         boolean returnValue = false;
93         final IRubyObject ret = parsedjruby.run();
94         if (ret != null) {
95             final Boolean retbool = ret.toJava(java.lang.Boolean.class);
96             if (retbool != null) {
97                 returnValue = true;
98             }
99         }
100
101         // Do the execution post work
102         executePost(returnValue);
103
104         // Send back the return event
105         return getOutgoing();
106     }
107
108     /**
109      * Cleans up the state finalizer after processing.
110      *
111      * @throws StateMachineException thrown when a state machine execution error occurs
112      */
113     @Override
114     public void cleanUp() throws StateMachineException {
115         LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getLogicFlavour() + ","
116                         + getSubject().getLogic());
117         container.terminate();
118         container = null;
119     }
120 }