X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Facr%2FSingleUserContentRepository.java;fp=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Facr%2FSingleUserContentRepository.java;h=c44200243d83847d94d56be513330d719b4fa75b;hb=eb4cc3db3bf141c229f0f7ff929daff108bee6d2;hp=0000000000000000000000000000000000000000;hpb=d01c0151201161462367dc240a1e6903c830de87;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/acr/SingleUserContentRepository.java b/org.argeo.cms/src/org/argeo/cms/acr/SingleUserContentRepository.java new file mode 100644 index 000000000..c44200243 --- /dev/null +++ b/org.argeo.cms/src/org/argeo/cms/acr/SingleUserContentRepository.java @@ -0,0 +1,72 @@ +package org.argeo.cms.acr; + +import java.util.Locale; +import java.util.Objects; + +import javax.security.auth.Subject; + +import org.argeo.api.acr.ContentSession; +import org.argeo.api.acr.spi.ProvidedRepository; + +/** + * A standalone {@link ProvidedRepository} with a single {@link Subject} (which + * also provides the system session). + */ +public class SingleUserContentRepository extends AbstractContentRepository { + private final Subject subject; + private final Locale locale; + + // the single session + private CmsContentSession contentSession; + + public SingleUserContentRepository(Subject subject) { + this(subject, Locale.getDefault()); + + initRootContentProvider(null); + } + + public SingleUserContentRepository(Subject subject, Locale locale) { + Objects.requireNonNull(subject); + Objects.requireNonNull(locale); + + this.subject = subject; + this.locale = locale; + } + + @Override + public void start() { + Objects.requireNonNull(subject); + Objects.requireNonNull(locale); + + super.start(); + if (contentSession != null) + throw new IllegalStateException("Repository is already started, stop it first."); + contentSession = new CmsContentSession(this, subject, locale); + } + + @Override + public void stop() { + if (contentSession != null) + contentSession.close(); + contentSession = null; + super.stop(); + } + + @Override + public ContentSession get(Locale locale) { + if (!this.locale.equals(locale)) + throw new UnsupportedOperationException("This repository does not support multi-locale sessions"); + return contentSession; + } + + @Override + public ContentSession get() { + return contentSession; + } + + @Override + protected CmsContentSession newSystemSession() { + return new CmsContentSession(this, subject, Locale.getDefault()); + } + +}