felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1424838 - in /felix/sandbox/cziegeler/status-printer: ./ src/main/java/org/apache/felix/status/ src/main/java/org/apache/felix/status/impl/ src/main/java/org/apache/felix/status/impl/webconsole/
Date Fri, 21 Dec 2012 10:10:22 GMT
Author: cziegeler
Date: Fri Dec 21 10:10:22 2012
New Revision: 1424838

URL: http://svn.apache.org/viewvc?rev=1424838&view=rev
Log:
Use easier attachment interface

Added:
    felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/package-info.java
  (with props)
Modified:
    felix/sandbox/cziegeler/status-printer/pom.xml
    felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/StatusPrinterHandler.java
    felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/ZipAttachmentProvider.java
    felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/AbstractWebConsolePlugin.java
    felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/DefaultWebConsolePlugin.java
    felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/StatusPrinterAdapter.java
    felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/WebConsoleAdapter.java

Modified: felix/sandbox/cziegeler/status-printer/pom.xml
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/status-printer/pom.xml?rev=1424838&r1=1424837&r2=1424838&view=diff
==============================================================================
--- felix/sandbox/cziegeler/status-printer/pom.xml (original)
+++ felix/sandbox/cziegeler/status-printer/pom.xml Fri Dec 21 10:10:22 2012
@@ -91,5 +91,11 @@
             <version>2.4</version>
             <scope>provided</scope>
         </dependency>
+        <dependency>
+            <groupId>biz.aQute</groupId>
+            <artifactId>bndlib</artifactId>
+            <version>1.43.0</version>
+            <scope>provided</scope>
+        </dependency>
     </dependencies>
 </project>

Modified: felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/StatusPrinterHandler.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/StatusPrinterHandler.java?rev=1424838&r1=1424837&r2=1424838&view=diff
==============================================================================
--- felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/StatusPrinterHandler.java
(original)
+++ felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/StatusPrinterHandler.java
Fri Dec 21 10:10:22 2012
@@ -18,14 +18,12 @@
  */
 package org.apache.felix.status;
 
-import java.io.PrintWriter;
-import java.net.URL;
 
 /**
  * The status printer handler can be used by clients to access
  * a status printer.
  */
-public interface StatusPrinterHandler {
+public interface StatusPrinterHandler extends StatusPrinter, ZipAttachmentProvider {
 
     /** The unique name of the printer. */
     String getName();
@@ -41,10 +39,4 @@ public interface StatusPrinterHandler {
 
     /** Whether the printer supports this mode. */
     boolean supports( final PrinterMode mode );
-
-    /** Print the configuration in the mode. */
-    void printConfiguration( PrinterMode mode, PrintWriter printWriter );
-
-    /** Get the attachments for the zip. */
-    URL[] getAttachmentsForZip();
 }

Modified: felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/ZipAttachmentProvider.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/ZipAttachmentProvider.java?rev=1424838&r1=1424837&r2=1424838&view=diff
==============================================================================
--- felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/ZipAttachmentProvider.java
(original)
+++ felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/ZipAttachmentProvider.java
Fri Dec 21 10:10:22 2012
@@ -18,7 +18,8 @@
  */
 package org.apache.felix.status;
 
-import java.net.URL;
+import java.io.IOException;
+import java.util.zip.ZipOutputStream;
 
 /**
  * This is an optional extension of the {@link StatusPrinter}.
@@ -37,5 +38,6 @@ public interface ZipAttachmentProvider e
      * attachments.
      * @return An array of URLs or null.
      */
-    URL[] getAttachments();
+    void addAttachments(final String namePrefix, final ZipOutputStream zos)
+    throws IOException;
 }

Modified: felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/AbstractWebConsolePlugin.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/AbstractWebConsolePlugin.java?rev=1424838&r1=1424837&r2=1424838&view=diff
==============================================================================
--- felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/AbstractWebConsolePlugin.java
(original)
+++ felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/AbstractWebConsolePlugin.java
Fri Dec 21 10:10:22 2012
@@ -17,12 +17,9 @@
 package org.apache.felix.status.impl;
 
 import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.PrintWriter;
 import java.io.Writer;
-import java.net.URL;
 import java.text.MessageFormat;
 import java.util.Date;
 import java.util.zip.Deflater;
@@ -63,33 +60,27 @@ public abstract class AbstractWebConsole
         if ( handler == null ) {
             for(final StatusPrinterHandler sph : this.statusPrinterManager.getHandlers(mode))
{
                 pw.title(sph.getTitle());
-                sph.printConfiguration(mode, pw);
+                sph.print(mode, pw);
                 pw.end();
             }
         } else {
             if ( handler.supports(mode) ) {
                 pw.title(handler.getTitle());
-                handler.printConfiguration(mode, pw);
+                handler.print(mode, pw);
                 pw.end();
             }
         }
     }
 
