This is an automated email from the ASF dual-hosted git repository.

jbonofre pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/karaf.git


The following commit(s) were added to refs/heads/main by this push:
     new a310779708 KARAF-5421 - add user friendly error when installing non 
OSGi bundle via bundle:install
     new df7ac38397 Merge pull request #1713 from awrb/KARAF-5421
a310779708 is described below

commit a310779708e7df59bd9be1026411bce4f918966f
Author: Aleksy Wróblewski <[email protected]>
AuthorDate: Tue Feb 7 22:50:39 2023 +0100

    KARAF-5421 - add user friendly error when installing non OSGi bundle via 
bundle:install
---
 .../org/apache/karaf/bundle/command/Install.java   | 22 ++++++++++++++++------
 .../java/org/apache/karaf/itests/BundleTest.java   | 12 ++++++++++++
 2 files changed, 28 insertions(+), 6 deletions(-)

diff --git 
a/bundle/core/src/main/java/org/apache/karaf/bundle/command/Install.java 
b/bundle/core/src/main/java/org/apache/karaf/bundle/command/Install.java
index 1a8701faeb..7af1474c59 100644
--- a/bundle/core/src/main/java/org/apache/karaf/bundle/command/Install.java
+++ b/bundle/core/src/main/java/org/apache/karaf/bundle/command/Install.java
@@ -49,13 +49,13 @@ public class Install implements Action {
 
     @Option(name = "-l", aliases={"--start-level"}, description="Sets the 
start level of the bundles", required = false, multiValued = false)
     Integer level;
-    
+
     @Option(name = "--force", aliases = {"-f"}, description = "Forces the 
command to execute", required = false, multiValued = false)
     boolean force;
 
     @Option(name = "--r3-bundles", description = "Allow OSGi R3 bundles")
     boolean allowR3;
-    
+
     @Reference
     Session session;
 
@@ -82,15 +82,26 @@ public class Install implements Action {
         for (URI url : urls) {
             try {
                 Bundle bundle = bundleContext.installBundle(url.toString(), 
null);
-                if 
(!"2".equals(bundle.getHeaders().get(Constants.BUNDLE_MANIFESTVERSION))) {
+                String manifestVersion = 
bundle.getHeaders().get(Constants.BUNDLE_MANIFESTVERSION);
+
+                if (!"2".equals(manifestVersion)) {
                     if (allowR3) {
                         if (!r3warned) {
                             System.err.println("WARNING: use of OSGi r3 
bundles is discouraged");
                             r3warned = true;
                         }
                     } else {
+                        String error;
+                        if (manifestVersion == null) {
+                            error = "The provided URL is not a valid OSGi 
bundle. Consider using Karaf "
+                                    + "wrap feature, e.g. `install 
wrap:mvn:groupId/artifactId/version`. See more here:"
+                                    + " 
https://karaf.apache.org/manual/latest/#_wrap_deployer";;
+                        } else {
+                            error = "OSGi R3 bundle not supported";
+                        }
+
                         bundle.uninstall();
-                        throw new BundleException("OSGi R3 bundle not 
supported");
+                        throw new BundleException(error);
                     }
                 }
                 bundles.add(bundle);
@@ -118,7 +129,7 @@ public class Install implements Action {
                 }
             }
         }
-        
+
         // print the installed bundles
         if (bundles.size() == 1) {
             System.out.println("Bundle ID: " + bundles.get(0).getBundleId());
@@ -131,5 +142,4 @@ public class Install implements Action {
         MultiException.throwIf("Error installing bundles", exceptions);
         return null;
     }
-
 }
diff --git a/itests/test/src/test/java/org/apache/karaf/itests/BundleTest.java 
b/itests/test/src/test/java/org/apache/karaf/itests/BundleTest.java
index 529efc6fa0..9e96a19295 100644
--- a/itests/test/src/test/java/org/apache/karaf/itests/BundleTest.java
+++ b/itests/test/src/test/java/org/apache/karaf/itests/BundleTest.java
@@ -18,6 +18,7 @@ package org.apache.karaf.itests;
 
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import javax.management.MBeanServer;
 import javax.management.ObjectName;
@@ -122,6 +123,17 @@ public class BundleTest extends BaseTest {
         assertBundleNotInstalled("org.apache.servicemix.bundles.commons-lang");
     }
 
+    @Test
+    public void installNonOsgiBundle() throws Exception {
+        try {
+            executeCommand("bundle:install mvn:junit/junit/4.13.2", 
ADMIN_ROLES);
+            fail();
+        } catch (Exception e) {
+            assertContains("The provided URL is not a valid OSGi bundle."
+                    + " Consider using Karaf wrap feature", e.getMessage());
+        }
+    }
+
     @Test
     public void showTreeCommand() throws Exception {
         String bundleTreeOutput = executeCommand("bundle:tree-show 
org.apache.karaf.management.server", ADMIN_ROLES);

Reply via email to