]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.e4/src/org/argeo/cms/e4/OsgiFilterContextFunction.java
Work on exception management.
[lgpl/argeo-commons.git] / org.argeo.cms.e4 / src / org / argeo / cms / e4 / OsgiFilterContextFunction.java
1 package org.argeo.cms.e4;
2
3 import org.argeo.cms.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;
11
12 /** An Eclipse 4 {@link ContextFunction} based on an OSGi filter. */
13 public class OsgiFilterContextFunction extends ContextFunction {
14
15 private BundleContext bc = FrameworkUtil.getBundle(OsgiFilterContextFunction.class).getBundleContext();
16
17 @Override
18 public Object compute(IEclipseContext context, String contextKey) {
19 ServiceReference<?>[] srs;
20 try {
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);
24 }
25 if (srs == null || srs.length == 0) {
26 return IInjector.NOT_A_VALUE;
27 } else {
28 // return the first one
29 return bc.getService(srs[0]);
30 }
31 }
32
33 }