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.java;
23 import java.lang.reflect.Method;
26 import org.onap.policy.apex.context.ContextException;
27 import org.onap.policy.apex.core.engine.executor.StateFinalizerExecutor;
28 import org.onap.policy.apex.core.engine.executor.context.StateFinalizerExecutionContext;
29 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
30 import org.slf4j.ext.XLogger;
31 import org.slf4j.ext.XLoggerFactory;
34 * The Class JavaStateFinalizerExecutor is the state finalizer executor for state finalizer logic written in Java.
36 * @author Liam Fallon (liam.fallon@ericsson.com)
38 public class JavaStateFinalizerExecutor extends StateFinalizerExecutor {
39 // Logger for this class
40 private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaStateFinalizerExecutor.class);
42 // The Java State Finalizer executor class
43 private Object stateFinalizerLogicObject = null;
46 * Prepares the state finalizer for processing.
48 * @throws StateMachineException thrown when a state machine execution error occurs
51 public void prepare() throws StateMachineException {
52 // Call generic prepare logic
55 // Get the class for state finalizer execution
57 // Create the state finalizer logic object from the byte code of the class
58 stateFinalizerLogicObject = Class.forName(getSubject().getLogic()).newInstance();
59 } catch (final Exception e) {
60 LOGGER.error("instantiation error on Java class \"" + getSubject().getLogic() + "\"", e);
61 throw new StateMachineException("instantiation error on Java class \"" + getSubject().getLogic() + "\"", e);
66 * Executes the executor for the state finalizer logic in a sequential manner.
68 * @param executionId the execution ID for the current APEX policy execution
69 * @param incomingFields the incoming fields for finalisation
70 * @return The state output for the state
71 * @throws StateMachineException on an execution error
72 * @throws ContextException on context errors
75 public String execute(final long executionId, final Map<String, Object> incomingFields)
76 throws StateMachineException, ContextException {
77 // Do execution pre work
78 executePre(executionId, incomingFields);
80 // Check and execute the Java logic
81 boolean returnValue = false;
83 // Find and call the method with the signature "public boolean getStateOutput(final
84 // StateFinalizerExecutionContext executor) throws ApexException"
86 // task logic in the Java class
87 final Method method = stateFinalizerLogicObject.getClass().getDeclaredMethod("getStateOutput", (Class[])
88 new Class[] { StateFinalizerExecutionContext.class });
89 returnValue = (boolean) method.invoke(stateFinalizerLogicObject, getExecutionContext());
90 } catch (final Exception e) {
91 LOGGER.error("execute: state finalizer logic failed to run for state finalizer \"" + getSubject().getId()
93 throw new StateMachineException(
94 "state finalizer logic failed to run for state finalizer \"" + getSubject().getId() + "\"", e);
97 // Do the execution post work
98 executePost(returnValue);
100 // Send back the return event
102 return getOutgoing();
109 * Cleans up the state finalizer after processing.
111 * @throws StateMachineException thrown when a state machine execution error occurs
114 public void cleanUp() throws StateMachineException {
115 LOGGER.debug("cleanUp:" + getSubject().getId() + "," + getSubject().getLogicFlavour() + ","
116 + getSubject().getLogic());