Distinguish between output format and delivery channel (web console or zip)

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1450616 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/inventory/src/main/java/org/apache/felix/inventory/InventoryPrinter.java b/inventory/src/main/java/org/apache/felix/inventory/InventoryPrinter.java
index f1ad0f4..16376f4 100644
--- a/inventory/src/main/java/org/apache/felix/inventory/InventoryPrinter.java
+++ b/inventory/src/main/java/org/apache/felix/inventory/InventoryPrinter.java
@@ -66,6 +66,13 @@
     String CONFIG_TITLE = "felix.inventory.printer.title"; //$NON-NLS-1$
 
     /**
+     * Optional property controlling whether the printer will be displayed
+     * in the web console. By default, a printer is displayed in the
+     * web console, unless this property is added with the value 'false'.
+     */
+    String CONFIG_WEBCONSOLE = "felix.inventory.printer.webconsole"; //$NON-NLS-1$
+
+    /**
      * Prints the configuration report to the given <code>printWriter</code>.
      * Implementations are free to print whatever information they deem useful.
      *
@@ -75,6 +82,7 @@
      * @param mode The render mode.
      * @param printWriter where to write the configuration data. It might be flushed,
      * but must not be closed.
+     * @param isZip whether this is included in a zip file or used directly
      */
-    void print( PrinterMode mode, PrintWriter printWriter );
+    void print( PrinterMode mode, PrintWriter printWriter, boolean isZip );
 }
diff --git a/inventory/src/main/java/org/apache/felix/inventory/PrinterMode.java b/inventory/src/main/java/org/apache/felix/inventory/PrinterMode.java
index fe1fdf6..478c4d0 100644
--- a/inventory/src/main/java/org/apache/felix/inventory/PrinterMode.java
+++ b/inventory/src/main/java/org/apache/felix/inventory/PrinterMode.java
@@ -26,18 +26,12 @@
     // plain text
     public static PrinterMode TEXT = new PrinterMode("TEXT");
 
-    // HTML which can be placed inside a HTML body element (no external references)
-    public static PrinterMode HTML_BODY = new PrinterMode("HTML_BODY");
+    // valid HTML fragment (no external references)
+    public static PrinterMode HTML_FRAGMENT = new PrinterMode("HTML_FRAGMENT");
 
     // JSON output
     public static PrinterMode JSON = new PrinterMode("JSON");
 
