This is an automated email from the ASF dual-hosted git repository.

cziegeler pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/felix-dev.git


The following commit(s) were added to refs/heads/master by this push:
     new fe7d77357a FELIX-6631 : Migrate webconsole plugins to jakarta.servlet 
api
fe7d77357a is described below

commit fe7d77357ace4d3d0bf1a45cb60dafd847ec1b37
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Mon Aug 21 14:42:36 2023 +0200

    FELIX-6631 : Migrate webconsole plugins to jakarta.servlet api
---
 webconsole-plugins/deppack/pom.xml                 |  31 ++--
 .../plugins/deppack/internal/Activator.java        |  36 ++---
 .../plugins/deppack/internal/WebConsolePlugin.java | 161 ++++++++++-----------
 3 files changed, 106 insertions(+), 122 deletions(-)

diff --git a/webconsole-plugins/deppack/pom.xml 
b/webconsole-plugins/deppack/pom.xml
index 371f8aeef0..a7d7f7a2af 100644
--- a/webconsole-plugins/deppack/pom.xml
+++ b/webconsole-plugins/deppack/pom.xml
@@ -16,7 +16,7 @@
                <groupId>org.apache.felix</groupId>
                <artifactId>felix-parent</artifactId>
                <version>7</version>
-               <relativePath>../../../pom/pom.xml</relativePath>
+               <relativePath/>
        </parent>
 
        <artifactId>org.apache.felix.webconsole.plugins.deppack</artifactId>
@@ -82,12 +82,12 @@
                        <plugin>
                                <groupId>org.apache.felix</groupId>
                                <artifactId>maven-bundle-plugin</artifactId>
-                               <version>3.2.0</version>
+                               <version>5.1.9</version>
                                <extensions>true</extensions>
                                <configuration>
                                        <instructions>
                                                <Bundle-SymbolicName>
-                                                       ${artifactId}
+                                                       ${project.artifactId}
                         </Bundle-SymbolicName>
                                                <Bundle-Activator>
                                                        
org.apache.felix.webconsole.plugins.deppack.internal.Activator
@@ -103,40 +103,33 @@
 
        <dependencies>
                <dependency>
-                       <groupId>javax.servlet</groupId>
-                       <artifactId>servlet-api</artifactId>
-                       <version>2.4</version>
+                       <groupId>jakarta.servlet</groupId>
+                       <artifactId>jakarta.servlet-api</artifactId>
+                       <version>5.0.0</version>
                        <scope>provided</scope>
                </dependency>
-               <dependency>
-                       <groupId>commons-fileupload</groupId>
-                       <artifactId>commons-fileupload</artifactId>
-                       <version>1.5</version>
-                       <scope>provided</scope>
-                       <optional>true</optional>
-               </dependency>
                <dependency>
                        <groupId>org.osgi</groupId>
-                       <artifactId>org.osgi.core</artifactId>
-                       <version>4.0.0</version>
+                       <artifactId>osgi.core</artifactId>
+                       <version>6.0.0</version>
                        <scope>provided</scope>
                </dependency>
                <dependency>
                        <groupId>org.osgi</groupId>
-                       <artifactId>org.osgi.compendium</artifactId>
-                       <version>4.1.0</version>
+                       
<artifactId>org.osgi.service.deploymentadmin</artifactId>
+                       <version>1.1.0</version>
                        <scope>provided</scope>
                </dependency>
                <dependency>
                        <groupId>org.apache.felix</groupId>
                        <artifactId>org.apache.felix.webconsole</artifactId>
-                       <version>3.0.0</version>
+                       <version>4.8.13-SNAPSHOT</version>
                        <scope>provided</scope>
                </dependency>
                <dependency>
                        <groupId>org.apache.felix</groupId>
                        <artifactId>org.apache.felix.utils</artifactId>
-                       <version>1.9.0</version>
+                       <version>1.11.8</version>
                        <scope>provided</scope>
                </dependency>
        </dependencies>
