/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.karaf.bundle.command;

import java.util.Collection;

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.BundleWiring;

@Command(scope = "bundle", name = "classes", description = "Displays a list of classes/resources contained in the bundle")
@Service
public class Classes extends BundlesCommand {

    @Option(name = "-a", aliases={"--display-all-files"}, description="List all classes and files in the bundle", required = false, multiValued = false)
    boolean displayAllFiles;

    @Override
    protected void executeOnBundle(Bundle bundle) throws Exception {
        BundleWiring wiring = bundle.adapt(BundleWiring.class);
        if (wiring != null){
            Collection<String> resources;
            if (displayAllFiles){
                resources = wiring.listResources("/", null, BundleWiring.LISTRESOURCES_RECURSE);
            } else {
                resources = wiring.listResources("/", "*class", BundleWiring.LISTRESOURCES_RECURSE);
            }
            Collection<String> localResources;
            if (displayAllFiles) {
                localResources = wiring.listResources("/", null, BundleWiring.LISTRESOURCES_RECURSE | BundleWiring.LISTRESOURCES_LOCAL);
            } else {
                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);
                }
            }
        } else {
            System.out.println("Bundle " + bundle.getBundleId() + " is not resolved.");
        }
    }


}

@@ -855,26 +855,99 @@
 va.util.
-Collection
+ArrayList;%0Aimport java.util.Collection;%0Aimport java.util.List;%0Aimport java.util.Map
 ;%0A%0Aimpor
@@ -1189,16 +1189,116 @@
 Bundle;%0A
+import org.osgi.framework.wiring.BundleCapability;%0Aimport org.osgi.framework.wiring.BundleRevision;%0A
 import o
@@ -1909,32 +1909,87 @@
 ing%3E resources;%0A
+            List%3CString%3E exports = getExports(bundle);%0A
             if (
@@ -2701,45 +2701,166 @@
     
-if (localResources.contains(resource)
+StringBuilder stringBuilder = new StringBuilder();%0A                boolean localResource = localResources.contains(resource);%0A                if(localResource
 ) %7B%0A
@@ -2879,34 +2879,36 @@
         
-System.out.println
+stringBuilder.append
 (SimpleA
@@ -2929,273 +2929,1523 @@
 BOLD
- + resource + SimpleAnsi.INTENSITY_NORMAL);%0A                %7D else %7B%0A                    System.out.println(resource);%0A         
+);%0A                %7D%0A                if(ids == null %7C%7C ids.size() != 1) %7B%0A                    stringBuilder.append(bundle.getBundleId() + %22 %7C %22);%0A                %7D%0A                stringBuilder.append(resource + %22 %7C %22);%0A                stringBuilder.append(%22exported: %22 + isExported(resource, exports));%0A                if(localResource) %7B%0A                    stringBuilder.append(SimpleAnsi.INTENSITY_NORMAL);%0A                %7D%0A%0A                System.out.println(stringBuilder.toString());%0A            %7D%0A        %7D else %7B%0A            System.out.println(%22Bundle %22 + bundle.getBundleId() + %22 is not resolved.%22);%0A        %7D%0A    %7D%0A%0A    private boolean isExported(String className, List%3CString%3E exports) throws Exception %7B%0A        boolean exported = false;%0A        String packageName = className.substring(0, className.lastIndexOf(%22/%22)).replaceAll(%22/%22, %22.%22);%0A        if(exports.contains(packageName)) %7B%0A
        
-%7D%0A
      
-       %7D%0A        %7D else %7B%0A            System.out.println(%22Bundle %22 + bundle.getBundleId() + %22 is not resolved.%22);%0A        %7D
+exported = true;%0A        %7D%0A        return exported;%0A    %7D%0A%0A    private List%3CString%3E getExports(Bundle bundle) throws Exception %7B%0A        List%3CString%3E exports = new ArrayList%3C%3E();%0A        BundleRevision rev = bundle.adapt(BundleRevision.class);%0A        List%3CBundleCapability%3E caps = rev.getDeclaredCapabilities(BundleRevision.PACKAGE_NAMESPACE);%0A        for (BundleCapability cap : caps) %7B%0A            Map%3CString, Object%3E attr = cap.getAttributes();%0A            String packageName = (String)attr.get(BundleRevision.PACKAGE_NAMESPACE);%0A            exports.add(packageName);%0A        %7D%0A        return exports;
 %0A   

Add bundle ID in bundle:classes output