From 6973ec9109fd2651e2284663b6c8039bd830a677 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Sat, 22 Sep 2018 21:07:43 +0100 Subject: Fix sonar problems in Apex Fixed some easy to resolve Sonar issues. Issue-ID: POLICY-1034 Change-Id: Ia8e4606bd4307daca499b4a74c96135211e572fd Signed-off-by: liamfallon --- .../executor/jython/JythonTaskExecutor.java | 47 ++++++++++++-------- .../executor/jython/JythonTaskSelectExecutor.java | 50 ++++++++++++++-------- 2 files changed, 61 insertions(+), 36 deletions(-) (limited to 'plugins/plugins-executor/plugins-executor-jython/src/main') diff --git a/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/JythonTaskExecutor.java b/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/JythonTaskExecutor.java index 39ca0dc43..bddb63b42 100644 --- a/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/JythonTaskExecutor.java +++ b/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/JythonTaskExecutor.java @@ -100,23 +100,7 @@ public class JythonTaskExecutor extends TaskExecutor { // Set up the Jython engine interpreter.set("executor", getExecutionContext()); interpreter.exec(compiled); - try { - final Object ret = interpreter.get("returnValue", java.lang.Boolean.class); - if (ret == null) { - LOGGER.error("execute: task logic failed to set a return value for task \"" - + getSubject().getKey().getId() + "\""); - throw new StateMachineException("execute: task logic failed to set a return value for task \"" - + getSubject().getKey().getId() + "\""); - } - returnValue = (Boolean) ret; - } catch (NullPointerException | ClassCastException e) { - LOGGER.error("execute: task selection logic failed to set a correct return value for state \"" - + getSubject().getKey().getId() + "\"", e); - throw new StateMachineException( - "execute: task selection logic failed to set a return value for state \"" - + getSubject().getKey().getId() + "\"", - e); - } + returnValue = handleInterpreterResult(); } /* */ } catch (final Exception e) { @@ -136,6 +120,35 @@ public class JythonTaskExecutor extends TaskExecutor { } } + /** + * Handle the result returned by the interpreter. + * + * @return true if the result was successful + * @throws StateMachineException on interpreter failures + */ + private boolean handleInterpreterResult() throws StateMachineException { + boolean returnValue = false; + + try { + final Object ret = interpreter.get("returnValue", java.lang.Boolean.class); + if (ret == null) { + LOGGER.error("execute: task logic failed to set a return value for task \"" + + getSubject().getKey().getId() + "\""); + throw new StateMachineException("execute: task logic failed to set a return value for task \"" + + getSubject().getKey().getId() + "\""); + } + returnValue = (Boolean) ret; + } catch (NullPointerException | ClassCastException e) { + LOGGER.error("execute: task selection logic failed to set a correct return value for state \"" + + getSubject().getKey().getId() + "\"", e); + throw new StateMachineException( + "execute: task selection logic failed to set a return value for state \"" + + getSubject().getKey().getId() + "\"", + e); + } + return returnValue; + } + /** * Cleans up the task after processing. * diff --git a/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/JythonTaskSelectExecutor.java b/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/JythonTaskSelectExecutor.java index 3ff061fa4..9a2433122 100644 --- a/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/JythonTaskSelectExecutor.java +++ b/plugins/plugins-executor/plugins-executor-jython/src/main/java/org/onap/policy/apex/plugins/executor/jython/JythonTaskSelectExecutor.java @@ -103,25 +103,7 @@ public class JythonTaskSelectExecutor extends TaskSelectExecutor { // Set up the Jython engine interpreter.set("executor", getExecutionContext()); interpreter.exec(compiled); - - try { - final Object ret = interpreter.get("returnValue", java.lang.Boolean.class); - if (ret == null) { - LOGGER.error(TSL_FAILED_PREFIX - + getSubject().getKey().getId() + "\""); - throw new StateMachineException( - TSL_FAILED_PREFIX - + getSubject().getKey().getId() + "\""); - } - returnValue = (Boolean) ret; - } catch (NullPointerException | ClassCastException e) { - LOGGER.error("execute: task selection logic failed to set a correct return value for state \"" - + getSubject().getKey().getId() + "\"", e); - throw new StateMachineException( - TSL_FAILED_PREFIX - + getSubject().getKey().getId() + "\"", - e); - } + returnValue = handleInterpreterResult(); } /* */ } catch (final Exception e) { @@ -142,6 +124,36 @@ public class JythonTaskSelectExecutor extends TaskSelectExecutor { } } + /** + * Handle the result returned by the interpreter. + * + * @return true if the result was successful + * @throws StateMachineException on interpreter errors + */ + private boolean handleInterpreterResult() throws StateMachineException { + boolean returnValue = false; + + try { + final Object ret = interpreter.get("returnValue", java.lang.Boolean.class); + if (ret == null) { + LOGGER.error(TSL_FAILED_PREFIX + + getSubject().getKey().getId() + "\""); + throw new StateMachineException( + TSL_FAILED_PREFIX + + getSubject().getKey().getId() + "\""); + } + returnValue = (Boolean) ret; + } catch (NullPointerException | ClassCastException e) { + LOGGER.error("execute: task selection logic failed to set a correct return value for state \"" + + getSubject().getKey().getId() + "\"", e); + throw new StateMachineException( + TSL_FAILED_PREFIX + + getSubject().getKey().getId() + "\"", + e); + } + return returnValue; + } + /** * Cleans up the task after processing. * -- cgit