You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@santuario.apache.org by gi...@apache.org on 2012/10/06 10:47:24 UTC

svn commit: r1394970 - in /santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax: ext/ ext/stax/ impl/processor/output/ impl/stax/

Author: giger
Date: Sat Oct  6 08:47:23 2012
New Revision: 1394970

URL: http://svn.apache.org/viewvc?rev=1394970&view=rev
Log:
SANTUARIO-346 - Performance update. Memory consumption for signature case is now much lower. This improves
the overall "time" performance a little bit too.


Modified:
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/ext/AbstractOutputProcessor.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/ext/stax/XMLSecEventFactory.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/ext/stax/XMLSecStartElement.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/output/AbstractEncryptOutputProcessor.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/stax/XMLSecAttributeImpl.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/stax/XMLSecNamespaceImpl.java
    santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/stax/XMLSecStartElementImpl.java

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/ext/AbstractOutputProcessor.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/ext/AbstractOutputProcessor.java?rev=1394970&r1=1394969&r2=1394970&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/ext/AbstractOutputProcessor.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/ext/AbstractOutputProcessor.java Sat Oct  6 08:47:23 2012
@@ -132,15 +132,16 @@ public abstract class AbstractOutputProc
 
     public XMLSecStartElement addAttributes(XMLSecStartElement xmlSecStartElement,
                                             List<XMLSecAttribute> attributeList) throws XMLStreamException {
-        xmlSecStartElement.getOnElementDeclaredAttributes().addAll(attributeList);
 
         List<XMLSecNamespace> declaredNamespaces = xmlSecStartElement.getOnElementDeclaredNamespaces();
         for (int i = 0; i < attributeList.size(); i++) {
             XMLSecAttribute xmlSecAttribute = attributeList.get(i);
+            xmlSecStartElement.addAttribute(xmlSecAttribute);
+
             final QName attributeName = xmlSecAttribute.getName();
             if (attributeName.getNamespaceURI() != null && !"".equals(attributeName.getNamespaceURI()) 
                 && !declaredNamespaces.contains(xmlSecAttribute.getAttributeNamespace())) {
-                declaredNamespaces.add(xmlSecAttribute.getAttributeNamespace());
+                xmlSecStartElement.addNamespace(xmlSecAttribute.getAttributeNamespace());
             }
         }
         return xmlSecStartElement;

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/ext/stax/XMLSecEventFactory.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/ext/stax/XMLSecEventFactory.java?rev=1394970&r1=1394969&r2=1394970&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/ext/stax/XMLSecEventFactory.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/ext/stax/XMLSecEventFactory.java Sat Oct  6 08:47:23 2012
@@ -55,7 +55,7 @@ public class XMLSecEventFactory {
                 if (namespaceCount > 0) {
                     comparableNamespaces = new ArrayList<XMLSecNamespace>(namespaceCount);
                     for (int i = 0; i < namespaceCount; i++) {
-                        comparableNamespaces.add(new XMLSecNamespaceImpl(xmlStreamReader.getNamespacePrefix(i), xmlStreamReader.getNamespaceURI(i)));
+                        comparableNamespaces.add(XMLSecNamespaceImpl.getInstance(xmlStreamReader.getNamespacePrefix(i), xmlStreamReader.getNamespaceURI(i)));
                     }
                 }
                 return new XMLSecStartElementImpl(xmlStreamReader.getName(), comparableAttributes, comparableNamespaces, parentXMLSecStartElement);
@@ -142,6 +142,6 @@ public class XMLSecEventFactory {
     }
 
     public static XMLSecNamespace createXMLSecNamespace(String prefix, String uri) {
-        return new XMLSecNamespaceImpl(prefix, uri);
+        return XMLSecNamespaceImpl.getInstance(prefix, uri);
     }
 }

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/ext/stax/XMLSecStartElement.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/ext/stax/XMLSecStartElement.java?rev=1394970&r1=1394969&r2=1394970&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/ext/stax/XMLSecStartElement.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/ext/stax/XMLSecStartElement.java Sat Oct  6 08:47:23 2012
@@ -33,10 +33,14 @@ public interface XMLSecStartElement exte
 
     List<XMLSecNamespace> getOnElementDeclaredNamespaces();
 
+    void addNamespace(XMLSecNamespace xmlSecNamespace);
+
     void getAttributesFromCurrentScope(List<XMLSecAttribute> comparableAttributeList);
 
     List<XMLSecAttribute> getOnElementDeclaredAttributes();
 
+    void addAttribute(XMLSecAttribute xmlSecAttribute);
+
     @Override
     XMLSecStartElement asStartElement();
 }

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/output/AbstractEncryptOutputProcessor.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/output/AbstractEncryptOutputProcessor.java?rev=1394970&r1=1394969&r2=1394970&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/output/AbstractEncryptOutputProcessor.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/processor/output/AbstractEncryptOutputProcessor.java Sat Oct  6 08:47:23 2012
@@ -28,7 +28,6 @@ import org.apache.xml.security.stax.ext.
 import org.apache.xml.security.stax.ext.stax.*;
 import org.apache.xml.security.stax.impl.EncryptionPartDef;
 import org.apache.xml.security.stax.impl.util.TrimmerOutputStream;
