]> git.argeo.org Git - lgpl/argeo-commons.git/blob - src/org/argeo/cms/acr/SingleUserContentRepository.java
Prepare next development cycle
[lgpl/argeo-commons.git] / 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 initRootContentProvider(null);
31 }
32
33 public SingleUserContentRepository(Subject subject, Locale locale) {
34 Objects.requireNonNull(subject);
35 Objects.requireNonNull(locale);
36
37 this.subject = subject;
38 this.locale = locale;
39 }
40
41 @Override
42 public void start() {
43 Objects.requireNonNull(subject);
44 Objects.requireNonNull(locale);
45
46 super.start();
47 if (contentSession != null)
48 throw new IllegalStateException("Repository is already started, stop it first.");
49 contentSession = new CmsContentSession(this, subject, locale);
50 }
51
52 @Override
53 public void stop() {
54 if (contentSession != null)
55 contentSession.close();
56 contentSession = null;
57 super.stop();
58 }
59
60 @Override
61 public ContentSession get(Locale locale) {
62 if (!this.locale.equals(locale))
63 throw new UnsupportedOperationException("This repository does not support multi-locale sessions");
64 return contentSession;
65 }
66
67 @Override
68 public ContentSession get() {
69 return contentSession;
70 }
71
72 @Override
73 protected CmsContentSession newSystemSession() {
74 return new CmsContentSession(this, subject, Locale.getDefault());
75 }
76
77 public static void main(String... args) {
78 Path homePath = Paths.get(System.getProperty("user.home"));
79 String username = System.getProperty("user.name");
80 X500Principal principal = new X500Principal(LdapAttrs.uid + "=" + username + ",dc=localhost");
81 Subject subject = new Subject();
82 subject.getPrincipals().add(principal);
83
84 SingleUserContentRepository contentRepository = new SingleUserContentRepository(subject);
85 FsContentProvider homeContentProvider = new FsContentProvider("/home", homePath);
86 contentRepository.addProvider(homeContentProvider);
87 Runtime.getRuntime().addShutdownHook(new Thread(() -> contentRepository.stop(), "Shutdown content repository"));
88 contentRepository.start();
89
90 ContentSession contentSession = contentRepository.get();
91 ContentUtils.traverse(contentSession.get("/"), (c, depth) -> ContentUtils.print(c, System.out, depth, false),
92 2);
93
94 }
95 }