Refactor configuration render type detection into a sngle class
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1022476 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationPrinterAdapter.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationPrinterAdapter.java
index 7be7e32..b44be99 100644
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationPrinterAdapter.java
+++ b/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ConfigurationPrinterAdapter.java
@@ -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 );
- public ConfigurationPrinterAdapter(final Object service,
+ // 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;
+ }
+
+ 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 )
+ {
+ if ( printer instanceof ModeAwareConfigurationPrinter )
+ {
+ ( ( ModeAwareConfigurationPrinter ) printer ).printConfiguration( pw, mode );
+ }
+ else if ( printer instanceof ConfigurationPrinter )
+ {
+ ( ( ConfigurationPrinter ) printer ).printConfiguration( pw );
+ }
+ else if ( this.modeAwarePrintMethod != null )
+ {
+ this.invoke(this.modeAwarePrintMethod, new Object[] {pw, mode});
+ } else if ( this.printMethod != null )
+ {
+ this.invoke(this.printMethod, new Object[] {pw});
+ }
+ }
+
+ public URL[] getAttachments( final String mode )
+ {
+ // check if printer implements binary configuration printer
+ URL[] attachments = null;
+ if ( printer instanceof AttachmentProvider )
+ {
+ 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
{
- printMethod.invoke(service, args);
+ return obj.getClass().getDeclaredMethod(mName, params);
}
- catch (IllegalArgumentException e)
+ catch (Throwable nsme)
{
- // ignore
+ // ignore, we catch Throwable above to not only catch NoSuchMethodException
+ // but also other ones like ClassDefNotFoundError etc.
}
- catch (IllegalAccessException e)
- {
- // ignore
- }
- catch (InvocationTargetException e)
- {
- // ignore
- }
+ 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;
}
}
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 8fba8a6..73593a9 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
@@ -18,7 +18,6 @@
import java.io.*;
-import java.lang.reflect.Method;
import java.net.URL;
import java.text.*;
import java.util.*;
@@ -168,7 +167,7 @@
{
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 @@
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 @@
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 @@
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 @@
}
}
- 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 @@
final Object service = cfgPrinterTracker.getService( ref );
if ( service != null )
{
- if ( service instanceof ConfigurationPrinter )
+ final ConfigurationPrinterAdapter desc = ConfigurationPrinterAdapter.createAdapter(service, ref);
+ if ( desc != null )
{
- 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
- {
- 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 @@
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 @@
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 @@
// }
- 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 @@
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 @@
}
- 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
{
diff --git a/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ModeAwareConfigurationPrinterAdapter.java b/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ModeAwareConfigurationPrinterAdapter.java
deleted file mode 100644
index 21eba00..0000000
--- a/webconsole/src/main/java/org/apache/felix/webconsole/internal/misc/ModeAwareConfigurationPrinterAdapter.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * 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.internal.misc;
-
-
-import java.io.PrintWriter;
-import java.lang.reflect.Method;
-
-import org.apache.felix.webconsole.ConfigurationPrinter;
-import org.apache.felix.webconsole.ModeAwareConfigurationPrinter;
-
-
-/**
- * Adapter for a service acting as mode aware configuration printer.
- */
-public class ModeAwareConfigurationPrinterAdapter
- extends ConfigurationPrinterAdapter
- implements ModeAwareConfigurationPrinter
-{
- public ModeAwareConfigurationPrinterAdapter(final Object service,
- final String title,
- final Method printMethod)
- {
- super(service, title, printMethod);
- }
-
- public void printConfiguration(final PrintWriter printWriter)
- {
- printConfiguration(printWriter, ConfigurationPrinter.MODE_ALWAYS);
- }
-
- public void printConfiguration(final PrintWriter printWriter, final String mode)
- {
- invoke(new Object[] {printWriter, mode});
- }
-}