-    // file content for a zip
-    public static PrinterMode ZIP_FILE_TEXT = new PrinterMode("ZIP_FILE_TEXT");
-
-    // json file content for a zip
-    public static PrinterMode ZIP_FILE_JSON = new PrinterMode("ZIP_FILE_JSON");
-
     private final String mode;
 
     private PrinterMode(final String mode) {
@@ -47,14 +41,10 @@
     public static PrinterMode valueOf(final String m) {
         if ( TEXT.name().equals(m) ) {
             return TEXT;
-        } else if ( HTML_BODY.name().equals(m) ) {
-            return HTML_BODY;
+        } else if ( HTML_FRAGMENT.name().equals(m) ) {
+            return HTML_FRAGMENT;
         } else if ( JSON.name().equals(m) ) {
             return JSON;
-        } else if ( ZIP_FILE_TEXT.name().equals(m) ) {
-            return ZIP_FILE_TEXT;
-        } else if ( ZIP_FILE_JSON.name().equals(m) ) {
-            return ZIP_FILE_JSON;
         }
         return null;
     }
diff --git a/inventory/src/main/java/org/apache/felix/inventory/impl/AbstractWebConsolePlugin.java b/inventory/src/main/java/org/apache/felix/inventory/impl/AbstractWebConsolePlugin.java
index 71de906..312e09f 100644
--- a/inventory/src/main/java/org/apache/felix/inventory/impl/AbstractWebConsolePlugin.java
+++ b/inventory/src/main/java/org/apache/felix/inventory/impl/AbstractWebConsolePlugin.java
@@ -20,7 +20,6 @@
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.io.Writer;
-import java.text.MessageFormat;
 import java.util.Date;
 import java.util.zip.Deflater;
 import java.util.zip.ZipEntry;
@@ -139,9 +138,8 @@
             zip.closeEntry();
 
             final ZipConfigurationWriter pw = new ZipConfigurationWriter( zip );
-            printConfigurationInventory( pw, PrinterMode.ZIP_FILE_TEXT, handler );
-            pw.counter = 0;
-            printConfigurationInventory( pw, PrinterMode.ZIP_FILE_JSON, handler );
+            printConfigurationInventory( pw, PrinterMode.TEXT, handler );
+            printConfigurationInventory( pw, PrinterMode.JSON, handler );
 
             zip.finish();
         } else if ( request.getPathInfo().endsWith( ".nfo" ) ) {
@@ -157,11 +155,15 @@
             pw.println ( "<html xmlns=\"http://www.w3.org/1999/xhtml\">" );
             pw.println ( "<head><title>dummy</title></head><body><div>" );
 
-            if ( handler.supports(PrinterMode.HTML_BODY) ) {
-                handler.print(PrinterMode.HTML_BODY, pw);
+            if ( handler.supports(PrinterMode.HTML_FRAGMENT) ) {
+                handler.print(PrinterMode.HTML_FRAGMENT, pw, false);
+            } else if ( handler.supports(PrinterMode.TEXT) ) {
+                pw.enableFilter( true );
+                handler.print(PrinterMode.TEXT, pw, false);
+                pw.enableFilter( false );
             } else {
                 pw.enableFilter( true );
-                handler.print(PrinterMode.TEXT, pw);
+                handler.print(PrinterMode.JSON, pw, false);
                 pw.enableFilter( false );
             }
             pw.println( "</div></body></html>" );
@@ -218,20 +220,18 @@
             if ( handler.supports(PrinterMode.JSON) ) {
                 pw.print("<button type=\"button\" class=\"downloadJson\" style=\"float: right; margin-right: 30px; margin-top: 5px;\">Download As JSON</button>");
             }
-            if ( handler.supports(PrinterMode.ZIP_FILE_TEXT) || handler.supports(PrinterMode.ZIP_FILE_JSON) ) {
-                pw.print("<button type=\"button\" class=\"downloadZip\" style=\"float: right; margin-right: 30px; margin-top: 5px;\">Download As Zip</button>");
-            }
+            pw.print("<button type=\"button\" class=\"downloadZip\" style=\"float: right; margin-right: 30px; margin-top: 5px;\">Download As Zip</button>");
             if ( handler.supports(PrinterMode.TEXT ) ) {
                 pw.print("<button type=\"button\" class=\"downloadTxt\" style=\"float: right; margin-right: 30px; margin-top: 5px;\">Download As Text</button>");
             }
 
             pw.println("<br/>&nbsp;</p>"); // status line
             pw.print("<div>");
-            if ( handler.supports(PrinterMode.HTML_BODY) ) {
-                handler.print(PrinterMode.HTML_BODY, pw);
+            if ( handler.supports(PrinterMode.HTML_FRAGMENT) ) {
+                handler.print(PrinterMode.HTML_FRAGMENT, pw, false);
             } else {
                 pw.enableFilter( true );
-                handler.print(PrinterMode.TEXT, pw);
+                handler.print(PrinterMode.TEXT, pw, false);
                 pw.enableFilter( false );
             }
             pw.print("</div>");
@@ -261,7 +261,7 @@
                 final InventoryPrinterHandler handler)
         throws IOException {
             this.title(handler.getTitle());
-            handler.print(mode, this);
+            handler.print(mode, this, false);
             this.end();
         }
     }
@@ -412,54 +412,34 @@
 
         private final ZipOutputStream zip;
 
-        private int counter;
-
         ZipConfigurationWriter( final ZipOutputStream zip ) {
             super( new OutputStreamWriter( zip ) );
             this.zip = zip;
         }
 
-        private String getFormattedTitle(final String title) {
-            return MessageFormat.format( "{0,number,000}-{1}", new Object[]
-                    { new Integer( counter ), title } );
-        }
-
-        protected void title( final String title ) throws IOException {
-            counter++;
-
-            final String name = getFormattedTitle(title).concat(".txt");
-
-            final ZipEntry entry = new ZipEntry( name );
-            zip.putNextEntry( entry );
-        }
-
-        protected void end() throws IOException {
-            flush();
-
-            zip.closeEntry();
-        }
-
         public void printInventory(
                 final PrinterMode mode,
                 final InventoryPrinterHandler handler)
         throws IOException {
-            if ( mode == PrinterMode.ZIP_FILE_TEXT ) {
-                super.printInventory(mode, handler);
-                final String title = getFormattedTitle(handler.getTitle());
-                handler.addAttachments(title.concat("/"), this.zip);
-            } else {
-                counter++;
-                final String title = getFormattedTitle(handler.getTitle());
-                final String name = "json/".concat(title).concat(".json");
+            if ( mode == PrinterMode.TEXT ) {
+                final ZipEntry entry = new ZipEntry( handler.getName().concat(".txt") );
+                zip.putNextEntry( entry );
+                handler.print(mode, this, false);
+                flush();
+                zip.closeEntry();
+
+                handler.addAttachments(handler.getName().concat("/"), this.zip);
+            } else if ( mode == PrinterMode.JSON ) {
+                final String name = "json/".concat(handler.getName()).concat(".json");
 
                 final ZipEntry entry = new ZipEntry( name );
                 zip.putNextEntry( entry );
-                handler.print(PrinterMode.ZIP_FILE_JSON, this);
+                handler.print(PrinterMode.JSON, this, true);
                 flush();
 
                 zip.closeEntry();
-                if ( !handler.supports(PrinterMode.ZIP_FILE_TEXT) ) {
-                    handler.addAttachments(title.concat("/"), this.zip);
+                if ( !handler.supports(PrinterMode.TEXT) ) {
+                    handler.addAttachments(handler.getName().concat("/"), this.zip);
                 }
             }
         }
diff --git a/inventory/src/main/java/org/apache/felix/inventory/impl/DefaultWebConsolePlugin.java b/inventory/src/main/java/org/apache/felix/inventory/impl/DefaultWebConsolePlugin.java
index b77592d..48d5536 100644
--- a/inventory/src/main/java/org/apache/felix/inventory/impl/DefaultWebConsolePlugin.java
+++ b/inventory/src/main/java/org/apache/felix/inventory/impl/DefaultWebConsolePlugin.java
@@ -77,9 +77,9 @@
     }
 
     /**
-     * @see org.apache.felix.inventory.InventoryPrinter#print(org.apache.felix.inventory.PrinterMode, java.io.PrintWriter)
+     * @see org.apache.felix.inventory.InventoryPrinter#print(org.apache.felix.inventory.PrinterMode, java.io.PrintWriter, boolean)
      */
-    public void print(final PrinterMode mode, final PrintWriter printWriter) {
+    public void print(final PrinterMode mode, final PrintWriter printWriter, final boolean isZip) {
         final InventoryPrinterHandler[] handlers = this.inventoryPrinterManager.getAllHandlers();
         printWriter.print("Currently registered ");
         printWriter.print(String.valueOf(handlers.length));
diff --git a/inventory/src/main/java/org/apache/felix/inventory/impl/InventoryPrinterAdapter.java b/inventory/src/main/java/org/apache/felix/inventory/impl/InventoryPrinterAdapter.java
index 29b1f2d..a896647 100644
--- a/inventory/src/main/java/org/apache/felix/inventory/impl/InventoryPrinterAdapter.java
+++ b/inventory/src/main/java/org/apache/felix/inventory/impl/InventoryPrinterAdapter.java
@@ -56,7 +56,7 @@
 
             // print(String, PrintWriter)
             printMethod = ClassUtils.searchMethod(service.getClass(), "print",
-                    new Class[] {String.class, PrintWriter.class});
+                    new Class[] {String.class, PrintWriter.class, Boolean.class});
             if ( printMethod == null ) {
                 return null;
             }
@@ -113,9 +113,11 @@
     }
 
     public void registerConsole(final BundleContext context, final InventoryPrinterManagerImpl manager) {
-        if ( this.registration == null &&
-             (supports(PrinterMode.HTML_BODY) || supports(PrinterMode.TEXT))) {
-            this.registration = WebConsolePlugin.register(context, manager, this.description);
+        if ( this.registration == null ) {
+            final Object value = this.description.getServiceReference().getProperty(InventoryPrinter.CONFIG_WEBCONSOLE);
+            if ( value == null || !"false".equalsIgnoreCase(value.toString()) ) {
+                this.registration = WebConsolePlugin.register(context, manager, this.description);
+            }
         }
     }
 
@@ -176,12 +178,13 @@
      * @see org.apache.felix.inventory.InventoryPrinter#print(org.apache.felix.inventory.PrinterMode, java.io.PrintWriter)
      */
     public void print(final PrinterMode mode,
-            final PrintWriter printWriter) {
+            final PrintWriter printWriter,
+            final boolean isZip) {
         if ( this.supports(mode) ) {
             if ( this.printer instanceof InventoryPrinter ) {
-                ((InventoryPrinter)this.printer).print(mode, printWriter);
+                ((InventoryPrinter)this.printer).print(mode, printWriter, isZip);
             } else {
-                ClassUtils.invoke(this.printer, this.printMethod, new Object[] {mode.toString(), printWriter});
+                ClassUtils.invoke(this.printer, this.printMethod, new Object[] {mode.toString(), printWriter, Boolean.valueOf(isZip)});
             }
         }
     }
diff --git a/inventory/src/main/java/org/apache/felix/inventory/impl/InventoryPrinterDescription.java b/inventory/src/main/java/org/apache/felix/inventory/impl/InventoryPrinterDescription.java
index d8abf59..8b20eff 100644
--- a/inventory/src/main/java/org/apache/felix/inventory/impl/InventoryPrinterDescription.java
+++ b/inventory/src/main/java/org/apache/felix/inventory/impl/InventoryPrinterDescription.java
@@ -43,12 +43,26 @@
         // check modes
         final Object modesCfg = ref.getProperty(InventoryPrinter.CONFIG_PRINTER_MODES);
         if ( modesCfg instanceof String ) {
-            this.modes = new PrinterMode[] { PrinterMode.valueOf((String)modesCfg)};
+            final PrinterMode mode = PrinterMode.valueOf((String)modesCfg);
+            if ( mode != null ) {
+                this.modes = new PrinterMode[] {mode};
+            } else {
+                this.modes = null;
+            }
         } else if ( modesCfg instanceof String[] ) {
             final String[] modesCfgArray = (String[])modesCfg;
-            this.modes = new PrinterMode[modesCfgArray.length];
+            final PrinterMode[] pModes = new PrinterMode[modesCfgArray.length];
+            boolean invalid = false;
             for(int i=0; i<modesCfgArray.length;i++) {
-                this.modes[i] = PrinterMode.valueOf(modesCfgArray[i]);
+                pModes[i] = PrinterMode.valueOf(modesCfgArray[i]);
+                if ( pModes[i] == null ) {
+                    invalid = true;
+                }
+            }
+            if ( invalid ) {
+                this.modes = null;
+            } else {
+                this.modes = pModes;
             }
         } else {
             this.modes = null;
diff --git a/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/ConfigurationPrinterAdapter.java b/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/ConfigurationPrinterAdapter.java
index 11911a9..01c9559 100644
--- a/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/ConfigurationPrinterAdapter.java
+++ b/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/ConfigurationPrinterAdapter.java
@@ -184,11 +184,11 @@
     public String[] getPrinterModes() {
         final Set list = new HashSet();
         if ( this.match(ConsoleConstants.MODE_TXT) || this.match(ConsoleConstants.MODE_ZIP) ) {
-            list.add(PrinterMode.ZIP_FILE_TEXT.name());
+            list.add(PrinterMode.TEXT.name());
         }
         if ( this.match(ConsoleConstants.MODE_WEB) ) {
             if ( !escapeHtml ) {
-                list.add(PrinterMode.HTML_BODY.name());
+                list.add(PrinterMode.HTML_FRAGMENT.name());
             } else {
                 list.add(PrinterMode.TEXT.name());
             }
diff --git a/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/WebConsoleAdapter.java b/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/WebConsoleAdapter.java
index 3949f18..423ed8a 100644
--- a/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/WebConsoleAdapter.java
+++ b/inventory/src/main/java/org/apache/felix/inventory/impl/webconsole/WebConsoleAdapter.java
@@ -162,13 +162,13 @@
         /**
          * @see org.apache.felix.inventory.InventoryPrinter#print(org.apache.felix.inventory.PrinterMode, java.io.PrintWriter)
          */
-        public void print(final PrinterMode mode, final PrintWriter printWriter) {
+        public void print(final PrinterMode mode, final PrintWriter printWriter, final boolean isZip ) {
             final String m;
-            if ( mode == PrinterMode.HTML_BODY ) {
+            if ( !isZip && mode == PrinterMode.HTML_FRAGMENT ) {
                 m = ConsoleConstants.MODE_WEB;
-            } else if ( mode == PrinterMode.TEXT ) {
+            } else if ( !isZip && mode == PrinterMode.TEXT ) {
                 m = ConsoleConstants.MODE_TXT;
-            } else if ( mode == PrinterMode.ZIP_FILE_TEXT ) {
+            } else if (isZip && (mode == PrinterMode.TEXT || mode == PrinterMode.HTML_FRAGMENT) ) {
                 m = ConsoleConstants.MODE_ZIP;
             } else {
                 m = null;