-import org.apache.xml.security.stax.impl.util.UnsynchronizedBufferedOutputStream;
 
 import javax.crypto.Cipher;
 import javax.crypto.CipherOutputStream;
@@ -45,9 +44,7 @@ import java.lang.reflect.InvocationTarge
 import java.security.InvalidAlgorithmParameterException;
 import java.security.InvalidKeyException;
 import java.security.NoSuchAlgorithmException;
-import java.util.ArrayList;
-import java.util.Iterator;
-import java.util.List;
+import java.util.*;
 
 /**
  * Processor to encrypt XML structures
@@ -147,11 +144,11 @@ public abstract class AbstractEncryptOut
                     outputStream = constructor.newInstance(outputStream);
                 }
                 //the trimmer output stream is needed to strip away the dummy wrapping element which must be added
-                cipherOutputStream = new TrimmerOutputStream(outputStream, 1024, 3, 4);
+                cipherOutputStream = new TrimmerOutputStream(outputStream, 8192 * 10, 3, 4);
 
                 //we create a new StAX writer for optimized namespace writing.
                 //spec says (4.2): "The cleartext octet sequence obtained in step 3 is interpreted as UTF-8 encoded character data."
-                xmlEventWriter = XMLSecurityConstants.xmlOutputFactory.createXMLEventWriter(new UnsynchronizedBufferedOutputStream(cipherOutputStream, 8192), "UTF-8");
+                xmlEventWriter = XMLSecurityConstants.xmlOutputFactory.createXMLEventWriter(cipherOutputStream, "UTF-8");
                 //we have to output a fake element to workaround text-only encryption:
                 xmlEventWriter.add(wrapperStartElement);
             } catch (NoSuchPaddingException e) {
@@ -237,14 +234,14 @@ public abstract class AbstractEncryptOut
                     encryptEvent(xmlSecEvent);
 
                     //push all buffered encrypted character events through the chain
-                    final List<XMLSecCharacters> charactersBuffer = characterEventGeneratorOutputStream.getCharactersBuffer();
-                    if (!charactersBuffer.isEmpty()) {
+                    final Deque<XMLSecCharacters> charactersBuffer = characterEventGeneratorOutputStream.getCharactersBuffer();
+                    if (charactersBuffer.size() > 5) {
                         OutputProcessorChain subOutputProcessorChain = outputProcessorChain.createSubChain(this);
-                        int size = charactersBuffer.size();
-                        int i = 0;
-                        while (i < size) {
-                            XMLSecCharacters xmlSecCharacters = charactersBuffer.remove(i++);
-                            outputAsEvent(subOutputProcessorChain, xmlSecCharacters);
+                        Iterator<XMLSecCharacters> charactersIterator = charactersBuffer.iterator();
+                        while (charactersIterator.hasNext()) {
+                            XMLSecCharacters characters = charactersIterator.next();
+                            outputAsEvent(subOutputProcessorChain, characters);
+                            charactersIterator.remove();
                         }
                     }
                     break;
@@ -311,7 +308,7 @@ public abstract class AbstractEncryptOut
             }
 
             //push all buffered encrypted character events through the chain
-            final List<XMLSecCharacters> charactersBuffer = characterEventGeneratorOutputStream.getCharactersBuffer();
+            final Deque<XMLSecCharacters> charactersBuffer = characterEventGeneratorOutputStream.getCharactersBuffer();
             if (!charactersBuffer.isEmpty()) {
                 Iterator<XMLSecCharacters> charactersIterator = charactersBuffer.iterator();
                 while (charactersIterator.hasNext()) {
@@ -356,30 +353,30 @@ public abstract class AbstractEncryptOut
      */
     public class CharacterEventGeneratorOutputStream extends OutputStream {
 
-        private final List<XMLSecCharacters> charactersBuffer = new ArrayList<XMLSecCharacters>();
+        private final Deque<XMLSecCharacters> charactersBuffer = new ArrayDeque<XMLSecCharacters>();
         private final String encoding;
 
         public CharacterEventGeneratorOutputStream(String encoding) {
             this.encoding = encoding;
         }
 
-        public List<XMLSecCharacters> getCharactersBuffer() {
+        public Deque<XMLSecCharacters> getCharactersBuffer() {
             return charactersBuffer;
         }
 
         @Override
         public void write(int b) throws IOException {
-            charactersBuffer.add(createCharacters(new String(new byte[]{((byte) b)}, encoding)));
+            charactersBuffer.offer(createCharacters(new String(new byte[]{((byte) b)}, encoding)));
         }
 
         @Override
         public void write(byte[] b) throws IOException {
-            charactersBuffer.add(createCharacters(new String(b, encoding)));
+            charactersBuffer.offer(createCharacters(new String(b, encoding)));
         }
 
         @Override
         public void write(byte[] b, int off, int len) throws IOException {
-            charactersBuffer.add(createCharacters(new String(b, off, len, encoding)));
+            charactersBuffer.offer(createCharacters(new String(b, off, len, encoding)));
         }
     }
 }

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/stax/XMLSecAttributeImpl.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/stax/XMLSecAttributeImpl.java?rev=1394970&r1=1394969&r2=1394970&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/stax/XMLSecAttributeImpl.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/stax/XMLSecAttributeImpl.java Sat Oct  6 08:47:23 2012
@@ -73,7 +73,7 @@ public class XMLSecAttributeImpl extends
     @Override
     public XMLSecNamespace getAttributeNamespace() {
         if (this.attributeNamespace == null) {
-            this.attributeNamespace = new XMLSecNamespaceImpl(this.name.getPrefix(), this.name.getNamespaceURI());
+            this.attributeNamespace = XMLSecNamespaceImpl.getInstance(this.name.getPrefix(), this.name.getNamespaceURI());
         }
         return this.attributeNamespace;
     }

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/stax/XMLSecNamespaceImpl.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/stax/XMLSecNamespaceImpl.java?rev=1394970&r1=1394969&r2=1394970&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/stax/XMLSecNamespaceImpl.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/stax/XMLSecNamespaceImpl.java Sat Oct  6 08:47:23 2012
@@ -23,6 +23,8 @@ import org.apache.xml.security.stax.ext.
 import javax.xml.XMLConstants;
 import javax.xml.namespace.QName;
 import javax.xml.stream.XMLStreamConstants;
