1 package org
.argeo
.cms
.e4
;
3 import org
.argeo
.cms
.swt
.CmsException
;
4 import org
.eclipse
.e4
.core
.contexts
.ContextFunction
;
5 import org
.eclipse
.e4
.core
.contexts
.IEclipseContext
;
6 import org
.eclipse
.e4
.core
.di
.IInjector
;
7 import org
.osgi
.framework
.BundleContext
;
8 import org
.osgi
.framework
.FrameworkUtil
;
9 import org
.osgi
.framework
.InvalidSyntaxException
;
10 import org
.osgi
.framework
.ServiceReference
;
12 /** An Eclipse 4 {@link ContextFunction} based on an OSGi filter. */
13 public class OsgiFilterContextFunction
extends ContextFunction
{
15 private BundleContext bc
= FrameworkUtil
.getBundle(OsgiFilterContextFunction
.class).getBundleContext();
18 public Object
compute(IEclipseContext context
, String contextKey
) {
19 ServiceReference
<?
>[] srs
;
21 srs
= bc
.getServiceReferences((String
) null, contextKey
);
22 } catch (InvalidSyntaxException e
) {
23 throw new CmsException("Context key " + contextKey
+ " must be a valid osgi filter", e
);
25 if (srs
== null || srs
.length
== 0) {
26 return IInjector
.NOT_A_VALUE
;
28 // return the first one
29 return bc
.getService(srs
[0]);