diff --git 
a/webconsole-plugins/deppack/src/main/java/org/apache/felix/webconsole/plugins/deppack/internal/Activator.java
 
b/webconsole-plugins/deppack/src/main/java/org/apache/felix/webconsole/plugins/deppack/internal/Activator.java
index 5bf71d4359..8a386e12af 100644
--- 
a/webconsole-plugins/deppack/src/main/java/org/apache/felix/webconsole/plugins/deppack/internal/Activator.java
+++ 
b/webconsole-plugins/deppack/src/main/java/org/apache/felix/webconsole/plugins/deppack/internal/Activator.java
@@ -16,7 +16,6 @@
  */
 package org.apache.felix.webconsole.plugins.deppack.internal;
 
-import org.apache.felix.webconsole.SimpleWebConsolePlugin;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.ServiceReference;
@@ -27,21 +26,19 @@ import org.osgi.util.tracker.ServiceTrackerCustomizer;
 /**
  * Activator is the main starting class.
  */
-public class Activator implements BundleActivator, ServiceTrackerCustomizer
+public class Activator implements BundleActivator, 
ServiceTrackerCustomizer<DeploymentAdmin, WebConsolePlugin>
 {
 
-    private ServiceTracker tracker;
+    private ServiceTracker<DeploymentAdmin, WebConsolePlugin> tracker;
     private BundleContext context;
 
-    private SimpleWebConsolePlugin plugin;
-
     /**
      * @see 
org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
      */
     public final void start(BundleContext context) throws Exception
     {
         this.context = context;
-        this.tracker = new ServiceTracker(context, 
DeploymentAdmin.class.getName(), this);
+        this.tracker = new ServiceTracker(context, DeploymentAdmin.class, 
this);
         this.tracker.open();
     }
 
@@ -62,37 +59,32 @@ public class Activator implements BundleActivator, 
ServiceTrackerCustomizer
      * @see 
org.osgi.util.tracker.ServiceTrackerCustomizer#modifiedService(org.osgi.framework.ServiceReference,
      *      java.lang.Object)
      */
-    public final void modifiedService(ServiceReference reference, Object 
service)
+    public final void modifiedService(ServiceReference<DeploymentAdmin> 
reference, WebConsolePlugin service)
     {/* unused */
     }
 
     /**
      * @see 
org.osgi.util.tracker.ServiceTrackerCustomizer#addingService(org.osgi.framework.ServiceReference)
      */
-    public final Object addingService(ServiceReference reference)
+    public final WebConsolePlugin 
addingService(ServiceReference<DeploymentAdmin> reference)
     {
-        SimpleWebConsolePlugin plugin = this.plugin;
-        if (plugin == null)
-        {
-            this.plugin = plugin = new 
WebConsolePlugin(tracker).register(context);
+        final DeploymentAdmin admin = context.getService(reference);
+        if (admin != null) {
+            final WebConsolePlugin plugin = new WebConsolePlugin(admin);
+            plugin.register(context);
+            return plugin;
         }
 
-        return context.getService(reference);
+        return null;
     }
 
     /**
      * @see 
org.osgi.util.tracker.ServiceTrackerCustomizer#removedService(org.osgi.framework.ServiceReference,
      *      java.lang.Object)
      */
-    public final void removedService(ServiceReference reference, Object 
service)
+    public final void removedService(ServiceReference<DeploymentAdmin> 
reference, WebConsolePlugin plugin)
     {
-        SimpleWebConsolePlugin plugin = this.plugin;
-
-        if (tracker.getTrackingCount() == 0 && plugin != null)
-        {
-            plugin.unregister();
-            this.plugin = null;
-        }
-
+        plugin.unregister();
+        this.context.ungetService(reference);
     }
 }
