/* * 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; import at.gv.egovernment.moa.spss.slinterface.URLRewriter; /** * @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"; protected static final String XERCES_DEFER_NODE_EXPANSION_ = "http://apache.org/xml/features/dom/defer-node-expansion"; 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); } // Put helper object for rewriting URLs in the result page into the context URLRewriter urlRewriter = new URLRewriter((Properties)context.getAttribute(Constants.WSCP_INIT_PROPS_)); context.setAttribute(Constants.WSCP_URL_REWRITER_, urlRewriter); // 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); // Schema for Security-Layer 1.2 alpha (including LocRefContent) Properties initProps = (Properties) context.getAttribute(Constants.WSCP_INIT_PROPS_); String slSchemaLoc = initProps.getProperty(Constants.IP_SL_SCHEMA_); preparseSchema(context, preparser, slSchemaLoc); // Schema for MOA 1.2 String moaSchemaLoc = initProps.getProperty(Constants.IP_MOA_SCHEMA_); preparseSchema(context, preparser, moaSchemaLoc); // Schema for SLXHTML 1.0 String slxhtmlSchemaLoc = initProps.getProperty(Constants.IP_SLXHTML_SCHEMA_); preparseSchema(context, preparser, slxhtmlSchemaLoc); // TODO parser is not threadsafe 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); xmlParser.setFeature(XERCES_DEFER_NODE_EXPANSION_, true); xmlParser.setFeature("http://apache.org/xml/features/validation/schema/augment-psvi", false); // TODO Stringkonstante xmlParser.setErrorHandler(new XMLParserErrorHandler(false, true, true)); } 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); } } }