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 7f226495b6 FELIX-6631 : Migrate webconsole plugins to jakarta.servlet
api
7f226495b6 is described below
commit 7f226495b65bcc7ef76ca2d51ac38a25060f9b4a
Author: Carsten Ziegeler <[email protected]>
AuthorDate: Tue Aug 22 09:03:06 2023 +0200
FELIX-6631 : Migrate webconsole plugins to jakarta.servlet api
---
webconsole-plugins/packageadmin/pom.xml | 29 +++---
.../plugins/packageadmin/internal/Activator.java | 106 ++++++-------------
.../internal/ExportedPackageComparator.java | 1 +
.../packageadmin/internal/WebConsolePlugin.java | 112 ++++++++++-----------
.../packageadmin/internal/WebConsolePrinter.java | 18 +---
5 files changed, 101 insertions(+), 165 deletions(-)
diff --git a/webconsole-plugins/packageadmin/pom.xml
b/webconsole-plugins/packageadmin/pom.xml
index 3a250f8cf8..2de700839b 100644
--- a/webconsole-plugins/packageadmin/pom.xml
+++ b/webconsole-plugins/packageadmin/pom.xml
@@ -73,13 +73,18 @@
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
- <version>2.3.6</version>
+ <version>5.1.9</version>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>
${project.artifactId}
</Bundle-SymbolicName>
+ <Import-Package>
+ jakarta.servlet;version="[5,7)",
+ jakarta.servlet.http;version="[5,7)",
+ *
+ </Import-Package>
<Private-Package>
org.apache.felix.webconsole.plugins.packageadmin.*
</Private-Package>
@@ -114,38 +119,32 @@
<dependencies>
<dependency>
<groupId>org.osgi</groupId>
- <artifactId>org.osgi.core</artifactId>
- <version>4.3.1</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.osgi</groupId>
- <artifactId>org.osgi.compendium</artifactId>
- <version>4.0.0</version>
+ <artifactId>osgi.core</artifactId>
+ <version>6.0.0</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>
<dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>servlet-api</artifactId>
- <version>2.3</version>
+ <groupId>jakarta.servlet</groupId>
+ <artifactId>jakarta.servlet-api</artifactId>
+ <version>5.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
- <version>2.5</version>
+ <version>2.13.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.webconsole</artifactId>
- <version>4.2.0</version>
+ <version>4.8.13-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
diff --git
a/webconsole-plugins/packageadmin/src/main/java/org/apache/felix/webconsole/plugins/packageadmin/internal/Activator.java
b/webconsole-plugins/packageadmin/src/main/java/org/apache/felix/webconsole/plugins/packageadmin/internal/Activator.java
index dddb4f1740..f3b18f5cc4 100644
---
a/webconsole-plugins/packageadmin/src/main/java/org/apache/felix/webconsole/plugins/packageadmin/internal/Activator.java
+++
b/webconsole-plugins/packageadmin/src/main/java/org/apache/felix/webconsole/plugins/packageadmin/internal/Activator.java
@@ -23,106 +23,58 @@ import java.util.Hashtable;
import org.apache.felix.inventory.Format;
import org.apache.felix.inventory.InventoryPrinter;
-import org.apache.felix.webconsole.SimpleWebConsolePlugin;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
-import org.osgi.util.tracker.ServiceTracker;
-import org.osgi.util.tracker.ServiceTrackerCustomizer;
+import org.osgi.service.packageadmin.PackageAdmin;
+
+import jakarta.servlet.Servlet;
/**
* This is the main starting class of the bundle.
*/
-public class Activator implements BundleActivator, ServiceTrackerCustomizer
-{
-
- private ServiceTracker pkgAdminTracker;
+@SuppressWarnings("deprecation")
+public class Activator implements BundleActivator {
- private BundleContext context;
- private SimpleWebConsolePlugin plugin;
+ private ServiceRegistration<Servlet> plugin;
private ServiceRegistration<InventoryPrinter> printerReg;
/**
* @see
org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
- public void start(final BundleContext context) throws Exception
- {
- this.context = context;
- this.pkgAdminTracker = new ServiceTracker(context,
- "org.osgi.service.packageadmin.PackageAdmin", this); //$NON-NLS-1$
- this.pkgAdminTracker.open();
-
- // register configuration printer
- final Dictionary<String, Object> props = new Hashtable<String,
Object>();
- props.put(InventoryPrinter.NAME, "duplicate_exports"); //$NON-NLS-1$
- props.put(InventoryPrinter.TITLE, "Duplicate Exports"); //$NON-NLS-1$
- props.put(InventoryPrinter.FORMAT, new String[] {
Format.TEXT.toString() });
-
- printerReg = context.registerService(
- InventoryPrinter.class,
- new WebConsolePrinter(context, pkgAdminTracker), props);
+ public void start(final BundleContext context) throws Exception {
+ // package admin is always available
+ final ServiceReference<PackageAdmin> packageAdmin =
context.getServiceReference(PackageAdmin.class);
+ if (packageAdmin != null ) {
+ final PackageAdmin pa = context.getService(packageAdmin);
+ if (pa != null) {
+ // register configuration printer
+ final Dictionary<String, Object> props = new Hashtable<String,
Object>();
+ props.put(InventoryPrinter.NAME, "duplicate_exports");
//$NON-NLS-1$
+ props.put(InventoryPrinter.TITLE, "Duplicate Exports");
//$NON-NLS-1$
+ props.put(InventoryPrinter.FORMAT, new String[] {
Format.TEXT.toString() });
+
+ this.printerReg = context.registerService(
+ InventoryPrinter.class,
+ new WebConsolePrinter(context, pa), props);
+ this.plugin = new WebConsolePlugin(context, pa).register();
+
+ }
+ }
}
/**
* @see
org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
- public void stop(final BundleContext context) throws Exception
- {
- if (printerReg != null)
- {
+ public void stop(final BundleContext context) throws Exception {
+ if (printerReg != null) {
printerReg.unregister();
printerReg = null;
}
-
- if (this.pkgAdminTracker != null)
- {
- this.pkgAdminTracker.close();
- this.pkgAdminTracker = null;
- }
-
- this.context = null;
- }
-
- // - begin tracker
- /**
- * @see
org.osgi.util.tracker.ServiceTrackerCustomizer#modifiedService(org.osgi.framework.ServiceReference,
- * java.lang.Object)
- */
- public final void modifiedService(ServiceReference reference, Object
service)
- {/* unused */
- }
-
- /**
- * @see
org.osgi.util.tracker.ServiceTrackerCustomizer#addingService(org.osgi.framework.ServiceReference)
- */
- public final Object addingService(ServiceReference reference)
- {
- SimpleWebConsolePlugin plugin = this.plugin;
- Object ret = null;
- if (plugin == null)
- {
- ret = context.getService(reference);
- this.plugin = plugin = new WebConsolePlugin(context,
ret).register(context);
- }
-
- return ret;
- }
-
- /**
- * @see
org.osgi.util.tracker.ServiceTrackerCustomizer#removedService(org.osgi.framework.ServiceReference,
- * java.lang.Object)
- */
- public final void removedService(ServiceReference reference, Object
service)
- {
- SimpleWebConsolePlugin plugin = this.plugin;
-
- if (pkgAdminTracker.size() <= 1 && plugin != null)
- {
- plugin.unregister();
+ if (this.plugin != null) {
+ this.plugin.unregister();
this.plugin = null;
}
-
}
-
}
diff --git
a/webconsole-plugins/packageadmin/src/main/java/org/apache/felix/webconsole/plugins/packageadmin/internal/ExportedPackageComparator.java
b/webconsole-plugins/packageadmin/src/main/java/org/apache/felix/webconsole/plugins/packageadmin/internal/ExportedPackageComparator.java
index 3186fd068e..397d170383 100644
---
a/webconsole-plugins/packageadmin/src/main/java/org/apache/felix/webconsole/plugins/packageadmin/internal/ExportedPackageComparator.java
+++
b/webconsole-plugins/packageadmin/src/main/java/org/apache/felix/webconsole/plugins/packageadmin/internal/ExportedPackageComparator.java
@@ -22,6 +22,7 @@ import java.util.Comparator;
import org.osgi.service.packageadmin.ExportedPackage;
+@SuppressWarnings("deprecation")
final class ExportedPackageComparator implements Comparator<ExportedPackage>
{
diff --git
a/webconsole-plugins/packageadmin/src/main/java/org/apache/felix/webconsole/plugins/packageadmin/internal/WebConsolePlugin.java
b/webconsole-plugins/packageadmin/src/main/java/org/apache/felix/webconsole/plugins/packageadmin/internal/WebConsolePlugin.java
index e4f749d804..fd9485c42d 100644
---
a/webconsole-plugins/packageadmin/src/main/java/org/apache/felix/webconsole/plugins/packageadmin/internal/WebConsolePlugin.java
+++
b/webconsole-plugins/packageadmin/src/main/java/org/apache/felix/webconsole/plugins/packageadmin/internal/WebConsolePlugin.java
@@ -24,8 +24,10 @@ import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
+import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashMap;
+import java.util.Hashtable;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
@@ -37,20 +39,22 @@ import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
-import javax.servlet.ServletException;
-import javax.servlet.http.HttpServlet;
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
+import jakarta.servlet.Servlet;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServlet;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.io.IOUtils;
import org.apache.felix.utils.json.JSONWriter;
import org.apache.felix.utils.manifest.Clause;
import org.apache.felix.utils.manifest.Parser;
-import org.apache.felix.webconsole.SimpleWebConsolePlugin;
-import org.apache.felix.webconsole.WebConsoleUtil;
+import org.apache.felix.webconsole.servlet.AbstractServlet;
+import org.apache.felix.webconsole.servlet.ServletConstants;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceRegistration;
import org.osgi.service.packageadmin.ExportedPackage;
import org.osgi.service.packageadmin.PackageAdmin;
@@ -58,51 +62,47 @@ import org.osgi.service.packageadmin.PackageAdmin;
* Provides a Web bases interface to the Packages Admin service, allowing
* the user to find package / maven information and identify duplicate exports.
*/
-class WebConsolePlugin extends SimpleWebConsolePlugin
-{
+@SuppressWarnings("deprecation")
+class WebConsolePlugin extends AbstractServlet {
+
private static final long serialVersionUID = 1L;
- private static final String LABEL = "depfinder"; //$NON-NLS-1$
- private static final String TITLE = "%pluginTitle"; //$NON-NLS-1$
- private static final String CATEGORY = "OSGi"; //$NON-NLS-1$
- private static final String CSS[] = { "/" + LABEL + "/res/plugin.css" };
//$NON-NLS-1$ //$NON-NLS-2$
+ private static final String LABEL = "depfinder";
+ private static final String TITLE = "%pluginTitle";
+ private static final String CATEGORY = "OSGi";
+ private static final String CSS[] = { "/" + LABEL + "/res/plugin.css" };
- @SuppressWarnings("deprecation")
private static final Comparator<ExportedPackage> EXPORT_PACKAGE_COMPARATOR
= new ExportedPackageComparator();
- @SuppressWarnings("deprecation")
private final PackageAdmin pa;
private final BundleContext bc;
// templates
private final String TEMPLATE;
- @SuppressWarnings("deprecation")
- WebConsolePlugin(BundleContext bc, Object pa)
- {
- super(LABEL, TITLE, CATEGORY, CSS);
-
+ WebConsolePlugin(BundleContext bc, PackageAdmin pa) {
this.pa = (PackageAdmin) pa;
this.bc = bc;
// load templates
- TEMPLATE = readTemplateFile("/res/plugin.html"); //$NON-NLS-1$
+ try {
+ TEMPLATE = readTemplateFile("/res/plugin.html");
+ } catch (IOException e) {
+ throw new RuntimeException("Cannot load template files", e);
+ }
}
-
- @Override
- public String getCategory()
- {
- return CATEGORY;
+ public ServiceRegistration<Servlet> register() {
+ 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);
+ return this.bc.registerService(Servlet.class, this, props);
}
-
- /**
- * @see
org.apache.felix.webconsole.AbstractWebConsolePlugin#renderContent(HttpServletRequest,
HttpServletResponse)
- */
@Override
- protected final void renderContent(HttpServletRequest req,
- HttpServletResponse response) throws ServletException, IOException
- {
+ public final void renderContent(HttpServletRequest req,
+ HttpServletResponse response) throws ServletException, IOException {
response.getWriter().print(TEMPLATE);
}
@@ -115,12 +115,11 @@ class WebConsolePlugin extends SimpleWebConsolePlugin
{
final Object json;
- String action = req.getParameter("action"); //$NON-NLS-1$
- if ("deps".equals(action)) { //$NON-NLS-1$
+ String action = req.getParameter("action");
+ if ("deps".equals(action)) {
json = doFindDependencies(req, pa);
}
- else if ("dups".equals(action)) { //$NON-NLS-1$
- @SuppressWarnings("deprecation")
+ else if ("dups".equals(action)) {
Map<String, Set<ExportedPackage>> packages =
collectExportedPackages(
pa, bc);
json = doFindDuplicates(packages);
@@ -130,14 +129,13 @@ class WebConsolePlugin extends SimpleWebConsolePlugin
throw new ServletException("Invalid action: " + action);
}
- WebConsoleUtil.setNoCache(resp);
- resp.setContentType("application/json; utf-8"); //$NON-NLS-1$
+ AbstractServlet.setNoCache(resp);
+ resp.setContentType("application/json; utf-8");
JSONWriter writer = new JSONWriter(resp.getWriter());
writer.value(json);
writer.flush();
}
- @SuppressWarnings("deprecation")
static final Map<String, Set<ExportedPackage>> collectExportedPackages(
final PackageAdmin pa, final BundleContext bundleContext)
{
@@ -165,13 +163,12 @@ class WebConsolePlugin extends SimpleWebConsolePlugin
return exports;
}
- @SuppressWarnings("deprecation")
private static final Map<String, Object>
doFindDependencies(HttpServletRequest req,
PackageAdmin pa)
{
final Map<String, Object> json = new HashMap<String, Object>();
- final String findField = req.getParameter("plugin.find"); //$NON-NLS-1$
+ final String findField = req.getParameter("plugin.find");
if (findField != null)
{
Set<String> packageNames = getPackageNames(findField);
@@ -181,7 +178,7 @@ class WebConsolePlugin extends SimpleWebConsolePlugin
{
String name = i.next();
@SuppressWarnings("unchecked")
- List<Object> pl = (List<Object>)json.get("packages");
//$NON-NLS-1$
+ List<Object> pl = (List<Object>)json.get("packages");
if ( pl == null )
{
pl = new ArrayList<Object>();
@@ -191,7 +188,7 @@ class WebConsolePlugin extends SimpleWebConsolePlugin
}
final Map<String, Object> mavenJson = new HashMap<String,
Object>();
- json.put("maven", mavenJson); //$NON-NLS-1$
+ json.put("maven", mavenJson);
for (Iterator<Bundle> i = exportingBundles.iterator();
i.hasNext();)
{
Bundle bundle = i.next();
@@ -207,7 +204,6 @@ class WebConsolePlugin extends SimpleWebConsolePlugin
}
- @SuppressWarnings("deprecation")
private static final Collection<Object> doFindDuplicates(
final Map<String, Set<ExportedPackage>> exports)
{
@@ -224,13 +220,13 @@ class WebConsolePlugin extends SimpleWebConsolePlugin
{
ExportedPackage exportedPackage = packageIter.next();
final Map<String, Object> json = toJSON(exportedPackage);
- container.put("name", exportedPackage.getName());
//$NON-NLS-1$
+ container.put("name", exportedPackage.getName());
@SuppressWarnings("unchecked")
- List<Object> imps = (List<Object>)
container.get("entries"); //$NON-NLS-1$
+ List<Object> imps = (List<Object>)
container.get("entries");
if ( imps == null )
{
imps = new ArrayList<Object>();
- container.put("entries", imps); //$NON-NLS-1$
+ container.put("entries", imps);
}
imps.add(json);
}
@@ -241,10 +237,10 @@ class WebConsolePlugin extends SimpleWebConsolePlugin
private static final void toJSON(Bundle bundle, Map<String, Object> json)
{
- json.put("bid", bundle.getBundleId()); //$NON-NLS-1$
+ json.put("bid", bundle.getBundleId());
if ( bundle.getSymbolicName() != null )
{
- json.put("bsn", bundle.getSymbolicName()); //$NON-NLS-1$
+ json.put("bsn", bundle.getSymbolicName());
}
}
@@ -267,27 +263,25 @@ class WebConsolePlugin extends SimpleWebConsolePlugin
}
}
@SuppressWarnings("unchecked")
- List<Object> imps = (List<Object>) json.get("importers");
//$NON-NLS-1$
+ List<Object> imps = (List<Object>) json.get("importers");
if ( imps == null )
{
imps = new ArrayList<Object>();
- json.put("importers", imps); //$NON-NLS-1$
+ json.put("importers", imps);
}
imps.add(usingJson);
}
}
- @SuppressWarnings("deprecation")
private static final Map<String, Object> toJSON(final ExportedPackage pkg)
{
final Map<String, Object> ret = new HashMap<String, Object>();
- ret.put("version", pkg.getVersion()); //$NON-NLS-1$
+ ret.put("version", pkg.getVersion());
toJSON(pkg.getExportingBundle(), ret);
toJSON(pkg.getName(), pkg.getImportingBundles(), ret);
return ret;
}
- @SuppressWarnings("deprecation")
private static final Map<String, Object> getPackageInfo(String
packageName, PackageAdmin pa,
Set<Bundle> exportingBundles)
{
@@ -297,16 +291,16 @@ class WebConsolePlugin extends SimpleWebConsolePlugin
{
final ExportedPackage x = exports[i];
@SuppressWarnings("unchecked")
- List<Object> el = (List<Object>)ret.get("exporters"); //$NON-NLS-1$
+ List<Object> el = (List<Object>)ret.get("exporters");
if ( el == null )
{
el = new ArrayList<Object>();
- ret.put("exporters", el); //$NON-NLS-1$
+ ret.put("exporters", el);
}
el.add(toJSON(x));
exportingBundles.add(x.getExportingBundle());
}
- ret.put("name", packageName); //$NON-NLS-1$
+ ret.put("name", packageName);
return ret;
}
@@ -314,7 +308,7 @@ class WebConsolePlugin extends SimpleWebConsolePlugin
{
Map<Object, Object> ret = null;
- Enumeration<URL> entries = bundle.findEntries("META-INF/maven",
"pom.properties", true); //$NON-NLS-1$ //$NON-NLS-2$
+ Enumeration<URL> entries = bundle.findEntries("META-INF/maven",
"pom.properties", true); //$NON-NLS-2$
if (entries != null)
{
URL u = entries.nextElement();
@@ -341,7 +335,7 @@ class WebConsolePlugin extends SimpleWebConsolePlugin
static final Set<String> getPackageNames(String findField)
{
- StringTokenizer tok = new StringTokenizer(findField, " \t\n\f\r");
//$NON-NLS-1$
+ StringTokenizer tok = new StringTokenizer(findField, " \t\n\f\r");
SortedSet<String> result = new TreeSet<String>();
while (tok.hasMoreTokens())
{
diff --git
a/webconsole-plugins/packageadmin/src/main/java/org/apache/felix/webconsole/plugins/packageadmin/internal/WebConsolePrinter.java
b/webconsole-plugins/packageadmin/src/main/java/org/apache/felix/webconsole/plugins/packageadmin/internal/WebConsolePrinter.java
index ad2ef16ee5..e0baefc17d 100644
---
a/webconsole-plugins/packageadmin/src/main/java/org/apache/felix/webconsole/plugins/packageadmin/internal/WebConsolePrinter.java
+++
b/webconsole-plugins/packageadmin/src/main/java/org/apache/felix/webconsole/plugins/packageadmin/internal/WebConsolePrinter.java
@@ -32,34 +32,26 @@ import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.service.packageadmin.ExportedPackage;
import org.osgi.service.packageadmin.PackageAdmin;
-import org.osgi.util.tracker.ServiceTracker;
+@SuppressWarnings("deprecation")
class WebConsolePrinter implements InventoryPrinter
{
- private final ServiceTracker tracker;
+ private final PackageAdmin pa;
private final BundleContext bc;
- WebConsolePrinter(BundleContext bc, ServiceTracker tracker)
+ WebConsolePrinter(BundleContext bc, PackageAdmin pa)
{
this.bc = bc;
- this.tracker = tracker;
+ this.pa = pa;
}
/**
* @see org.apache.felix.inventory.InventoryPrinter#print(
* java.io.PrintWriter, org.apache.felix.inventory.Format, boolean)
*/
- @SuppressWarnings("deprecation")
public void print(PrintWriter pw, Format format, boolean isZip)
{
- final PackageAdmin pa = (PackageAdmin) tracker.getService();
- if (pa == null)
- {
- pw.println("Status: PackageAdmin Service not registered");
- return;
- }
-
try
{
Map<String, Set<ExportedPackage>> exports =
WebConsolePlugin.collectExportedPackages(
@@ -78,7 +70,6 @@ class WebConsolePrinter implements InventoryPrinter
}
}
- @SuppressWarnings("deprecation")
private void dumpDuplicatesAsTxt(final PrintWriter pw,
final Map<String, Set<ExportedPackage>> exports)
{
@@ -170,5 +161,4 @@ class WebConsolePrinter implements InventoryPrinter
{
return "Duplicate Exports";
}
-
}