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;
25 import org.onap.policy.apex.context.ContextException;
26 import org.onap.policy.apex.core.engine.event.EnEvent;
27 import org.onap.policy.apex.core.engine.executor.TaskSelectExecutor;
28 import org.onap.policy.apex.core.engine.executor.context.TaskSelectionExecutionContext;
29 import org.onap.policy.apex.core.engine.executor.exception.StateMachineException;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
31 import org.slf4j.ext.XLogger;
32 import org.slf4j.ext.XLoggerFactory;
35 * The Class JavaTaskSelectExecutor is the task selection executor for task selection logic written in Java.
37 * @author Liam Fallon (liam.fallon@ericsson.com)
39 public class JavaTaskSelectExecutor extends TaskSelectExecutor {
40 // Logger for this class
41 private static final XLogger LOGGER = XLoggerFactory.getXLogger(JavaTaskSelectExecutor.class);
43 // The Java Task Selection executor class
44 private Object taskSelectionLogicObject = 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 selection
58 // Create the task logic object from the byte code of the class
59 taskSelectionLogicObject = Class.forName(getSubject().getTaskSelectionLogic().getLogic()).newInstance();
60 } catch (final Exception e) {
61 LOGGER.error("instantiation error on Java class \"" + getSubject().getTaskSelectionLogic().getLogic()
63 throw new StateMachineException("instantiation error on Java class \""
64 + getSubject().getTaskSelectionLogic().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 incomingEvent the incoming event
73 * @return The outgoing event
74 * @throws StateMachineException on an execution error
75 * @throws ContextException on context errors
78 public AxArtifactKey execute(final long executionId, final EnEvent incomingEvent)
79 throws StateMachineException, ContextException {
80 // Do execution pre work
81 executePre(executionId, incomingEvent);
83 // Check and execute the Java logic
84 boolean returnValue = false;
86 // Find and call the method with the signature "public boolean getTask(final TaskSelectionExecutionContext
87 // executor)" to invoke the task selection
88 // logic in the Java class
89 final Method method = taskSelectionLogicObject.getClass().getDeclaredMethod("getTask",
90 (Class[]) new Class[] { TaskSelectionExecutionContext.class });
91 returnValue = (boolean) method.invoke(taskSelectionLogicObject, getExecutionContext());
92 } catch (final Exception e) {
93 LOGGER.error("execute: task selection logic failed to run for state \"" + getSubject().getKey().getId()
95 throw new StateMachineException(
96 "task selection logic failed to run for state \"" + getSubject().getKey().getId() + "\"",
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() + ","
115 + getSubject().getTaskSelectionLogic().getLogicFlavour() + ","
116 + getSubject().getTaskSelectionLogic().getLogic());