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;
27 import org.onap.policy.apex.context.ContextException;
28 import org.onap.policy.apex.core.engine.executor.TaskExecutor;
29 import org.onap.policy.apex.core.engine.executor.context.TaskExecutionContext;
30 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
35 * The Class JavaTaskExecutor is the task executor for task logic written in Java.
37 * @author Liam Fallon (liam.fallon@ericsson.com)
39 public class JavaTaskExecutor extends TaskExecutor {
40 // Logger for this class
41 private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaTaskExecutor.class);
43 // The Java Task executor class
44 private Object taskLogicObject = null;
47 * Prepares the task for processing.
49 * @throws StateMachineException thrown when a state machine execution error occurs
52 public void prepare() throws StateMachineException {
53 // Call generic prepare logic
56 // Get the class for task execution
58 // Create the task logic object from the byte code of the class
60 Class.forName(getSubject().getTaskLogic().getLogic()).getDeclaredConstructor().newInstance();
61 } catch (final Exception e) {
62 LOGGER.error("instantiation error on Java class \"" + getSubject().getTaskLogic().getLogic() + "\"", e);
63 throw new StateMachineException(
64 "instantiation error on Java class \"" + getSubject().getTaskLogic().getLogic() + "\"", e);
69 * Executes the executor for the task in a sequential manner.
71 * @param executionId the execution ID for the current APEX policy execution
72 * @param executionProperties properties for the current APEX policy execution
73 * @param incomingFields the incoming fields
74 * @return The outgoing fields
75 * @throws StateMachineException on an execution error
76 * @throws ContextException on context errors
79 public Map<String, Object> execute(final long executionId, final Properties executionProperties,
80 final Map<String, Object> incomingFields) throws StateMachineException, ContextException {
81 // Do execution pre work
82 executePre(executionId, executionProperties, incomingFields);
84 // Check and execute the Java logic
85 boolean returnValue = false;
87 // Find and call the method with the signature "public boolean getEvent(final TaskExecutionContext executor)
88 // throws ApexException" to invoke the
89 // task logic in the Java class
91 taskLogicObject.getClass().getDeclaredMethod("getEvent", new Class[] {TaskExecutionContext.class});
92 returnValue = (boolean) method.invoke(taskLogicObject, getExecutionContext());
93 } catch (final Exception e) {
94 LOGGER.error("execute: task logic failed to run for task \"" + getSubject().getKey().getId() + "\"");
95 throw new StateMachineException(
96 "task logic failed to run for task \"" + getSubject().getKey().getId() + "\"", e);
99 // Do the execution post work
100 executePost(returnValue);
102 // Send back the return event
103 return getOutgoing();
107 * Cleans up the task after processing.
109 * @throws StateMachineException thrown when a state machine execution error occurs
112 public void cleanUp() throws StateMachineException {
113 LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getTaskLogic().getLogicFlavour()
114 + "," + getSubject().getTaskLogic().getLogic());