Skip to content

Add feature:contains command (#2523) - #2541

Open
CMoH wants to merge 1 commit into
apache:karaf-4.4.xfrom
CMoH:feature-contains-bundle-command
Open

CMoH wants to merge 1 commit into
apache:karaf-4.4.xfrom
CMoH:feature-contains-bundle-command

Conversation

@CMoH

@CMoH CMoH commented Apr 15, 2026

Copy link
Copy Markdown
Contributor

The command lists features that contain a specific bundle

I suppose this can also be applied on other branches.

@CMoH
CMoH force-pushed the feature-contains-bundle-command branch from 22d1d4f to f085ddb Compare April 15, 2026 11:12
The command lists features that contain a specific bundle
@CMoH
CMoH force-pushed the feature-contains-bundle-command branch from f085ddb to 6f2a387 Compare April 15, 2026 11:15
@jbonofre
jbonofre self-requested a review April 15, 2026 11:39
Comment thread features/command/pom.xml
Comment on lines +57 to +61
<dependency>
<!-- for ContainsBundleCommand -->
<groupId>org.apache.karaf.bundle</groupId>
<artifactId>org.apache.karaf.bundle.core</artifactId>
</dependency>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +42 to +43
@Option(name = "-b", description = "Show features containing the given bundle id", required = false, multiValued = false)
Integer bundleId;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: OSGi bundle IDs are long (Bundle.getBundleId()), so Long bundleId; is preferable to Integer.

Comment on lines +51 to +52
@Option(name = "-i", aliases = {"--installed"}, description = "Display a list of all installed features only", required = false, multiValued = false)
boolean onlyInstalled;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +58 to +59
@Reference
BundleService bundleService;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +64 to +79
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");
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few issues in the bundle resolution block:

  • Mutually exclusive options: Passing more than one of -b, -s, -u silently ignores the others. Consider validating that at most one is provided and throwing CommandException otherwise.
  • Dead null check on -b: bundleService.getBundle(bundleId.toString()) never returns null, it print "Bundle ... is invalid" to System.err and throws IllegalArgumentException if missing (or forbidden). Using bundleContext.getBundle(bundleId) returns null when not found, making the CommandException check 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 (using Arrays.stream(bundleContext.getBundles())) and matching features against any of them handles conflicting versions property.
  • Bundle-UpdateLocation vs bundle.getLocation(): getUpdateLocation() prefers the Bundle-UpdateLocation header over bundle.getLocation(), which may differ from the feature's BundleInfo.getLocation() (e.g. from wrap: bundles). Checking bundle.getLocation() (and optionally Constants.BUNDLE_UPDATELOCATION if present) is safer.
  • Blank -u: Should also reject empty/blank bundleUrl so -u "" doesn't match every feature.

Comment on lines +84 to +88
Feature[] features;
if (repository != null && !onlyInstalled)
features = featuresService.getFeatures(repository);
else
features = featuresService.listFeatures();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe there are two bugs here:

  1. featuresService.getFeatures(repository) looks up features matching repository by name/range, not a repository. It should use featuresService.getRepository(repository) (checking for null and throwing a CommandException if the repository is unknown) and call .getFeatures(), as ListFeaturesCommand does.
  2. && !onlyInstalled causes --repository to be silently ignored when -i/--installed is also specified. --repository should select the repository's features regardless of onlyInstalled, 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)))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +95 to +98
containingFeatures.stream()
.map(f -> String.format("%s %s", f.getName(), f.getVersion()))
.sorted()
.forEach(System.out::println);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +101 to +109
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));
};
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for all the pointers. I will review and apply them shortly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants