re-use parser instance
authorSmokowski, Kevin (ks6305) <ks6305@us.att.com>
Mon, 5 Mar 2018 21:41:54 +0000 (16:41 -0500)
committerSmokowski, Kevin (ks6305) <ks6305@att.com>
Wed, 7 Mar 2018 20:16:13 +0000 (20:16 +0000)
add an additional logging statement and save memory

Change-Id: Idc8bdefb00ac61317cc38848dce670d76b6d89f7
Issue-ID: CCSDK-204
Signed-off-by: Smokowski, Kevin (ks6305) <ks6305@att.com>
sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicLoader.java
sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicParser.java

index 95f73f9..37d7faa 100644 (file)
@@ -39,15 +39,18 @@ public class SvcLogicLoader {
     private static final Logger LOGGER = LoggerFactory.getLogger(SvcLogicLoader.class);
     protected SvcLogicStore store;
     protected String directoryRoot;
+    protected SvcLogicParser parser;
 
     public SvcLogicLoader(String directoryRoot, SvcLogicStore store) {
         this.store = store;
         this.directoryRoot = directoryRoot;
+        this.parser = new SvcLogicParser();
     }
-
+    
     public SvcLogicLoader(String directoryRoot, String propFile) {
         this.store = SvcLogicParser.getStore(propFile);
         this.directoryRoot = directoryRoot;
+        this.parser = new SvcLogicParser();
     }
 
     public void loadAndActivate() throws IOException {
@@ -58,7 +61,7 @@ public class SvcLogicLoader {
         activateGraphs(activationEntries);
     }
 
-    private List<ActivationEntry> processActivationFiles(List<Path> activationPaths) {
+    protected List<ActivationEntry> processActivationFiles(List<Path> activationPaths) {
         List<ActivationEntry> activationEntries = new ArrayList<ActivationEntry>();
         for (Path activationFile : activationPaths) {
             activationEntries.addAll(getActivationEntries(activationFile));
@@ -66,10 +69,12 @@ public class SvcLogicLoader {
         return activationEntries;
     }
 
-    private void activateGraphs(List<ActivationEntry> activationEntries) {
+    protected void activateGraphs(List<ActivationEntry> activationEntries) {
         for (ActivationEntry entry : activationEntries) {
             try {
                 if (store.hasGraph(entry.module, entry.rpc, entry.version, entry.mode)) {
+                    LOGGER.info("Activating SvcLogicGraph [module=" + entry.module + ", rpc=" + entry.rpc + ", mode="
+                            + entry.mode + ", version=" + entry.version + "]");
                     store.activate(entry.module, entry.rpc, entry.version, entry.mode);
                 } else {
                     LOGGER.error("hasGraph returned false for " + entry.toString());
@@ -113,13 +118,12 @@ public class SvcLogicLoader {
         }
     }
 
-    private void saveGraph(String xmlFile) throws SvcLogicException {
+    protected void saveGraph(String xmlFile) throws SvcLogicException {
         File f = new File(xmlFile);
         if (!f.canRead()) {
             throw new ConfigurationException("Cannot read xml file (" + xmlFile + ")");
         }
 
-        SvcLogicParser parser = new SvcLogicParser();
         LinkedList<SvcLogicGraph> graphs = null;
 
         try {
index 51eb18e..3d9caff 100644 (file)
@@ -26,6 +26,7 @@ import java.io.IOException;
 import java.net.URL;
 import java.util.LinkedList;
 import javax.xml.XMLConstants;
+import javax.xml.parsers.ParserConfigurationException;
 import javax.xml.parsers.SAXParser;
 import javax.xml.parsers.SAXParserFactory;
 import javax.xml.validation.Schema;
@@ -59,6 +60,7 @@ public class SvcLogicParser {
     private static final Logger LOGGER = LoggerFactory.getLogger(SvcLogicParser.class);
     private static final String SLI_VALIDATING_PARSER = "org.onap.ccsdk.sli.parser.validate";
     private static final String SVCLOGIC_XSD = "/svclogic.xsd";
+    private SAXParser saxParser;
 
     private class SvcLogicHandler extends DefaultHandler {
         private Locator locator = null;
@@ -309,41 +311,12 @@ public class SvcLogicParser {
     public LinkedList<SvcLogicGraph> parse(String fileName) throws SvcLogicException {
         LinkedList<SvcLogicGraph> graphs;
 
-        URL xsdUrl = null;
-        Schema schema = null;
-        String validateSchema = System.getProperty(SLI_VALIDATING_PARSER, "true");
-
-        if ("true".equalsIgnoreCase(validateSchema)) {
-            xsdUrl = getClass().getResource(SVCLOGIC_XSD);
-        }
-
-        if (xsdUrl != null) {
-            try {
-                SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
-                schema = schemaFactory.newSchema(xsdUrl);
-                LOGGER.info("Schema path {}", xsdUrl.getPath());
-            } catch (Exception e) {
-                LOGGER.warn("Could not validate using schema {}", xsdUrl.getPath(), e);
-            }
-        } else {
-            LOGGER.warn("Could not find resource {}", SVCLOGIC_XSD);
-        }
-
         try {
-            SAXParserFactory factory = SAXParserFactory.newInstance();
-
-            if (schema != null) {
-                factory.setNamespaceAware(true);
-                factory.setSchema(schema);
-            }
-            SAXParser saxParser = factory.newSAXParser();
-
-            if (saxParser.isValidating()) {
-                LOGGER.info("Parser configured to validate XML {}", (xsdUrl != null ? xsdUrl.getPath() : null));
+            if (saxParser == null) {
+                saxParser = initParser();
             }
 
             graphs = new LinkedList<>();
-
             saxParser.parse(fileName, new SvcLogicHandler(graphs));
 
             try {
@@ -353,7 +326,6 @@ public class SvcLogicParser {
             } catch (Exception exc) {
                 LOGGER.error("Couldn't set md5sum on graphs", exc);
             }
-
         } catch (Exception e) {
             LOGGER.error("Parsing failed ", e);
             String msg = e.getMessage();
@@ -363,7 +335,6 @@ public class SvcLogicParser {
                 throw new SvcLogicException("Compiler error: " + fileName, e);
             }
         }
-
         return graphs;
     }
 
@@ -466,6 +437,7 @@ public class SvcLogicParser {
 
     }
 
+    
     public static void load(String xmlfile, SvcLogicStore store) throws SvcLogicException {
         File xmlFile = new File(xmlfile);
         if (!xmlFile.canRead()) {
@@ -588,5 +560,41 @@ public class SvcLogicParser {
 
         System.exit(1);
     }
+    
+    protected SAXParser initParser() throws ParserConfigurationException, SAXException {
+        URL xsdUrl = null;
+        Schema schema = null;
+        String validateSchema = System.getProperty(SLI_VALIDATING_PARSER, "true");
+
+        if ("true".equalsIgnoreCase(validateSchema)) {
+            xsdUrl = getClass().getResource(SVCLOGIC_XSD);
+        }
+
+        if (xsdUrl != null) {
+            try {
+                SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
+                schema = schemaFactory.newSchema(xsdUrl);
+                LOGGER.info("Schema path {}", xsdUrl.getPath());
+            } catch (Exception e) {
+                LOGGER.warn("Could not validate using schema {}", xsdUrl.getPath(), e);
+            }
+        } else {
+            LOGGER.warn("Could not find resource {}", SVCLOGIC_XSD);
+        }
+
+        SAXParserFactory factory = SAXParserFactory.newInstance();
+
+        if (schema != null) {
+            factory.setNamespaceAware(true);
+            factory.setSchema(schema);
+        }
+
+        SAXParser saxParser = factory.newSAXParser();
+        if (saxParser.isValidating()) {
+            LOGGER.info("Parser configured to validate XML {}", (xsdUrl != null ? xsdUrl.getPath() : null));
+        }
+        return saxParser;
+    }
+
 
 }