Author: chetanm
Date: Wed May 31 08:14:28 2017
New Revision: 1796995

URL: http://svn.apache.org/viewvc?rev=1796995&view=rev
Log:
OAK-6286 - Use BlobStore in read only mode by default unless read-write mode 
enabled

Added:
    
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/cli/ReadOnlyBlobStoreWrapper.java
   (with props)
    
jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/cli/BlobStoreFixtureProviderTest.java
   (with props)
    
jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/cli/ReadOnlyBlobStoreWrapperTest.java
   (with props)
Modified:
    
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/cli/BlobStoreFixtureProvider.java

Modified: 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/cli/BlobStoreFixtureProvider.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/cli/BlobStoreFixtureProvider.java?rev=1796995&r1=1796994&r2=1796995&view=diff
==============================================================================
--- 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/cli/BlobStoreFixtureProvider.java
 (original)
+++ 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/cli/BlobStoreFixtureProvider.java
 Wed May 31 08:14:28 2017
@@ -99,21 +99,25 @@ public class BlobStoreFixtureProvider {
             delegate.init(null);
         }
         DataStoreBlobStore blobStore = new DataStoreBlobStore(delegate);
-        return new DataStoreFixture(blobStore, closer);
+        return new DataStoreFixture(blobStore, closer, 
!options.getCommonOpts().isReadWrite());
     }
 
     private static class DataStoreFixture implements BlobStoreFixture {
         private final DataStoreBlobStore blobStore;
         private final Closer closer;
+        private final boolean readOnly;
+        private final BlobStore readOnlyBlobStore;
 
-        private DataStoreFixture(DataStoreBlobStore blobStore, Closer closer) {
+        private DataStoreFixture(DataStoreBlobStore blobStore, Closer closer, 
boolean readOnly) {
             this.blobStore = blobStore;
             this.closer = closer;
+            this.readOnly = readOnly;
+            this.readOnlyBlobStore = readOnly ? 
ReadOnlyBlobStoreWrapper.wrap(blobStore) : null;
         }
 
         @Override
         public BlobStore getBlobStore() {
-            return blobStore;
+            return readOnly ? readOnlyBlobStore : blobStore;
         }
 
         @Override
@@ -139,12 +143,7 @@ public class BlobStoreFixtureProvider {
     }
 
     private static Closeable asCloseable(final File dir) {
-        return new Closeable() {
-            @Override
-            public void close() throws IOException {
-                FileUtils.deleteDirectory(dir);
-            }
-        };
+        return () -> FileUtils.deleteDirectory(dir);
     }
 
     private static Map<String, ?> asMap(Properties props) {

Added: 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/cli/ReadOnlyBlobStoreWrapper.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/cli/ReadOnlyBlobStoreWrapper.java?rev=1796995&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/cli/ReadOnlyBlobStoreWrapper.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/cli/ReadOnlyBlobStoreWrapper.java
 Wed May 31 08:14:28 2017
@@ -0,0 +1,60 @@
+/*
+ * 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.run.cli;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Set;
+
+import com.google.common.collect.ImmutableSet;
+import org.apache.jackrabbit.oak.spi.blob.BlobStore;
+
+class ReadOnlyBlobStoreWrapper {
+
+    public static BlobStore wrap(BlobStore delegate){
+        Class[] interfaces = delegate.getClass().getInterfaces();
+        return (BlobStore) 
Proxy.newProxyInstance(ReadOnlyBlobStoreWrapper.class.getClassLoader(),
+                interfaces,
+                new ReadOnlyHandler(delegate));
+    }
+
+    private static class ReadOnlyHandler implements InvocationHandler {
+        private final Set<String> writableMethods = ImmutableSet.of(
+                "writeBlob",        //BlobStore
+                "deleteChunks",     //GarbageCollectableBlobStore
+                "countDeleteChunks" //GarbageCollectableBlobStore
+        );
+        private final BlobStore delegate;
+
+        ReadOnlyHandler(BlobStore delegate) {
+            this.delegate = delegate;
+        }
+
+        @Override
+        public Object invoke(Object proxy, Method method, Object[] args) 
throws Throwable {
+            String methodName = method.getName();
+            if (writableMethods.contains(methodName)) {
+                throw new UnsupportedOperationException("Readonly BlobStore - 
Cannot invoked " + method);
+            }
+            return method.invoke(delegate, args);
+        }
+    }
+}

Propchange: 
jackrabbit/oak/trunk/oak-run/src/main/java/org/apache/jackrabbit/oak/run/cli/ReadOnlyBlobStoreWrapper.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/cli/BlobStoreFixtureProviderTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/cli/BlobStoreFixtureProviderTest.java?rev=1796995&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/cli/BlobStoreFixtureProviderTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/cli/BlobStoreFixtureProviderTest.java
 Wed May 31 08:14:28 2017
@@ -0,0 +1,80 @@
+/*
+ * 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.run.cli;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.IOException;
+
+import joptsimple.OptionParser;
+import org.apache.jackrabbit.oak.plugins.blob.BlobTrackingStore;
+import org.apache.jackrabbit.oak.plugins.blob.datastore.TypedDataStore;
+import org.apache.jackrabbit.oak.spi.blob.BlobStore;
+import org.apache.jackrabbit.oak.spi.blob.GarbageCollectableBlobStore;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.fail;
+
+public class BlobStoreFixtureProviderTest {
+
+    @Rule
+    public final TemporaryFolder temporaryFolder = new TemporaryFolder(new 
File("target"));
+
+    @Test
+    public void fileDataStore() throws Exception{
+        String[] args = {"--fds-path", 
temporaryFolder.getRoot().getAbsolutePath(), "--read-write"};
+        try (BlobStoreFixture fixture = 
BlobStoreFixtureProvider.create(createFDSOptions(args))){
+            String blobId = fixture.getBlobStore().writeBlob(new 
ByteArrayInputStream("foo".getBytes()));
+            assertNotNull(blobId);
+        }
+    }
+
+    @Test
+    public void readOnlyFileDataStore() throws Exception{
+        String[] args = {"--fds-path", 
temporaryFolder.getRoot().getAbsolutePath()};
+        try (BlobStoreFixture fixture = 
BlobStoreFixtureProvider.create(createFDSOptions(args))){
+            try {
+                BlobStore blobStore = fixture.getBlobStore();
+
+                assertThat(blobStore, 
instanceOf(GarbageCollectableBlobStore.class));
+                assertThat(blobStore, instanceOf(TypedDataStore.class));
+                assertThat(blobStore, instanceOf(BlobTrackingStore.class));
+
+                fixture.getBlobStore().writeBlob(new 
ByteArrayInputStream("foo".getBytes()));
+                fail();
+            } catch (Exception ignore) {
+
+            }
+
+        }
+    }
+
+    private Options createFDSOptions(String... args) throws IOException {
+        OptionParser parser = new OptionParser();
+        Options opts = new Options().withDisableSystemExit();
+        opts.parseAndConfigure(parser, args);
+        return opts;
+    }
+}
\ No newline at end of file

Propchange: 
jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/cli/BlobStoreFixtureProviderTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: 
jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/cli/ReadOnlyBlobStoreWrapperTest.java
URL: 
http://svn.apache.org/viewvc/jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/cli/ReadOnlyBlobStoreWrapperTest.java?rev=1796995&view=auto
==============================================================================
--- 
jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/cli/ReadOnlyBlobStoreWrapperTest.java
 (added)
+++ 
jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/cli/ReadOnlyBlobStoreWrapperTest.java
 Wed May 31 08:14:28 2017
@@ -0,0 +1,65 @@
+/*
+ * 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.run.cli;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.InputStream;
+
+import org.apache.jackrabbit.core.data.FileDataStore;
+import org.apache.jackrabbit.oak.plugins.blob.datastore.DataStoreBlobStore;
+import org.apache.jackrabbit.oak.spi.blob.BlobStore;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import static org.junit.Assert.*;
+
+public class ReadOnlyBlobStoreWrapperTest {
+
+    @Rule
+    public final TemporaryFolder temporaryFolder = new TemporaryFolder(new 
File("target"));
+
+    @Test
+    public void readOnly() throws Exception{
+        FileDataStore fds = new FileDataStore();
+        fds.setPath(temporaryFolder.getRoot().getAbsolutePath());
+        fds.init(null);
+
+        DataStoreBlobStore writableBS = new DataStoreBlobStore(fds);
+
+        BlobStore readOnly = ReadOnlyBlobStoreWrapper.wrap(writableBS);
+
+        try {
+            readOnly.writeBlob(new ByteArrayInputStream("foo".getBytes()));
+            fail();
+        } catch (Exception ignore) {
+
+        }
+
+        String blobId = writableBS.writeBlob(new 
ByteArrayInputStream("foo".getBytes()));
+
+        try(InputStream is = readOnly.getInputStream(blobId)) {
+            assertNotNull(is);
+        }
+
+    }
+
+}
\ No newline at end of file

Propchange: 
jackrabbit/oak/trunk/oak-run/src/test/java/org/apache/jackrabbit/oak/run/cli/ReadOnlyBlobStoreWrapperTest.java
------------------------------------------------------------------------------
    svn:eol-style = native


Reply via email to