|
16 | 16 | */ |
17 | 17 | package org.apache.karaf.bundle.command; |
18 | 18 |
|
| 19 | +import java.util.List; |
| 20 | +import org.apache.karaf.shell.api.action.Argument; |
19 | 21 | import org.apache.karaf.shell.api.action.Command; |
| 22 | +import org.apache.karaf.shell.api.action.Option; |
20 | 23 | import org.apache.karaf.shell.api.action.lifecycle.Service; |
21 | 24 | import org.osgi.framework.Bundle; |
22 | 25 |
|
23 | 26 | /** |
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 |
25 | 28 | * wired imports. |
26 | 29 | */ |
27 | 30 | @Command(scope = "bundle", name = "dynamic-import", description = "Enables/disables dynamic-import for a given bundle.") |
28 | 31 | @Service |
29 | 32 | public class DynamicImport extends BundleCommand { |
30 | 33 |
|
| 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 | + |
31 | 43 | @Override |
32 | 44 | 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)) { |
34 | 62 | System.out.printf("Disabling dynamic imports on bundle %s%n", bundle); |
35 | | - bundleService.disableDynamicImports(bundle); |
| 63 | + bundleService.setDynamicImports(bundle, List.of()); |
36 | 64 | } else { |
37 | 65 | System.out.printf("Enabling dynamic imports on bundle %s%n", bundle); |
38 | | - bundleService.enableDynamicImports(bundle); |
| 66 | + bundleService.setDynamicImports(bundle, List.of("*")); |
39 | 67 | } |
40 | 68 | return null; |
41 | 69 | } |
|
0 commit comments