]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/acr/MountManager.java
Mini desktop graalvm packaging.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / acr / MountManager.java
1 package org.argeo.cms.acr;
2
3 import java.util.Map;
4 import java.util.NavigableMap;
5 import java.util.Objects;
6 import java.util.TreeMap;
7 import java.util.function.Function;
8
9 import org.argeo.api.acr.Content;
10 import org.argeo.api.acr.CrName;
11 import org.argeo.api.acr.spi.ContentProvider;
12
13 /** Manages the structural and dynamic mounts within the content repository. */
14 class MountManager {
15 private final NavigableMap<String, ContentProvider> partitions = new TreeMap<>();
16
17 private final CmsContentSession systemSession;
18
19 public MountManager(CmsContentSession systemSession) {
20 this.systemSession = systemSession;
21 }
22
23 synchronized void addStructuralContentProvider(ContentProvider contentProvider) {
24 String mountPath = contentProvider.getMountPath();
25 Objects.requireNonNull(mountPath);
26 if (partitions.containsKey(mountPath))
27 throw new IllegalStateException("A provider is already registered for " + mountPath);
28 partitions.put(mountPath, contentProvider);
29 if ("/".equals(mountPath))// root
30 return;
31 String[] parentPath = ContentUtils.getParentPath(mountPath);
32 Content parent = systemSession.get(parentPath[0]);
33 Content mount = parent.add(parentPath[1]);
34 mount.put(CrName.MOUNT.get(), "true");
35
36 }
37
38 synchronized ContentProvider getOrAddMountedProvider(String mountPath, Function<String, ContentProvider> factory) {
39 Objects.requireNonNull(factory);
40 if (!partitions.containsKey(mountPath)) {
41 ContentProvider contentProvider = factory.apply(mountPath);
42 if (!mountPath.equals(contentProvider.getMountPath()))
43 throw new IllegalArgumentException("Mount path " + mountPath + " is inconsistent with content provider "
44 + contentProvider.getMountPath());
45 partitions.put(mountPath, contentProvider);
46 }
47 return partitions.get(mountPath);
48 }
49
50 synchronized ContentProvider findContentProvider(String path) {
51 Map.Entry<String, ContentProvider> entry = partitions.floorEntry(path);
52 if (entry == null)
53 throw new IllegalArgumentException("No entry provider found for " + path);
54 String mountPath = entry.getKey();
55 ContentProvider contentProvider = entry.getValue();
56 assert mountPath.equals(contentProvider.getMountPath());
57 return contentProvider;
58 }
59 }