1 package org
.argeo
.cms
.osgi
;
3 import java
.util
.Collection
;
5 import javax
.security
.auth
.Subject
;
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
;
13 public class CmsOsgiUtils
{
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())
19 CmsSessionId cmsSessionId
= subject
.getPrivateCredentials(CmsSessionId
.class).iterator().next();
20 String uuid
= cmsSessionId
.getUuid().toString();
21 Collection
<ServiceReference
<CmsSession
>> sr
;
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
);
27 ServiceReference
<CmsSession
> cmsSessionRef
;
29 cmsSessionRef
= sr
.iterator().next();
30 return bc
.getService(cmsSessionRef
);
31 } else if (sr
.size() == 0) {
34 throw new IllegalStateException(sr
.size() + " CMS sessions registered for " + uuid
);
38 private CmsOsgiUtils() {