-    private void addAttachments( final ConfigurationWriter cf, final StatusPrinterHandler
handler )
+    private void addAttachments( final ZipConfigurationWriter cf, final StatusPrinterHandler
handler )
     throws IOException {
         if ( handler == null ) {
             for(final StatusPrinterHandler sph : this.statusPrinterManager.getHandlers(PrinterMode.ZIP_FILE))
{
-                final URL[] attachments = sph.getAttachmentsForZip();
-                if ( attachments != null ) {
-                    cf.handleAttachments( sph.getTitle(), attachments );
-                }
+                sph.addAttachments(cf.getAttachmentPrefix(sph.getTitle()), cf.getZipOutputStream());
             }
         } else {
             if ( handler.supports(PrinterMode.ZIP_FILE) ) {
-                final URL[] attachments = handler.getAttachmentsForZip();
-                if ( attachments != null ) {
-                    cf.handleAttachments( handler.getTitle(), attachments );
-                }
+                handler.addAttachments(cf.getAttachmentPrefix(handler.getTitle()), cf.getZipOutputStream());
             }
         }
     }
@@ -162,13 +153,11 @@ public abstract class AbstractWebConsole
 
             zip.write(sb.toString().getBytes("UTF-8"));
             zip.closeEntry();
-            zip.flush();
 
-            final ConfigurationWriter pw = new ZipConfigurationWriter( zip );
+            final ZipConfigurationWriter pw = new ZipConfigurationWriter( zip );
             printConfigurationStatus( pw, PrinterMode.ZIP_FILE, handler );
-            pw.flush();
 