+import java.util.Map;
+import java.util.WeakHashMap;
 
 /**
  * Class to let XML-Namespaces be comparable how it is requested by C14N
@@ -32,16 +34,41 @@ import javax.xml.stream.XMLStreamConstan
  */
 public class XMLSecNamespaceImpl extends XMLSecEventBaseImpl implements XMLSecNamespace {
 
-    private String prefix = "";
+    private static final Map<String, Map<String, XMLSecNamespace>> xmlSecNamespaceMap =
+            new WeakHashMap<String, Map<String, XMLSecNamespace>>();
+
+    private String prefix;
     private final String uri;
+    private QName qName;
 
-    public XMLSecNamespaceImpl(String prefix, String uri) {
-        if (prefix != null) {
-            this.prefix = prefix;
-        }
+    private XMLSecNamespaceImpl(String prefix, String uri) {
+        this.prefix = prefix;
         this.uri = uri;
     }
 
+    public static XMLSecNamespace getInstance(String prefix, String uri) {
+        if (prefix == null) {
+            prefix = "";
+        }
+        Map<String, XMLSecNamespace> nsMap = xmlSecNamespaceMap.get(prefix);
+        if (nsMap != null) {
+            XMLSecNamespace xmlSecNamespace = nsMap.get(uri);
+            if (xmlSecNamespace != null) {
+                return xmlSecNamespace;
+            } else {
+                xmlSecNamespace = new XMLSecNamespaceImpl(prefix, uri);
+                nsMap.put(uri, xmlSecNamespace);
+                return xmlSecNamespace;
+            }
+        } else {
+            nsMap = new WeakHashMap<String, XMLSecNamespace>();
+            XMLSecNamespace xmlSecNamespace = new XMLSecNamespaceImpl(prefix, uri);
+            nsMap.put(uri, xmlSecNamespace);
+            xmlSecNamespaceMap.put(prefix, nsMap);
+            return xmlSecNamespace;
+        }
+    }
+
     public int compareTo(XMLSecNamespace o) {
         //An element's namespace nodes are sorted lexicographically by local name
         //(the default namespace node, if one exists, has no local name and is therefore lexicographically least).
@@ -70,7 +97,10 @@ public class XMLSecNamespaceImpl extends
 
     @Override
     public QName getName() {
-        return new QName(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, this.prefix);
+        if (this.qName == null) {
+            this.qName = new QName(XMLConstants.XMLNS_ATTRIBUTE_NS_URI, this.prefix);
+        }
+        return this.qName;
     }
 
     @Override

Modified: santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/stax/XMLSecStartElementImpl.java
URL: http://svn.apache.org/viewvc/santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/stax/XMLSecStartElementImpl.java?rev=1394970&r1=1394969&r2=1394970&view=diff
==============================================================================
--- santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/stax/XMLSecStartElementImpl.java (original)
+++ santuario/xml-security-java/trunk/src/main/java/org/apache/xml/security/stax/impl/stax/XMLSecStartElementImpl.java Sat Oct  6 08:47:23 2012
@@ -67,7 +67,7 @@ public class XMLSecStartElementImpl exte
     @Override
     public XMLSecNamespace getElementNamespace() {
         if (this.elementNamespace == null) {
-            this.elementNamespace = new XMLSecNamespaceImpl(this.elementName.getPrefix(), this.elementName.getNamespaceURI());
+            this.elementNamespace = XMLSecNamespaceImpl.getInstance(this.elementName.getPrefix(), this.elementName.getNamespaceURI());
         }
         return this.elementNamespace;
     }
@@ -90,10 +90,15 @@ public class XMLSecStartElementImpl exte
 
     @Override
     public List<XMLSecAttribute> getOnElementDeclaredAttributes() {
+        return this.attributes;
+    }
+
+    @Override
+    public void addAttribute(XMLSecAttribute xmlSecAttribute) {
         if (this.attributes == Collections.<XMLSecAttribute>emptyList()) {
-            this.attributes = new ArrayList<XMLSecAttribute>();
+            this.attributes = new ArrayList<XMLSecAttribute>(1);
         }
-        return this.attributes;
+        this.attributes.add(xmlSecAttribute);
     }
 
     public int getDocumentLevel() {
@@ -134,10 +139,15 @@ public class XMLSecStartElementImpl exte
 
     @Override
     public List<XMLSecNamespace> getOnElementDeclaredNamespaces() {
+        return this.namespaces;
+    }
+
+    @Override
+    public void addNamespace(XMLSecNamespace xmlSecNamespace) {
         if (this.namespaces == Collections.<XMLSecNamespace>emptyList()) {
-            this.namespaces = new ArrayList<XMLSecNamespace>();
+            this.namespaces = new ArrayList<XMLSecNamespace>(1);
         }
-        return this.namespaces;
+        this.namespaces.add(xmlSecNamespace);
     }
 
     @Override