Author: rwesten
Date: Tue Jan 22 08:42:54 2013
New Revision: 1436798

URL: http://svn.apache.org/viewvc?rev=1436798&view=rev
Log:
fix for STANBOL-898: The StreamSource does no longer close the parsed 
InputStream. In addition it returns an non-closeable InputStream on calls to 
#getStream() to ensure that also Users that obtain an reference to the parsed 
stream are not able to close it. The Documentation of the create** methods in 
the ContentItemFactory where also updated as requested by the issue; Minor: 
changed references to the deprecated junit.framework.Assert class to 
org.junit.Assert

Modified:
    
stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentItemFactory.java
    
stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/impl/StreamSource.java
    
stanbol/trunk/enhancer/generic/servicesapi/src/test/java/org/apache/stanbol/enhancer/serviceapi/impl/ContentSourceTest.java

Modified: 
stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentItemFactory.java
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentItemFactory.java?rev=1436798&r1=1436797&r2=1436798&view=diff
==============================================================================
--- 
stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentItemFactory.java
 (original)
+++ 
stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/ContentItemFactory.java
 Tue Jan 22 08:42:54 2013
@@ -36,7 +36,14 @@ public interface ContentItemFactory {
      * generates as unique ID from the passed content.
      * Implementors might want to use
      * {@link ContentItemHelper#streamDigest(InputStream, 
java.io.OutputStream, String)
-     * for generating an ID while reading the data from the ContentSource.
+     * for generating an ID while reading the data from the ContentSource.<p>
+     * The content provided by the {@link ContentSource} is consumed by the
+     * this method and stored as {@link Blob} with the returned {@link 
ContentItem}.
+     * In addition implementors need to ensure that this {@link Blob} is also
+     * used by the {@link ContentItem#getBlob()}, {@link 
ContentItem#getStream()},
+     * and {@link ContentItem#getMimeType()} methods.<p>
+     * Callers can safely close any resource related to the parsed {@link 
ContentSource}
+     * method after this method returns.
      * @param source The content source
      * @return the {@link ContentItem} with a generated id and the passed
      * content as content-part of type {@link Blob} at index <code>0</code>
@@ -54,7 +61,14 @@ public interface ContentItemFactory {
      * values should typically end with an separator char (e.g. '/', '#', ':').
      * Implementors might want to use
      * {@link 
org.apache.stanbol.enhancer.servicesapi.helper.ContentItemHelper#streamDigest(InputStream,
 java.io.OutputStream, String)
-     * for generating an ID while reading the data from the ContentSource.
+     * for generating an ID while reading the data from the ContentSource.<p>
+     * The content provided by the {@link ContentSource} is consumed by the
+     * this method and stored as {@link Blob} with the returned {@link 
ContentItem}.
+     * In addition implementors need to ensure that this {@link Blob} is also
+     * used by the {@link ContentItem#getBlob()}, {@link 
ContentItem#getStream()},
+     * and {@link ContentItem#getMimeType()} methods.<p>
+     * Callers can safely close any resource related to the parsed {@link 
ContentSource}
+     * method after this method returns.
      * @param source The content source
      * @return the {@link ContentItem} with a generated id and the passed
      * content as content-part of type {@link Blob} at index <code>0</code>
@@ -70,7 +84,14 @@ public interface ContentItemFactory {
      * @param id the id for the ContentItem or <code>null</code> to generate 
an id.
      * If <code>null</code> is passed as ID, implementors might want to use
      * {@link ContentItemHelper#streamDigest(InputStream, 
java.io.OutputStream, String)
-     * for generating an ID while reading the data from the ContentSource.
+     * for generating an ID while reading the data from the ContentSource.<p>
+     * The content provided by the {@link ContentSource} is consumed by the
+     * this method and stored as {@link Blob} with the returned {@link 
ContentItem}.
+     * In addition implementors need to ensure that this {@link Blob} is also
+     * used by the {@link ContentItem#getBlob()}, {@link 
ContentItem#getStream()},
+     * and {@link ContentItem#getMimeType()} methods.<p>
+     * Callers can safely close any resource related to the parsed {@link 
ContentSource}
+     * method after this method returns.
      * @param source The content source
      * @return the {@link ContentItem} with a passed/generated id and the 
passed
      * content as content-part of type {@link Blob} at index <code>0</code>
@@ -88,7 +109,14 @@ public interface ContentItemFactory {
      * values should typically end with an separator char (e.g. '/', '#', ':').
      * Implementors might want to use
      * {@link ContentItemHelper#streamDigest(InputStream, 
java.io.OutputStream, String)
-     * for generating an ID while reading the data from the ContentSource
+     * for generating an ID while reading the data from the ContentSource.<p>
+     * The content provided by the {@link ContentSource} is consumed by the
+     * this method and stored as {@link Blob} with the returned {@link 
ContentItem}.
+     * In addition implementors need to ensure that this {@link Blob} is also
+     * used by the {@link ContentItem#getBlob()}, {@link 
ContentItem#getStream()},
+     * and {@link ContentItem#getMimeType()} methods.<p>
+     * Callers can safely close any resource related to the parsed {@link 
ContentSource}
+     * method after this method returns.
      * @param source The content source
      * @param metadata an {@link MGraph} with the metadata or <code>null</code>
      * if none. Implementation are free to use the passed instance or to 
generate 
@@ -109,7 +137,14 @@ public interface ContentItemFactory {
      * @param id the id for the ContentItem or <code>null</code> to generate 
an id.
      * If <code>null</code> is passed as ID, implementors might want to use
      * {@link ContentItemHelper#streamDigest(InputStream, 
java.io.OutputStream, String)
-     * for generating an ID while reading the data from the ContentSource.
+     * for generating an ID while reading the data from the ContentSource.<p>
+     * The content provided by the {@link ContentSource} is consumed by the
+     * this method and stored as {@link Blob} with the returned {@link 
ContentItem}.
+     * In addition implementors need to ensure that this {@link Blob} is also
+     * used by the {@link ContentItem#getBlob()}, {@link 
ContentItem#getStream()},
+     * and {@link ContentItem#getMimeType()} methods.<p>
+     * Callers can safely close any resource related to the parsed {@link 
ContentSource}
+     * method after this method returns.
      * @param source The content source
      * @param metadata an {@link MGraph} with the metadata or <code>null</code>
      * if none. Implementation are free to use the passed instance or to 
generate 
@@ -163,7 +198,11 @@ public interface ContentItemFactory {
      */
     ContentItem createContentItem(ContentReference reference, MGraph metadata) 
throws IOException;
     /**
-     * Creates a new Blob based on the passed {@link ContentSource}
+     * Creates a new Blob based on the passed {@link ContentSource}<p>
+     * The content provided by the {@link ContentSource} is consumed by the
+     * this method and stored in the returned {@link Blob}. Callers can safely 
+     * close any resource related to the parsed {@link ContentSource}
+     * method after this method returns.
      * @param source The source 
      * @return the Blob
      * @throws IllegalArgumentException of the passed source is 
<code>null</code>

Modified: 
stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/impl/StreamSource.java
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/impl/StreamSource.java?rev=1436798&r1=1436797&r2=1436798&view=diff
==============================================================================
--- 
stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/impl/StreamSource.java
 (original)
+++ 
stanbol/trunk/enhancer/generic/servicesapi/src/main/java/org/apache/stanbol/enhancer/servicesapi/impl/StreamSource.java
 Tue Jan 22 08:42:54 2013
@@ -17,6 +17,7 @@
 package org.apache.stanbol.enhancer.servicesapi.impl;
 
 import java.io.ByteArrayInputStream;
+import java.io.FilterInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.Collections;
@@ -126,17 +127,12 @@ public class StreamSource implements Con
             throw new IllegalStateException("This InputStream of this 
ContentSource is already consumed!");
         }
         consumed = true;
-        return in;
+        return new NonCloseableInputStream(in);
     }
     @Override
-    public byte[] getData() throws IOException {
+    public synchronized byte[] getData() throws IOException {
         if(data == null){
-            InputStream in = getStream();
-            try {
-                data = IOUtils.toByteArray(in);
-            } finally {
-                IOUtils.closeQuietly(in);
-            }
+            data = IOUtils.toByteArray(in);
         }
         return data;
     }
@@ -154,9 +150,26 @@ public class StreamSource implements Con
     public Map<String,List<String>> getHeaders() {
         return headers;
     }
-    @Override
-    protected void finalize() throws Throwable {
-        IOUtils.closeQuietly(in);
-    }
+// The parsed Stream MUST NOT be closed (STANBOL-898)
+//    @Override
+//    protected void finalize() throws Throwable {
+//        IOUtils.closeQuietly(in);
+//    }
     
+    /**
+     * Internally used to ensure that InputStreams returned by
+     * {@link StreamSource#getStream()} can not be closed by callers (as 
+     * requested by STANBOL-898)
+     */
+    private static class NonCloseableInputStream extends FilterInputStream {
+
+        protected NonCloseableInputStream(InputStream in) {
+            super(in);
+        }
+    
+        @Override
+        public void close() throws IOException {
+            //ignore call to close
+        }
+    }
 }
\ No newline at end of file

Modified: 
stanbol/trunk/enhancer/generic/servicesapi/src/test/java/org/apache/stanbol/enhancer/serviceapi/impl/ContentSourceTest.java
URL: 
http://svn.apache.org/viewvc/stanbol/trunk/enhancer/generic/servicesapi/src/test/java/org/apache/stanbol/enhancer/serviceapi/impl/ContentSourceTest.java?rev=1436798&r1=1436797&r2=1436798&view=diff
==============================================================================
--- 
stanbol/trunk/enhancer/generic/servicesapi/src/test/java/org/apache/stanbol/enhancer/serviceapi/impl/ContentSourceTest.java
 (original)
+++ 
stanbol/trunk/enhancer/generic/servicesapi/src/test/java/org/apache/stanbol/enhancer/serviceapi/impl/ContentSourceTest.java
 Tue Jan 22 08:42:54 2013
@@ -16,11 +16,11 @@
 */
 package org.apache.stanbol.enhancer.serviceapi.impl;
 
-import static junit.framework.Assert.assertEquals;
-import static junit.framework.Assert.assertNotNull;
-import static junit.framework.Assert.assertNull;
-import static junit.framework.Assert.assertSame;
-import static junit.framework.Assert.assertTrue;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -31,7 +31,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import junit.framework.Assert;
+import org.junit.Assert;
 
 import org.apache.commons.io.IOUtils;
 import org.apache.stanbol.enhancer.servicesapi.ContentSource;


Reply via email to