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.TaskExecutor;
28 import org.onap.policy.apex.core.engine.executor.context.TaskExecutionContext;
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 JavaTaskExecutor is the task executor for task logic written in Java.
36 * @author Liam Fallon (liam.fallon@ericsson.com)
38 public class JavaTaskExecutor extends TaskExecutor {
39 // Logger for this class
40 private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaTaskExecutor.class);
42 // The Java Task executor class
43 private Object taskLogicObject = null;
46 * Prepares the task 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 task execution
57 // Create the task logic object from the byte code of the class
58 taskLogicObject = Class.forName(getSubject().getTaskLogic().getLogic()).newInstance();
59 } catch (final Exception e) {
60 LOGGER.error("instantiation error on Java class \"" + getSubject().getTaskLogic().getLogic() + "\"", e);
61 throw new StateMachineException(
62 "instantiation error on Java class \"" + getSubject().getTaskLogic().getLogic() + "\"", e);
67 * Executes the executor for the task in a sequential manner.
69 * @param executionId the execution ID for the current APEX policy execution
70 * @param incomingFields the incoming fields
71 * @return The outgoing fields
72 * @throws StateMachineException on an execution error
73 * @throws ContextException on context errors
76 public Map<String, Object> execute(final long executionId, final Map<String, Object> incomingFields)
77 throws StateMachineException, ContextException {
78 // Do execution pre work
79 executePre(executionId, incomingFields);
81 // Check and execute the Java logic
82 boolean returnValue = false;
84 // Find and call the method with the signature "public boolean getEvent(final TaskExecutionContext executor)
85 // throws ApexException" to invoke the
86 // task logic in the Java class
87 final Method method = taskLogicObject.getClass().getDeclaredMethod("getEvent", (Class[])
88 new Class[] { TaskExecutionContext.class });
89 returnValue = (boolean) method.invoke(taskLogicObject, getExecutionContext());
90 } catch (final Exception e) {
91 LOGGER.error("execute: task logic failed to run for task \"" + getSubject().getKey().getId() + "\"");
92 throw new StateMachineException(
93 "task logic failed to run for task \"" + getSubject().getKey().getId() + "\"", e);
96 // Do the execution post work
97 executePost(returnValue);
99 // Send back the return event
100 return getOutgoing();
104 * Cleans up the task after processing.
106 * @throws StateMachineException thrown when a state machine execution error occurs
109 public void cleanUp() throws StateMachineException {
110 LOGGER.debug("cleanUp:" + getSubject().getKey().getId() + "," + getSubject().getTaskLogic().getLogicFlavour()
111 + "," + getSubject().getTaskLogic().getLogic());