FELIX-1993 : Enhance configuration printer support
Add new BinaryConfigurationPrinter which allows to add a set of files to the downloadable zip (POC)

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@903666 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/BinaryConfigurationPrinter.java b/webconsole/src/main/java/org/apache/felix/webconsole/BinaryConfigurationPrinter.java
new file mode 100644
index 0000000..045db39
--- /dev/null
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/BinaryConfigurationPrinter.java
@@ -0,0 +1,60 @@
+/*
+ * 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.felix.webconsole;
+
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+
+/**
+ * This is an optional extension of the {@link ConfigurationPrinter}.
+ * If a configuration printer implements this interface, the printer
+ * can add additional files to the output of the configuration rendering.
+ *
+ * Currently this is only supported for the ZIP mode.
+ *
+ * A service implementing this method must still register itself
+ * as a {@link ConfigurationPrinter} but not as a
+ * {@link BinaryConfigurationPrinter} service.
+ * @since 3.0
+ */
+public interface BinaryConfigurationPrinter
+    extends ConfigurationPrinter
+{
+
+    /**
+     * Return a list of filename for the render mode.
+     * The returned list should just contain filenames without
+     * any path information.
+     * @return An array of filenames or null.
+     */
+    String[] getFilenames(String mode);
+
+    /**
+     * Write the contents of the file to the output stream.
+     * @param name The filename.
+     * @param out The output stream.
+     * @throws IOException If any problem occurs during writing.
+     * @throws IllegalArgumentException If the name is null or not
+     *         one from the list returned by {@link #getFilenames(String)}.
+     */
+    void writeFile(String name, String mode,  OutputStream out)
+    throws IOException;
+}
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java
index 65835f0..31d3de5 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java
@@ -100,10 +100,11 @@
             zip.setLevel( 9 );
             zip.setMethod( ZipOutputStream.DEFLATED );
 
-            ConfigurationWriter pw = new ZipConfigurationWriter( zip );
+            final ZipConfigurationWriter pw = new ZipConfigurationWriter( zip );
             printConfigurationStatus( pw, ConfigurationPrinter.MODE_ZIP );
             pw.flush();
 
+            addBinaries( pw );
             zip.finish();
         }
         else
@@ -264,8 +265,8 @@
 
 
     private void printConfigurationPrinter( final ConfigurationWriter pw,
-            final ConfigurationPrinter cp,
-            final String mode )
+                                            final ConfigurationPrinter cp,
+                                            final String mode )
     {
         pw.title(  cp.getTitle() );
         if ( cp instanceof ModeAwareConfigurationPrinter )
@@ -541,6 +542,35 @@
         }
     }
 
+    private void addBinaries( final ZipConfigurationWriter cf )
+    throws IOException
+    {
+        final String mode = ConfigurationPrinter.MODE_ZIP;
+        for ( Iterator cpi = getConfigurationPrinters().iterator(); cpi.hasNext(); )
+        {
+            // check if printer supports zip mode
+            final PrinterDesc desc = (PrinterDesc) cpi.next();
+            if ( desc.match(mode) )
+            {
+                // check if printer implements binary configuration printer
+                if ( desc.printer instanceof BinaryConfigurationPrinter )
+                {
+                    final String[] names = ((BinaryConfigurationPrinter)desc.printer).getFilenames(mode);
+                    if ( names != null )
+                    {
+                        for(int i = 0; i < names.length; i++)
+                        {
+                            final OutputStream os = cf.startFile(desc.printer.getTitle(), names[i]);
+                            ((BinaryConfigurationPrinter)desc.printer).writeFile(names[i], mode, os);
+                            cf.end();
+                        }
+                    }
+                }
+            }
+        }
+
+    }
+
     private static final class PrinterDesc
     {
         private final String[] modes;
@@ -670,6 +700,21 @@
             }
         }
 
+        public OutputStream startFile( String title, String name)
+        {
+            final String path = MessageFormat.format( "{0,number,000}-{1}/{2}", new Object[]
+                 { new Integer( counter ), title, name } );
+            ZipEntry entry = new ZipEntry( path );
+            try
+            {
+                zip.putNextEntry( entry );
+            }
+            catch ( IOException ioe )
+            {
+                // should handle
+            }
+            return zip;
+        }
 
         public void end()
         {