felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cziege...@apache.org
Subject svn commit: r1022476 - in /felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc: ConfigurationPrinterAdapter.java ConfigurationRender.java ModeAwareConfigurationPrinterAdapter.java
Date Thu, 14 Oct 2010 12:05:34 GMT
Author: cziegeler
Date: Thu Oct 14 12:05:33 2010
New Revision: 1022476

URL: http://svn.apache.org/viewvc?rev=1022476&view=rev
Log:
Refactor configuration render type detection into a sngle class

Removed:
    felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ModeAwareConfigurationPrinterAdapter.java
Modified:
    felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationPrinterAdapter.java
    felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java

Modified: felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationPrinterAdapter.java
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationPrinterAdapter.java?rev=1022476&r1=1022475&r2=1022476&view=diff
==============================================================================
--- felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationPrinterAdapter.java
(original)
+++ felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationPrinterAdapter.java
Thu Oct 14 12:05:33 2010
@@ -16,62 +16,252 @@
  */
 package org.apache.felix.webconsole.internal.misc;
 
-
 import java.io.PrintWriter;
-import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
 
-import org.apache.felix.webconsole.ConfigurationPrinter;
-
+import org.apache.felix.webconsole.*;
+import org.osgi.framework.ServiceReference;
 
 /**
- * Adapter for a service acting as configuration printer.
+ * Helper class for a configuration printer.
  */
