You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2022/11/13 10:23:37 UTC

[ws-axiom] branch master updated: [AXIOM-506] Migrate OptimizationPolicyImpl to Blob

This is an automated email from the ASF dual-hosted git repository.

veithen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ws-axiom.git


The following commit(s) were added to refs/heads/master by this push:
     new 54cb5401d [AXIOM-506] Migrate OptimizationPolicyImpl to Blob
54cb5401d is described below

commit 54cb5401d43b9489eb9a671762de1052a32ec7a5
Author: Andreas Veithen <an...@gmail.com>
AuthorDate: Sun Nov 13 10:23:28 2022 +0000

    [AXIOM-506] Migrate OptimizationPolicyImpl to Blob
---
 .../axiom/util/activation/DataHandlerUtils.java    | 39 ----------------------
 .../om/impl/stream/xop}/CountingOutputStream.java  |  8 ++---
 .../om/impl/stream/xop/OptimizationPolicyImpl.java | 19 ++++++++---
 .../stream/xop}/SizeLimitExceededException.java    | 10 +++---
 4 files changed, 22 insertions(+), 54 deletions(-)

diff --git a/axiom-api/src/main/java/org/apache/axiom/util/activation/DataHandlerUtils.java b/axiom-api/src/main/java/org/apache/axiom/util/activation/DataHandlerUtils.java
index 900af1e3e..9f8162100 100644
--- a/axiom-api/src/main/java/org/apache/axiom/util/activation/DataHandlerUtils.java
+++ b/axiom-api/src/main/java/org/apache/axiom/util/activation/DataHandlerUtils.java
@@ -18,11 +18,7 @@
  */
 package org.apache.axiom.util.activation;
 
-import java.io.IOException;
-import java.io.OutputStream;
-
 import javax.activation.DataHandler;
-import javax.activation.DataSource;
 
 import org.apache.axiom.blob.Blob;
 import org.apache.axiom.mime.activation.PartDataHandler;
