FELIX-3888 change ScrInfo interface style to use single PrintWriter for normal output and throw IllegalArgumentException for problems

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1442009 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/main/java/org/apache/felix/scr/ScrInfo.java b/scr/src/main/java/org/apache/felix/scr/ScrInfo.java
index a26fea8..15343b1 100644
--- a/scr/src/main/java/org/apache/felix/scr/ScrInfo.java
+++ b/scr/src/main/java/org/apache/felix/scr/ScrInfo.java
@@ -18,12 +18,12 @@
  */
 package org.apache.felix.scr;
 
-import java.io.PrintStream;
+import java.io.PrintWriter;
 
 /**
  * Abstraction of command interface.
  * 
- * @Since 1.6.4
+ * @Since 1.8
  */
 public interface ScrInfo
 {
@@ -32,23 +32,23 @@
      * List in text the components for the bundle specified, or all components if null, sorted by component ID
      * @param bundleIdentifier bundle the components are in or null for all components
      * @param out PrintStream for normal output
-     * @param err PrintStream for errors.
+     * @throws IllegalArgumentException if nothing can be found
      */
-    void list(String bundleIdentifier, PrintStream out, PrintStream err);
+    void list(String bundleIdentifier, PrintWriter out);
 
     /**
      * List in text detailed information about the specified components.  Components can be specified by 
      * numeric componentId, component name, a regexp to match for component name, or null for all components.
      * @param componentId specifier for desired components
      * @param out PrintStream for normal output
-     * @param err PrintStream for errors.
+     * @throws IllegalArgumentException if nothing can be found
      */
-    void info(String componentId, PrintStream out, PrintStream err);
+    void info(String componentId, PrintWriter out);
 
     /**
      * List in text the current SCR configuration
      * @param out PrintStream for output.
      */
-    void config(PrintStream out);
+    void config(PrintWriter out);
 
 }
\ No newline at end of file
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/ScrCommand.java b/scr/src/main/java/org/apache/felix/scr/impl/ScrCommand.java
index 7dde985..13fab45 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/ScrCommand.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/ScrCommand.java
@@ -19,6 +19,7 @@
 package org.apache.felix.scr.impl;
 
 import java.io.PrintStream;
+import java.io.PrintWriter;
 import java.lang.reflect.Constructor;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -144,7 +145,7 @@
     /* (non-Javadoc)
      * @see org.apache.felix.scr.impl.ScrInfo#list(java.lang.String, java.io.PrintStream, java.io.PrintStream)
      */
-    public void list(final String bundleIdentifier, final PrintStream out, final PrintStream err)
+    public void list(final String bundleIdentifier, final PrintWriter out)
     {
         Component[] components;
 
@@ -172,8 +173,7 @@
 
             if (bundle == null)
             {
-                err.println("Missing bundle with ID " + bundleIdentifier);
-                return;
+                throw new IllegalArgumentException("Missing bundle with ID " + bundleIdentifier);
             }
             if (ComponentRegistry.isBundleActive(bundle))
             {
@@ -215,14 +215,15 @@
         {
             out.println( String.format( "[%1$4d] [%2$s] [%3$4d] %4$s", component.getId(), toStateString( component.getState() ), component.getBundle().getBundleId(), component.getName() ) );
         }
-    }
+        out.flush();
+   }
 
     /* (non-Javadoc)
      * @see org.apache.felix.scr.impl.ScrInfo#info(java.lang.String, java.io.PrintStream, java.io.PrintStream)
      */
-    public void info(final String componentId, PrintStream out, PrintStream err)
+    public void info(final String componentId, PrintWriter out)
     {
-        Component[] components = getComponentFromArg(componentId, err);
+        Component[] components = getComponentFromArg(componentId);
         if (components == null)
         {
             return;
@@ -394,11 +395,13 @@
                 }
             }
         }
+        out.flush();
     }
 
-    void change(final String componentIdentifier, PrintStream out, PrintStream err, boolean enable)
+    void change(final String componentIdentifier, PrintWriter out, boolean enable)
     {
-        Component[] components = getComponentFromArg(componentIdentifier, err);
+        Component[] components = getComponentFromArg(componentIdentifier);
+        ArrayList<String> disposed = new ArrayList<String>();
         if (components == null)
         {
             return;
@@ -408,7 +411,7 @@
         {
             if ( component.getState() == Component.STATE_DISPOSED )
             {
-                err.println( "Component " + component.getName() + " already disposed, cannot change state" );
+                disposed.add(component.getName());
             }
             else if ( enable )
             {
@@ -435,12 +438,18 @@
                 }
             }
         }
+        out.flush();
+        if ( !disposed.isEmpty() )
+        {
+            throw new IllegalArgumentException( "Components " + disposed + " already disposed, cannot change state" );
+
+        }
     }
 
     /* (non-Javadoc)
      * @see org.apache.felix.scr.impl.ScrInfo#config(java.io.PrintStream)
      */
-    public void config(PrintStream out)
+    public void config(PrintWriter out)
     {
         out.print("Log Level: ");
         out.println(scrConfiguration.getLogLevel());
@@ -481,7 +490,7 @@
         }
     }
 
