synapse-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ind...@apache.org
Subject svn commit: r555278 - in /webservices/synapse/trunk/java: modules/core/src/main/java/org/apache/synapse/mediators/builtin/ValidateMediator.java src/main/bin/synapse.bat src/main/bin/synapse.sh
Date Wed, 11 Jul 2007 14:07:21 GMT
Author: indika
Date: Wed Jul 11 07:07:20 2007
New Revision: 555278

URL: http://svn.apache.org/viewvc?view=rev&rev=555278
Log:
Performance improvement to the schema validation using the  XML grammer caching feature of
the xerces

Modified:
    webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ValidateMediator.java
    webservices/synapse/trunk/java/src/main/bin/synapse.bat
    webservices/synapse/trunk/java/src/main/bin/synapse.sh

Modified: webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ValidateMediator.java
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ValidateMediator.java?view=diff&rev=555278&r1=555277&r2=555278
==============================================================================
--- webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ValidateMediator.java
(original)
+++ webservices/synapse/trunk/java/modules/core/src/main/java/org/apache/synapse/mediators/builtin/ValidateMediator.java
Wed Jul 11 07:07:20 2007
@@ -33,10 +33,7 @@
 import org.apache.synapse.mediators.AbstractListMediator;
 import org.apache.synapse.mediators.MediatorProperty;
 import org.jaxen.JaxenException;
-import org.xml.sax.InputSource;
-import org.xml.sax.SAXException;
-import org.xml.sax.SAXParseException;
-import org.xml.sax.XMLReader;
+import org.xml.sax.*;
 import org.xml.sax.helpers.DefaultHandler;
 import org.xml.sax.helpers.XMLReaderFactory;
 
@@ -44,9 +41,11 @@
 import javax.xml.stream.XMLStreamWriter;
 import javax.xml.transform.sax.SAXSource;
 import javax.xml.transform.stream.StreamSource;
+import javax.xml.transform.Source;
 import javax.xml.validation.Schema;
 import javax.xml.validation.SchemaFactory;
 import javax.xml.validation.Validator;
+import javax.xml.XMLConstants;
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
@@ -66,11 +65,6 @@
     private static final Log trace = LogFactory.getLog(Constants.TRACE_LOGGER);
 
     /**
-     * Default schema language (http://www.w3.org/2001/XMLSchema) and validator feature ids.
-     */
-    private static final String DEFAULT_SCHEMA_LANGUAGE = "http://www.w3.org/2001/XMLSchema";
-
-    /**
      * A list of property keys, referring to the schemas to be used for the validation
      */
     private List schemaKeys = new ArrayList();
@@ -89,21 +83,16 @@
     private List properties = new ArrayList();
 
     /**
-     * This is the actual Validator instance used to validate messages - probably
-     * by multiple threads. Always *USE* validatorLock to synchronize access to this
+     * This is the actual schema instance used to create a new schema
+     * This is a thred-safe instance.
      */
-    private Validator validator = null;
+    private Schema cachedSchema;
 
     /**
      * Lock used to ensure thread-safe creation and use of the above Validator
      */
     private final Object validatorLock = new Object();
 
-    /**
-     * This is the reference to the DefaultHandler instance
-     */
-    private final MyErrorHandler errorHandler = new MyErrorHandler();
-
     private static final String DEFAULT_XPATH = "//s11:Envelope/s11:Body/child::*[position()=1]
| " +
         "//s12:Envelope/s12:Body/child::*[position()=1]";
 
