]> git.argeo.org Git - lgpl/argeo-commons.git/blob - jcr/org.argeo.cms.jcr/src/org/argeo/cms/internal/jcr/LocalFsDataStore.java
Improve migration
[lgpl/argeo-commons.git] / jcr / org.argeo.cms.jcr / src / org / argeo / cms / internal / jcr / LocalFsDataStore.java
1 package org.argeo.cms.internal.jcr;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.nio.file.Files;
6 import java.nio.file.Paths;
7
8 import org.apache.jackrabbit.core.data.DataIdentifier;
9 import org.apache.jackrabbit.core.data.DataRecord;
10 import org.apache.jackrabbit.core.data.DataStoreException;
11 import org.apache.jackrabbit.core.data.FileDataStore;
12
13 /**
14 * <b>experimental</b> Duplicate added entries in another directory (typically a
15 * remote mount).
16 */
17 @SuppressWarnings("restriction")
18 public class LocalFsDataStore extends FileDataStore {
19 String redundantPath;
20 FileDataStore redundantStore;
21
22 @Override
23 public void init(String homeDir) {
24 // init primary first
25 super.init(homeDir);
26
27 if (redundantPath != null) {
28 // redundant directory must be created first
29 // TODO implement some polling?
30 if (Files.exists(Paths.get(redundantPath))) {
31 redundantStore = new FileDataStore();
32 redundantStore.setPath(redundantPath);
33 redundantStore.init(homeDir);
34 }
35 }
36 }
37
38 @Override
39 public DataRecord addRecord(InputStream input) throws DataStoreException {
40 DataRecord dataRecord = super.addRecord(input);
41 syncRedundantRecord(dataRecord);
42 return dataRecord;
43 }
44
45 @Override
46 public DataRecord getRecord(DataIdentifier identifier) throws DataStoreException {
47 DataRecord dataRecord = super.getRecord(identifier);
48 syncRedundantRecord(dataRecord);
49 return dataRecord;
50 }
51
52 protected void syncRedundantRecord(DataRecord dataRecord) throws DataStoreException {
53 if (redundantStore == null)
54 return;
55 if (redundantStore.getRecordIfStored(dataRecord.getIdentifier()) == null) {
56 try (InputStream redundant = dataRecord.getStream()) {
57 redundantStore.addRecord(redundant);
58 } catch (IOException e) {
59 throw new DataStoreException("Cannot add redundant record.", e);
60 }
61 }
62 }
63
64 public void setRedundantPath(String redundantPath) {
65 this.redundantPath = redundantPath;
66 }
67
68 }