1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.mycore.datamodel.niofs.ifs2;
20
21 import java.io.IOException;
22 import java.nio.file.FileStore;
23 import java.nio.file.Files;
24 import java.nio.file.NoSuchFileException;
25 import java.nio.file.Path;
26 import java.nio.file.attribute.FileAttributeView;
27 import java.nio.file.attribute.FileStoreAttributeView;
28 import java.util.concurrent.ExecutionException;
29
30 import org.apache.logging.log4j.LogManager;
31 import org.mycore.datamodel.ifs2.MCRFile;
32 import org.mycore.datamodel.ifs2.MCRStore;
33 import org.mycore.datamodel.ifs2.MCRStoreManager;
34 import org.mycore.datamodel.ifs2.MCRStoredNode;
35 import org.mycore.datamodel.niofs.MCRAbstractFileStore;
36 import org.mycore.datamodel.niofs.MCRPath;
37
38 import com.google.common.cache.CacheBuilder;
39 import com.google.common.cache.CacheLoader;
40 import com.google.common.cache.LoadingCache;
41
42 public class MCRFileStore extends MCRAbstractFileStore {
43
44 private MCRStore contentStore;
45
46 private String baseId;
47
48 private FileStore baseFileStore;
49
50 private static LoadingCache<String, MCRFileStore> instanceHolder = CacheBuilder.newBuilder().weakKeys()
51 .build(new CacheLoader<String, MCRFileStore>() {
52
53 @Override
54 public MCRFileStore load(String contentStoreID) throws Exception {
55 MCRStore store = MCRStoreManager.getStore(contentStoreID);
56 return new MCRFileStore(store);
57 }
58 });
59
60 private MCRFileStore(MCRStore contentStore) throws IOException {
61 this.contentStore = contentStore;
62 Path baseDir = this.contentStore.getBaseDirectory();
63 this.baseFileStore = getFileStore(baseDir);
64 this.baseId = contentStore.getID().substring(MCRFileSystemUtils.STORE_ID_PREFIX.length());
65 if (baseFileStore == null) {
66 String reason = "Cannot access base directory or any parent directory of Content Store "
67 + contentStore.getID();
68 throw new NoSuchFileException(baseDir.toString(), null, reason);
69 }
70 }
71
72 private static FileStore getFileStore(Path baseDir) throws IOException {
73 if (baseDir == null) {
74 return null;
75 }
76
77 return Files.exists(baseDir) ? Files.getFileStore(baseDir) : getFileStore(baseDir.getParent());
78 }
79
80 static MCRFileStore getInstance(String storeId) throws IOException {
81 try {
82 return instanceHolder.get(storeId);
83 } catch (ExecutionException e) {
84 Throwable cause = e.getCause();
85 if (cause instanceof IOException) {
86 throw (IOException) cause;
87 }
88 throw new IOException("Error while geting instance of " + MCRFileStore.class.getSimpleName(), cause);
89 }
90 }
91
92 public static MCRFileStore getInstance(MCRStoredNode node) throws IOException {
93 return getInstance(node.getRoot().getStore().getID());
94 }
95
96 @Override
97 public String name() {
98 return contentStore.getID();
99 }
100
101 @Override
102 public String type() {
103 return contentStore.getClass().getCanonicalName();
104 }
105
106 @Override
107 public boolean isReadOnly() {
108 return baseFileStore.isReadOnly();
109 }
110
111 @Override
112 public long getTotalSpace() throws IOException {
113 return baseFileStore.getTotalSpace();
114 }
115
116 @Override
117 public long getUsableSpace() throws IOException {
118 return baseFileStore.getUsableSpace();
119 }
120
121 @Override
122 public long getUnallocatedSpace() throws IOException {
123 return baseFileStore.getUnallocatedSpace();
124 }
125
126 @Override
127 public boolean supportsFileAttributeView(Class<? extends FileAttributeView> type) {
128 return this.baseFileStore.supportsFileAttributeView(type);
129 }
130
131 @Override
132 public boolean supportsFileAttributeView(String name) {
133 return this.baseFileStore.supportsFileAttributeView(name);
134 }
135
136 @Override
137 public <V extends FileStoreAttributeView> V getFileStoreAttributeView(Class<V> type) {
138 return this.baseFileStore.getFileStoreAttributeView(type);
139 }
140
141 @Override
142 public Object getAttribute(String attribute) throws IOException {
143 return this.baseFileStore.getAttribute(attribute);
144 }
145
146 @Override
147 public Path getBaseDirectory() throws IOException {
148 return this.contentStore.getBaseDirectory();
149 }
150
151 @Override
152 public Path getPhysicalPath(MCRPath path) {
153 MCRFileSystemUtils.checkPathAbsolute(path);
154 LogManager.getLogger().warn("Checking if {} is of baseId {}", path, baseId);
155 if (!path.getOwner().startsWith(baseId + "_")) {
156 LogManager.getLogger().warn("{} is not of baseId {}", path, baseId);
157 return null;
158 }
159 try {
160 MCRFile mcrFile = MCRFileSystemUtils.getMCRFile(path, false, false, true);
161 return mcrFile.getLocalPath();
162 } catch (IOException e) {
163 LogManager.getLogger(getClass()).info(e.getMessage());
164 return null;
165 }
166 }
167
168 }