-    private Component[] getComponentFromArg(final String componentIdentifier, PrintStream err)
+    private Component[] getComponentFromArg(final String componentIdentifier)
     {
         Component[] components = null;
         if (componentIdentifier != null)
@@ -492,7 +501,7 @@
                 Component component = scrService.getComponent(componentId);
                 if (component == null)
                 {
-                    err.println("Missing Component with ID " + componentId);
+                    throw new IllegalArgumentException("Missing Component with ID " + componentId);
                 }
                 else
                 {
@@ -505,10 +514,6 @@
                 
                 // check whether it is a component name
                 components = scrService.getComponents(componentIdentifier);
-                if (components == null)
-                {
-                    err.println("Missing Component with ID " + componentIdentifier);
-                }
             }
         }
         if ( components == null)
@@ -525,6 +530,10 @@
                         cs.add( component );
                     }
                 }
+                if (cs.isEmpty())
+                {
+                    throw new IllegalArgumentException("No Component with ID or matching " + componentIdentifier);
+                }
                 components = cs.toArray( new Component[cs.size()] );
             }
         }
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/ScrGogoCommand.java b/scr/src/main/java/org/apache/felix/scr/impl/ScrGogoCommand.java
index 838c82c..3a12e87 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/ScrGogoCommand.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/ScrGogoCommand.java
@@ -18,6 +18,8 @@
  */
 package org.apache.felix.scr.impl;
 
+import java.io.PrintWriter;
+
 import org.apache.felix.service.command.Descriptor;
 
 /**
@@ -57,37 +59,72 @@
     @Descriptor("List all components")
     public void list()
     {
-        scrCommand.list(null, System.out, System.err);
+        try
+        {
+            scrCommand.list(null, new PrintWriter(System.out));
+        }
+        catch ( IllegalArgumentException e )
+        {
+            System.err.println(e.getMessage());
+        }
     }
 
     @Descriptor("List components of a specific bundle")
     public void list(@Descriptor("Symbolic name or ID of the bundle") final String bundleIdentifier)
     {
-        scrCommand.list(bundleIdentifier, System.out, System.err);
+        try
+        {
+            scrCommand.list(bundleIdentifier, new PrintWriter(System.out));
+        }
+        catch ( IllegalArgumentException e )
+        {
+            System.err.println(e.getMessage());
+        }
     }
 
     @Descriptor("Dump information of a component")
     public void info(@Descriptor("Name or ID of the component") final String componentIdentifier)
     {
-        scrCommand.info(componentIdentifier, System.out, System.err);
+        try
+        {
+            scrCommand.info(componentIdentifier, new PrintWriter(System.out));
+        }
+        catch ( IllegalArgumentException e )
+        {
+            System.err.println(e.getMessage());
+        }
     }
 
     @Descriptor("Enable a disabled component")
     public void enable(@Descriptor("Name or ID of the component") final String componentIdentifier)
     {
-        scrCommand.change(componentIdentifier, System.out, System.err, true);
+        try
+        {
+            scrCommand.change(componentIdentifier, new PrintWriter(System.out), true);
+        }
+        catch ( IllegalArgumentException e )
+        {
+            System.err.println(e.getMessage());
+        }
     }
 
     @Descriptor("Disable an enabled component")
     public void disable(@Descriptor("Name or ID of the component") final String componentIdentifier)
     {
-        scrCommand.change(componentIdentifier, System.out, System.err, false);
+        try
+        {
+            scrCommand.change(componentIdentifier, new PrintWriter(System.out), false);
+        }
+        catch ( IllegalArgumentException e )
+        {
+            System.err.println(e.getMessage());
+        }
     }
 
     @Descriptor("Show the current SCR configuration")
     public void config()
     {
-        scrCommand.config(System.out);
+        scrCommand.config(new PrintWriter(System.out));
     }
 
 }
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/ScrShellCommand.java b/scr/src/main/java/org/apache/felix/scr/impl/ScrShellCommand.java
index c77331c..b7045ee 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/ScrShellCommand.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/ScrShellCommand.java
@@ -19,6 +19,7 @@
 package org.apache.felix.scr.impl;
 
 import java.io.PrintStream;
+import java.io.PrintWriter;
 import java.util.StringTokenizer;
 import org.apache.felix.shell.Command;
 
@@ -84,29 +85,37 @@
         else
         {
             String arg = (st.hasMoreTokens()) ? st.nextToken() : null;
-            if (command.equals(LIST_CMD))
+            PrintWriter pw = new PrintWriter(out);
+            try
             {
-                scrCommand.list(arg, out, err);
+                if (command.equals(LIST_CMD))
+                {
+                    scrCommand.list(arg, pw);
+                }
+                else if (command.equals(INFO_CMD))
+                {
+                    scrCommand.info(arg, pw);
+                }
+                else if (command.equals(ENABLE_CMD))
+                {
+                    scrCommand.change(arg, pw, true);
+                }
+                else if (command.equals(DISABLE_CMD))
+                {
+                    scrCommand.change(arg, pw, false);
+                }
+                else if (command.equals(CONFIG_CMD))
+                {
+                    scrCommand.config(pw);
+                }
+                else
+                {
+                    err.println("Unknown command: " + command);
+                }
             }
-            else if (command.equals(INFO_CMD))
+            catch ( IllegalArgumentException e )
             {
-                scrCommand.info(arg, out, err);
-            }
-            else if (command.equals(ENABLE_CMD))
-            {
-                scrCommand.change(arg, out, err, true);
-            }
-            else if (command.equals(DISABLE_CMD))
-            {
-                scrCommand.change(arg, out, err, false);
-            }
-            else if (command.equals(CONFIG_CMD))
-            {
-                scrCommand.config(out);
-            }
-            else
-            {
-                err.println("Unknown command: " + command);
+                System.err.println(e.getMessage());
             }
         }
     }