From 0c64059cf60d80d96874c91d7d376d4e741789b0 Mon Sep 17 00:00:00 2001 From: Imad ELALI Date: Fri, 5 Mar 2021 01:56:41 +0100 Subject: [PATCH] -Solve issue with Sling Starter (use bundle location as fallback) --- .../plugins/search/core/BundleInfo.java | 56 ++++++++++--------- .../plugins/search/core/OsgiExplorer.java | 31 +++++----- 2 files changed, 46 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/neva/felix/webconsole/plugins/search/core/BundleInfo.java b/src/main/java/com/neva/felix/webconsole/plugins/search/core/BundleInfo.java index 7c407ab..14f26e1 100644 --- a/src/main/java/com/neva/felix/webconsole/plugins/search/core/BundleInfo.java +++ b/src/main/java/com/neva/felix/webconsole/plugins/search/core/BundleInfo.java @@ -1,9 +1,9 @@ package com.neva.felix.webconsole.plugins.search.core; +import com.google.common.collect.ImmutableMap; import com.neva.felix.webconsole.plugins.search.rest.BundleClassesServlet; import com.neva.felix.webconsole.plugins.search.rest.BundleDownloadServlet; import com.neva.felix.webconsole.plugins.search.utils.BundleUtils; -import com.google.common.collect.ImmutableMap; import org.osgi.framework.Bundle; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; @@ -13,38 +13,44 @@ public class BundleInfo implements Serializable { - private final String symbolicName; + private final String symbolicName; - private final String name; + private final String name; + private final String location; - private final long id; + private final long id; - private final Map context; + private final Map context; - public BundleInfo(Bundle bundle, BundleContext context) { - this.symbolicName = bundle.getSymbolicName(); - this.name = (String) bundle.getHeaders().get(Constants.BUNDLE_NAME); - this.id = bundle.getBundleId(); - this.context = ImmutableMap.of( - "consoleUrl", BundleUtils.consolePath(context, bundle), - "bundleDownloadUrl", BundleDownloadServlet.url(context, bundle), - "bundleClassesUrl", BundleClassesServlet.url(context, bundle) - ); - } + public BundleInfo(Bundle bundle, BundleContext context) { + this.symbolicName = bundle.getSymbolicName(); + this.name = (String) bundle.getHeaders().get(Constants.BUNDLE_NAME); + this.id = bundle.getBundleId(); + this.location = bundle.getLocation(); + this.context = ImmutableMap.of( + "consoleUrl", BundleUtils.consolePath(context, bundle), + "bundleDownloadUrl", BundleDownloadServlet.url(context, bundle), + "bundleClassesUrl", BundleClassesServlet.url(context, bundle) + ); + } - public String getSymbolicName() { - return symbolicName; - } + public String getSymbolicName() { + return symbolicName; + } - public String getName() { - return name; - } + public String getName() { + return name; + } + + public long getId() { + return id; + } - public long getId() { - return id; + public String getLocation() { + return location; } public Map getContext() { - return context; - } + return context; + } } diff --git a/src/main/java/com/neva/felix/webconsole/plugins/search/core/OsgiExplorer.java b/src/main/java/com/neva/felix/webconsole/plugins/search/core/OsgiExplorer.java index 349d5d8..ecb2d93 100644 --- a/src/main/java/com/neva/felix/webconsole/plugins/search/core/OsgiExplorer.java +++ b/src/main/java/com/neva/felix/webconsole/plugins/search/core/OsgiExplorer.java @@ -26,7 +26,6 @@ import java.io.File; import java.io.IOException; import java.util.Collections; -import java.util.Comparator; import java.util.List; import java.util.Set; @@ -70,22 +69,20 @@ public File findJar(String bundleId) { } public File findJar(Long bundleId) { - return findJar(findDir(bundleId)); + File jar = findJar(findDir(bundleId)); + if (jar == null) { + //If jat not found through storage dir, try to resolve bundle location (Sling starter sources jars from maven repository) + Bundle bundle = findBundle(bundleId); + return new File(StringUtils.replace(bundle.getLocation(), "reference:file:", "")); + } + return jar; } public File findJar(File bundleDir) { if (bundleDir.exists()) { - List files = FluentIterable.from(FileUtils.listFiles(bundleDir, new String[]{JAR_EXT}, true)).filter(new Predicate() { - @Override - public boolean apply(File file) { - return file.getName().equalsIgnoreCase(BUNDLE_JAR_FILE); - } - }).toSortedList(new Comparator() { - @Override - public int compare(File f1, File f2) { - return f2.getAbsolutePath().compareTo(f1.getAbsolutePath()); - } - }); + List files = FluentIterable.from(FileUtils.listFiles(bundleDir, new String[]{JAR_EXT}, true)) + .filter(file -> file.getName().equalsIgnoreCase(BUNDLE_JAR_FILE)) + .toSortedList((f1, f2) -> f2.getAbsolutePath().compareTo(f1.getAbsolutePath())); return Iterables.getFirst(files, null); } @@ -147,7 +144,7 @@ public String decompileClass(Decompilers type, Long bundleId, String className) return decompileClass(type, findJar(bundleId), className); } - public String decompileClass(Decompilers type,File jar, String className) { + public String decompileClass(Decompilers type, File jar, String className) { String source = StringUtils.EMPTY; try { source = DecompilerFactory.get(type).decompile(jar, className, false); @@ -259,9 +256,11 @@ private BundleClass obtainClassMetadata(BundleClass clazz, String classPid) { } public Bundle findBundle(String id) { - final Long longId = Longs.tryParse(id); + return findBundle(Longs.tryParse(id)); + } - return longId != null ? context.getBundle(longId) : null; + public Bundle findBundle(Long id) { + return id != null ? context.getBundle(id) : null; } public Iterable findBundles(List ids) {