From d1fcd126f971380f3e6a2401902ca4a261147324 Mon Sep 17 00:00:00 2001 From: "Smokowski, Kevin (ks6305)" Date: Mon, 5 Mar 2018 16:41:54 -0500 Subject: re-use parser instance add an additional logging statement and save memory Change-Id: Idc8bdefb00ac61317cc38848dce670d76b6d89f7 Issue-ID: CCSDK-204 Signed-off-by: Smokowski, Kevin (ks6305) --- .../onap/ccsdk/sli/core/sli/SvcLogicLoader.java | 14 ++-- .../onap/ccsdk/sli/core/sli/SvcLogicParser.java | 74 ++++++++++++---------- 2 files changed, 50 insertions(+), 38 deletions(-) diff --git a/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicLoader.java b/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicLoader.java index 95f73f96..37d7faae 100644 --- a/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicLoader.java +++ b/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicLoader.java @@ -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 processActivationFiles(List activationPaths) { + protected List processActivationFiles(List activationPaths) { List activationEntries = new ArrayList(); for (Path activationFile : activationPaths) { activationEntries.addAll(getActivationEntries(activationFile)); @@ -66,10 +69,12 @@ public class SvcLogicLoader { return activationEntries; } - private void activateGraphs(List activationEntries) { + protected void activateGraphs(List 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 graphs = null; try { diff --git a/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicParser.java b/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicParser.java index 51eb18e4..3d9caffb 100644 --- a/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicParser.java +++ b/sli/common/src/main/java/org/onap/ccsdk/sli/core/sli/SvcLogicParser.java @@ -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 parse(String fileName) throws SvcLogicException { LinkedList 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; + } + } -- cgit 1.2.3-korg