-public class ConfigurationPrinterAdapter implements ConfigurationPrinter
+public class ConfigurationPrinterAdapter
 {
 
-    private final Object service;
+    private final Object printer;
+    public String title;
+    public String label;
+    private final String[] modes;
+    private Method modeAwarePrintMethod;
+    private Method printMethod;
+    private Method attachmentMethod;
+    private boolean checkedAttachmentMethod = false;
 
-    private final String title;
+    private static final List CUSTOM_MODES = new ArrayList();
+    static
+    {
+        CUSTOM_MODES.add( ConfigurationPrinter.MODE_TXT );
+        CUSTOM_MODES.add( ConfigurationPrinter.MODE_WEB );
+        CUSTOM_MODES.add( ConfigurationPrinter.MODE_ZIP );
+    }
 
-    private final Method printMethod;
+    public static ConfigurationPrinterAdapter createAdapter(
+            final Object service,
+            final ServiceReference ref)
+    {
+        String title;
+        Object cfgPrinter = null;
+        Object modes = null;
+        Method printMethod = null;
+        if ( service instanceof ConfigurationPrinter )
+        {
+            modes = ref.getProperty(WebConsoleConstants.CONFIG_PRINTER_MODES);
+            if ( modes == null )
+            {
+                modes = ref.getProperty( ConfigurationPrinter.PROPERTY_MODES );
+            }
+            cfgPrinter = service;
+            title = ((ConfigurationPrinter) service).getTitle();
+        }
+        else
+        {
+            modes = ref.getProperty( WebConsoleConstants.CONFIG_PRINTER_MODES );
+            title = (String)ref.getProperty(  WebConsoleConstants.PLUGIN_TITLE );
+
+            // first: printConfiguration(PrintWriter, String)
+            final Method method2Params = searchMethod(service, "printConfiguration",
+                    new Class[] {PrintWriter.class, String.class});
+            if ( method2Params != null )
+            {
+                cfgPrinter = service;
+                printMethod = method2Params;
+            }
+
+            if ( cfgPrinter == null )
+            {
+                // second: printConfiguration(PrintWriter)
+                final Method method1Params = searchMethod(service, "printConfiguration",
+                        new Class[] {PrintWriter.class});
+                if ( method1Params != null )
+                {
+                    cfgPrinter = service;
+                    printMethod = method1Params;
+                }
+            }
+        }
+        if ( cfgPrinter != null )
+        {
+            Object label =  ref.getProperty( WebConsoleConstants.PLUGIN_LABEL);
+            return new ConfigurationPrinterAdapter(
+                    cfgPrinter,
+                    printMethod,
+                    title,
+                    (label instanceof String ? (String)label : null),
+                    modes);
+        }
+        return null;
+    }
 
-    public ConfigurationPrinterAdapter(final Object service,
+    private ConfigurationPrinterAdapter( final Object printer,
+            final Method printMethod,
             final String title,
-            final Method printMethod)
+            final String label,
+            final Object modes )
     {
+        this.printer = printer;
         this.title = title;
-        this.service = service;
-        this.printMethod = printMethod;
+        this.label = label;
+        if ( printMethod != null )
+        {
+            if ( printMethod.getParameterTypes().length > 1 )
+            {
+                this.modeAwarePrintMethod = printMethod;
+            }
+            else
+            {
+                this.printMethod = printMethod;
+            }
+        }
+        if ( modes == null || !( modes instanceof String || modes instanceof String[] ) )
+        {
+            this.modes = null;
+        }
+        else
+        {
+            if ( modes instanceof String )
+            {
+                if ( CUSTOM_MODES.contains(modes) )
+                {
+                    this.modes = new String[] {modes.toString()};
+                }
+                else
+                {
+                    this.modes = null;
+                }
+            }
+            else
+            {
+                final String[] values = (String[])modes;
+                boolean valid = values.length > 0;
+                for(int i=0; i<values.length; i++)
+                {
+                    if ( !CUSTOM_MODES.contains(values[i]) )
+                    {
+                        valid = false;
+                        break;
+                    }
+                }
+                if ( valid)
+                {
+                    this.modes = values;
+                }
+                else
+                {
+                    this.modes = null;
+                }
+            }
+        }
     }
 
-    public String getTitle()
+    public boolean match(final String mode)
     {
-        return this.title;
+        if ( this.modes == null)
+        {
+            return true;
+        }
+        for(int i=0; i<this.modes.length; i++)
+        {
+            if ( this.modes[i].equals(mode) )
+            {
+                return true;
+            }
+        }
+        return false;
     }
 
-    protected void invoke(final Object[] args)
+    public final void printConfiguration( final PrintWriter pw,
+            final String mode )
     {
-        try
+        if ( printer instanceof ModeAwareConfigurationPrinter )
         {
-            printMethod.invoke(service, args);
+            ( ( ModeAwareConfigurationPrinter ) printer ).printConfiguration( pw, mode );
         }
-        catch (IllegalArgumentException e)
+        else if ( printer instanceof ConfigurationPrinter )
         {
-            // ignore
+            ( ( ConfigurationPrinter ) printer ).printConfiguration( pw );
         }
-        catch (IllegalAccessException e)
+        else if ( this.modeAwarePrintMethod != null )
         {
-            // ignore
+            this.invoke(this.modeAwarePrintMethod, new Object[] {pw, mode});
+        } else if ( this.printMethod != null )
+        {
+            this.invoke(this.printMethod, new Object[] {pw});
         }
-        catch (InvocationTargetException e)
+    }
+
+    public URL[] getAttachments( final String mode )
+    {
+        // check if printer implements binary configuration printer
+        URL[] attachments = null;
+        if ( printer instanceof AttachmentProvider )
         {
-            // ignore
+            attachments = ((AttachmentProvider)printer).getAttachments(mode);
+        }
+        else
+        {
+            if ( !checkedAttachmentMethod )
+            {
+                attachmentMethod = searchMethod(printer, "getAttachments", new Class[] {String.class});
+                checkedAttachmentMethod = true;
+            }
+            if ( attachmentMethod != null )
+            {
+                attachments = (URL[])invoke(attachmentMethod, new Object[] {mode});
+            }
+        }
+        return attachments;
+    }
+
+    /**
+     * @see java.lang.Object#toString()
+     */
+    public String toString() {
+        return title + " (" + printer.getClass() + ")";
+    }
+
+    /**
+     * Search a method with the given name and signature
+     */
+    private static Method searchMethod(final Object obj, final String mName, final Class[]
params)
+    {
+        try
+        {
+            return obj.getClass().getDeclaredMethod(mName, params);
+        }
+        catch (Throwable nsme)
+        {
+            // ignore, we catch Throwable above to not only catch NoSuchMethodException
+            // but also other ones like ClassDefNotFoundError etc.
         }
+        return null;
     }
 
-    public void printConfiguration(final PrintWriter printWriter)
+    /**
+     * Invoke the method on the printer with the arguments.
+     */
+    protected Object invoke(final Method m, final Object[] args)
     {
-        invoke(new Object[] {printWriter});
+        try
+        {
+            return m.invoke(printer, args);
+        }
+        catch (Throwable e)
+        {
+            // ignore
+        }
+        return null;
     }
 }