@@ -34,41 +30,6 @@ import org.apache.axiom.mime.activation.PartDataHandlerBlob;
 public final class DataHandlerUtils {
     private DataHandlerUtils() {}
 
-    /**
-     * Check if the given {@link DataHandler} will produce a byte stream that is longer than a given
-     * limit. It will first attempt to determine the size using
-     * {@link DataSourceUtils#getSize(DataSource)}. If that fails, it will use
-     * {@link DataHandler#writeTo(OutputStream)} to determine if the size is larger than the limit.
-     * 
-     * @param dh
-     *            the {@link DataHandler} to check
-     * @param limit
-     *            the limit
-     * @return {@code true} if the size is larger than {@code limit}, {@code false} otherwise
-     * @throws IOException
-     *             if {@link DataHandler#writeTo(OutputStream)} produced an unexpected exception
-     */
-    public static boolean isLargerThan(DataHandler dh, long limit) throws IOException {
-        long size = DataSourceUtils.getSize(dh.getDataSource());
-        if (size != -1) {
-            return size > limit;
-        } else {
-            // In all other cases, we prefer DataHandler#writeTo over DataSource#getInputStream.
-            // The reason is that if the DataHandler was constructed from an Object rather than
-            // a DataSource, a call to DataSource#getInputStream() will start a new thread and
-            // return a PipedInputStream. This is so for Geronimo's as well as Sun's JAF
-            // implementation. The reason is that DataContentHandler only has a writeTo and no
-            // getInputStream method. Obviously starting a new thread just to check the size of
-            // the data is an overhead that we should avoid.
-            try {
-                dh.writeTo(new CountingOutputStream(limit));
-                return false;
-            } catch (SizeLimitExceededException ex) {
-                return true;
-            }
-        }
-    }
-
     /**
      * Get the {@link DataHandler} wrapped by the given {@link Blob}.
      * 
diff --git a/axiom-api/src/main/java/org/apache/axiom/util/activation/CountingOutputStream.java b/mixins/om-mixins/src/main/java/org/apache/axiom/om/impl/stream/xop/CountingOutputStream.java
similarity index 97%
rename from axiom-api/src/main/java/org/apache/axiom/util/activation/CountingOutputStream.java
rename to mixins/om-mixins/src/main/java/org/apache/axiom/om/impl/stream/xop/CountingOutputStream.java
index 91e15fcb2..ad02e78bc 100644
--- a/axiom-api/src/main/java/org/apache/axiom/util/activation/CountingOutputStream.java
+++ b/mixins/om-mixins/src/main/java/org/apache/axiom/om/impl/stream/xop/CountingOutputStream.java
@@ -16,7 +16,7 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.axiom.util.activation;
+package org.apache.axiom.om.impl.stream.xop;
 
 import java.io.IOException;
 import java.io.OutputStream;
@@ -28,7 +28,7 @@ import java.io.OutputStream;
 final class CountingOutputStream extends OutputStream {
     private final long maxSize;
     private long size;
-    
+
     CountingOutputStream(long maxSize) {
         this.maxSize = maxSize;
     }
@@ -54,7 +54,7 @@ final class CountingOutputStream extends OutputStream {
         size++;
         checkSize();
     }
-    
+
     private void checkSize() throws SizeLimitExceededException {
         if (maxSize != -1 && size > maxSize) {
             // Throw a cached exception instance to avoid the overhead of building the
@@ -66,4 +66,4 @@ final class CountingOutputStream extends OutputStream {
     long getSize() {
         return size;
     }
-}
\ No newline at end of file
+}
diff --git a/mixins/om-mixins/src/main/java/org/apache/axiom/om/impl/stream/xop/OptimizationPolicyImpl.java b/mixins/om-mixins/src/main/java/org/apache/axiom/om/impl/stream/xop/OptimizationPolicyImpl.java
index 246d21e82..680225c14 100644
--- a/mixins/om-mixins/src/main/java/org/apache/axiom/om/impl/stream/xop/OptimizationPolicyImpl.java
+++ b/mixins/om-mixins/src/main/java/org/apache/axiom/om/impl/stream/xop/OptimizationPolicyImpl.java
@@ -22,9 +22,9 @@ package org.apache.axiom.om.impl.stream.xop;
 import java.io.IOException;
 
 import org.apache.axiom.blob.Blob;
+import org.apache.axiom.ext.io.StreamCopyException;
 import org.apache.axiom.ext.stax.BlobProvider;
 import org.apache.axiom.om.OMOutputFormat;
-import org.apache.axiom.util.activation.DataHandlerUtils;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
@@ -52,11 +52,20 @@ public final class OptimizationPolicyImpl implements OptimizationPolicy {
         if (threshold == 0) {
             return true;
         }
+        long size = blob.getSize();
+        if (size != -1) {
+            return size > threshold;
+        }
         try {
-            return DataHandlerUtils.isLargerThan(DataHandlerUtils.toDataHandler(blob), threshold);
-        } catch (IOException ex) {
-            log.warn("DataHandler.writeTo(OutputStream) threw IOException", ex);
-            return true;
+            blob.writeTo(new CountingOutputStream(threshold));
+            return false;
+        } catch (StreamCopyException ex) {
+            if (ex.getOperation() == StreamCopyException.WRITE) {
+                return true;
+            } else {
+                log.warn("Blob.writeTo(OutputStream) threw IOException", ex.getCause());
+                return true;
+            }
         }
     }
 
diff --git a/axiom-api/src/main/java/org/apache/axiom/util/activation/SizeLimitExceededException.java b/mixins/om-mixins/src/main/java/org/apache/axiom/om/impl/stream/xop/SizeLimitExceededException.java
similarity index 88%
rename from axiom-api/src/main/java/org/apache/axiom/util/activation/SizeLimitExceededException.java
rename to mixins/om-mixins/src/main/java/org/apache/axiom/om/impl/stream/xop/SizeLimitExceededException.java
index 945bce647..8bd69c081 100644
--- a/axiom-api/src/main/java/org/apache/axiom/util/activation/SizeLimitExceededException.java
+++ b/mixins/om-mixins/src/main/java/org/apache/axiom/om/impl/stream/xop/SizeLimitExceededException.java
@@ -16,19 +16,17 @@
  * specific language governing permissions and limitations
  * under the License.
  */
-package org.apache.axiom.util.activation;
+package org.apache.axiom.om.impl.stream.xop;
 
 import java.io.IOException;
 
-/**
- * Exception used by {@link CountingOutputStream} if the size limit has been exceeded.
- */
+/** Exception used by {@link CountingOutputStream} if the size limit has been exceeded. */
 final class SizeLimitExceededException extends IOException {
     private static final long serialVersionUID = 1L;
 
     static final SizeLimitExceededException INSTANCE = new SizeLimitExceededException();
-    
+
     private SizeLimitExceededException() {
         setStackTrace(new StackTraceElement[0]);
     }
-}
\ No newline at end of file
+}