]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/osgi/CmsOsgiUtils.java
Introduce CMS UX
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / osgi / CmsOsgiUtils.java
1 package org.argeo.cms.osgi;
2
3 import java.util.Collection;
4
5 import javax.security.auth.Subject;
6
7 import org.argeo.api.cms.CmsSession;
8 import org.argeo.api.cms.CmsSessionId;
9 import org.osgi.framework.BundleContext;
10 import org.osgi.framework.InvalidSyntaxException;
11 import org.osgi.framework.ServiceReference;
12
13 public class CmsOsgiUtils {
14
15 /** @return The {@link CmsSession} for this {@link Subject} or null. */
16 public static CmsSession getCmsSession(BundleContext bc, Subject subject) {
17 if (subject.getPrivateCredentials(CmsSessionId.class).isEmpty())
18 return null;
19 CmsSessionId cmsSessionId = subject.getPrivateCredentials(CmsSessionId.class).iterator().next();
20 String uuid = cmsSessionId.getUuid().toString();
21 Collection<ServiceReference<CmsSession>> sr;
22 try {
23 sr = bc.getServiceReferences(CmsSession.class, "(" + CmsSession.SESSION_UUID + "=" + uuid + ")");
24 } catch (InvalidSyntaxException e) {
25 throw new IllegalArgumentException("Cannot get CMS session for uuid " + uuid, e);
26 }
27 ServiceReference<CmsSession> cmsSessionRef;
28 if (sr.size() == 1) {
29 cmsSessionRef = sr.iterator().next();
30 return bc.getService(cmsSessionRef);
31 } else if (sr.size() == 0) {
32 return null;
33 } else
34 throw new IllegalStateException(sr.size() + " CMS sessions registered for " + uuid);
35 }
36
37 /** Singleton.*/
38 private CmsOsgiUtils() {
39 }
40 }