Modified: felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java
URL: http://svn.apache.org/viewvc/felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java?rev=1022476&r1=1022475&r2=1022476&view=diff
==============================================================================
--- felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java
(original)
+++ felix/trunk/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationRender.java
Thu Oct 14 12:05:33 2010
@@ -18,7 +18,6 @@ package org.apache.felix.webconsole.inte
 
 
 import java.io.*;
-import java.lang.reflect.Method;
 import java.net.URL;
 import java.text.*;
 import java.util.*;
@@ -168,7 +167,7 @@ public class ConfigurationRender extends
             {
                 for (Iterator i = printers.iterator(); i.hasNext();)
                 {
-                    final PrinterDesc desc = (PrinterDesc) i.next();
+                    final ConfigurationPrinterAdapter desc = (ConfigurationPrinterAdapter)
i.next();
                     printConfigurationPrinter( pw, desc, ConfigurationPrinter.MODE_WEB );
                     pw.println( "</div></body></html>" );
                     return;
@@ -226,7 +225,7 @@ public class ConfigurationRender extends
         Collection printers = getConfigurationPrinters();
         for (Iterator i = printers.iterator(); i.hasNext();)
         {
-            final PrinterDesc desc = (PrinterDesc) i.next();
+            final ConfigurationPrinterAdapter desc = (ConfigurationPrinterAdapter) i.next();
             if ( desc.match(ConfigurationPrinter.MODE_WEB) )
             {
                 final String label = desc.label;
@@ -249,7 +248,7 @@ public class ConfigurationRender extends
         List list = null;
         for ( Iterator cpi = getConfigurationPrinters().iterator(); cpi.hasNext(); )
         {
-            final PrinterDesc desc = (PrinterDesc) cpi.next();
+            final ConfigurationPrinterAdapter desc = (ConfigurationPrinterAdapter) cpi.next();
             if (desc.label.equals( label ) )
             {
                 if ( list == null )
@@ -274,7 +273,7 @@ public class ConfigurationRender extends
 
         for ( Iterator cpi = printers.iterator(); cpi.hasNext(); )
         {
-            final PrinterDesc desc = (PrinterDesc) cpi.next();
+            final ConfigurationPrinterAdapter desc = (ConfigurationPrinterAdapter) cpi.next();
             if ( desc.match(mode) )
             {
                 printConfigurationPrinter( pw, desc, mode );
@@ -282,20 +281,6 @@ public class ConfigurationRender extends
         }
     }
 
-    private Method searchMethod(final Object obj, final String mName, final Class[] params)
-    {
-        try
-        {
-            return obj.getClass().getDeclaredMethod(mName, params);
-        }
-        catch (Throwable nsme)
-        {
-            // ignore, we catch Throwable above to not only catch NoSuchMethodException
-            // but also other ones like ClassDefNotFoundError etc.
-        }
-        return null;
-    }
-
     private final synchronized List getConfigurationPrinters()
     {
         if ( cfgPrinterTracker == null )
@@ -329,49 +314,10 @@ public class ConfigurationRender extends
                     final Object service = cfgPrinterTracker.getService( ref );
                     if ( service != null )
                     {
-                        if ( service instanceof ConfigurationPrinter )
-                        {
-                            Object modes = ref.getProperty(WebConsoleConstants.CONFIG_PRINTER_MODES);
-                            if ( modes == null )
-                            {
-                                modes = ref.getProperty( ConfigurationPrinter.PROPERTY_MODES
);
-                            }
-                            final ConfigurationPrinter cfgPrinter = (ConfigurationPrinter)
service;
-                            addConfigurationPrinter( cp, cfgPrinter, refs[i].getBundle(),
-                                    ref.getProperty( WebConsoleConstants.PLUGIN_LABEL ),
-                                    modes );
-                        }
-                        else
+                        final ConfigurationPrinterAdapter desc = ConfigurationPrinterAdapter.createAdapter(service,
ref);
+                        if ( desc != null )
                         {
-                            ConfigurationPrinter cfgPrinter = null;
-
-                            // first: printConfiguration(PrintWriter, String)
-                            final Method method2Params = this.searchMethod(service, "printConfiguration",
-                                    new Class[] {PrintWriter.class, String.class});
-                            if ( method2Params != null )
-                            {
-                                cfgPrinter = new ModeAwareConfigurationPrinterAdapter(service,
-                                        (String)ref.getProperty(  WebConsoleConstants.PLUGIN_TITLE
), method2Params);
-                            }
-
-                            if ( cfgPrinter == null )
-                            {
-                                // second: printConfiguration(PrintWriter)
-                                final Method method1Params = this.searchMethod(service, "printConfiguration",
-                                        new Class[] {PrintWriter.class});
-                                if ( method1Params != null )
-                                {
-                                   cfgPrinter = new ConfigurationPrinterAdapter(service,
-                                           (String)ref.getProperty(  WebConsoleConstants.PLUGIN_TITLE
), method1Params);
-                                }
-                            }
-
-                            if ( cfgPrinter != null )
-                            {
-                                addConfigurationPrinter( cp, cfgPrinter, ref.getBundle(),
-                                        ref.getProperty( WebConsoleConstants.PLUGIN_LABEL
),
-                                        ref.getProperty( WebConsoleConstants.CONFIG_PRINTER_MODES
) );
-                            }
+                            addConfigurationPrinter( cp, desc, ref.getBundle() );
                         }
                     }
                 }
@@ -385,13 +331,11 @@ public class ConfigurationRender extends
 
 
     private final void addConfigurationPrinter( final SortedMap printers,
-            final ConfigurationPrinter cfgPrinter,
-            final Bundle provider,
-            final Object labelProperty,
-            final Object mode )
+            final ConfigurationPrinterAdapter desc,
+            final Bundle provider)
     {
-        final String title = getTitle( cfgPrinter.getTitle(), provider );
-        String sortKey = title;
+        desc.title = getTitle(desc.title, provider );
+        String sortKey = desc.title;
         if ( printers.containsKey( sortKey ) )
         {
             int idx = -1;
@@ -404,8 +348,11 @@ public class ConfigurationRender extends
             while ( printers.containsKey( idxTitle ) );
             sortKey = idxTitle;
         }
-        String label = ( labelProperty instanceof String ) ? ( String ) labelProperty : sortKey;
-        printers.put( sortKey, new PrinterDesc( cfgPrinter, title, label, mode ) );
+        if ( desc.label == null )
+        {
+            desc.label = sortKey;
+        }
+        printers.put( sortKey, desc );
     }
 
 
@@ -441,28 +388,21 @@ public class ConfigurationRender extends
     //    }
 
 
-    private final void printConfigurationPrinter( final ConfigurationWriter pw, final PrinterDesc
desc,
-        final String mode )
+    private final void printConfigurationPrinter( final ConfigurationWriter pw,
+            final ConfigurationPrinterAdapter desc,
+            final String mode )
     {
         pw.title( desc.title );
-        final ConfigurationPrinter cp = desc.printer;
         try
         {
-            if ( cp instanceof ModeAwareConfigurationPrinter )
-            {
-                ( ( ModeAwareConfigurationPrinter ) cp ).printConfiguration( pw, mode );
-            }
-            else
-            {
-                cp.printConfiguration( pw );
-            }
+            desc.printConfiguration(pw, mode);
         }
         catch ( Throwable t )
         {
             pw.println();
             pw.println( "Configuration Printer failed: " + t.toString() );
             pw.println();
-            log( "Configuration Printer " + desc.title + " (" + cp.getClass() + ") failed",
t );
+            log( "Configuration Printer " + desc + " failed", t );
         }
         pw.end();
     }
@@ -670,30 +610,10 @@ public class ConfigurationRender extends
         for ( Iterator cpi = getConfigurationPrinters().iterator(); cpi.hasNext(); )
         {
             // check if printer supports zip mode
-            final PrinterDesc desc = (PrinterDesc) cpi.next();
+            final ConfigurationPrinterAdapter desc = (ConfigurationPrinterAdapter) cpi.next();
             if ( desc.match(mode) )
             {
-                // check if printer implements binary configuration printer
-                URL[] attachments = null;
-                if ( desc.printer instanceof AttachmentProvider )
-                {
-                    attachments = ((AttachmentProvider)desc.printer).getAttachments(mode);
-                }
-                else
-                {
-                    final Method m = this.searchMethod(desc.printer, "getAttachments", new
Class[] {String.class});
-                    if ( m != null )
-                    {
-                        try
-                        {
-                            attachments = (URL[])m.invoke(desc.printer, new Object[] {mode});
-                        }
-                        catch (Throwable t)
-                        {
-                            // ignore this!
-                        }
-                    }
-                }
+                final URL[] attachments = desc.getAttachments(mode);
                 if ( attachments != null )
                 {
                     cf.handleAttachments( desc.title, attachments );
@@ -703,86 +623,6 @@ public class ConfigurationRender extends
 
     }
 
-    private static final class PrinterDesc
-    {
-        public final ConfigurationPrinter printer;
-        public final String title;
-        public final String label;
-        private final String[] modes;
-
-        private static final List CUSTOM_MODES = new ArrayList();
-        static
-        {
-            CUSTOM_MODES.add( ConfigurationPrinter.MODE_TXT );
-            CUSTOM_MODES.add( ConfigurationPrinter.MODE_WEB );
-            CUSTOM_MODES.add( ConfigurationPrinter.MODE_ZIP );
-        }
-
-
-        public PrinterDesc( final ConfigurationPrinter printer, final String title, final
String label,
-            final Object modes )
-        {
-            this.printer = printer;
-            this.title = title;
-            this.label = label;
-            if ( modes == null || !( modes instanceof String || modes instanceof String[]
) )
-            {
-                this.modes = null;
-            }
-            else
-            {
-                if ( modes instanceof String )
-                {
-                    if ( CUSTOM_MODES.contains(modes) )
-                    {
-                        this.modes = new String[] {modes.toString()};
-                    }
-                    else
-                    {
-                        this.modes = null;
-                    }
-                }
-                else
-                {
-                    final String[] values = (String[])modes;
-                    boolean valid = values.length > 0;
-                    for(int i=0; i<values.length; i++)
-                    {
-                        if ( !CUSTOM_MODES.contains(values[i]) )
-                        {
-                            valid = false;
-                            break;
-                        }
-                    }
-                    if ( valid)
-                    {
-                        this.modes = values;
-                    }
-                    else
-                    {
-                        this.modes = null;
-                    }
-                }
-            }
-        }
-
-        public boolean match(final String mode)
-        {
-            if ( this.modes == null)
-            {
-                return true;
-            }
-            for(int i=0; i<this.modes.length; i++)
-            {
-                if ( this.modes[i].equals(mode) )
-                {
-                    return true;
-                }
-            }
-            return false;
-        }
-    }
-
     private static class PlainTextConfigurationWriter extends ConfigurationWriter
     {
 



Mime
View raw message