You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by re...@apache.org on 2019/10/07 11:59:33 UTC

svn commit: r1868079 - in /jackrabbit/oak/trunk: oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/ oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/io/ oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/io/ oak-run/src/ma...

Author: reschke
Date: Mon Oct  7 11:59:32 2019
New Revision: 1868079

URL: http://svn.apache.org/viewvc?rev=1868079&view=rev
Log:
OAK-8661: deprecate public LazyInputStream(ByteSource byteSource)

Added:
    jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/GuavaDeprecation.java   (with props)
Modified:
    jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/io/LazyInputStream.java
    jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/io/package-info.java
    jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/package-info.java
    jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/io/LazyInputStreamTest.java
    jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/plugins/tika/TextExtractor.java
    jackrabbit/oak/trunk/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/spi/binary/FulltextBinaryTextExtractor.java

Added: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/GuavaDeprecation.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/GuavaDeprecation.java?rev=1868079&view=auto
==============================================================================
--- jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/GuavaDeprecation.java (added)
+++ jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/GuavaDeprecation.java Mon Oct  7 11:59:32 2019
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.jackrabbit.oak.commons;
+
+import java.util.Locale;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class GuavaDeprecation {
+
+    private static final Logger LOG = LoggerFactory.getLogger(GuavaDeprecation.class);
+
+    private static final String DEFAULT = "debug";
+
+    private static final String TLOGLEVEL = System.getProperty(GuavaDeprecation.class + ".LOGLEVEL", DEFAULT);
+
+    private static String LOGLEVEL;
+
+    static {
+        String t;
+
+        switch (TLOGLEVEL.toLowerCase(Locale.ENGLISH)) {
+            case "error":
+            case "warn":
+            case "info":
+            case "debug":
+                t = TLOGLEVEL.toLowerCase(Locale.ENGLISH);
+                break;
+            default:
+                t = DEFAULT;
+                break;
+        }
+
+        LOGLEVEL = t;
+    }
+
+    private GuavaDeprecation() {
+    }
+
+    public static void handleCall(String ticket) throws UnsupportedOperationException {
+        String message = "use of deprecated Guava-related API - this method is going to be removed in future Oak releases - see %s for details";
+
+        switch (LOGLEVEL) {
+            case "error":
+                if (LOG.isErrorEnabled()) {
+                    LOG.error(String.format(message, ticket), new Exception("call stack"));
+                }
+                break;
+            case "warn":
+                if (LOG.isWarnEnabled()) {
+                    LOG.warn(String.format(message, ticket), new Exception("call stack"));
+                }
+                break;
+            case "info":
+                if (LOG.isInfoEnabled()) {
+                    LOG.info(String.format(message, ticket), new Exception("call stack"));
+                }
+                break;
+            case "debug":
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug(String.format(message, ticket), new Exception("call stack"));
+                }
+                break;
+        }
+    }
+
+    // for testing
+    public static String setLogLevel(String level) {
+        String before = LOGLEVEL;
+        LOGLEVEL = level;
+        return before;
+    }
+}

Propchange: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/GuavaDeprecation.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/io/LazyInputStream.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/io/LazyInputStream.java?rev=1868079&r1=1868078&r2=1868079&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/io/LazyInputStream.java (original)
+++ jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/io/LazyInputStream.java Mon Oct  7 11:59:32 2019
@@ -21,20 +21,37 @@ package org.apache.jackrabbit.oak.common
 
 import java.io.FilterInputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.util.function.Supplier;
 
 import com.google.common.io.ByteSource;
 import org.apache.commons.io.input.ClosedInputStream;
