diff options
Diffstat (limited to 'spss.slinterface/WEB-INF/src/at/gv/egovernment/moa/spss/slinterface/listeners')
-rw-r--r-- | spss.slinterface/WEB-INF/src/at/gv/egovernment/moa/spss/slinterface/listeners/ContextListener.java | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/spss.slinterface/WEB-INF/src/at/gv/egovernment/moa/spss/slinterface/listeners/ContextListener.java b/spss.slinterface/WEB-INF/src/at/gv/egovernment/moa/spss/slinterface/listeners/ContextListener.java new file mode 100644 index 000000000..5b32a482b --- /dev/null +++ b/spss.slinterface/WEB-INF/src/at/gv/egovernment/moa/spss/slinterface/listeners/ContextListener.java @@ -0,0 +1,153 @@ +/* + * Created on 18.11.2003 + * + * (c) Stabsstelle IKT-Strategie des Bundes + */ +package at.gv.egovernment.moa.spss.slinterface.listeners; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Properties; + +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; + +import org.apache.log4j.Logger; +import org.apache.xerces.parsers.DOMParser; +import org.apache.xerces.parsers.XMLGrammarPreparser; +import org.apache.xerces.util.SymbolTable; +import org.apache.xerces.util.XMLGrammarPoolImpl; +import org.apache.xerces.xni.grammars.XMLGrammarDescription; +import org.apache.xerces.xni.grammars.XMLGrammarPool; +import org.apache.xerces.xni.parser.XMLInputSource; +import org.xml.sax.SAXException; + +import at.gv.egovernment.moa.spss.slinterface.Constants; + +/** + * @author Gregor Karlinger (mailto:gregor.karlinger@cio.gv.at) + */ +public class ContextListener implements ServletContextListener +{ + private static final String SAX_NAMESPACES_FEATURE = "http://xml.org/sax/features/namespaces"; + private static final String SAX_VALIDATION_FEATURE = "http://xml.org/sax/features/validation"; + + private static final String XERCES_SCHEMA_VALIDATION_FEATURE = + "http://apache.org/xml/features/validation/schema"; + private static final String XERCES_NORMALIZED_VALUE_FEATURE = + "http://apache.org/xml/features/validation/schema/normalized-value"; + private static final String XERCES_INCLUDE_IGNORABLE_WHITESPACE_FEATURE = + "http://apache.org/xml/features/dom/include-ignorable-whitespace"; + private static final String XERCES_CREATE_ENTITY_REF_NODES_FEATURE = + "http://apache.org/xml/features/dom/create-entity-ref-nodes"; + + private static final int BIG_PRIME = 2039; + + private static Logger logger_ = Logger.getLogger(Constants.LH_LISTENERS_); + + /** + * Initializes the web application. + * + * @see javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent) + */ + public void contextInitialized(ServletContextEvent event) + { + logger_.debug("Context is being initialized."); + + ServletContext context = event.getServletContext(); + + String initPropsLoc = System.getProperty(Constants.SP_INIT_PROPS_LOC_); + if (initPropsLoc == null) + { + logger_.error("System property \"" + Constants.SP_INIT_PROPS_LOC_ + "\" not set."); + return; + } + + // Load init properties + try + { + InputStream initPropsIS = context.getResourceAsStream(initPropsLoc); + Properties initProps = new Properties(); + initProps.load(initPropsIS); + context.setAttribute(Constants.WSCP_INIT_PROPS_, initProps); + } + catch (IOException e) + { + logger_.error("Cannot load initialization properties from location \"" + initPropsLoc + "\".", e); + } + + // Initialize XML parser + SymbolTable symbolTable = new SymbolTable(BIG_PRIME); + XMLGrammarPool grammarPool = new XMLGrammarPoolImpl(); + + XMLGrammarPreparser preparser = new XMLGrammarPreparser(symbolTable); + preparser.registerPreparser(XMLGrammarDescription.XML_SCHEMA, null); + preparser.setProperty(org.apache.xerces.impl.Constants.XERCES_PROPERTY_PREFIX + + org.apache.xerces.impl.Constants.XMLGRAMMAR_POOL_PROPERTY, grammarPool); + preparser.setFeature(SAX_NAMESPACES_FEATURE, true); + preparser.setFeature(SAX_VALIDATION_FEATURE, true); + + Properties initProps = (Properties) context.getAttribute(Constants.WSCP_INIT_PROPS_); + String slSchemaLoc = initProps.getProperty(Constants.IP_SL_SCHEMA_); + preparseSchema(context, preparser, slSchemaLoc); + String moaSchemaLoc = initProps.getProperty(Constants.IP_MOA_SCHEMA_); + preparseSchema(context, preparser, moaSchemaLoc); + + DOMParser xmlParser = new DOMParser(symbolTable, grammarPool); + try + { + xmlParser.setFeature(SAX_NAMESPACES_FEATURE, true); + xmlParser.setFeature(SAX_VALIDATION_FEATURE, true); + xmlParser.setFeature(XERCES_SCHEMA_VALIDATION_FEATURE, true); + xmlParser.setFeature(XERCES_NORMALIZED_VALUE_FEATURE, false); + xmlParser.setFeature(XERCES_INCLUDE_IGNORABLE_WHITESPACE_FEATURE, true); + xmlParser.setFeature(XERCES_CREATE_ENTITY_REF_NODES_FEATURE, false); + } + catch (SAXException e) + { + String message = "Initialization of XML parser failed."; + logger_.error(message, e); + } + context.setAttribute(Constants.WSCP_XMLPARSER_, xmlParser); + } + + /* ---------------------------------------------------------------------------------------------------- */ + + /** + * Does some clean up at finalization of the web application. + * + * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent) + */ + public void contextDestroyed(ServletContextEvent event) + { + // Remove init properties from web service context + Properties initProps = (Properties) event.getServletContext().getAttribute(Constants.WSCP_INIT_PROPS_); + if (initProps != null) event.getServletContext().removeAttribute(Constants.WSCP_INIT_PROPS_); + } + + /* ---------------------------------------------------------------------------------------------------- */ + + private void preparseSchema(ServletContext context, XMLGrammarPreparser preparser, String schemaLoc) + { + InputStream schemaIS = context.getResourceAsStream(schemaLoc); + if (schemaIS == null) + { + String message = "Cannot load schema from location \"" + schemaLoc + "\"."; + logger_.error(message); + } + try + { + String schemaSystemId = context.getResource(schemaLoc).toExternalForm(); + preparser.preparseGrammar(XMLGrammarDescription.XML_SCHEMA, + new XMLInputSource(null, schemaSystemId, null, schemaIS, null)); + } + catch (Exception e) + { + String message = "Parsing schema loaded from location \"" + schemaLoc + "\" failed."; + logger_.error(message, e); + } + } + + +} |