X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Fosgi%2FCmsOsgiUtils.java;fp=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Fosgi%2FCmsOsgiUtils.java;h=424d62f68f01319f5d4822fdaa96cae5d0aeb33d;hb=b7683883512d924a039a43c2e1102290aa49f64d;hp=0000000000000000000000000000000000000000;hpb=03f646fd0d7e7ce393694c836c779bc67a4eef55;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/osgi/CmsOsgiUtils.java b/org.argeo.cms/src/org/argeo/cms/osgi/CmsOsgiUtils.java new file mode 100644 index 000000000..424d62f68 --- /dev/null +++ b/org.argeo.cms/src/org/argeo/cms/osgi/CmsOsgiUtils.java @@ -0,0 +1,40 @@ +package org.argeo.cms.osgi; + +import java.util.Collection; + +import javax.security.auth.Subject; + +import org.argeo.api.cms.CmsSession; +import org.argeo.api.cms.CmsSessionId; +import org.osgi.framework.BundleContext; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceReference; + +public class CmsOsgiUtils { + + /** @return The {@link CmsSession} for this {@link Subject} or null. */ + public static CmsSession getCmsSession(BundleContext bc, Subject subject) { + if (subject.getPrivateCredentials(CmsSessionId.class).isEmpty()) + return null; + CmsSessionId cmsSessionId = subject.getPrivateCredentials(CmsSessionId.class).iterator().next(); + String uuid = cmsSessionId.getUuid().toString(); + Collection> sr; + try { + sr = bc.getServiceReferences(CmsSession.class, "(" + CmsSession.SESSION_UUID + "=" + uuid + ")"); + } catch (InvalidSyntaxException e) { + throw new IllegalArgumentException("Cannot get CMS session for uuid " + uuid, e); + } + ServiceReference cmsSessionRef; + if (sr.size() == 1) { + cmsSessionRef = sr.iterator().next(); + return bc.getService(cmsSessionRef); + } else if (sr.size() == 0) { + return null; + } else + throw new IllegalStateException(sr.size() + " CMS sessions registered for " + uuid); + } + + /** Singleton.*/ + private CmsOsgiUtils() { + } +}