Changed try to try with resource 61/15061/2
authorshashikanth <shashikanth.vh@huawei.com>
Mon, 25 Sep 2017 12:45:57 +0000 (18:15 +0530)
committerShashikanth VH <shashikanth.vh@huawei.com>
Mon, 25 Sep 2017 12:49:15 +0000 (12:49 +0000)
Change this condition so that it does not always evaluate to "false"
https://sonar.onap.org/component_issues?id=org.openecomp.so%3Aso#resolved=false|severities=BLOCKER

Issue-Id:SO-118
Change-Id: Ib5331efaeddf0b15e8d55a6b6619d319f8194707
Signed-off-by: shashikanth.vh <shashikanth.vh@huawei.com>
bpmn/MSOCoreBPMN/src/main/java/org/openecomp/mso/bpmn/core/XQueryScriptTask.java

index 4b34ddf..6080768 100644 (file)
-/*-\r
- * ============LICENSE_START=======================================================\r
- * ONAP - SO\r
- * ================================================================================\r
- * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.\r
- * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.\r
- * ================================================================================\r
- * Licensed under the Apache License, Version 2.0 (the "License");\r
- * you may not use this file except in compliance with the License.\r
- * You may obtain a copy of the License at\r
- * \r
- *      http://www.apache.org/licenses/LICENSE-2.0\r
- * \r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- * ============LICENSE_END=========================================================\r
- */\r
-\r
-package org.openecomp.mso.bpmn.core;\r
-\r
-import java.io.ByteArrayInputStream;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.math.BigDecimal;\r
-import java.net.URI;\r
-import java.util.Iterator;\r
-\r
-import javax.xml.transform.stream.StreamSource;\r
-\r
-import org.camunda.bpm.engine.ProcessEngineException;\r
-import org.camunda.bpm.engine.delegate.DelegateExecution;\r
-//import java.util.logging.Logger;\r
-import org.camunda.bpm.engine.delegate.Expression;\r
-\r
-import org.openecomp.mso.logger.MessageEnum;\r
-import org.openecomp.mso.logger.MsoLogger;\r
-\r
-import net.sf.saxon.Configuration;\r
-import net.sf.saxon.s9api.DocumentBuilder;\r
-import net.sf.saxon.s9api.Processor;\r
-import net.sf.saxon.s9api.QName;\r
-import net.sf.saxon.s9api.XQueryCompiler;\r
-import net.sf.saxon.s9api.XQueryEvaluator;\r
-import net.sf.saxon.s9api.XQueryExecutable;\r
-import net.sf.saxon.s9api.XdmAtomicValue;\r
-import net.sf.saxon.s9api.XdmItem;\r
-import net.sf.saxon.s9api.XdmNode;\r
-\r
-/**\r
- * Executes an XQuery script.\r
- * <p>\r
- * Required fields:<br/><br/>\r
- * &nbsp;&nbsp;&nbsp;&nbsp;scriptFile: the XQuery script file path<br/>\r
- * &nbsp;&nbsp;&nbsp;&nbsp;outputVariable: the output variable name<br/>\r
- * <p>\r
- * Optional fields:<br/><br/>\r
- * &nbsp;&nbsp;&nbsp;&nbsp;xmlInputVariables: CSV list of variables containing\r
- *             XML data to be injected into the script<br/>\r
- * &nbsp;&nbsp;&nbsp;&nbsp;atomicInputVariables: CSV list of variables containing\r
- *             atomic data to be injected into the script<br/>\r
- */\r
-public class XQueryScriptTask extends BaseTask {\r
-       \r
-       private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);\r
-\r
-       private Expression scriptFile;\r
-       private Expression xmlInputVariables;\r
-       private Expression atomicInputVariables;\r
-       private Expression outputVariable;\r
-\r
-       public void execute(DelegateExecution execution) throws Exception {\r
-               if (msoLogger.isDebugEnabled()) {\r
-                       msoLogger.debug("Started Executing " + getTaskName());\r
-               }\r
-\r
-               String theScriptFile =\r
-                       getStringField(scriptFile, execution, "scriptFile");\r
-               String theXmlInputVariables =\r
-                       getOptionalStringField(xmlInputVariables, execution, "xmlInputVariables");\r
-               String theAtomicInputVariables =\r
-                       getOptionalStringField(atomicInputVariables, execution, "atomicInputVariables");\r
-               String theOutputVariable =\r
-                       getStringField(outputVariable, execution, "outputVariable");\r
-\r
-               if (msoLogger.isDebugEnabled()) {\r
-                   msoLogger.debug ("scriptFile = " + theScriptFile\r
-                               + " xmlInputVariables = " + theXmlInputVariables\r
-                               + " atomicInputVariables = " + theAtomicInputVariables\r
-                               + "outputVariable = " + theOutputVariable);\r
-               }\r
-\r
-               String[] xmlInputVariableArray = (theXmlInputVariables == null)\r
-                       ? new String[0] : theXmlInputVariables.split(",[ ]*");\r
-\r
-               String[] atomicInputVariableArray = (theAtomicInputVariables == null)\r
-                       ? new String[0] : theAtomicInputVariables.split(",[ ]*");\r
-\r
-               Boolean shouldFail = (Boolean) execution.getVariable("shouldFail");\r
-\r
-               if (shouldFail != null && shouldFail) {\r
-                       throw new ProcessEngineException(getClass().getSimpleName() + " Failed");\r
-               }\r
-\r
-               // The script could be compiled once and reused, but we are reading it\r
-               // and compiling it every time.\r
-               Configuration configuration = new Configuration();\r
-               Processor processor = new Processor(configuration);\r
-               XQueryCompiler compiler = processor.newXQueryCompiler();\r
-               XQueryExecutable executable = compile(compiler, theScriptFile);\r
-\r
-               // The evaluator must not be shared by multiple threads.  Here is where\r
-               // the initial context may be set, as well as values of external variables.\r
-               XQueryEvaluator evaluator = executable.load();\r
-\r
-               // Convert XML string variable content to document-node objects and inject\r
-               // these into the evaluator.  Note: the script must accept the document-node\r
-               // type.  Most MSO scripts today expect element() input, not document-node\r
-               // input.  TODO: figure out how to pass the variable data as element() types.\r
-\r
-               for (String xmlInputVariable : xmlInputVariableArray) {\r
-                       if (msoLogger.isDebugEnabled()) {\r
-                               msoLogger.debug("Injecting XML variable '" + xmlInputVariable + "'");\r
-                               msoLogger.debug("printing the variable content>>'" + execution.getVariable(xmlInputVariable) +"'");\r
-                       }\r
-\r
-                       String xml = (String) execution.getVariable(xmlInputVariable);\r
-                       DocumentBuilder documentBuilder = processor.newDocumentBuilder();\r
-                       StreamSource source = new StreamSource(new ByteArrayInputStream(xml.getBytes("UTF-8")));\r
-                       XdmNode xdmNode = documentBuilder.build(source);\r
-\r
-                       // Inject the document-node object into the XQueryEvaluator.\r
-                       // TODO: transform it to an element()\r
-                       QName variable = new QName(xmlInputVariable);\r
-                       evaluator.setExternalVariable(variable, xdmNode);\r
-               }\r
-\r
-               // Inject atomic variables into the evaluator.\r
-\r
-               for (String atomicInputVariable : atomicInputVariableArray) {\r
-                       \r
-                       if (msoLogger.isDebugEnabled()) {\r
-                           msoLogger.debug ("Injecting object variable '"\r
-                                       + atomicInputVariable + "'");\r
-                       }\r
-\r
-                       QName variable = new QName(atomicInputVariable);\r
-                       Object value = execution.getVariable(atomicInputVariable);\r
-\r
-                       if (value == null) {\r
-                               // The variable value is null, so we have no way to know what\r
-                               // type it is.  I don't know how to deal with this, so for\r
-                               // now, just skip it.\r
-                               \r
-                               msoLogger.warn (MessageEnum.BPMN_VARIABLE_NULL, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.DataError, atomicInputVariable);\r
-                               \r
-                               continue;\r
-                       }\r
-\r
-                       // There might be a better way to do this...\r
-                       if (value instanceof BigDecimal) {\r
-                               evaluator.setExternalVariable(variable,\r
-                                       new XdmAtomicValue((BigDecimal) value));\r
-                       } else if (value instanceof Boolean) {\r
-                               evaluator.setExternalVariable(variable,\r
-                                       new XdmAtomicValue((Boolean) value));\r
-                       } else if (value instanceof Double) {\r
-                               evaluator.setExternalVariable(variable,\r
-                                       new XdmAtomicValue((Double) value));\r
-                       } else if (value instanceof Float) {\r
-                               evaluator.setExternalVariable(variable,\r
-                                       new XdmAtomicValue((Float) value));\r
-                       } else if (value instanceof Long) {\r
-                               evaluator.setExternalVariable(variable,\r
-                                       new XdmAtomicValue((Long) value));\r
-                       } else if (value instanceof String) {\r
-                               evaluator.setExternalVariable(variable,\r
-                                       new XdmAtomicValue((String) value));\r
-                       } else if (value instanceof URI) {\r
-                               evaluator.setExternalVariable(variable,\r
-                                       new XdmAtomicValue((URI) value));\r
-                       } else {\r
-                               throw new BadInjectedFieldException(\r
-                                       "atomicInputVariables", getTaskName(),\r
-                                       "'" + atomicInputVariable + "' type is not supported: "\r
-                                               + value.getClass());\r
-                       }\r
-               }\r
-\r
-               // Evaluate the query and collect the output.\r
-               StringBuilder output = new StringBuilder();\r
-               Iterator<XdmItem> xdmItems = evaluator.iterator();\r
-               while (xdmItems.hasNext()) {\r
-                       XdmItem item = xdmItems.next();\r
-                       \r
-                       if (msoLogger.isDebugEnabled()) {\r
-                               msoLogger.debug("XQuery result item = " + item);\r
-                       }\r
-\r
-                       output.append(item.toString());\r
-               }\r
-\r
-               // Set the output variable.\r
-               execution.setVariable(theOutputVariable, output.toString());\r
-\r
-               if (msoLogger.isDebugEnabled()) {\r
-                       msoLogger.debug("Done Executing " + getTaskName());\r
-               }\r
-       }\r
-       \r
-       /**\r
-        * Compiles an XQuery script contained in a resource (file).\r
-        * @param compiler the XQueryCompiler\r
-        * @param resource the resource path\r
-        * @return an XQueryExecutable\r
-        * @throws Exception on error\r
-        */\r
-       private XQueryExecutable compile(XQueryCompiler compiler, String resource)\r
-                       throws Exception {\r
-               InputStream xqStream = null;\r
-               try {\r
-                       xqStream = getClass().getResourceAsStream(resource);\r
-\r
-                       if (xqStream == null) {\r
-                               throw new IOException("Resource not found: " + resource);\r
-                       }\r
-\r
-                       XQueryExecutable executable = compiler.compile(xqStream);\r
-                       xqStream.close();\r
-                       xqStream = null;\r
-                       return executable;\r
-               } finally {\r
-                       if (xqStream != null) {\r
-                               try {\r
-                                       xqStream.close();\r
-                               } catch (Exception e) {\r
-                                   msoLogger.debug ("Exception:", e);\r
-                               }\r
-                       }\r
-               }\r
-       }\r
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP - SO
+ * ================================================================================
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2017 Huawei Technologies Co., Ltd. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.openecomp.mso.bpmn.core;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.math.BigDecimal;
+import java.net.URI;
+import java.util.Iterator;
+
+import javax.xml.transform.stream.StreamSource;
+
+import org.camunda.bpm.engine.ProcessEngineException;
+import org.camunda.bpm.engine.delegate.DelegateExecution;
+//import java.util.logging.Logger;
+import org.camunda.bpm.engine.delegate.Expression;
+
+import org.openecomp.mso.logger.MessageEnum;
+import org.openecomp.mso.logger.MsoLogger;
+
+import net.sf.saxon.Configuration;
+import net.sf.saxon.s9api.DocumentBuilder;
+import net.sf.saxon.s9api.Processor;
+import net.sf.saxon.s9api.QName;
+import net.sf.saxon.s9api.XQueryCompiler;
+import net.sf.saxon.s9api.XQueryEvaluator;
+import net.sf.saxon.s9api.XQueryExecutable;
+import net.sf.saxon.s9api.XdmAtomicValue;
+import net.sf.saxon.s9api.XdmItem;
+import net.sf.saxon.s9api.XdmNode;
+
+/**
+ * Executes an XQuery script.
+ * <p>
+ * Required fields:<br/><br/>
+ * &nbsp;&nbsp;&nbsp;&nbsp;scriptFile: the XQuery script file path<br/>
+ * &nbsp;&nbsp;&nbsp;&nbsp;outputVariable: the output variable name<br/>
+ * <p>
+ * Optional fields:<br/><br/>
+ * &nbsp;&nbsp;&nbsp;&nbsp;xmlInputVariables: CSV list of variables containing
+ *             XML data to be injected into the script<br/>
+ * &nbsp;&nbsp;&nbsp;&nbsp;atomicInputVariables: CSV list of variables containing
+ *             atomic data to be injected into the script<br/>
+ */
+public class XQueryScriptTask extends BaseTask {
+       
+       private static MsoLogger msoLogger = MsoLogger.getMsoLogger(MsoLogger.Catalog.BPEL);
+
+       private Expression scriptFile;
+       private Expression xmlInputVariables;
+       private Expression atomicInputVariables;
+       private Expression outputVariable;
+
+       public void execute(DelegateExecution execution) throws Exception {
+               if (msoLogger.isDebugEnabled()) {
+                       msoLogger.debug("Started Executing " + getTaskName());
+               }
+
+               String theScriptFile =
+                       getStringField(scriptFile, execution, "scriptFile");
+               String theXmlInputVariables =
+                       getOptionalStringField(xmlInputVariables, execution, "xmlInputVariables");
+               String theAtomicInputVariables =
+                       getOptionalStringField(atomicInputVariables, execution, "atomicInputVariables");
+               String theOutputVariable =
+                       getStringField(outputVariable, execution, "outputVariable");
+
+               if (msoLogger.isDebugEnabled()) {
+                   msoLogger.debug ("scriptFile = " + theScriptFile
+                               + " xmlInputVariables = " + theXmlInputVariables
+                               + " atomicInputVariables = " + theAtomicInputVariables
+                               + "outputVariable = " + theOutputVariable);
+               }
+
+               String[] xmlInputVariableArray = (theXmlInputVariables == null)
+                       ? new String[0] : theXmlInputVariables.split(",[ ]*");
+
+               String[] atomicInputVariableArray = (theAtomicInputVariables == null)
+                       ? new String[0] : theAtomicInputVariables.split(",[ ]*");
+
+               Boolean shouldFail = (Boolean) execution.getVariable("shouldFail");
+
+               if (shouldFail != null && shouldFail) {
+                       throw new ProcessEngineException(getClass().getSimpleName() + " Failed");
+               }
+
+               // The script could be compiled once and reused, but we are reading it
+               // and compiling it every time.
+               Configuration configuration = new Configuration();
+               Processor processor = new Processor(configuration);
+               XQueryCompiler compiler = processor.newXQueryCompiler();
+               XQueryExecutable executable = compile(compiler, theScriptFile);
+                if (executable == null) {
+                    throw new ProcessEngineException(getClass().getSimpleName() + " Failed");
+                }
+
+               // The evaluator must not be shared by multiple threads.  Here is where
+               // the initial context may be set, as well as values of external variables.
+               XQueryEvaluator evaluator = executable.load();
+
+               // Convert XML string variable content to document-node objects and inject
+               // these into the evaluator.  Note: the script must accept the document-node
+               // type.  Most MSO scripts today expect element() input, not document-node
+               // input.  TODO: figure out how to pass the variable data as element() types.
+
+               for (String xmlInputVariable : xmlInputVariableArray) {
+                       if (msoLogger.isDebugEnabled()) {
+                               msoLogger.debug("Injecting XML variable '" + xmlInputVariable + "'");
+                               msoLogger.debug("printing the variable content>>'" + execution.getVariable(xmlInputVariable) +"'");
+                       }
+
+                       String xml = (String) execution.getVariable(xmlInputVariable);
+                       DocumentBuilder documentBuilder = processor.newDocumentBuilder();
+                       StreamSource source = new StreamSource(new ByteArrayInputStream(xml.getBytes("UTF-8")));
+                       XdmNode xdmNode = documentBuilder.build(source);
+
+                       // Inject the document-node object into the XQueryEvaluator.
+                       // TODO: transform it to an element()
+                       QName variable = new QName(xmlInputVariable);
+                       evaluator.setExternalVariable(variable, xdmNode);
+               }
+
+               // Inject atomic variables into the evaluator.
+
+               for (String atomicInputVariable : atomicInputVariableArray) {
+                       
+                       if (msoLogger.isDebugEnabled()) {
+                           msoLogger.debug ("Injecting object variable '"
+                                       + atomicInputVariable + "'");
+                       }
+
+                       QName variable = new QName(atomicInputVariable);
+                       Object value = execution.getVariable(atomicInputVariable);
+
+                       if (value == null) {
+                               // The variable value is null, so we have no way to know what
+                               // type it is.  I don't know how to deal with this, so for
+                               // now, just skip it.
+                               
+                               msoLogger.warn (MessageEnum.BPMN_VARIABLE_NULL, "BPMN", MsoLogger.getServiceName(), MsoLogger.ErrorCode.DataError, atomicInputVariable);
+                               
+                               continue;
+                       }
+
+                       // There might be a better way to do this...
+                       if (value instanceof BigDecimal) {
+                               evaluator.setExternalVariable(variable,
+                                       new XdmAtomicValue((BigDecimal) value));
+                       } else if (value instanceof Boolean) {
+                               evaluator.setExternalVariable(variable,
+                                       new XdmAtomicValue((Boolean) value));
+                       } else if (value instanceof Double) {
+                               evaluator.setExternalVariable(variable,
+                                       new XdmAtomicValue((Double) value));
+                       } else if (value instanceof Float) {
+                               evaluator.setExternalVariable(variable,
+                                       new XdmAtomicValue((Float) value));
+                       } else if (value instanceof Long) {
+                               evaluator.setExternalVariable(variable,
+                                       new XdmAtomicValue((Long) value));
+                       } else if (value instanceof String) {
+                               evaluator.setExternalVariable(variable,
+                                       new XdmAtomicValue((String) value));
+                       } else if (value instanceof URI) {
+                               evaluator.setExternalVariable(variable,
+                                       new XdmAtomicValue((URI) value));
+                       } else {
+                               throw new BadInjectedFieldException(
+                                       "atomicInputVariables", getTaskName(),
+                                       "'" + atomicInputVariable + "' type is not supported: "
+                                               + value.getClass());
+                       }
+               }
+
+               // Evaluate the query and collect the output.
+               StringBuilder output = new StringBuilder();
+               Iterator<XdmItem> xdmItems = evaluator.iterator();
+               while (xdmItems.hasNext()) {
+                       XdmItem item = xdmItems.next();
+                       
+                       if (msoLogger.isDebugEnabled()) {
+                               msoLogger.debug("XQuery result item = " + item);
+                       }
+
+                       output.append(item.toString());
+               }
+
+               // Set the output variable.
+               execution.setVariable(theOutputVariable, output.toString());
+
+               if (msoLogger.isDebugEnabled()) {
+                       msoLogger.debug("Done Executing " + getTaskName());
+               }
+       }
+       
+       /**
+        * Compiles an XQuery script contained in a resource (file).
+        * @param compiler the XQueryCompiler
+        * @param resource the resource path
+        * @return an XQueryExecutable
+        * @throws Exception on error
+        */
+       private XQueryExecutable compile(XQueryCompiler compiler, String resource)
+                       throws Exception {
+           try (InputStream xqStream = getClass().getResourceAsStream(resource)) {
+               XQueryExecutable executable = compiler.compile(xqStream);
+               return executable;
+           } catch (Exception e) {
+               msoLogger.debug ("Exception at resourceFile stream:", e);
+               return null;
+           }
+       }
 }
\ No newline at end of file