diff --git 
a/webconsole-plugins/deppack/src/main/java/org/apache/felix/webconsole/plugins/deppack/internal/WebConsolePlugin.java
 
b/webconsole-plugins/deppack/src/main/java/org/apache/felix/webconsole/plugins/deppack/internal/WebConsolePlugin.java
index 4c847d7846..ab2eccdf0a 100644
--- 
a/webconsole-plugins/deppack/src/main/java/org/apache/felix/webconsole/plugins/deppack/internal/WebConsolePlugin.java
+++ 
b/webconsole-plugins/deppack/src/main/java/org/apache/felix/webconsole/plugins/deppack/internal/WebConsolePlugin.java
@@ -19,52 +19,60 @@ package 
org.apache.felix.webconsole.plugins.deppack.internal;
 import java.io.IOException;
 import java.io.PrintWriter;
 import java.io.StringWriter;
+import java.util.Collection;
+import java.util.Dictionary;
+import java.util.Hashtable;
 import java.util.Map;
 
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
+import jakarta.servlet.Servlet;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import jakarta.servlet.http.Part;
 
-import org.apache.commons.fileupload.FileItem;
-import org.apache.felix.webconsole.AbstractWebConsolePlugin;
-import org.apache.felix.webconsole.DefaultVariableResolver;
-import org.apache.felix.webconsole.SimpleWebConsolePlugin;
-import org.apache.felix.webconsole.WebConsoleUtil;
-import org.apache.felix.webconsole.internal.Util;
+import org.apache.felix.webconsole.servlet.AbstractServlet;
+import org.apache.felix.webconsole.servlet.ServletConstants;
+import org.apache.felix.webconsole.servlet.RequestVariableResolver;
 import org.apache.felix.utils.json.JSONWriter;
 import org.osgi.service.deploymentadmin.DeploymentAdmin;
 import org.osgi.service.deploymentadmin.DeploymentPackage;
 import org.osgi.util.tracker.ServiceTracker;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
 
 /**
  * DepPackServlet provides a plugin for managing deployment admin packages.
  */
-class WebConsolePlugin extends SimpleWebConsolePlugin
+class WebConsolePlugin extends AbstractServlet
 {
 
-    private static final String LABEL = "deppack"; //$NON-NLS-1$
-    private static final String TITLE = "%deppack.pluginTitle"; //$NON-NLS-1$
-    private static final String CSS[] = { "/" + LABEL + "/res/plugin.css" }; 
//$NON-NLS-1$ //$NON-NLS-2$
-    private static final String CATEGORY = "OSGi"; //$NON-NLS-1$
+    private static final String LABEL = "deppack";
+    private static final String TITLE = "%deppack.pluginTitle";
+    private static final String CSS[] = { "/" + LABEL + "/res/plugin.css" };
+    private static final String CATEGORY = "OSGi";
 
     //
-    private static final String ACTION_DEPLOY = "deploydp"; //$NON-NLS-1$
-    private static final String ACTION_UNINSTALL = "uninstalldp"; //$NON-NLS-1$
-    private static final String PARAMETER_PCK_FILE = "pckfile"; //$NON-NLS-1$
+    private static final String ACTION_DEPLOY = "deploydp";
+    private static final String ACTION_UNINSTALL = "uninstalldp";
+    private static final String PARAMETER_PCK_FILE = "pckfile";
 
     // templates
     private final String TEMPLATE;
 
-    private final ServiceTracker adminTracker;
+    private final DeploymentAdmin admin;
+
+    private volatile ServiceRegistration<Servlet> serviceRegistration;
 
     /** Default constructor */
-    WebConsolePlugin(ServiceTracker adminTracker)
+    WebConsolePlugin(DeploymentAdmin admin)
     {
-        super(LABEL, TITLE, CSS);
-
         // load templates
-        TEMPLATE = readTemplateFile("/res/plugin.html"); //$NON-NLS-1$
-        this.adminTracker = adminTracker;
+        try {
+        TEMPLATE = readTemplateFile("/res/plugin.html");
+        } catch (IOException ioe) {
+            throw new RuntimeException("Unable to load template file");
+        }
+        this.admin = admin;
     }
 
     public String getCategory()
@@ -72,6 +80,27 @@ class WebConsolePlugin extends SimpleWebConsolePlugin
         return CATEGORY;
     }
 
