package fitnesse.plugins;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import fitnesse.authentication.Authenticator;
import fitnesse.responders.ResponderFactory;
import fitnesse.responders.editing.ContentFilter;
import fitnesse.testrunner.TestSystemFactoryRegistry;
import fitnesse.testsystems.slim.CustomComparatorRegistry;
import fitnesse.testsystems.slim.tables.SlimTableFactory;
import fitnesse.wiki.WikiPageFactoryRegistry;
import fitnesse.wikitext.parser.SymbolProvider;

/**
 * Wraps old-style plugins in the new PluginFeatureFactory service.
 */
public class LegacyPluginFeatureFactory implements PluginFeatureFactory {

  private final Object plugin;

  public LegacyPluginFeatureFactory(Object plugin) {
    this.plugin = plugin;
  }

  @Override
  public Authenticator getAuthenticator() {
    return null;
  }

  @Override
  public ContentFilter getContentFilter() {
    return null;
  }

  @Override
  public void registerResponders(ResponderFactory responderFactory) throws PluginException {
    register(plugin, "registerResponders", ResponderFactory.class, responderFactory);
  }

  @Override
  public void registerSymbolTypes(SymbolProvider symbolProvider) throws PluginException {
    register(plugin, "registerSymbolTypes", SymbolProvider.class, symbolProvider);
  }

  @Override
  public void registerWikiPageFactories(WikiPageFactoryRegistry wikiPageFactoryRegistry) throws PluginException {
    register(plugin, "registerWikiPageFactories", WikiPageFactoryRegistry.class, wikiPageFactoryRegistry);
  }

  @Override
  public void registerTestSystemFactories(TestSystemFactoryRegistry testSystemFactoryRegistry) throws PluginException {
    register(plugin, "registerTestSystemFactories", TestSystemFactoryRegistry.class, testSystemFactoryRegistry);

  }

  @Override
  public void registerSlimTables(SlimTableFactory slimTableFactory) throws PluginException {
    register(plugin, "registerSlimTableFactories", SlimTableFactory.class, slimTableFactory);
  }

  @Override
  public void registerCustomComparators(CustomComparatorRegistry customComparatorRegistry) throws PluginException {
    register(plugin, "registerCustomComparatorRegistries", CustomComparatorRegistry.class, customComparatorRegistry);
  }

  private <T> void register(Object plugin, String methodName, Class<T> registrarType, T registrar)
          throws PluginException {
    Method method;
    try {
      method = plugin.getClass().getMethod(methodName, registrarType);
    } catch (NoSuchMethodException e) {
      // ok, no widgets to register in this plugin
      return;
    }

    try {
      method.invoke(plugin, registrar);
    } catch (InvocationTargetException e) {
      throw new PluginException("Unable to execute method " + methodName, e);
    } catch (IllegalAccessException e) {
      throw new PluginException("Unable to execute method " + methodName, e);
    }
  }
}

@@ -126,131 +126,35 @@
 sse.
-authentication.Authenticator;%0Aimport fitnesse.responders.ResponderFactory;%0Aimport fitnesse.responders.editing.ContentFilter
+responders.ResponderFactory
 ;%0Aim
@@ -537,17 +537,14 @@
 ory 
-implement
+extend
 s Pl
@@ -561,16 +561,20 @@
 eFactory
+Base
  %7B%0A%0A  pr
@@ -689,164 +689,8 @@
  %7D%0A%0A
-  @Override%0A  public Authenticator getAuthenticator() %7B%0A    return null;%0A  %7D%0A%0A  @Override%0A  public ContentFilter getContentFilter() %7B%0A    return null;%0A  %7D%0A%0A
   @O
@@ -786,32 +786,36 @@
 Exception %7B%0A    
+if (
 register(plugin,
@@ -870,33 +870,114 @@
 esponderFactory)
-;
+) %7B%0A      LOG.info(%22Registered responders from: %22 + getPluginDescription());%0A    %7D
 %0A  %7D%0A%0A  @Overrid
@@ -1064,32 +1064,36 @@
 Exception %7B%0A    
+if (
 register(plugin,
@@ -1153,17 +1153,100 @@
 rovider)
-;
+) %7B%0A      LOG.info(%22Registered Symbol types from: %22 + getPluginDescription());%0A    %7D
 %0A  %7D%0A%0A  
@@ -1365,32 +1365,36 @@
 Exception %7B%0A    
+if (
 register(plugin,
@@ -1470,33 +1470,123 @@
 FactoryRegistry)
-;
+) %7B%0A      LOG.info(%22Registered wiki page factories from: %22 + getPluginDescription());%0A    %7D
 %0A  %7D%0A%0A  @Overrid
@@ -1703,32 +1703,36 @@
 Exception %7B%0A    
+if (
 register(plugin,
@@ -1822,18 +1822,109 @@
 egistry)
-;%0A
+) %7B%0A      LOG.info(%22Registered test system factories from: %22 + getPluginDescription());%0A    %7D
 %0A  %7D%0A%0A  
@@ -2022,32 +2022,36 @@
 Exception %7B%0A    
+if (
 register(plugin,
@@ -2122,17 +2122,108 @@
 Factory)
-;
+) %7B%0A      LOG.info(%22Registered Slim table factories from: %22 + getPluginDescription());%0A    %7D
 %0A  %7D%0A%0A  
@@ -2352,16 +2352,20 @@
 n %7B%0A    
+if (
 register
@@ -2468,16 +2468,196 @@
 egistry)
+) %7B%0A      LOG.info(%22Registered custom comparator registries from: %22 + getPluginDescription());%0A    %7D%0A  %7D%0A%0A  protected Class%3C?%3E getPluginDescription() %7B%0A    return plugin.getClass()
 ;%0A  %7D%0A%0A 
@@ -2669,20 +2669,23 @@
 ate %3CT%3E 
-void
+boolean
  registe
@@ -2995,16 +2995,22 @@
   return
+ false
 ;%0A    %7D%0A
@@ -3303,18 +3303,35 @@
 , e);%0A    %7D%0A
+    return true;%0A
   %7D%0A%7D%0A

Make LegacyWrapper benefit from base class and log