Introduce OSGi-filter based context function
authorMathieu Baudier <mbaudier@argeo.org>
Sun, 8 Apr 2018 09:04:17 +0000 (11:04 +0200)
committerMathieu Baudier <mbaudier@argeo.org>
Sun, 8 Apr 2018 09:04:17 +0000 (11:04 +0200)
org.argeo.cms.e4.rap/src/org/argeo/cms/e4/rap/CmsLoginLifecycle.java
org.argeo.cms.e4/OSGI-INF/homeRepository.xml [new file with mode: 0644]
org.argeo.cms.e4/bnd.bnd
org.argeo.cms.e4/build.properties
org.argeo.cms.e4/src/org/argeo/cms/e4/contexts/OsgiFilterContextFunction.java [new file with mode: 0644]
org.argeo.cms.ui/src/org/argeo/cms/ui/eclipse/forms/ManagedForm.java

index 6872906260209d51588a0c4833e721ccb2795aa1..8b437a0e7988889b0cc77ac381c81e2989bc0d9b 100644 (file)
@@ -23,7 +23,19 @@ public class CmsLoginLifecycle implements CmsView {
        private LoginContext loginContext;
 
        @PostContextCreate
-       boolean login(Display d) {
+       boolean login(Display d) {// , IEclipseContext eclipseContext) {
+               // RepositoryFactory repositoryFactory =
+               // eclipseContext.get(RepositoryFactory.class);
+               // Map<String, String> params = new HashMap<>();
+               // params.put("cn", "home");
+               // Repository homeRepository;
+               // try {
+               // homeRepository = repositoryFactory.getRepository(params);
+               // } catch (RepositoryException e1) {
+               // throw new CmsException("Cannot get home repository", e1);
+               // }
+               // eclipseContext.set("(cn=home)", homeRepository);
+
                Subject subject = Subject.getSubject(AccessController.getContext());
                Display display = Display.getCurrent();
                CmsLoginShell loginShell = new CmsLoginShell(this);
@@ -63,12 +75,12 @@ public class CmsLoginLifecycle implements CmsView {
                if (loginContext == null)
                        throw new CmsException("Login context cannot be null");
                // logout previous login context
-//             if (this.loginContext != null)
-//                     try {
-//                             this.loginContext.logout();
-//                     } catch (LoginException e1) {
-//                             System.err.println("Could not log out: " + e1);
-//                     }
+               // if (this.loginContext != null)
+               // try {
+               // this.loginContext.logout();
+               // } catch (LoginException e1) {
+               // System.err.println("Could not log out: " + e1);
+               // }
                this.loginContext = loginContext;
        }
 
diff --git a/org.argeo.cms.e4/OSGI-INF/homeRepository.xml b/org.argeo.cms.e4/OSGI-INF/homeRepository.xml
new file mode 100644 (file)
index 0000000..c03e62e
--- /dev/null
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" immediate="true" name="Home Repository">
+   <implementation class="org.argeo.cms.e4.contexts.OsgiFilterContextFunction"/>
+   <property name="service.context.key" type="String" value="(cn=home)"/>
+   <service>
+      <provide interface="org.eclipse.e4.core.contexts.IContextFunction"/>
+   </service>
+</scr:component>
index a1fbdcd826cd7222127cec174f476676b3852d31..8c15aafbb5d5ee6e379488d6f0e69d63d488bd04 100644 (file)
@@ -1,3 +1,5 @@
+Service-Component: OSGI-INF/homeRepository.xml
+Bundle-ActivationPolicy: lazy
 Import-Package: org.eclipse.swt,\
 org.eclipse.e4.ui.model.application.ui,\
 org.eclipse.e4.ui.model.application,\
index 0b085594ef6e83a4c22c5edbaa95d64689e322a2..4d2873afff56e4fecf68dc248e96900fc955f670 100644 (file)
@@ -1,5 +1,6 @@
-source.. = src/\r
-output.. = bin/\r
-bin.includes = META-INF/,\\r
-               OSGI-INF/,\\r
-               .\r
+output.. = bin/
+bin.includes = META-INF/,\
+               OSGI-INF/,\
+               .,\
+               OSGI-INF/homeRepository.xml
+source.. = src/
diff --git a/org.argeo.cms.e4/src/org/argeo/cms/e4/contexts/OsgiFilterContextFunction.java b/org.argeo.cms.e4/src/org/argeo/cms/e4/contexts/OsgiFilterContextFunction.java
new file mode 100644 (file)
index 0000000..b0fdcc1
--- /dev/null
@@ -0,0 +1,33 @@
+package org.argeo.cms.e4.contexts;
+
+import org.argeo.cms.CmsException;
+import org.eclipse.e4.core.contexts.ContextFunction;
+import org.eclipse.e4.core.contexts.IEclipseContext;
+import org.eclipse.e4.core.di.IInjector;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+
+@SuppressWarnings("restriction")
+public class OsgiFilterContextFunction extends ContextFunction {
+
+       private BundleContext bc = FrameworkUtil.getBundle(OsgiFilterContextFunction.class).getBundleContext();
+
+       @Override
+       public Object compute(IEclipseContext context, String contextKey) {
+               ServiceReference<?>[] srs;
+               try {
+                       srs = bc.getServiceReferences((String) null, contextKey);
+               } catch (InvalidSyntaxException e) {
+                       throw new CmsException("Context key " + contextKey + " must be a valid osgi filter", e);
+               }
+               if (srs == null || srs.length == 0) {
+                       return IInjector.NOT_A_VALUE;
+               } else {
+                       // return the first one
+                       return bc.getService(srs[0]);
+               }
+       }
+
+}
index 19e8211e6220e4beab1666c7d38682dd10fb7d38..4140465a1434e972a9b1c673bdff06631762bdae 100644 (file)
@@ -51,6 +51,7 @@ public class ManagedForm implements IManagedForm {
                toolkit = new FormToolkit(parent.getDisplay());
                ownsToolkit = true;
                form = toolkit.createScrolledForm(parent);
+               
        }
 
        /**