Skip to content

Commit aa3f71f

Browse files
Added option to configure dynamic imports (#2682) (#2724)
Co-authored-by: CptBartender <fryderyk.wysocki@gmail.com>
1 parent ec61b76 commit aa3f71f

3 files changed

Lines changed: 54 additions & 13 deletions

File tree

bundle/core/src/main/java/org/apache/karaf/bundle/command/DynamicImport.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,54 @@
1616
*/
1717
package org.apache.karaf.bundle.command;
1818

19+
import java.util.List;
20+
import org.apache.karaf.shell.api.action.Argument;
1921
import org.apache.karaf.shell.api.action.Command;
22+
import org.apache.karaf.shell.api.action.Option;
2023
import org.apache.karaf.shell.api.action.lifecycle.Service;
2124
import org.osgi.framework.Bundle;
2225

2326
/**
24-
* Command for enabling/disabling debug logging on a bundle and calculating the difference in
27+
* Command for enabling/disabling dynamic imports on a bundle and calculating the difference in
2528
* wired imports.
2629
*/
2730
@Command(scope = "bundle", name = "dynamic-import", description = "Enables/disables dynamic-import for a given bundle.")
2831
@Service
2932
public class DynamicImport extends BundleCommand {
3033

34+
@Argument(index = 1, name = "packages", description = "Bundle URLs separated by whitespaces", required = false, multiValued = true)
35+
List<String> packages;
36+
37+
@Option(name = "--enable", aliases = {"-e"}, description = "Forces the command to execute", required = false, multiValued = false)
38+
boolean enable;
39+
40+
@Option(name = "--disable", aliases = {"-d"}, description = "Forces the command to execute", required = false, multiValued = false)
41+
boolean disable;
42+
3143
@Override
3244
protected Object doExecute(Bundle bundle) throws Exception {
33-
if (bundleService.isDynamicImport(bundle)) {
45+
if (enable && disable) {
46+
throw new IllegalArgumentException("Cannot 'enable' and 'disable' at the same time");
47+
}
48+
if ((enable || disable) && (packages != null && !packages.isEmpty())) {
49+
throw new IllegalArgumentException("Options are incompatible with providing package list");
50+
}
51+
if (enable) {
52+
System.out.printf("Enabling dynamic imports on bundle %s%n", bundle);
53+
bundleService.setDynamicImports(bundle, List.of("*"));
54+
} else if (disable) {
55+
System.out.printf("Disabling dynamic imports on bundle %s%n", bundle);
56+
bundleService.setDynamicImports(bundle, List.of());
57+
} else if (packages != null && !packages.isEmpty()) {
58+
System.out.printf("Enabling dynamic imports for [%s] on bundle %s%n",
59+
String.join(", ",packages), bundle);
60+
bundleService.setDynamicImports(bundle, packages);
61+
} else if (bundleService.isDynamicImport(bundle)) {
3462
System.out.printf("Disabling dynamic imports on bundle %s%n", bundle);
35-
bundleService.disableDynamicImports(bundle);
63+
bundleService.setDynamicImports(bundle, List.of());
3664
} else {
3765
System.out.printf("Enabling dynamic imports on bundle %s%n", bundle);
38-
bundleService.enableDynamicImports(bundle);
66+
bundleService.setDynamicImports(bundle, List.of("*"));
3967
}
4068
return null;
4169
}

bundle/core/src/main/java/org/apache/karaf/bundle/core/BundleService.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ public interface BundleService {
4444

4545
boolean isDynamicImport(Bundle bundle);
4646

47-
void enableDynamicImports(Bundle bundle);
48-
49-
void disableDynamicImports(Bundle bundle);
47+
void setDynamicImports(Bundle bundle, List<String> packages);
5048

5149
int getSystemBundleThreshold();
5250

bundle/core/src/main/java/org/apache/karaf/bundle/core/internal/BundleServiceImpl.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818

1919
import java.net.URL;
2020
import java.util.ArrayList;
21+
import java.util.Arrays;
2122
import java.util.Collections;
2223
import java.util.HashMap;
2324
import java.util.Iterator;
2425
import java.util.List;
2526
import java.util.Map;
27+
import java.util.Optional;
2628
import java.util.Set;
2729
import java.util.concurrent.CopyOnWriteArrayList;
2830

@@ -223,18 +225,27 @@ private boolean canBeSatisfied(BundleRequirement req) {
223225
return false;
224226
}
225227

228+
public void setDynamicImports(Bundle bundle, List<String> packages) {
229+
if (packages == null || packages.isEmpty()) {
230+
disableDynamicImports(bundle);
231+
} else {
232+
enableDynamicImports(bundle, String.join(",", packages));
233+
}
234+
}
235+
226236
/*
227237
* Enable DynamicImport=* on the bundle
228238
*/
229-
public void enableDynamicImports(Bundle bundle) {
239+
private void enableDynamicImports(Bundle bundle, String packageString) {
230240
String location =
231241
String.format("wrap:%s$" +
232242
"Bundle-UpdateLocation=%s&" +
233-
"DynamicImport-Package=*&" +
243+
"DynamicImport-Package=%s&" +
234244
"%s=%s&" +
235245
"overwrite=merge",
236246
bundle.getLocation(),
237247
bundle.getLocation(),
248+
packageString,
238249
ORIGINAL_WIRES,
239250
explode(getWiredBundles(bundle).keySet()));
240251
LOG.debug(format("Updating %s with URL %s", bundle, location));
@@ -254,11 +265,15 @@ public void enableDynamicImports(Bundle bundle) {
254265
* At this time, we will also calculate the difference in package wiring for the bundle compared to
255266
* when we enabled the DynamicImport
256267
*/
257-
public void disableDynamicImports(Bundle bundle) {
268+
private void disableDynamicImports(Bundle bundle) {
258269
Set<String> current = getWiredBundles(bundle).keySet();
259-
for (String original : bundle.getHeaders().get(ORIGINAL_WIRES).split(",")) {
260-
current.remove(original);
261-
}
270+
Optional.of(bundle)
271+
.map(Bundle::getHeaders)
272+
.map(dict -> dict.get(ORIGINAL_WIRES))
273+
.map(wires -> wires.split(","))
274+
.stream()
275+
.flatMap(Arrays::stream)
276+
.forEach(current::remove);
262277

263278
if (current.isEmpty()) {
264279
LOG.debug("No additional packages have been wired since dynamic import was enabled");

0 commit comments

Comments
 (0)