+import org.apache.jackrabbit.oak.commons.GuavaDeprecation;
 
 /**
- * * This input stream delays accessing the ByteSource until the first byte is read
+ * * This input stream delays accessing the {@link InputStream} until the first byte is read
  */
 public class LazyInputStream extends FilterInputStream {
+
     private final ByteSource byteSource;
+    private final Supplier<InputStream> inputStreamSupplier;
+
     private boolean opened;
 
-    public LazyInputStream(ByteSource byteSource) {
+    public LazyInputStream(Supplier<InputStream> inputStreamSupplier) {
+        super(null);
+        this.byteSource = null;
+        this.inputStreamSupplier = inputStreamSupplier;
+    }
+
+    /**
+     * @deprecated Use {@link #LazyInputStream(Supplier<InputStream>)} instead
+     */
+    @Deprecated public LazyInputStream(ByteSource byteSource) {
         super(null);
         this.byteSource = byteSource;
+        this.inputStreamSupplier = null;
+        GuavaDeprecation.handleCall("OAK-8661");
     }
 
     @Override
@@ -101,7 +118,7 @@ public class LazyInputStream extends Fil
     private void ensureOpen() throws IOException {
         if (!opened) {
             opened = true;
-            in = byteSource.openStream();
+            in = inputStreamSupplier != null ? inputStreamSupplier.get() : byteSource.openStream();
         }
     }
 

Modified: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/io/package-info.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/io/package-info.java?rev=1868079&r1=1868078&r2=1868079&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/io/package-info.java (original)
+++ jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/io/package-info.java Mon Oct  7 11:59:32 2019
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-@Version("1.0.0")
+@Version("1.1.0")
 package org.apache.jackrabbit.oak.commons.io;
 
 import org.osgi.annotation.versioning.Version;
\ No newline at end of file

Modified: jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/package-info.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/package-info.java?rev=1868079&r1=1868078&r2=1868079&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/package-info.java (original)
+++ jackrabbit/oak/trunk/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/package-info.java Mon Oct  7 11:59:32 2019
@@ -14,7 +14,7 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-@Version("1.5.0")
+@Version("1.6.0")
 package org.apache.jackrabbit.oak.commons;
 
 import org.osgi.annotation.versioning.Version;

Modified: jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/io/LazyInputStreamTest.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/io/LazyInputStreamTest.java?rev=1868079&r1=1868078&r2=1868079&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/io/LazyInputStreamTest.java (original)
+++ jackrabbit/oak/trunk/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/io/LazyInputStreamTest.java Mon Oct  7 11:59:32 2019
@@ -25,31 +25,116 @@ import static org.junit.Assert.assertFal
 import static org.junit.Assert.fail;
 
 import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Locale;
+import java.util.function.Supplier;
 
+import org.apache.jackrabbit.oak.commons.GuavaDeprecation;
+import org.apache.jackrabbit.oak.commons.junit.LogCustomizer;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
+import org.slf4j.event.Level;
 
 /**
  * Tests the LazyInputStream class.
  */
 public class LazyInputStreamTest {
-    
+
     private File file;
-    
+
     @Rule
     public final TemporaryFolder temporaryFolder = new TemporaryFolder(new File("target"));
 
     @Test
     public void test() throws IOException {
         createFile();
-        
+
+        // test open / close (without reading)
+        LazyInputStream in = new LazyInputStream(asInputStreamSupplier(file));
+        in.close();
+
+        // test reading too much and closing too much
+        in = new LazyInputStream(asInputStreamSupplier(file));
+        assertEquals(0, in.read());
+        assertEquals(-1, in.read());
+        assertEquals(-1, in.read());
+        assertEquals(-1, in.read());
+        in.close();
+        in.close();
+        in.close();
+
+        // test markSupported, mark, and reset
+        in = new LazyInputStream(asInputStreamSupplier(file));
+        assertFalse(in.markSupported());
+        in.mark(1);
+        assertEquals(0, in.read());
+        try {
+            in.reset();
+            fail();
+        } catch (IOException e) {
+            // expected
+        }
+        assertEquals(-1, in.read());
+        in.close();
+
+        // test read(byte[])
+        in = new LazyInputStream(asInputStreamSupplier(file));
+        byte[] test = new byte[2];
+        assertEquals(1, in.read(test));
+        in.close();
+
+        // test read(byte[],int,int)
+        in = new LazyInputStream(asInputStreamSupplier(file));
+        assertEquals(1, in.read(test, 0, 2));
+        in.close();
+
+        // test skip
+        in = new LazyInputStream(asInputStreamSupplier(file));
+        assertEquals(2, in.skip(2));
+        assertEquals(-1, in.read(test));
+        in.close();
+
+        createFile();
+
+        // test that the file is closed after reading the last byte
+        in = new LazyInputStream(asInputStreamSupplier(file));
+        assertEquals(0, in.read());
+        assertEquals(-1, in.read());
+
+        in.close();
+
+        file.delete();
+    }
+
+    @Test
+    public void testDeprecatedLogs() throws IOException {
+        for (Level level : new Level[] { Level.DEBUG, Level.INFO, Level.WARN, Level.ERROR }) {
+            LogCustomizer lc = LogCustomizer.forLogger(GuavaDeprecation.class).enable(level).create();
+            lc.starting();
+            String defaultLevel = GuavaDeprecation.setLogLevel(level.toString().toLowerCase(Locale.ENGLISH));
+            try {
+                testDeprecated();
+                assertEquals(7, lc.getLogs().size());
+            } finally {
+                lc.finished();
+                GuavaDeprecation.setLogLevel(defaultLevel);
+            }
+        }
+    }
+
+    private void testDeprecated() throws IOException {
+        createFile();
+
         // test open / close (without reading)
         LazyInputStream in = new LazyInputStream(asByteSource(file));
         in.close();
-        
+
         // test reading too much and closing too much
         in = new LazyInputStream(asByteSource(file));
         assertEquals(0, in.read());
@@ -73,17 +158,17 @@ public class LazyInputStreamTest {
         }
         assertEquals(-1, in.read());
         in.close();
-        
+
         // test read(byte[])
         in = new LazyInputStream(asByteSource(file));
         byte[] test = new byte[2];
         assertEquals(1, in.read(test));
-        in.close();        
-        
+        in.close();
+
         // test read(byte[],int,int)
         in = new LazyInputStream(asByteSource(file));
         assertEquals(1, in.read(test, 0, 2));
-        in.close();        
+        in.close();
 
         // test skip
         in = new LazyInputStream(asByteSource(file));
@@ -92,7 +177,7 @@ public class LazyInputStreamTest {
         in.close();
 
         createFile();
-        
+
         // test that the file is closed after reading the last byte
         in = new LazyInputStream(asByteSource(file));
         assertEquals(0, in.read());
@@ -101,7 +186,20 @@ public class LazyInputStreamTest {
         in.close();
 
         file.delete();
-        
+    }
+
+    private static Supplier<InputStream> asInputStreamSupplier(final File file) {
+        return new Supplier<InputStream> () {
+            @Override
+            public InputStream get() {
+                try {
+                    return new FileInputStream(file);
+                }
+                catch (FileNotFoundException ex) {
+                    throw new IllegalStateException(ex);
+                }
+            }
+        };
     }
 
     private void createFile() throws IOException {
@@ -110,5 +208,4 @@ public class LazyInputStreamTest {
         out.write(new byte[1]);
         out.close();
     }
-
 }

Modified: jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/plugins/tika/TextExtractor.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/plugins/tika/TextExtractor.java?rev=1868079&r1=1868078&r2=1868079&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/plugins/tika/TextExtractor.java (original)
+++ jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/plugins/tika/TextExtractor.java Mon Oct  7 11:59:32 2019
@@ -22,6 +22,7 @@ package org.apache.jackrabbit.oak.plugin
 import java.io.Closeable;
 import java.io.File;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.PrintWriter;
 import java.io.StringWriter;
 import java.util.concurrent.ArrayBlockingQueue;
@@ -31,6 +32,7 @@ import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
+import java.util.function.Supplier;
 
 import com.google.common.io.ByteSource;
 import com.google.common.io.CountingInputStream;
@@ -245,7 +247,18 @@ class TextExtractor implements Closeable
         long start = System.currentTimeMillis();
         long size = 0;
         try {
-            CountingInputStream stream = new CountingInputStream(new LazyInputStream(byteSource));
+            Supplier<InputStream> inputStreamSupplier = new Supplier<InputStream> () {
+                @Override
+                public InputStream get() {
+                    try {
+                        return byteSource.openStream();
+                    }
+                    catch (IOException ex) {
+                        throw new IllegalStateException(ex);
+                    }
+                }
+            };
+            CountingInputStream stream = new CountingInputStream(new LazyInputStream(inputStreamSupplier));
             try {
                 tika.getParser().parse(stream, handler, metadata, new ParseContext());
             } finally {

Modified: jackrabbit/oak/trunk/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/spi/binary/FulltextBinaryTextExtractor.java
URL: http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/spi/binary/FulltextBinaryTextExtractor.java?rev=1868079&r1=1868078&r2=1868079&view=diff
==============================================================================
--- jackrabbit/oak/trunk/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/spi/binary/FulltextBinaryTextExtractor.java (original)
+++ jackrabbit/oak/trunk/oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/spi/binary/FulltextBinaryTextExtractor.java Mon Oct  7 11:59:32 2019
@@ -152,7 +152,7 @@ public class FulltextBinaryTextExtractor
       log.debug("Extracting {}, {} bytes, id {}", path, length, v.getContentIdentity());
     }
     try {
-      CountingInputStream stream = new CountingInputStream(new LazyInputStream(new BlobByteSource(v)));
+      CountingInputStream stream = new CountingInputStream(new LazyInputStream(() -> v.getNewStream()));
       try {
         if (length > SMALL_BINARY) {
           String name = "Extracting " + path + ", " + length + " bytes";