Convert web-osgi-impl to use OSGi DS 80/89380/12
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 25 Apr 2020 19:56:25 +0000 (21:56 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 2 Aug 2020 09:17:27 +0000 (11:17 +0200)
Declarative Services are a much nicer fit than blueprint for
the use case at handle. SCR can deal with most of the inter-bundle
complexity by having a component with scope=BUNDLE -- lowering
the amount of actual code we need.

Also mark all of this for future evolution, as the OSGi R7 certainly
changes things a bit.

Change-Id: I74e1a7eb34e1b1e94ec751588df0c2c643ce8efc
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
docs/pom.xml
web/impl-osgi/pom.xml
web/impl-osgi/src/main/java/org/opendaylight/aaa/web/osgi/PaxWebServer.java

index 990ce034e093a6b63cabf8a008eabea4dd342b5e..663980a62b98dce62c5021cca0dc8d5c77c10a3b 100644 (file)
             <artifactId>org.apache.karaf.shell.core</artifactId>
             <scope>provided</scope>
         </dependency>
-        <dependency>
-            <groupId>org.apache.aries.blueprint</groupId>
-            <artifactId>blueprint-maven-plugin-annotation</artifactId>
-            <scope>provided</scope>
-        </dependency>
         <dependency>
             <groupId>org.osgi</groupId>
             <artifactId>org.osgi.core</artifactId>
index f052b236f22e8ccb0f92e5cbbf486fa49cc0a422..792f55e8298d6a48fa0da2d22b35b0803a6cb134 100644 (file)
       <artifactId>guava</artifactId>
     </dependency>
     <dependency>
-      <groupId>javax.inject</groupId>
-      <artifactId>javax.inject</artifactId>
-      <optional>true</optional>
-    </dependency>
-    <dependency>
-      <groupId>org.apache.aries.blueprint</groupId>
-      <artifactId>blueprint-maven-plugin-annotation</artifactId>
-      <optional>true</optional>
-    </dependency>
-    <dependency>
-      <groupId>javax.annotation</groupId>
-      <artifactId>javax.annotation-api</artifactId>
-      <optional>true</optional>
+      <groupId>org.osgi</groupId>
+      <artifactId>org.osgi.core</artifactId>
     </dependency>
     <dependency>
       <groupId>org.osgi</groupId>
-      <artifactId>org.osgi.core</artifactId>
+      <artifactId>osgi.cmpn</artifactId>
     </dependency>
   </dependencies>
-
-  <build>
-    <plugins>
-      <plugin>
-        <groupId>org.apache.aries.blueprint</groupId>
-        <artifactId>blueprint-maven-plugin</artifactId>
-      </plugin>
-    </plugins>
-  </build>
-
 </project>
index 07d9ef53fce19f51330eea22978c277935516d64..405defb1c2d8aebeaade65ede55ded1baa38e724 100644 (file)
@@ -7,18 +7,16 @@
  */
 package org.opendaylight.aaa.web.osgi;
 
+import static com.google.common.base.Verify.verifyNotNull;
+
 import java.util.ArrayList;
 import java.util.EventListener;
 import java.util.List;
 import java.util.Map;
-import javax.annotation.PreDestroy;
-import javax.inject.Inject;
-import javax.inject.Singleton;
 import javax.servlet.Filter;
 import javax.servlet.Servlet;
 import javax.servlet.ServletContextListener;
 import javax.servlet.ServletException;
-import org.apache.aries.blueprint.annotation.service.Reference;
 import org.opendaylight.aaa.web.FilterDetails;
 import org.opendaylight.aaa.web.ResourceDetails;
 import org.opendaylight.aaa.web.ServletDetails;
@@ -29,9 +27,13 @@ import org.ops4j.pax.web.service.WebContainer;
 import org.ops4j.pax.web.service.WebContainerDTO;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceFactory;
 import org.osgi.framework.ServiceReference;
-import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ServiceScope;
 import org.osgi.service.http.HttpContext;
 import org.osgi.service.http.HttpService;
 import org.osgi.service.http.NamespaceException;
@@ -44,31 +46,26 @@ import org.slf4j.LoggerFactory;
  *
  * @author Michael Vorburger.ch - original author
  * @author Tom Pantelis - added ServiceFactory to solve possible class loading issues in web components
+ * @author Robert Varga - reworked to use OSGi DS, which cuts the implementation down to bare bones.
  */
-@Singleton
-public class PaxWebServer implements ServiceFactory<WebServer> {
-
-    // TODO write an IT (using Pax Exam) which tests this, re-use JettyLauncherTest
-
+// FIXME: this really acts as an extender (note how we lookup in the context of target bundle) and should really be
+//        eliminated in favor of such
+// FIXME: even if not, OSGi R7 is changing the picture and should allow us to work without this crud
+// TODO write an IT (using Pax Exam) which tests this, re-use JettyLauncherTest
+@Component(scope = ServiceScope.BUNDLE)
+public final class PaxWebServer implements WebServer {
     private static final Logger LOG = LoggerFactory.getLogger(PaxWebServer.class);
 
-    private final WebContainer paxWeb;
-    private final ServiceRegistration<?> serviceRegistration;
-
-    @Inject
-    public PaxWebServer(final @Reference WebContainer paxWebContainer, final BundleContext bundleContext) {
-        this.paxWeb = paxWebContainer;
-        serviceRegistration = bundleContext.registerService(WebServer.class, this, null);
-        LOG.info("PaxWebServer initialized & WebServer service factory registered");
-    }
+    // Global reference, acts as an activation guard
+    @Reference
+    WebContainer global = null;
 
-    @PreDestroy
-    public void close() {
-        serviceRegistration.unregister();
-    }
+    private ServiceReference<WebContainer> ref;
+    private WebContainer local;
 
-    String getBaseURL() {
-        WebContainerDTO details = paxWeb.getWebcontainerDTO();
+    @Override
+    public String getBaseURL() {
+        final WebContainerDTO details = local.getWebcontainerDTO();
         if (details.securePort != null && details.securePort > 0) {
             return "https://" + details.listeningAddresses[0] + ":" + details.securePort;
         } else {
@@ -77,55 +74,29 @@ public class PaxWebServer implements ServiceFactory<WebServer> {
     }
 
     @Override
-    public WebServer getService(final Bundle bundle, final ServiceRegistration<WebServer> registration) {
-        LOG.info("Creating WebServer instance for bundle {}", bundle);
+    public WebContextRegistration registerWebContext(final WebContext webContext) throws ServletException {
+        return new WebContextImpl(local, webContext);
+    }
 
+    @Activate
+    void activate(final ComponentContext componentContext) {
+        final Bundle bundle = componentContext.getUsingBundle();
         final BundleContext bundleContext = bundle.getBundleContext();
 
-        // Get the WebContainer service using the given bundle's context so the WebContainer service instance uses
-        // the bundle's class loader.
-        final ServiceReference<WebContainer> webContainerServiceRef =
-                bundleContext.getServiceReference(WebContainer.class);
-
-        final WebContainer bundleWebContainer;
-        if (webContainerServiceRef != null) {
-            bundleWebContainer = bundleContext.getService(webContainerServiceRef);
-        } else {
-            bundleWebContainer = null;
-        }
-
-        if (bundleWebContainer == null) {
-            throw new IllegalStateException("WebContainer OSGi service not found using bundle: " + bundle.toString());
-        }
-
-        return new WebServer() {
-            @Override
-            public WebContextRegistration registerWebContext(final WebContext webContext) throws ServletException {
-                return new WebContextImpl(bundleWebContainer, webContext) {
-                    @Override
-                    public void close() {
-                        super.close();
-
-                        try {
-                            bundleContext.ungetService(webContainerServiceRef);
-                        } catch (IllegalStateException e) {
-                            LOG.debug("Error from ungetService", e);
-                        }
-                    }
-                };
-            }
-
-            @Override
-            public String getBaseURL() {
-                return PaxWebServer.this.getBaseURL();
-            }
-        };
+        ref = verifyNotNull(bundle.getBundleContext().getServiceReference(WebContainer.class),
+            "Failed to locate WebContext from %s", bundle);
+        local = verifyNotNull(bundleContext.getService(ref), "Failed to get WebContext in %s", bundle);
+        LOG.info("Activated WebServer instance for {}", bundleContext);
     }
 
-    @Override
-    public void ungetService(final Bundle bundle, final ServiceRegistration<WebServer> registration,
-            final WebServer service) {
-        // no-op
+    @Deactivate
+    void deactivate(final ComponentContext componentContext) {
+        final Bundle bundle = componentContext.getUsingBundle();
+        final BundleContext bundleContext = bundle.getBundleContext();
+        local = null;
+        bundleContext.ungetService(ref);
+        ref = null;
+        LOG.info("Deactivated WebServer instance for {}", bundle);
     }
 
     private static class WebContextImpl implements WebContextRegistration {