+    public void register(final BundleContext context) {
+        final Dictionary<String, Object> props = new Hashtable<String, 
Object>();
+        props.put(ServletConstants.PLUGIN_LABEL, LABEL);
+        props.put(ServletConstants.PLUGIN_TITLE, TITLE);
+        props.put(ServletConstants.PLUGIN_CATEGORY, CATEGORY);
+        props.put(ServletConstants.PLUGIN_CSS_REFERENCES, CSS);
+
+        this.serviceRegistration = context.registerService(Servlet.class, 
this, props);
+    }
+
+    public void unregister() {
+        if ( this.serviceRegistration != null) {
+            try {
+                this.serviceRegistration.unregister();
+            } catch ( final IllegalStateException ise) {
+                // ignore
+            }
+            this.serviceRegistration = null;
+        }
+    }
+
     /**
      * @see 
javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, 
javax.servlet.http.HttpServletResponse)
      */
@@ -79,20 +108,17 @@ class WebConsolePlugin extends SimpleWebConsolePlugin
         throws ServletException, IOException
     {
         // get the uploaded data
-        final String action = WebConsoleUtil.getParameter(req, 
Util.PARAM_ACTION);
+        final String action = req.getParameter("action");
         if (ACTION_DEPLOY.equals(action))
         {
-            Map params = (Map) 
req.getAttribute(AbstractWebConsolePlugin.ATTR_FILEUPLOAD);
-            if (params != null)
+            Collection<Part> parts = req.getParts();
+            for (Part part : parts)
             {
-                final FileItem pck = getFileItem(params, PARAMETER_PCK_FILE, 
false);
-                final DeploymentAdmin admin = (DeploymentAdmin) 
adminTracker.getService();
-                if (admin != null)
+                if (PARAMETER_PCK_FILE.equals(part.getName()))
                 {
                     try
                     {
-                        admin.installDeploymentPackage(pck.getInputStream());
-
+                        admin.installDeploymentPackage(part.getInputStream());
                         final String uri = req.getRequestURI();
                         resp.sendRedirect(uri);
                         return;
@@ -103,7 +129,6 @@ class WebConsolePlugin extends SimpleWebConsolePlugin
                     }
                 }
             }
-            throw new ServletException("Upload file or deployment admin 
missing.");
         }
         else if (ACTION_UNINSTALL.equals(action))
         {
@@ -111,23 +136,18 @@ class WebConsolePlugin extends SimpleWebConsolePlugin
                 req.getPathInfo().lastIndexOf('/') + 1);
             if (pckId != null && pckId.length() > 0)
             {
-                final DeploymentAdmin admin = (DeploymentAdmin) 
adminTracker.getService();
-                if (admin != null)
+                try
                 {
-                    try
-                    {
-                        final DeploymentPackage pck = 
admin.getDeploymentPackage(pckId);
-                        if (pck != null)
-                        {
-                            pck.uninstall();
-                        }
-                    }
-                    catch ( /*Deployment*/Exception e)
+                    final DeploymentPackage pck = 
admin.getDeploymentPackage(pckId);
+                    if (pck != null)
                     {
-                        throw new ServletException("Unable to undeploy 
package.", e);
+                        pck.uninstall();
                     }
                 }
-
+                catch ( /*Deployment*/Exception e)
+                {
+                    throw new ServletException("Unable to undeploy package.", 
e);
+                }
             }
 
             final PrintWriter pw = resp.getWriter();
@@ -137,46 +157,25 @@ class WebConsolePlugin extends SimpleWebConsolePlugin
         throw new ServletException("Unknown action: " + action);
     }
 
-    private static final FileItem getFileItem(Map params, String name, boolean 
isFormField)
-    {
-        FileItem[] items = (FileItem[]) params.get(name);
-        if (items != null)
-        {
-            for (int i = 0; i < items.length; i++)
-            {
-                if (items[i].isFormField() == isFormField)
-                {
-                    return items[i];
-                }
-            }
-        }
-
-        // nothing found, fail
-        return null;
-    }
-
     /**
      * @see 
org.apache.felix.webconsole.AbstractWebConsolePlugin#renderContent(javax.servlet.http.HttpServletRequest,
 javax.servlet.http.HttpServletResponse)
      */
-    protected void renderContent(HttpServletRequest request, 
HttpServletResponse response)
+    public void renderContent(HttpServletRequest request, HttpServletResponse 
response)
         throws ServletException, IOException
     {
-
-        final DeploymentAdmin admin = (DeploymentAdmin) 
adminTracker.getService();
-
         StringWriter w = new StringWriter();
         PrintWriter w2 = new PrintWriter(w);
         JSONWriter jw = new JSONWriter(w2);
         jw.object();
         if (null == admin)
         {
-            jw.key("error"); //$NON-NLS-1$
+            jw.key("error");
             jw.value(true);
         }
         else
         {
             final DeploymentPackage[] packages = 
admin.listDeploymentPackages();
-            jw.key("data"); //$NON-NLS-1$
+            jw.key("data");
 
             jw.array();
             for (int i = 0; i < packages.length; i++)
@@ -190,8 +189,8 @@ class WebConsolePlugin extends SimpleWebConsolePlugin
 
 
         // prepare variables
-        DefaultVariableResolver vars = ((DefaultVariableResolver) 
WebConsoleUtil.getVariableResolver(request));
-        vars.put("__data__", w.toString()); //$NON-NLS-1$
+        RequestVariableResolver vars = this.getVariableResolver(request);
+        vars.put("__data__", w.toString());
 
         response.getWriter().print(TEMPLATE);
     }
@@ -200,28 +199,28 @@ class WebConsolePlugin extends SimpleWebConsolePlugin
         throws IOException
     {
         jw.object();
-        jw.key("id"); //$NON-NLS-1$
+        jw.key("id");
         jw.value(pack.getName());
-        jw.key("name"); //$NON-NLS-1$
+        jw.key("name");
         jw.value(pack.getName());
-        jw.key("state"); //$NON-NLS-1$
+        jw.key("state");
         jw.value(pack.getVersion());
 
-        jw.key("actions"); //$NON-NLS-1$
+        jw.key("actions");
         jw.array();
 
         jw.object();
-        jw.key("enabled"); //$NON-NLS-1$
+        jw.key("enabled");
         jw.value(true);
-        jw.key("name"); //$NON-NLS-1$
+        jw.key("name");
         jw.value("Uninstall");
-        jw.key("link"); //$NON-NLS-1$
+        jw.key("link");
         jw.value(ACTION_UNINSTALL);
         jw.endObject();
 
         jw.endArray();
 
-        jw.key("props"); //$NON-NLS-1$
+        jw.key("props");
         jw.array();
         jw.object();
         jw.key("key");
@@ -241,9 +240,9 @@ class WebConsolePlugin extends SimpleWebConsolePlugin
         for (int i = 0; i < pack.getBundleInfos().length; i++)
         {
             buffer.append(pack.getBundleInfos()[i].getSymbolicName());
-            buffer.append(" - "); //$NON-NLS-1$
+            buffer.append(" - ");
             buffer.append(pack.getBundleInfos()[i].getVersion());
-            buffer.append("<br/>"); //$NON-NLS-1$
+            buffer.append("<br/>");
         }
         jw.object();
         jw.key("key");

Reply via email to