Index: oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/FileDataStoreService.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/FileDataStoreService.java	(revision 20afca5d74e00ef6193bfe9548fca981d48bdaa3)
+++ oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/FileDataStoreService.java	(revision ab4fbd79df60ea9c0653a7b13f1da3c41fe7adb7)
@@ -53,7 +53,9 @@
             String fsBackendPath = PropertiesUtil.toString(config.get(PATH), null);
             Preconditions.checkNotNull(fsBackendPath, "Cannot create " +
                     "FileDataStoreService with caching. [{path}] property not configured.");
-            CachingFDS dataStore = new CachingFDS();
+            OakCachingFDS dataStore = new OakCachingFDS();
+            dataStore.setFsBackendPath(fsBackendPath);
+
             // Disabling asyncUpload by default
             dataStore.setAsyncUploadLimit(PropertiesUtil.toInteger(config.get("asyncUploadLimit"), 0));
             config.remove(PATH);
Index: oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/OakCachingFDS.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/OakCachingFDS.java	(revision ab4fbd79df60ea9c0653a7b13f1da3c41fe7adb7)
+++ oak-core/src/main/java/org/apache/jackrabbit/oak/plugins/blob/datastore/OakCachingFDS.java	(revision ab4fbd79df60ea9c0653a7b13f1da3c41fe7adb7)
@@ -0,0 +1,58 @@
+/*
+ * 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.plugins.blob.datastore;
+
+import java.io.File;
+import java.io.IOException;
+import java.security.SecureRandom;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.jackrabbit.core.data.CachingFDS;
+import org.apache.jackrabbit.core.data.DataStoreException;
+
+/**
+ * Overrides the implementation of
+ * {@link org.apache.jackrabbit.core.data.CachingDataStore#getOrCreateReferenceKey}.
+ */
+public class OakCachingFDS extends CachingFDS {
+    /** The path for FS Backend **/
+    private String fsBackendPath;
+
+    public void setFsBackendPath(String fsBackendPath) {
+        this.fsBackendPath = fsBackendPath;
+    }
+
+    @Override
+    protected byte[] getOrCreateReferenceKey() throws DataStoreException {
+        File file = new File(fsBackendPath, "reference.key");
+        try {
+            if (file.exists()) {
+                return FileUtils.readFileToByteArray(file);
+            } else {
+                byte[] key = new byte[256];
+                new SecureRandom().nextBytes(key);
+                FileUtils.writeByteArrayToFile(file, key);
+                return key;
+            }
+        } catch (IOException e) {
+            throw new DataStoreException(
+                "Unable to access reference key file " + file.getPath(), e);
+        }
+    }
+}
Index: oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/datastore/OakCachingFDSTest.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/datastore/OakCachingFDSTest.java	(revision ab4fbd79df60ea9c0653a7b13f1da3c41fe7adb7)
+++ oak-core/src/test/java/org/apache/jackrabbit/oak/plugins/blob/datastore/OakCachingFDSTest.java	(revision ab4fbd79df60ea9c0653a7b13f1da3c41fe7adb7)
@@ -0,0 +1,118 @@
+/*
+ * 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.plugins.blob.datastore;
+
+import java.io.File;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.jackrabbit.core.data.DataStore;
+import org.apache.jackrabbit.oak.spi.blob.BlobStore;
+import org.apache.jackrabbit.oak.stats.StatisticsProvider;
+import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import static com.google.common.collect.Maps.newHashMap;
+import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertNotNull;
+
+/**
+ * Tests {@link OakCachingFDS} and OSGi registration using {@link FileDataStoreService}.
+ */
+public class OakCachingFDSTest {
+    @Rule
+    public TemporaryFolder folder = new TemporaryFolder(new File("target"));
+
+    @Rule
+    public OsgiContext context = new OsgiContext();
+
+    @Before
+    public void setUp() {
+    }
+
+    private OakCachingFDS dataStore;
+
+    private String fsBackendPath;
+
+    private String path;
+
+    @Before
+    public void setup() throws Exception {
+        fsBackendPath = folder.newFolder().getAbsolutePath();
+        path = folder.newFolder().getAbsolutePath();
+    }
+
+    @Test
+    public void createAndCheckReferenceKey() throws Exception {
+        createCachingFDS();
+        assertReferenceKey();
+    }
+
+    @Test
+    public void registerAndCheckReferenceKey() throws Exception {
+        context.registerService(StatisticsProvider.class, StatisticsProvider.NOOP);
+        registerCachingFDS();
+        assertReferenceKey();
+    }
+
+    public void assertReferenceKey() throws Exception {
+        byte[] key = dataStore.getOrCreateReferenceKey();
+
+        // Check bytes retrieved from reference.key file
+        File refFile = new File(fsBackendPath, "reference.key");
+        byte[] keyRet = FileUtils.readFileToByteArray(refFile);
+        assertArrayEquals(key, keyRet);
+
+        assertArrayEquals(key, dataStore.getOrCreateReferenceKey());
+    }
+
+    private void registerCachingFDS() {
+        Map<String, Object> props = newHashMap();
+        props.put("cachePath", path);
+        props.put("path", fsBackendPath);
+        props.put("cacheSize", "10");
+
+        context.registerInjectActivateService(new FileDataStoreService(), props);
+        assertNotNull(context.getService(BlobStore.class));
+        BlobStore blobStore = context.getService(BlobStore.class);
+        assert blobStore instanceof DataStoreBlobStore;
+
+        DataStore ds = ((DataStoreBlobStore) blobStore).getDataStore();
+        assert ds instanceof OakCachingFDS;
+        dataStore = (OakCachingFDS) ds;
+    }
+
+    private void createCachingFDS() throws Exception {
+        Properties props = new Properties();
+        props.put("fsBackendPath", fsBackendPath);
+        props.put("path", path);
+        props.put("cacheSize", "10");
+
+        dataStore = new OakCachingFDS();
+        dataStore.setFsBackendPath(fsBackendPath);
+        dataStore.setAsyncUploadLimit(0);
+        dataStore.setProperties(props);
+        dataStore.init(folder.newFolder().getAbsolutePath());
+    }
+}
