Author: tjwatson
Date: Fri Jan 25 22:26:31 2019
New Revision: 1852186

URL: http://svn.apache.org/viewvc?rev=1852186&view=rev
Log:
FELIX-6026 - Fix ScrInfo service issues

Modified:
    felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/Activator.java
    
felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/ComponentCommands.java
    
felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/ComponentCommandsScrInfo.java

Modified: felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/Activator.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/Activator.java?rev=1852186&r1=1852185&r2=1852186&view=diff
==============================================================================
--- felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/Activator.java 
(original)
+++ felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/Activator.java Fri 
Jan 25 22:26:31 2019
@@ -174,6 +174,7 @@ public class Activator extends AbstractE
         m_componentCommands = new ComponentCommands(m_context, runtime, 
m_configuration);
         m_componentCommands.register();
         
m_componentCommands.updateProvideScrInfoService(m_configuration.infoAsService());
+        m_configuration.setScrCommand(m_componentCommands);
     }
 
     @Override

Modified: 
felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/ComponentCommands.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/ComponentCommands.java?rev=1852186&r1=1852185&r2=1852186&view=diff
==============================================================================
--- 
felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/ComponentCommands.java 
(original)
+++ 
felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/ComponentCommands.java 
Fri Jan 25 22:26:31 2019
@@ -142,7 +142,14 @@ public class ComponentCommands {
 
     @Descriptor("List all components")
     public ComponentDescriptionDTO[] list() {
-        return scr.getComponentDescriptionDTOs().toArray(new 
ComponentDescriptionDTO[0]);
+        ComponentDescriptionDTO[] result = 
scr.getComponentDescriptionDTOs().toArray(new ComponentDescriptionDTO[0]);
+        Arrays.sort(result, new Comparator<ComponentDescriptionDTO>() {
+            @Override
+            public int compare(ComponentDescriptionDTO c1, 
ComponentDescriptionDTO c2) {
+                return Long.compare(c1.bundle.id, c2.bundle.id);
+            }
+        });
+        return result;
     }
 
     @Descriptor("List components of a specific bundle")

Modified: 
felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/ComponentCommandsScrInfo.java
URL: 
http://svn.apache.org/viewvc/felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/ComponentCommandsScrInfo.java?rev=1852186&r1=1852185&r2=1852186&view=diff
==============================================================================
--- 
felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/ComponentCommandsScrInfo.java
 (original)
+++ 
felix/trunk/scr/src/main/java/org/apache/felix/scr/impl/ComponentCommandsScrInfo.java
 Fri Jan 25 22:26:31 2019
@@ -38,24 +38,13 @@ public class ComponentCommandsScrInfo im
 
     @Override
     public void list(String bundleIdentifier, PrintWriter out) {
-        long bundleId;
-
-        try {
-            bundleId = Long.parseLong(bundleIdentifier);
-        } catch (NumberFormatException e) {
-            // might be a BSN
-            Bundle bundle = findBundle(bundleIdentifier);
-            if (bundle == null) throw new IllegalArgumentException("Cannot 
find bundle with ID: " + bundleIdentifier);
-            bundleId = bundle.getBundleId();
-        }
-
         final CharSequence formatted;
         try {
-            ComponentDescriptionDTO[] dtos = commands.list(bundleId);
+            ComponentDescriptionDTO[] dtos = bundleIdentifier == null ? 
commands.list() : commands.list(getBundleID(bundleIdentifier));
             if (dtos != null) {
                 formatted = commands.format(dtos, 1);
             } else {
-                formatted = "No components found for bundle  " + bundleId;
+                formatted = "No components found for bundle  " + 
bundleIdentifier;
             }
         } catch (Exception e) {
             throw new RuntimeException("Error listing or formatting SCR 
runtime information", e);
@@ -63,27 +52,55 @@ public class ComponentCommandsScrInfo im
         out.println(formatted);
     }
 
-    @Override
-    public void info(String componentId, PrintWriter out) {
-        Object infoObj;
+    private long getBundleID(String bundleIdentifier) {
         try {
-            long configId = Long.parseLong(componentId);
-            infoObj = commands.info(configId);
+            return Long.parseLong(bundleIdentifier);
         } catch (NumberFormatException e) {
-            infoObj = commands.info(componentId);
+            // might be a BSN
+            Bundle bundle = findBundle(bundleIdentifier);
+            if (bundle == null) throw new IllegalArgumentException("Cannot 
find bundle with ID: " + bundleIdentifier);
+            return bundle.getBundleId();
         }
+    }
 
-        CharSequence formatted;
-        if (infoObj != null) {
+    @Override
+    public void info(String componentId, PrintWriter out) {
+        if (componentId != null) {
+            Object infoObj;
+            try {
+                long configId = Long.parseLong(componentId);
+                infoObj = commands.info(configId);
+            } catch (NumberFormatException e) {
+                infoObj = commands.info(componentId);
+            }
+            CharSequence formatted = format(infoObj, out, Converter.INSPECT);
+            if (formatted == null) {
+                out.println("No component found with ID " + componentId);
+            } else {
+                out.println(formatted);
+            }
+            return;
+        }
+
+        ComponentDescriptionDTO[] components = commands.list();
+        if (components.length > 0) {
+            for (ComponentDescriptionDTO componentDescriptionDTO : components) 
{
+                out.println(format(componentDescriptionDTO, out, 
Converter.INSPECT));
+            }
+        } else {
+            out.println("No component found.");
+        }
+    }
+
+    private CharSequence format(Object info, PrintWriter out, int level) {
+        if (info != null) {
             try {
-                formatted = commands.format(infoObj, Converter.INSPECT);
+                return commands.format(info, level);
             } catch (Exception e) {
                 throw new RuntimeException("Error formatting SCR runtime 
information", e);
             }
-        } else {
-            formatted = "No component found with ID " + componentId;
         }
-        out.println(formatted);
+        return null;
     }
 
     @Override


Reply via email to