]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/acr/SingleUserContentRepository.java
Mini desktop graalvm packaging.
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / acr / SingleUserContentRepository.java
1 package org.argeo.cms.acr;
2
3 import java.nio.file.Path;
4 import java.nio.file.Paths;
5 import java.util.Locale;
6 import java.util.Objects;
7
8 import javax.security.auth.Subject;
9 import javax.security.auth.x500.X500Principal;
10
11 import org.argeo.api.acr.ContentSession;
12 import org.argeo.api.acr.spi.ProvidedRepository;
13 import org.argeo.cms.acr.fs.FsContentProvider;
14 import org.argeo.util.naming.LdapAttrs;
15
16 /**
17 * A standalone {@link ProvidedRepository} with a single {@link Subject} (which
18 * also provides the system session).
19 */
20 public class SingleUserContentRepository extends AbstractContentRepository {
21 private final Subject subject;
22 private final Locale locale;
23
24 // the single session
25 private CmsContentSession contentSession;
26
27 public SingleUserContentRepository(Subject subject) {
28 this(subject, Locale.getDefault());
29 }
30
31 public SingleUserContentRepository(Subject subject, Locale locale) {
32 Objects.requireNonNull(subject);
33 Objects.requireNonNull(locale);
34
35 this.subject = subject;
36 this.locale = locale;
37 }
38
39 @Override
40 public void start() {
41 Objects.requireNonNull(subject);
42 Objects.requireNonNull(locale);
43
44 super.start();
45 initRootContentProvider(null);
46 if (contentSession != null)
47 throw new IllegalStateException("Repository is already started, stop it first.");
48 contentSession = new CmsContentSession(this, subject, locale);
49 }
50
51 @Override
52 public void stop() {
53 if (contentSession != null)
54 contentSession.close();
55 contentSession = null;
56 super.stop();
57 }
58
59 @Override
60 public ContentSession get(Locale locale) {
61 if (!this.locale.equals(locale))
62 throw new UnsupportedOperationException("This repository does not support multi-locale sessions");
63 return contentSession;
64 }
65
66 @Override
67 public ContentSession get() {
68 return contentSession;
69 }
70
71 @Override
72 protected CmsContentSession newSystemSession() {
73 return new CmsContentSession(this, subject, Locale.getDefault());
74 }
75
76 public static void main(String... args) {
77 Path homePath = Paths.get(System.getProperty("user.home"));
78 String username = System.getProperty("user.name");
79 X500Principal principal = new X500Principal(LdapAttrs.uid + "=" + username + ",dc=localhost");
80 Subject subject = new Subject();
81 subject.getPrincipals().add(principal);
82
83 SingleUserContentRepository contentRepository = new SingleUserContentRepository(subject);
84 contentRepository.start();
85 FsContentProvider homeContentProvider = new FsContentProvider("/home", homePath);
86 contentRepository.addProvider(homeContentProvider);
87 Runtime.getRuntime().addShutdownHook(new Thread(() -> contentRepository.stop(), "Shutdown content repository"));
88
89 ContentSession contentSession = contentRepository.get();
90 ContentUtils.traverse(contentSession.get("/"), (c, depth) -> ContentUtils.print(c, System.out, depth, false),
91 2);
92
93 }
94 }