View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.datamodel.niofs.ifs2;
20  
21  import java.io.IOException;
22  import java.nio.file.DirectoryNotEmptyException;
23  import java.nio.file.FileAlreadyExistsException;
24  import java.nio.file.FileStore;
25  import java.nio.file.FileSystemException;
26  import java.nio.file.Path;
27  import java.util.Comparator;
28  import java.util.Objects;
29  
30  import org.apache.logging.log4j.LogManager;
31  import org.mycore.datamodel.ifs2.MCRDirectory;
32  import org.mycore.datamodel.ifs2.MCRStore;
33  import org.mycore.datamodel.ifs2.MCRStoreCenter;
34  import org.mycore.datamodel.metadata.MCRObjectID;
35  import org.mycore.datamodel.niofs.MCRAbstractFileSystem;
36  import org.mycore.datamodel.niofs.MCRPath;
37  
38  /**
39   * @author Thomas Scheffler (yagee)
40   *
41   */
42  public class MCRIFSFileSystem extends MCRAbstractFileSystem {
43  
44      private MCRFileSystemProvider provider;
45  
46      MCRIFSFileSystem(MCRFileSystemProvider provider) {
47          super();
48          this.provider = provider;
49      }
50  
51      /* (non-Javadoc)
52       * @see java.nio.file.FileSystem#provider()
53       */
54      @Override
55      public MCRFileSystemProvider provider() {
56          return provider;
57      }
58  
59      /* (non-Javadoc)
60       * @see java.nio.file.FileSystem#getRootDirectories()
61       */
62      @Override
63      public Iterable<Path> getRootDirectories() {
64          return MCRStoreCenter.instance().getCurrentStores(org.mycore.datamodel.ifs2.MCRFileStore.class)
65              .filter(s -> s.getID().startsWith(MCRFileSystemUtils.STORE_ID_PREFIX))
66              .sorted(Comparator.comparing(MCRStore::getID))
67              .flatMap(s -> s.getStoredIDs()
68                  .mapToObj(
69                      i -> MCRObjectID.formatID(s.getID().substring(MCRFileSystemUtils.STORE_ID_PREFIX.length()), i)))
70              .map(owner -> MCRAbstractFileSystem.getPath(owner, "/", this))
71              .map(Path.class::cast)::iterator;
72      }
73  
74      /* (non-Javadoc)
75       * @see java.nio.file.FileSystem#getFileStores()
76       */
77      @Override
78      public Iterable<FileStore> getFileStores() {
79          return MCRStoreCenter.instance().getCurrentStores(org.mycore.datamodel.ifs2.MCRFileStore.class)
80              .filter(s -> s.getID().startsWith(MCRFileSystemUtils.STORE_ID_PREFIX))
81              .sorted(Comparator.comparing(MCRStore::getID))
82              .map(MCRStore::getID)
83              .distinct()
84              .map(storeId -> {
85                  try {
86                      return MCRFileStore.getInstance(storeId);
87                  } catch (IOException e) {
88                      LogManager.getLogger().error("Error while iterating FileStores.", e);
89                      return null;
90                  }
91              })
92              .filter(Objects::nonNull)
93              .map(FileStore.class::cast)::iterator;
94      }
95  
96      @Override
97      public void createRoot(String owner) throws FileSystemException {
98          MCRObjectID derId = MCRObjectID.getInstance(owner);
99          org.mycore.datamodel.ifs2.MCRFileStore fileStore = MCRFileSystemUtils.getStore(derId.getBase());
100         MCRPath rootPath = getPath(owner, "", this);
101         try {
102             if (fileStore.retrieve(derId.getNumberAsInteger()) != null) {
103                 throw new FileAlreadyExistsException(rootPath.toString());
104             }
105             try {
106                 fileStore.create(derId.getNumberAsInteger());
107             } catch (RuntimeException e) {
108                 LogManager.getLogger(getClass()).warn("Catched RuntimeException while creating new root directory.", e);
109                 throw new FileSystemException(rootPath.toString(), null, e.getMessage());
110             }
111         } catch (FileSystemException e) {
112             throw e;
113         } catch (IOException e) {
114             throw new FileSystemException(rootPath.toString(), null, e.getMessage());
115         }
116         LogManager.getLogger(getClass()).info("Created root directory: {}", rootPath);
117     }
118 
119     @Override
120     public void removeRoot(String owner) throws FileSystemException {
121         MCRPath rootPath = getPath(owner, "", this);
122         MCRDirectory rootDirectory = null;
123         try {
124             rootDirectory = MCRFileSystemUtils.getFileCollection(owner);
125             if (rootDirectory.hasChildren()) {
126                 throw new DirectoryNotEmptyException(rootPath.toString());
127             }
128             rootDirectory.delete();
129         } catch (FileSystemException e) {
130             throw e;
131         } catch (IOException e) {
132             throw new FileSystemException(rootPath.toString(), null, e.getMessage());
133         } catch (RuntimeException e) {
134             LogManager.getLogger(getClass()).warn("Catched RuntimeException while removing root directory.", e);
135             throw new FileSystemException(rootPath.toString(), null, e.getMessage());
136         }
137         LogManager.getLogger(getClass()).info("Removed root directory: {}", rootPath);
138     }
139 
140 }