-            addAttachments( pw, handler );
+            this.addAttachments( pw, handler );
             zip.finish();
         } else if ( request.getPathInfo().endsWith( ".nfo" ) ) {
             if ( handler == null ) {
@@ -184,10 +173,10 @@ public abstract class AbstractWebConsole
             pw.println ( "<head><title>dummy</title></head><body><div>"
);
 
             if ( handler.supports(PrinterMode.HTML_BODY) ) {
-                handler.printConfiguration(PrinterMode.HTML_BODY, pw);
+                handler.print(PrinterMode.HTML_BODY, pw);
             } else {
                 pw.enableFilter( true );
-                handler.printConfiguration(PrinterMode.TEXT, pw);
+                handler.print(PrinterMode.TEXT, pw);
                 pw.enableFilter( false );
             }
             pw.println( "</div></body></html>" );
@@ -240,10 +229,10 @@ public abstract class AbstractWebConsole
             pw.println("<br/>&nbsp;</p>"); // status line
             pw.print("<div>");
             if ( handler.supports(PrinterMode.HTML_BODY) ) {
-                handler.printConfiguration(PrinterMode.HTML_BODY, pw);
+                handler.print(PrinterMode.HTML_BODY, pw);
             } else {
                 pw.enableFilter( true );
-                handler.printConfiguration(PrinterMode.TEXT, pw);
+                handler.print(PrinterMode.TEXT, pw);
                 pw.enableFilter( false );
             }
             pw.print("</div>");
@@ -264,11 +253,6 @@ public abstract class AbstractWebConsole
         public void end() throws IOException {
             // dummy implementation
         }
-
-        public void handleAttachments( final String title, final URL[] urls ) throws IOException
{
-            throw new UnsupportedOperationException( "handleAttachments not supported by
this configuration writer: "
-                + this );
-        }
     }
 
     private static class HtmlConfigurationWriter extends ConfigurationWriter {
@@ -404,7 +388,6 @@ public abstract class AbstractWebConsole
 
         private int counter;
 
-
         ZipConfigurationWriter( final ZipOutputStream zip ) {
             super( new OutputStreamWriter( zip ) );
             this.zip = zip;
@@ -412,65 +395,30 @@ public abstract class AbstractWebConsole
 
         @Override
         public void title( final String title ) throws IOException {
-            String name = MessageFormat.format( "{0,number,000}-{1}.txt", new Object[]
-                { new Integer( counter ), title } );
-
             counter++;
 
-            final ZipEntry entry = new ZipEntry( name );
-            zip.putNextEntry( entry );
-        }
+            final String name = MessageFormat.format( "{0,number,000}-{1}.txt", new Object[]
+                { new Integer( counter ), title } );
 
-        private OutputStream startFile( final String title, final String name) throws IOException
{
-            final String path = MessageFormat.format( "{0,number,000}-{1}/{2}", new Object[]
-                 { new Integer( counter ), title, name } );
-            final ZipEntry entry = new ZipEntry( path );
+            final ZipEntry entry = new ZipEntry( name );
             zip.putNextEntry( entry );
-
-            return zip;
         }
 
         @Override
-        public void handleAttachments( final String title, final URL[] attachments)
-        throws IOException {
-            for(int i = 0; i < attachments.length; i++) {
-                final URL current = attachments[i];
-                final String path = current.getPath();
-                final String name;
-                if ( path == null || path.length() == 0 ) {
-                    // sanity code, we should have a path, but if not let's
-                    // just create some random name
-                    name = "file" + Double.doubleToLongBits( Math.random() );
-                } else {
-                    final int pos = path.lastIndexOf('/');
-                    name = (pos == -1 ? path : path.substring(pos + 1));
-                }
-                final OutputStream os = this.startFile(title, name);
-                final InputStream is = current.openStream();
-                try {
-                    byte[] buffer = new byte[4096];
-                    int n = 0;
-                    while (-1 != (n = is.read(buffer))) {
-                        os.write(buffer, 0, n);
-                    }
-                } finally {
-                    if ( is != null ) {
-                        try { is.close(); } catch (final IOException ignore) {}
-                    }
-                }
-                this.end();
-            }
-
-            // increase the filename counter
-            counter++;
-        }
-
-
-        @Override
         public void end() throws IOException {
             flush();
 
             zip.closeEntry();
         }
+
+        public String getAttachmentPrefix(final String title) {
+            return MessageFormat.format( "{0,number,000}-{1}/", new Object[]
+                    { new Integer( counter ), title } );
+        }
+
+        public ZipOutputStream getZipOutputStream() {
+            counter++;
+            return this.zip;
+        }
     }
 }
\ No newline at end of file

Modified: felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/DefaultWebConsolePlugin.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/DefaultWebConsolePlugin.java?rev=1424838&r1=1424837&r2=1424838&view=diff
==============================================================================
--- felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/DefaultWebConsolePlugin.java
(original)
+++ felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/DefaultWebConsolePlugin.java
Fri Dec 21 10:10:22 2012
@@ -16,8 +16,9 @@
  */
 package org.apache.felix.status.impl;
 
+import java.io.IOException;
 import java.io.PrintWriter;
-import java.net.URL;
+import java.util.zip.ZipOutputStream;
 
 import org.apache.felix.status.PrinterMode;
 import org.apache.felix.status.StatusPrinterHandler;
@@ -79,9 +80,9 @@ public class DefaultWebConsolePlugin ext
     }
 
     /**
-     * @see org.apache.felix.status.StatusPrinterHandler#printConfiguration(org.apache.felix.status.PrinterMode,
java.io.PrintWriter)
+     * @see org.apache.felix.status.StatusPrinter#print(org.apache.felix.status.PrinterMode,
java.io.PrintWriter)
      */
