Repository navigation
Conversation
22d1d4f to
f085ddb
Compare
The command lists features that contain a specific bundle
f085ddb to
6f2a387
Compare
| <dependency> | ||
| <!-- for ContainsBundleCommand --> | ||
| <groupId>org.apache.karaf.bundle</groupId> | ||
| <artifactId>org.apache.karaf.bundle.core</artifactId> | ||
| </dependency> |
There was a problem hiding this comment.
Adding org.apache.karaf.bundle.core here makes org.apache.karaf.features.command require the org.apache.karaf.bundle.core package and BundleService OSGi service at runtime. However, in assemblies/features/standard/src/main/feature/feature.xml, the feature feature does not depend on the bundle feature.
As a result, in minimal/custom assemblies that include feature + shell without bundle, org.apache.karaf.features.command will fail to resolve, and because CommandExtension tracks all @Reference fields in the bundle via a single AggregateServiceTracker, a missing BundleService would delay registration of all feature:* commands.
We can drop this dependency completely by injecting OSGi BundleContext:
@Reference
BundleContext bundleContext;like RegionInfoCommand does, in ContainsBundleCommand.
| @Option(name = "-b", description = "Show features containing the given bundle id", required = false, multiValued = false) | ||
| Integer bundleId; |
There was a problem hiding this comment.
Nit: OSGi bundle IDs are long (Bundle.getBundleId()), so Long bundleId; is preferable to Integer.
| @Option(name = "-i", aliases = {"--installed"}, description = "Display a list of all installed features only", required = false, multiValued = false) | ||
| boolean onlyInstalled; |
There was a problem hiding this comment.
Nit: the description "Display a list of all installed features only" looks copied from ListFeaturesCommand.
Something like "Only list installed features" would fit feature:contains better.
| @Reference | ||
| BundleService bundleService; |
There was a problem hiding this comment.
If we replace BundleService with @Reference BundleContext bundleContext;, we avoid the mandatory runtime dependency on org.apache.karaf.bundle.core and BundleService (and also avoid BundleServiceImpl start-level < 50 / systembundles role filtering on getBundle / selectBundles.
| if (bundleId != null) { | ||
| Bundle bundle = bundleService.getBundle(bundleId.toString()); | ||
| if (bundle == null) | ||
| throw new CommandException("No bundles found with id " + bundleId); | ||
| bundleLocation = bundleService.getInfo(bundle).getUpdateLocation(); | ||
| } else if (bundleName != null) { | ||
| Bundle bundle = bundleService.selectBundles(List.of(), true).stream() | ||
| .filter(b -> bundleName.equals(b.getSymbolicName())) | ||
| .findAny() | ||
| .orElseThrow(() -> new CommandException("No bundles found with name " + bundleName)); | ||
| bundleLocation = bundleService.getInfo(bundle).getUpdateLocation(); | ||
| } else if (bundleUrl != null) { | ||
| bundleLocation = bundleUrl; | ||
| } else { | ||
| throw new CommandException("Either bundle id, name or URL must be specified"); | ||
| } |
There was a problem hiding this comment.
A few issues in the bundle resolution block:
- Mutually exclusive options: Passing more than one of
-b,-s,-usilently ignores the others. Consider validating that at most one is provided and throwingCommandExceptionotherwise. - Dead
nullcheck on-b:bundleService.getBundle(bundleId.toString())never returnsnull, it print"Bundle ... is invalid"toSystem.errand throwsIllegalArgumentExceptionif missing (or forbidden). UsingbundleContext.getBundle(bundleId)returnsnullwhen not found, making theCommandExceptioncheck work property. - Multiple versions with
-s:.findAny()arbitrarily picks one bundle if multiple versions with the same symbolic name are installed, which is the exact scenario described in Add command that lists features that contain a specific bundle #2523! Collecting the locations of all matching bundles (usingArrays.stream(bundleContext.getBundles())) and matching features against any of them handles conflicting versions property. Bundle-UpdateLocationvsbundle.getLocation():getUpdateLocation()prefers theBundle-UpdateLocationheader overbundle.getLocation(), which may differ from the feature'sBundleInfo.getLocation()(e.g. fromwrap:bundles). Checkingbundle.getLocation()(and optionallyConstants.BUNDLE_UPDATELOCATIONif present) is safer.- Blank
-u: Should also reject empty/blankbundleUrlso-u ""doesn't match every feature.
| Feature[] features; | ||
| if (repository != null && !onlyInstalled) | ||
| features = featuresService.getFeatures(repository); | ||
| else | ||
| features = featuresService.listFeatures(); |
There was a problem hiding this comment.
I believe there are two bugs here:
featuresService.getFeatures(repository)looks up features matchingrepositoryby name/range, not a repository. It should usefeaturesService.getRepository(repository)(checking fornulland throwing aCommandExceptionif the repository is unknown) and call.getFeatures(), asListFeaturesCommanddoes.&& !onlyInstalledcauses--repositoryto be silently ignored when-i/--installedis also specified.--repositoryshould select the repository's features regardless ofonlyInstalled, and let.filter(f -> !onlyInstalled || featuresService.isInstalled(f))handle-i.
|
|
||
| Set<Feature> containingFeatures = Arrays.stream(features) | ||
| .filter(f -> !onlyInstalled || featuresService.isInstalled(f)) | ||
| .filter(f -> f.getBundles().stream().anyMatch(byBundleLocation(bundleLocation))) |
There was a problem hiding this comment.
f.getBundles() only checks top-level <bundle> declarations and misses bundles declared inside <conditional> blocks (f.getConditional(), see InfoFeatureCommand).
Checking both f.getBundles() and f.getConditional().stream().flatMap(c -> c.getBundles().stream()) will ensure conditional bundles are found too.
| containingFeatures.stream() | ||
| .map(f -> String.format("%s %s", f.getName(), f.getVersion())) | ||
| .sorted() | ||
| .forEach(System.out::println); |
There was a problem hiding this comment.
Have you considered using ShellTable (with Name, Version, and possible State / Repository plus --no-format) for consistency with feature:list, feature:version-list, and feature:provided?
If plain name version is intentional for scripting, that's also fine, though an empty-result message or ShellTable.emptyTableText(...) can be helpful.
| private Predicate<BundleInfo> byBundleLocation(String bundleLocation) { | ||
| return bundleInfo -> | ||
| { | ||
| String location = bundleInfo.getLocation(); | ||
| String originalLocation = bundleInfo.getOriginalLocation(); | ||
| return (location != null && location.startsWith(bundleLocation)) || | ||
| (originalLocation != null && originalLocation.startsWith(bundleLocation)); | ||
| }; | ||
| } |
There was a problem hiding this comment.
Using startsWith(bundleLocation) can produce false positive, for example looking up bundle mvn:org.foo/bar/1.0 via -b or -s will also match features containing mvn:org.foo/bar/1.0.1, mvn:org.foo/bar/1.0-SNAPSHOT, or mvn:org.foo/bar/1.0/war.
Exact matching (equals) should be used here (at least when resolved from -b/-s, and arguably -u as well unless prefix/wildcard matching is explicitly intended and documented).
There was a problem hiding this comment.
Thank you for all the pointers. I will review and apply them shortly
The command lists features that contain a specific bundle
I suppose this can also be applied on other branches.