[ 
https://issues.apache.org/jira/browse/KARAF-5752?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16501811#comment-16501811
 ] 

ASF GitHub Bot commented on KARAF-5752:
---------------------------------------

jbonofre closed pull request #522: [KARAF-5752] Add bundle ID in bundle:classes 
output
URL: https://github.com/apache/karaf/pull/522
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git 
a/bundle/core/src/main/java/org/apache/karaf/bundle/command/Classes.java 
b/bundle/core/src/main/java/org/apache/karaf/bundle/command/Classes.java
index 72d987cba3..78c1bc1673 100644
--- a/bundle/core/src/main/java/org/apache/karaf/bundle/command/Classes.java
+++ b/bundle/core/src/main/java/org/apache/karaf/bundle/command/Classes.java
@@ -16,13 +16,18 @@
  */
 package org.apache.karaf.bundle.command;
 
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.List;
+import java.util.Map;
 
 import org.apache.karaf.shell.api.action.Command;
 import org.apache.karaf.shell.api.action.Option;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
 import org.apache.karaf.shell.support.ansi.SimpleAnsi;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.wiring.BundleCapability;
+import org.osgi.framework.wiring.BundleRevision;
 import org.osgi.framework.wiring.BundleWiring;
 
 @Command(scope = "bundle", name = "classes", description = "Displays a list of 
classes/resources contained in the bundle")
@@ -37,6 +42,7 @@ protected void executeOnBundle(Bundle bundle) throws 
Exception {
         BundleWiring wiring = bundle.adapt(BundleWiring.class);
         if (wiring != null){
             Collection<String> resources;
+            List<String> exports = getExports(bundle);
             if (displayAllFiles){
                 resources = wiring.listResources("/", null, 
BundleWiring.LISTRESOURCES_RECURSE);
             } else {
@@ -49,16 +55,47 @@ protected void executeOnBundle(Bundle bundle) throws 
Exception {
                 localResources = wiring.listResources("/", "/*.class", 
BundleWiring.LISTRESOURCES_RECURSE | BundleWiring.LISTRESOURCES_LOCAL);
             }
             for (String resource:resources){
-                if (localResources.contains(resource)) {
-                    System.out.println(SimpleAnsi.INTENSITY_BOLD + resource + 
SimpleAnsi.INTENSITY_NORMAL);
-                } else {
-                    System.out.println(resource);
+                StringBuilder stringBuilder = new StringBuilder();
+                boolean localResource = localResources.contains(resource);
+                if(localResource) {
+                    stringBuilder.append(SimpleAnsi.INTENSITY_BOLD);
                 }
+                if(ids == null || ids.size() != 1) {
+                    stringBuilder.append(bundle.getBundleId() + " | ");
+                }
+                stringBuilder.append(resource + " | ");
+                stringBuilder.append("exported: " + isExported(resource, 
exports));
+                if(localResource) {
+                    stringBuilder.append(SimpleAnsi.INTENSITY_NORMAL);
+                }
+
+                System.out.println(stringBuilder.toString());
             }
         } else {
             System.out.println("Bundle " + bundle.getBundleId() + " is not 
resolved.");
         }
     }
 
+    private boolean isExported(String className, List<String> exports) throws 
Exception {
+        boolean exported = false;
+        String packageName = className.substring(0, 
className.lastIndexOf("/")).replaceAll("/", ".");
+        if(exports.contains(packageName)) {
+            exported = true;
+        }
+        return exported;
+    }
+
+    private List<String> getExports(Bundle bundle) throws Exception {
+        List<String> exports = new ArrayList<>();
+        BundleRevision rev = bundle.adapt(BundleRevision.class);
+        List<BundleCapability> caps = 
rev.getDeclaredCapabilities(BundleRevision.PACKAGE_NAMESPACE);
+        for (BundleCapability cap : caps) {
+            Map<String, Object> attr = cap.getAttributes();
+            String packageName = 
(String)attr.get(BundleRevision.PACKAGE_NAMESPACE);
+            exports.add(packageName);
+        }
+        return exports;
+    }
+
 
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> Add bundle ID in bundle:classes output
> --------------------------------------
>
>                 Key: KARAF-5752
>                 URL: https://issues.apache.org/jira/browse/KARAF-5752
>             Project: Karaf
>          Issue Type: Improvement
>          Components: karaf-shell
>            Reporter: Jean-Baptiste Onofré
>            Assignee: Jean-Baptiste Onofré
>            Priority: Major
>             Fix For: 4.2.1
>
>
> It would be very convenient to have the bundle ID when simply using 
> {{bundle:classes}}.
> The output could look like:
> {code}
> bundleId | classes | exported
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to