2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020 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.java;
24 import java.lang.reflect.Method;
26 import java.util.Properties;
28 import org.onap.policy.apex.context.ContextException;
29 import org.onap.policy.apex.core.engine.executor.TaskExecutor;
30 import org.onap.policy.apex.core.engine.executor.context.TaskExecutionContext;
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 JavaTaskExecutor is the task executor for task logic written in Java.
38 * @author Liam Fallon (liam.fallon@ericsson.com)
40 public class JavaTaskExecutor extends TaskExecutor {
41 // Logger for this class
42 private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaTaskExecutor.class);
44 // The Java Task executor class
45 private Object taskLogicObject = null;
48 * Prepares the task for processing.
50 * @throws StateMachineException thrown when a state machine execution error occurs
53 public void prepare() throws StateMachineException {
54 // Call generic prepare logic
57 // Get the class for task execution
59 // Create the task logic object from the byte code of the class
61 Class.forName(getSubject().getTaskLogic().getLogic()).getDeclaredConstructor().newInstance();
62 } catch (final Exception e) {
63 LOGGER.error("instantiation error on Java class \"" + getSubject().getTaskLogic().getLogic() + "\"", e);
64 throw new StateMachineException(
65 "instantiation error on Java class \"" + getSubject().getTaskLogic().getLogic() + "\"", e);
70 * Executes the executor for the task in a sequential manner.
72 * @param executionId the execution ID for the current APEX policy execution
73 * @param executionProperties properties 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 Properties executionProperties,
81 final Map<String, Object> incomingFields) throws StateMachineException, ContextException {
82 // Do execution pre work
83 executePre(executionId, executionProperties, incomingFields);
85 // Check and execute the Java logic
86 boolean returnValue = false;
88 // Find and call the method with the signature "public boolean getEvent(final TaskExecutionContext executor)
89 // throws ApexException" to invoke the
90 // task logic in the Java class
92 taskLogicObject.getClass().getDeclaredMethod("getEvent", new Class[] {TaskExecutionContext.class});
93 returnValue = (boolean) method.invoke(taskLogicObject, getExecutionContext());
94 } catch (final Exception e) {
95 LOGGER.error("execute: task logic failed to run for task \"" + getSubject().getKey().getId() + "\"");
96 throw new StateMachineException(
97 "task logic failed to run for task \"" + getSubject().getKey().getId() + "\"", e);
100 // Do the execution post work
101 executePost(returnValue);
103 // Send back the return event
104 return getOutgoing();
108 * Cleans up the task after processing.
110 * @throws StateMachineException thrown when a state machine execution error occurs
113 public void cleanUp() throws StateMachineException {
114 LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getTaskLogic().getLogicFlavour()
115 + "," + getSubject().getTaskLogic().getLogic());