-    public void printConfiguration(final PrinterMode mode, final PrintWriter printWriter)
{
+    public void print(final PrinterMode mode, final PrintWriter printWriter) {
         final StatusPrinterHandler[] handlers = this.statusPrinterManager.getAllHandlers();
         printWriter.print("Currently registered ");
         printWriter.print(String.valueOf(handlers.length));
@@ -93,9 +94,10 @@ public class DefaultWebConsolePlugin ext
     }
 
     /**
-     * @see org.apache.felix.status.StatusPrinterHandler#getAttachmentsForZip()
+     * @see org.apache.felix.status.ZipAttachmentProvider#addAttachments(java.lang.String,
java.util.zip.ZipOutputStream)
      */
-    public URL[] getAttachmentsForZip() {
-        return null;
+    public void addAttachments(String namePrefix, ZipOutputStream zos)
+    throws IOException {
+        // no attachments support
     }
 }
\ No newline at end of file

Modified: felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/StatusPrinterAdapter.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/StatusPrinterAdapter.java?rev=1424838&r1=1424837&r2=1424838&view=diff
==============================================================================
--- felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/StatusPrinterAdapter.java
(original)
+++ felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/StatusPrinterAdapter.java
Fri Dec 21 10:10:22 2012
@@ -16,14 +16,15 @@
  */
 package org.apache.felix.status.impl;
 
+import java.io.IOException;
 import java.io.PrintWriter;
 import java.lang.reflect.Method;
-import java.net.URL;
 import java.text.DateFormat;
 import java.util.Comparator;
 import java.util.Dictionary;
 import java.util.Hashtable;
 import java.util.Locale;
+import java.util.zip.ZipOutputStream;
 
 import org.apache.felix.status.PrinterMode;
 import org.apache.felix.status.StatusPrinter;
@@ -67,9 +68,9 @@ public class StatusPrinterAdapter implem
         Method attachmentMethod = null;
         if ( !(service instanceof ZipAttachmentProvider) ) {
 
-            // getAttachments()
-            attachmentMethod = ClassUtils.searchMethod(service.getClass(), "getAttachments",
-                    null);
+            // addAttachments()
+            attachmentMethod = ClassUtils.searchMethod(service.getClass(), "addAttachments",
+                    new Class[] {String.class, ZipOutputStream.class});
         }
         return new StatusPrinterAdapter(
                 description,
@@ -173,18 +174,16 @@ public class StatusPrinterAdapter implem
     }
 
     /**
-     * @see org.apache.felix.status.StatusPrinterHandler#getAttachmentsForZip()
+     * @see org.apache.felix.status.ZipAttachmentProvider#addAttachments(java.lang.String,
java.util.zip.ZipOutputStream)
      */
-    public URL[] getAttachmentsForZip() {
-        // check if printer implements binary configuration printer
-        URL[] attachments = null;
+    public void addAttachments(final String namePrefix, final ZipOutputStream zos)
+    throws IOException {
+        // check if printer implements ZipAttachmentProvider
         if ( printer instanceof ZipAttachmentProvider ) {
-            attachments = ((ZipAttachmentProvider)printer).getAttachments();
+            ((ZipAttachmentProvider)printer).addAttachments(namePrefix, zos);
         } else if ( this.attachmentMethod != null ) {
-            attachments = (URL[])ClassUtils.invoke(this.printer, this.attachmentMethod, null);
+            ClassUtils.invoke(this.printer, this.attachmentMethod, new Object[] {namePrefix,
zos});
         }
-
-        return attachments;
     }
 
     /**
@@ -200,9 +199,9 @@ public class StatusPrinterAdapter implem
     }
 
     /**
-     * @see org.apache.felix.status.StatusPrinterHandler#printConfiguration(org.apache.felix.status.PrinterMode,
java.io.PrintWriter)
+     * @see org.apache.felix.status.StatusPrinter#print(org.apache.felix.status.PrinterMode,
java.io.PrintWriter)
      */
-    public void printConfiguration(final PrinterMode mode,
+    public void print(final PrinterMode mode,
             final PrintWriter printWriter) {
         if ( this.supports(mode) ) {
             if ( this.printer instanceof StatusPrinter ) {

Modified: felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/WebConsoleAdapter.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/WebConsoleAdapter.java?rev=1424838&r1=1424837&r2=1424838&view=diff
==============================================================================
--- felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/WebConsoleAdapter.java
(original)
+++ felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/impl/webconsole/WebConsoleAdapter.java
Fri Dec 21 10:10:22 2012
@@ -17,6 +17,8 @@
 package org.apache.felix.status.impl.webconsole;
 
 
+import java.io.IOException;
+import java.io.InputStream;
 import java.io.PrintWriter;
 import java.net.URL;
 import java.util.Dictionary;
@@ -24,6 +26,8 @@ import java.util.HashMap;
 import java.util.Hashtable;
 import java.util.Map;
 import java.util.ResourceBundle;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipOutputStream;
 
 import org.apache.felix.status.PrinterMode;
 import org.apache.felix.status.StatusPrinter;
@@ -120,11 +124,42 @@ public class WebConsoleAdapter implement
                 }
 
                 /**
-                 * @see org.apache.felix.status.ZipAttachmentProvider#getAttachments()
+                 * @see org.apache.felix.status.ZipAttachmentProvider#addAttachments(java.lang.String,
java.util.zip.ZipOutputStream)
                  */
-                public URL[] getAttachments() {
-                    return cpa.getAttachments();
+                public void addAttachments(final String namePrefix, final ZipOutputStream
zos)
+                throws IOException {
+                    final URL[] attachments = cpa.getAttachments();
+                    if ( attachments != null ) {
+                        for(final URL current : attachments) {
+                            final String path = current.getPath();
+                            final String name;
+                            if ( path == null || path.length() == 0 ) {
+                                // sanity code, we should have a path, but if not let's
+                                // just create some random name
+                                name = "file" + Double.doubleToLongBits( Math.random() );
+                            } else {
+                                final int pos = path.lastIndexOf('/');
+                                name = (pos == -1 ? path : path.substring(pos + 1));
+                            }
+                            final ZipEntry entry = new ZipEntry(namePrefix + name);
+                            zos.putNextEntry(entry);
+                            final InputStream is = current.openStream();
+                            try {
+                                byte[] buffer = new byte[4096];
+                                int n = 0;
+                                while (-1 != (n = is.read(buffer))) {
+                                    zos.write(buffer, 0, n);
+                                }
+                            } finally {
+                                if ( is != null ) {
+                                    try { is.close(); } catch (final IOException ignore)
{}
+                                }
+                            }
+                            zos.closeEntry();
+                        }
+                    }
                 }
+
             }, props);
             synchronized ( this.registrations ) {
                 this.registrations.put(reference, reg);

Added: felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/package-info.java
URL: http://svn.apache.org/viewvc/felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/package-info.java?rev=1424838&view=auto
==============================================================================
--- felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/package-info.java
(added)
+++ felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/package-info.java
Fri Dec 21 10:10:22 2012
@@ -0,0 +1,32 @@
+/*
+ * 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.
+ */
+
+/**
+ * Interfaces for getting status information about the current instance
+ *
+ * @version 1.0.0
+ */
+@Version("1.0.0")
+@Export(optional = "provide:=true")
+package org.apache.felix.status;
+
+import aQute.bnd.annotation.Export;
+import aQute.bnd.annotation.Version;
+
+

Propchange: felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/package-info.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/package-info.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: felix/sandbox/cziegeler/status-printer/src/main/java/org/apache/felix/status/package-info.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message