@@ -135,7 +124,7 @@
                 return (OMNode) ((List) o).get(0);  // Always fetches *only* the first
             } else {
                 handleException("The evaluation of the XPath expression "
-                    + source + " must result in an OMNode");
+                        + source + " must result in an OMNode");
             }
         } catch (JaxenException e) {
             handleException("Error evaluating XPath " + source + " on message");
@@ -146,16 +135,17 @@
     public boolean mediate(MessageContext synCtx) {
 
         log.debug("ValidateMediator - Validate mediator mediate()");
-        ByteArrayInputStream baisFromSource = null;
         boolean shouldTrace = shouldTrace(synCtx.getTracingState());
         if (shouldTrace) {
             trace.trace("Start : Validate mediator");
         }
+        // Input source for the validation
+        Source validateSrc = null;
         try {
             // create a byte array output stream and serialize the source node into it
             ByteArrayOutputStream baosForSource = new ByteArrayOutputStream();
             XMLStreamWriter xsWriterForSource =
-                XMLOutputFactory.newInstance().createXMLStreamWriter(baosForSource);
+                    XMLOutputFactory.newInstance().createXMLStreamWriter(baosForSource);
 
             // serialize the validation target and get an input stream into it
             OMNode validateSource = getValidateSource(synCtx);
@@ -163,40 +153,86 @@
                 trace.trace("Validate Source : " + validateSource.toString());
             }
             validateSource.serialize(xsWriterForSource);
-            baisFromSource = new ByteArrayInputStream(baosForSource.toByteArray());
-
+            ByteArrayInputStream baisFromSource = new ByteArrayInputStream(
+                    baosForSource.toByteArray());
+            XMLReader reader = XMLReaderFactory.createXMLReader();
+            validateSrc = new SAXSource(reader, new InputSource(baisFromSource));
         } catch (Exception e) {
             handleException("Error accessing source element for validation : " + source,
e);
         }
 
-        try {
-            XMLReader reader = XMLReaderFactory.createXMLReader();
-            SAXSource saxSrc = new SAXSource(reader, new InputSource(baisFromSource));
-
-            synchronized (validatorLock) {
-
-                // initialize schemas/Validator if required
-                initialize(synCtx);
+        // flag to check if we need to initialize/re-initialize the schema
+        boolean reCreate = false;
+        // if any of the schemas are not loaded or expired, load or re-load them
+        for (Iterator iter = schemaKeys.iterator(); iter.hasNext();) {
+            String propKey = (String) iter.next();
+            Entry dp = synCtx.getConfiguration().getEntryDefinition(propKey);
+            if (dp != null && dp.isDynamic()) {
+                if (!dp.isCached() || dp.isExpired()) {
+                    reCreate = true;       // request re-initialization of Validator
+                }
+            }
+        }
 
-                // perform actual validation
-                validator.validate(saxSrc);
+        // This is the reference to the DefaultHandler instance
+        MyErrorHandler errorHandler = new MyErrorHandler();
 
-                if (errorHandler.isValidationError()) {
-                    if (log.isDebugEnabled()) {
-                        log.debug(
-                            "Validation of element returned by XPath : " + source +
-                                " failed against the given schemas with Message : " +
-                                errorHandler.getSaxParseException().getMessage() +
-                                " Executing 'on-fail' sequence");
-                        log.debug("Failed message envelope : " + synCtx.getEnvelope());
+        // do not re-initialize schema unless required
+        synchronized (validatorLock) {
+            if (reCreate || cachedSchema == null) {
+                try {
+                    SchemaFactory factory = SchemaFactory.newInstance(
+                            XMLConstants.W3C_XML_SCHEMA_NS_URI);
+                    factory.setErrorHandler(errorHandler);
+                    // set any features on/off as requested
+                    for (Iterator iter = properties.iterator(); iter.hasNext();) {
+                        MediatorProperty prop = (MediatorProperty) iter.next();
+                        factory.setFeature(
+                                prop.getName(), prop.getValue() != null &&
+                                "true".equals(prop.getValue()));
                     }
-                    // super.mediate() invokes the "on-fail" sequence of mediators
-                    if (shouldTrace) {
-                        trace.trace("Validation failed. Invoking the \"on-fail\" " +
-                            "sequence of mediators");
+                    StreamSource[] sources = new StreamSource[schemaKeys.size()];
+                    int i = 0;
+                    for (Iterator iterator = schemaKeys.iterator(); iterator.hasNext();)
{
+                        String propName = (String) iterator.next();
+                        sources[i++] = Util.getStreamSource(synCtx.getEntry(propName));
+                    }
+                    cachedSchema = factory.newSchema(sources);
+                    if (errorHandler.isValidationError()) {
+                        //reset the errorhandler state
+                        errorHandler.setValidationError(false);
+                        if (log.isDebugEnabled()) {
+                            log.debug("Error occured during creating new schema ");
+                        }
                     }
-                    return super.mediate(synCtx);
+                } catch (SAXNotSupportedException e) {
+                    handleException("Error when setting a feature", e);
+                } catch (SAXException e) {
+                    handleException("Error when creating a new schema", e);
+                }
+            }
+        }
+        // no need to synchronized ,schema instance is tread-safe
+        try {
+            Validator validator = cachedSchema.newValidator();
+            validator.setErrorHandler(errorHandler);
+            // perform actual validation
+            validator.validate(validateSrc);
+            if (errorHandler.isValidationError()) {
+                if (log.isDebugEnabled()) {
+                    log.debug(
+                            "Validation of element returned by XPath : " + source +
+                                    " failed against the given schemas with Message : " +
+                                    errorHandler.getSaxParseException().getMessage() +
+                                    " Executing 'on-fail' sequence");
+                    log.debug("Failed message envelope : " + synCtx.getEnvelope());
+                }
+                // super.mediate() invokes the "on-fail" sequence of mediators
+                if (shouldTrace) {
+                    trace.trace("Validation failed. Invoking the \"on-fail\" " +
+                            "sequence of mediators");
                 }
+                return super.mediate(synCtx);
             }
         } catch (SAXException e) {
             handleException("Error validating " + source + " element" + e.getMessage(), e);
@@ -205,7 +241,7 @@
         }
 
         log.debug("validation of element returned by the XPath expression : " + source +
-            " succeeded against the given schemas and the current message");
+                " succeeded against the given schemas and the current message");
         if (shouldTrace) {
             trace.trace("End : Validate mediator");
         }
@@ -213,67 +249,6 @@
     }
 
     /**
-     * Perform actual initialization of this validate mediator instance - if required
-     */
-    private void initialize(MessageContext msgCtx) {
-
-        // flag to check if we need to initialize/re-initialize the schema Validator
-        boolean reCreate = false;
-
-        // if any of the schemas are not loaded or expired, load or re-load them
-        Iterator iter = schemaKeys.iterator();
-        while (iter.hasNext()) {
-            String propKey = (String) iter.next();
-            Entry dp = msgCtx.getConfiguration().getEntryDefinition(propKey);
-            if (dp != null && dp.isDynamic()) {
-                if (!dp.isCached() || dp.isExpired()) {
-                    reCreate = true;       // request re-initialization of Validator
-                }
-            }
-        }
-
-        // do not re-initialize Validator unless required
-        if (!reCreate && validator != null) {
-            return;
-        }
-
-        try {
-            // Create SchemaFactory and configure for the default schema language - XMLSchema
-            SchemaFactory factory = SchemaFactory.newInstance(DEFAULT_SCHEMA_LANGUAGE);
-            // Clear the previous state of the validation error
-            errorHandler.setValidationError(false);
-            factory.setErrorHandler(errorHandler);
-
-            // set any features on/off as requested
-            iter = properties.iterator();
-            while (iter.hasNext()) {
-                MediatorProperty prop = (MediatorProperty) iter.next();
-                factory.setFeature(
-                    prop.getName(), prop.getValue() != null && "true".equals(prop.getValue()));
-            }
-
-            Schema schema = null;
-
-            StreamSource[] sources = new StreamSource[schemaKeys.size()];
-            iter = schemaKeys.iterator();
-            int i = 0;
-            while (iter.hasNext()) {
-                String propName = (String) iter.next();
-                sources[i++] = Util.getStreamSource(msgCtx.getEntry(propName));
-            }
-            schema = factory.newSchema(sources);
-
-            // Setup validator and input source
-            // Features set for the SchemaFactory get propagated to Schema and Validator
(JAXP 1.4)
-            validator = schema.newValidator();
-            validator.setErrorHandler(errorHandler);
-
-        } catch (SAXException e) {
-            handleException("Error creating Validator", e);
-        }
-    }
-
-    /**
      * This class handles validation errors to be used for error reporting
      */
     private class MyErrorHandler extends DefaultHandler {
@@ -301,9 +276,8 @@
         public SAXParseException getSaxParseException() {
             return saxParseException;
         }
-
         /**
-         * To set explicitly validation error condition 
+         * To set explicitly validation error condition
          * @param validationError
          */
         public void setValidationError(boolean validationError) {
@@ -369,7 +343,7 @@
                 setProperty(prop.getName(), prop.getValue());
             } else {
                 handleException("Attempt to set invalid property type. " +
-                    "Expected MediatorProperty type got " + o.getClass().getName());
+                        "Expected MediatorProperty type got " + o.getClass().getName());
             }
         }
     }

Modified: webservices/synapse/trunk/java/src/main/bin/synapse.bat
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/src/main/bin/synapse.bat?view=diff&rev=555278&r1=555277&r2=555278
==============================================================================
--- webservices/synapse/trunk/java/src/main/bin/synapse.bat (original)
+++ webservices/synapse/trunk/java/src/main/bin/synapse.bat Wed Jul 11 07:07:20 2007
@@ -123,7 +123,7 @@
 echo Using JAVA_HOME:       %JAVA_HOME%
 echo Using SYNAPSE_XML:     %_SYNAPSE_XML%
 
-%_JAVACMD% %_PORT% %_SYNAPSE_XML% -Daxis2.xml="%SYNAPSE_HOME%\repository\conf\axis2.xml"
-Djava.endorsed.dirs=%SYNAPSE_ENDORSED% %_XDEBUG% -cp %SYNAPSE_CLASS_PATH% org.apache.synapse.SynapseServer
"%SYNAPSE_HOME%\repository"
+%_JAVACMD% %_PORT% %_SYNAPSE_XML% -Dorg.apache.xerces.xni.parser.XMLParserConfiguration=org.apache.xerces.parsers.XMLGrammarCachingConfiguration
-Daxis2.xml="%SYNAPSE_HOME%\repository\conf\axis2.xml" -Djava.endorsed.dirs=%SYNAPSE_ENDORSED%
%_XDEBUG% -cp %SYNAPSE_CLASS_PATH% org.apache.synapse.SynapseServer "%SYNAPSE_HOME%\repository"
 goto end
 
 :end

Modified: webservices/synapse/trunk/java/src/main/bin/synapse.sh
URL: http://svn.apache.org/viewvc/webservices/synapse/trunk/java/src/main/bin/synapse.sh?view=diff&rev=555278&r1=555277&r2=555278
==============================================================================
--- webservices/synapse/trunk/java/src/main/bin/synapse.sh (original)
+++ webservices/synapse/trunk/java/src/main/bin/synapse.sh Wed Jul 11 07:07:20 2007
@@ -152,4 +152,4 @@
 echo "Using JAVA_HOME:       $JAVA_HOME"
 echo "Using SYNAPSE_XML:     $SYNAPSE_XML"
 
-$JAVA_HOME/bin/java $XDEBUG $PORT $SYNAPSE_XML -Daxis2.xml=$SYNAPSE_HOME/repository/conf/axis2.xml
-Djava.endorsed.dirs=$SYNAPSE_ENDORSED -classpath $SYNAPSE_CLASSPATH org.apache.synapse.SynapseServer
$SYNAPSE_HOME/repository
+$JAVA_HOME/bin/java $XDEBUG $PORT $SYNAPSE_XML -Dorg.apache.xerces.xni.parser.XMLParserConfiguration=org.apache.xerces.parsers.XMLGrammarCachingConfiguration
-Daxis2.xml=$SYNAPSE_HOME/repository/conf/axis2.xml -Djava.endorsed.dirs=$SYNAPSE_ENDORSED
-classpath $SYNAPSE_CLASSPATH org.apache.synapse.SynapseServer $SYNAPSE_HOME/repository



---------------------------------------------------------------------
To unsubscribe, e-mail: synapse-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: synapse-dev-help@ws.apache.org


Mime
View raw message