incubator-any23-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simonetrip...@apache.org
Subject svn commit: r1293434 - in /incubator/any23/trunk: ./ core/src/main/java/org/apache/any23/cli/ core/src/main/java/org/apache/any23/plugin/ core/src/test/java/org/apache/any23/cli/ core/src/test/java/org/apache/any23/plugin/ core/src/test/resources/org/a...
Date Fri, 24 Feb 2012 21:38:12 GMT
Author: simonetripodi
Date: Fri Feb 24 21:38:11 2012
New Revision: 1293434

URL: http://svn.apache.org/viewvc?rev=1293434&view=rev
Log:
[ANY23-50] Simplify plugin loading avoiding the classpath scanning

Removed:
    incubator/any23/trunk/core/src/main/java/org/apache/any23/plugin/ClassFilter.java
    incubator/any23/trunk/core/src/test/resources/org/apache/any23/plugin/
Modified:
    incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/ExtractorDocumentation.java
    incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/MicrodataParser.java
    incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/MimeDetector.java
    incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/PluginVerifier.java
    incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/Rover.java
    incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/ToolRunner.java
    incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/Version.java
    incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/VocabPrinter.java
    incubator/any23/trunk/core/src/main/java/org/apache/any23/plugin/Any23PluginManager.java
    incubator/any23/trunk/core/src/test/java/org/apache/any23/cli/ToolRunnerTest.java
    incubator/any23/trunk/core/src/test/java/org/apache/any23/plugin/Any23PluginManagerTest.java
    incubator/any23/trunk/plugins/basic-crawler/   (props changed)
    incubator/any23/trunk/plugins/basic-crawler/src/main/java/org/apache/any23/cli/Crawler.java
    incubator/any23/trunk/plugins/html-scraper/src/main/java/org/apache/any23/plugin/htmlscraper/HTMLScraperExtractor.java
    incubator/any23/trunk/plugins/html-scraper/src/main/java/org/apache/any23/plugin/htmlscraper/HTMLScraperPlugin.java
    incubator/any23/trunk/plugins/office-scraper/   (props changed)
    incubator/any23/trunk/plugins/office-scraper/src/main/java/org/apache/any23/plugin/officescraper/ExcelExtractor.java
    incubator/any23/trunk/plugins/office-scraper/src/main/java/org/apache/any23/plugin/officescraper/ExcelPlugin.java
    incubator/any23/trunk/pom.xml

Modified: incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/ExtractorDocumentation.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/ExtractorDocumentation.java?rev=1293434&r1=1293433&r2=1293434&view=diff
==============================================================================
--- incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/ExtractorDocumentation.java
(original)
+++ incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/ExtractorDocumentation.java
Fri Feb 24 21:38:11 2012
@@ -26,6 +26,7 @@ import org.apache.any23.extractor.Extrac
 import org.apache.any23.extractor.Extractor.BlindExtractor;
 import org.apache.any23.extractor.Extractor.ContentExtractor;
 import org.apache.any23.extractor.Extractor.TagSoupDOMExtractor;
+import org.kohsuke.MetaInfServices;
 
 import java.io.IOException;
 
@@ -33,6 +34,7 @@ import java.io.IOException;
  * This class provides some command-line documentation
  * about available extractors and their usage.
  */
+@MetaInfServices
 @ToolRunner.Description("Utility for obtaining documentation about metadata extractors.")
 public class ExtractorDocumentation implements Tool {
 
@@ -186,7 +188,7 @@ public class ExtractorDocumentation impl
 
     /**
      * Prints a complete report on all the available extractors.
-     * 
+     *
      * @throws IOException
      * @throws ExtractionException
      */
@@ -232,5 +234,5 @@ public class ExtractorDocumentation impl
         }
         return "?";
     }
-    
+
 }

Modified: incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/MicrodataParser.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/MicrodataParser.java?rev=1293434&r1=1293433&r2=1293434&view=diff
==============================================================================
--- incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/MicrodataParser.java (original)
+++ incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/MicrodataParser.java Fri
Feb 24 21:38:11 2012
@@ -23,6 +23,7 @@ import org.apache.any23.source.DocumentS
 import org.apache.any23.source.FileDocumentSource;
 import org.apache.any23.source.HTTPDocumentSource;
 import org.apache.any23.util.StreamUtils;
+import org.kohsuke.MetaInfServices;
 
 import java.io.File;
 import java.io.IOException;
@@ -38,6 +39,7 @@ import java.util.regex.Pattern;
  *
  * @author Michele Mostarda (mostarda@fbk.eu)
  */
+@MetaInfServices
 @ToolRunner.Description("Commandline Tool for extracting Microdata from file/HTTP source.")
 public class MicrodataParser implements Tool {
 

Modified: incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/MimeDetector.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/MimeDetector.java?rev=1293434&r1=1293433&r2=1293434&view=diff
==============================================================================
--- incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/MimeDetector.java (original)
+++ incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/MimeDetector.java Fri Feb
24 21:38:11 2012
@@ -28,6 +28,7 @@ import org.apache.any23.source.DocumentS
 import org.apache.any23.source.FileDocumentSource;
 import org.apache.any23.source.HTTPDocumentSource;
 import org.apache.any23.source.StringDocumentSource;
+import org.kohsuke.MetaInfServices;
 
 import java.io.File;
 import java.net.URISyntaxException;
@@ -39,6 +40,7 @@ import java.net.URISyntaxException;
  *
  * @author Michele Mostarda (mostarda@fbk.eu)
  */
+@MetaInfServices
 @ToolRunner.Description("MIME Type Detector Tool.")
 public class MimeDetector implements Tool{
 

Modified: incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/PluginVerifier.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/PluginVerifier.java?rev=1293434&r1=1293433&r2=1293434&view=diff
==============================================================================
--- incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/PluginVerifier.java (original)
+++ incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/PluginVerifier.java Fri
Feb 24 21:38:11 2012
@@ -17,16 +17,18 @@
 
 package org.apache.any23.cli;
 
+import java.io.File;
+import java.io.PrintStream;
+import java.net.MalformedURLException;
+import java.util.Collection;
+import java.util.Iterator;
+
 import org.apache.any23.extractor.ExtractorFactory;
 import org.apache.any23.mime.MIMEType;
 import org.apache.any23.plugin.Any23PluginManager;
 import org.apache.any23.plugin.Author;
 import org.apache.any23.plugin.ExtractorPlugin;
-
-import java.io.File;
-import java.io.PrintStream;
-import java.net.MalformedURLException;
-import java.util.Collection;
+import org.kohsuke.MetaInfServices;
 
 /**
  * Commandline utility to verify the <b>Any23</b> plugins
@@ -34,6 +36,7 @@ import java.util.Collection;
  *
  * @author Michele Mostarda (mostarda@fbk.eu)
  */
+@MetaInfServices
 @ToolRunner.Description("Utility for plugin management verification.")
 public class PluginVerifier implements Tool {
 
@@ -55,17 +58,17 @@ public class PluginVerifier implements T
             return 2;
         }
 
-        final Class<ExtractorPlugin>[] plugins;
+        final Iterator<ExtractorPlugin> plugins;
         try{
             pluginManager.loadJARDir(pluginsDir);
-            plugins = pluginManager.getPlugins();
+            plugins = pluginManager.getExtractors();
         } catch (Exception e) {
             e.printStackTrace(System.err);
             return 3;
         }
-        for(Class<ExtractorPlugin> p : plugins) {
+        while (plugins.hasNext()) {
             System.out.println("-----------------------------");
-            printPluginData(p, System.out);
+            printPluginData(plugins.next().getClass(), System.out);
             System.out.println("-----------------------------");
         }
         return 0;
@@ -84,7 +87,7 @@ public class PluginVerifier implements T
         return sb.toString();
     }
 
-    private void printPluginData(Class<ExtractorPlugin> extractorPlugin, PrintStream
ps) {
+    private void printPluginData(Class<? extends ExtractorPlugin> extractorPlugin,
PrintStream ps) {
         final Author authorAnnotation = extractorPlugin.getAnnotation(Author.class);
         final ExtractorPlugin instance;
         try {
@@ -92,7 +95,7 @@ public class PluginVerifier implements T
         } catch (Exception e) {
             throw new IllegalStateException("Error while instantiating plugin.", e);
         }
-        final ExtractorFactory extractorFactory = instance.getExtractorFactory();
+        final ExtractorFactory<?> extractorFactory = instance.getExtractorFactory();
         ps.printf("Plugin class     : %s\n", extractorPlugin.getClass());
         ps.printf("Plugin author    : %s\n", authorAnnotation == null ? "<unknown>"
: authorAnnotation.name());
         ps.printf("Plugin factory   : %s\n", extractorFactory.getClass());

Modified: incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/Rover.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/Rover.java?rev=1293434&r1=1293433&r2=1293434&view=diff
==============================================================================
--- incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/Rover.java (original)
+++ incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/Rover.java Fri Feb 24 21:38:11
2012
@@ -39,6 +39,7 @@ import org.apache.commons.cli.HelpFormat
 import org.apache.commons.cli.Option;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.PosixParser;
+import org.kohsuke.MetaInfServices;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -62,6 +63,7 @@ import static org.apache.any23.extractor
  * @author Richard Cyganiak (richard@cyganiak.de)
  * @author Gabriele Renzi
  */
+@MetaInfServices
 @ToolRunner.Description("Any23 Command Line Tool.")
 public class Rover implements Tool {
 

Modified: incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/ToolRunner.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/ToolRunner.java?rev=1293434&r1=1293433&r2=1293434&view=diff
==============================================================================
--- incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/ToolRunner.java (original)
+++ incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/ToolRunner.java Fri Feb
24 21:38:11 2012
@@ -17,14 +17,15 @@
 
 package org.apache.any23.cli;
 
-import org.apache.any23.plugin.Any23PluginManager;
-
 import java.io.File;
 import java.io.IOException;
 import java.lang.annotation.ElementType;
 import java.lang.annotation.Retention;
 import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
+import java.util.Iterator;
+
+import org.apache.any23.plugin.Any23PluginManager;
 
 /**
  * This class is the main class responsible to provide a uniform command-line
@@ -33,7 +34,6 @@ import java.lang.annotation.Target;
  * @see ExtractorDocumentation
  * @see Rover
  */
-@ToolRunner.Skip
 public class ToolRunner {
 
     public static final File HOME_PLUGIN_DIR = new File(
@@ -48,29 +48,26 @@ public class ToolRunner {
 
     public static void main(String[] args) throws IOException {
         //Generate automatically the cli.
-        final Class<Tool>[] tools = getToolsInClasspath();
+        final Iterator<Tool> tools = getToolsInClasspath();
         try {
             if (args.length < 1) {
                 usage(null, tools);
             }
 
             final String toolName = args[0];
-            Class<Tool> targetTool = null;
-            for(Class<Tool> tool : tools) {
-                if(tool.getSimpleName().equals(toolName)) {
-                    targetTool = tool;
-                    break;
+
+            while (tools.hasNext()) {
+                Tool tool = tools.next();
+                if (tool.getClass().getSimpleName().equals(toolName)) {
+                    String[] mainArgs = new String[args.length - 1];
+                    System.arraycopy(args, 1, mainArgs, 0, mainArgs.length);
+
+                    System.exit(tool.run(mainArgs));
                 }
             }
-            if(targetTool == null) {
-                usage( String.format("[%s] is not a valid tool name.", toolName), tools);
-                throw new IllegalStateException();
-            }
 
-            String[] mainArgs = new String[args.length - 1];
-            System.arraycopy(args, 1, mainArgs, 0, mainArgs.length);
-            final Tool targetToolInstance = targetTool.newInstance();
-            targetToolInstance.run(mainArgs);
+            usage( String.format("[%s] is not a valid tool name.", toolName), tools);
+            throw new IllegalStateException();
         } catch (Throwable e) {
             e.printStackTrace();
             Throwable cause = e.getCause();
@@ -79,7 +76,7 @@ public class ToolRunner {
         }
     }
 
-    public static Class<Tool>[] getToolsInClasspath() throws IOException {
+    public static Iterator<Tool> getToolsInClasspath() throws IOException {
         final Any23PluginManager pluginManager =  Any23PluginManager.getInstance();
         if(HOME_PLUGIN_DIR.exists()) {
             pluginManager.loadJARDir(HOME_PLUGIN_DIR);
@@ -91,13 +88,14 @@ public class ToolRunner {
         return String.format("%1$#" + n + "s", s);
     }
 
-    private static String getUtilitiesMessage(Class<Tool>[] toolClasses) {
+    private static String getUtilitiesMessage(Iterator<Tool> toolClasses) {
         StringBuffer sb = new StringBuffer();
         sb.append(" where <utility> is one of:\n");
         Description description;
         String utilityName;
         int padding;
-        for (Class<Tool> toolClass :  toolClasses) {
+        while (toolClasses.hasNext()) {
+            Class<?> toolClass = toolClasses.next().getClass();
             utilityName = toolClass.getSimpleName();
             sb.append("\t").append(utilityName);
             description = toolClass.getAnnotation(Description.class);
@@ -110,24 +108,20 @@ public class ToolRunner {
         return sb.toString();
     }
 
-    private static void usage(String msg, Class<Tool>[] utilities) {
-        if(msg != null) {
+    private static void usage(String msg, Iterator<Tool> utilities) {
+        if (msg != null) {
             System.err.println("*** ERROR: " + msg);
             System.err.println();
         }
         System.err.println(USAGE);
-        if(utilities != null) {
-            System.err.println( getUtilitiesMessage(utilities) );
+        if (utilities != null) {
+            System.err.println(getUtilitiesMessage(utilities));
         }
         System.exit(1);
     }
 
     @Retention(RetentionPolicy.RUNTIME)
     @Target(ElementType.TYPE)
-    public @interface Skip {}
-
-    @Retention(RetentionPolicy.RUNTIME)
-    @Target(ElementType.TYPE)
     public @interface Description { String value();  }
 
 }

Modified: incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/Version.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/Version.java?rev=1293434&r1=1293433&r2=1293434&view=diff
==============================================================================
--- incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/Version.java (original)
+++ incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/Version.java Fri Feb 24
21:38:11 2012
@@ -18,12 +18,14 @@
 package org.apache.any23.cli;
 
 import org.apache.any23.Any23;
+import org.kohsuke.MetaInfServices;
 
 /**
  * Prints out the <b>Any23</b> library version.
  *
  * @author Michele Mostarda (mostarda@fbk.eu)
  */
+@MetaInfServices
 @ToolRunner.Description("Prints out the current library version and configuration information.")
 public class Version implements Tool {
 

Modified: incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/VocabPrinter.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/VocabPrinter.java?rev=1293434&r1=1293433&r2=1293434&view=diff
==============================================================================
--- incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/VocabPrinter.java (original)
+++ incubator/any23/trunk/core/src/main/java/org/apache/any23/cli/VocabPrinter.java Fri Feb
24 21:38:11 2012
@@ -24,6 +24,7 @@ import org.apache.commons.cli.HelpFormat
 import org.apache.commons.cli.Option;
 import org.apache.commons.cli.Options;
 import org.apache.commons.cli.PosixParser;
+import org.kohsuke.MetaInfServices;
 
 import java.io.BufferedOutputStream;
 import java.io.IOException;
@@ -34,6 +35,7 @@ import java.util.Arrays;
  *
  * @author Michele Mostarda (mostarda@fbk.eu)
  */
+@MetaInfServices
 @ToolRunner.Description("Prints out the RDF Schema of the vocabularies used by Any23.")
 public class VocabPrinter implements Tool {
 

Modified: incubator/any23/trunk/core/src/main/java/org/apache/any23/plugin/Any23PluginManager.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/core/src/main/java/org/apache/any23/plugin/Any23PluginManager.java?rev=1293434&r1=1293433&r2=1293434&view=diff
==============================================================================
--- incubator/any23/trunk/core/src/main/java/org/apache/any23/plugin/Any23PluginManager.java
(original)
+++ incubator/any23/trunk/core/src/main/java/org/apache/any23/plugin/Any23PluginManager.java
Fri Feb 24 21:38:11 2012
@@ -17,29 +17,27 @@
 
 package org.apache.any23.plugin;
 
-import org.apache.any23.cli.Tool;
-import org.apache.any23.configuration.DefaultConfiguration;
-import org.apache.any23.extractor.ExtractorFactory;
-import org.apache.any23.extractor.ExtractorGroup;
-import org.apache.any23.extractor.ExtractorRegistry;
-import org.apache.any23.util.FileUtils;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
+import static java.util.ServiceLoader.load;
 
 import java.io.File;
-import java.io.FileInputStream;
 import java.io.FilenameFilter;
 import java.io.IOException;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
-import java.net.URLDecoder;
 import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
-import java.util.jar.JarEntry;
-import java.util.jar.JarInputStream;
+
+import org.apache.any23.cli.Tool;
+import org.apache.any23.configuration.DefaultConfiguration;
+import org.apache.any23.extractor.ExtractorFactory;
+import org.apache.any23.extractor.ExtractorGroup;
+import org.apache.any23.extractor.ExtractorRegistry;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * The <i>Any23PluginManager</i> is responsible for inspecting
@@ -247,17 +245,9 @@ public class Any23PluginManager {
      * @return list of matching classes.
      * @throws IOException
      */
-    public synchronized <T> Set<Class<T>> getClassesInPackage(final String
packageName, final ClassFilter filter)
+    public synchronized <T> Iterator<T> getPlugins(final Class<T> type)
     throws IOException {
-        final Set<Class<T>> result = new HashSet<Class<T>>();
-        loadClassesInPackageFromClasspath(packageName, filter, result);
-        for(File jar : dynamicClassLoader.jars) {
-            loadClassesInPackageFromJAR(jar, packageName, filter, result);
-        }
-        for(File dir : dynamicClassLoader.dirs) {
-            loadClassesInPackageFromDir(dir, packageName, filter, result);
-        }
-        return result;
+        return load(type, dynamicClassLoader).iterator();
     }
 
     /**
@@ -266,17 +256,8 @@ public class Any23PluginManager {
      * @return not <code>null</code> list of tool classes.
      * @throws IOException
      */
-    public synchronized Class<Tool>[] getTools() throws IOException {
-        final Set<Class<Tool>> result = getClassesInPackage(
-                CLI_PACKAGE,
-                new ClassFilter() {
-                    @Override
-                    public boolean accept(Class clazz) {
-                        return !clazz.equals(Tool.class) && Tool.class.isAssignableFrom(clazz);
-                    }
-                }
-        );
-        return result.toArray( new Class[result.size()] );
+    public synchronized Iterator<Tool> getTools() throws IOException {
+        return getPlugins(Tool.class);
     }
 
     /**
@@ -285,17 +266,8 @@ public class Any23PluginManager {
      * @return not <code>null</code> list of plugin classes.
      * @throws IOException
      */
-    public synchronized Class<ExtractorPlugin>[] getPlugins() throws IOException {
-        final Set<Class<ExtractorPlugin>> result = getClassesInPackage(
-                PLUGINS_PACKAGE,
-                new ClassFilter() {
-                    @Override
-                    public boolean accept(Class clazz) {
-                        return !clazz.equals(ExtractorPlugin.class) && ExtractorPlugin.class.isAssignableFrom(clazz);
-                    }
-                }
-        );
-        return result.toArray( new Class[result.size()] );
+    public synchronized Iterator<ExtractorPlugin> getExtractors() throws IOException
{
+        return getPlugins(ExtractorPlugin.class);
     }
 
     /**
@@ -333,27 +305,26 @@ public class Any23PluginManager {
                 report.append("}\n");
             }
 
-            final Class<ExtractorPlugin>[] extractorPluginClasses = getPlugins();
-            if (extractorPluginClasses.length == 0) {
+            final List<ExtractorFactory<?>> newFactoryList = new ArrayList<ExtractorFactory<?>>();
+
+            Iterator<ExtractorPlugin> extarctors = getExtractors();
+            while (extarctors.hasNext()) {
+                ExtractorFactory<?> factory = extarctors.next().getExtractorFactory();
+
+                report.append("\n - found plugin: ").append(factory.getExtractorName()).append("\n");
+
+                newFactoryList.add(factory);
+            }
+
+            if (newFactoryList.isEmpty()) {
                 report.append("\n=== No plugins have been found.===\n");
-                return initialExtractorGroup;
-            } else {
-                report.append("\nThe following plugins have been found {\n");
-                final List<ExtractorFactory<?>> newFactoryList = new ArrayList<ExtractorFactory<?>>();
-                for (Class<ExtractorPlugin> extractorPluginClass : extractorPluginClasses)
{
-                    final ExtractorPlugin extractorPlugin = extractorPluginClass.newInstance();
-                    newFactoryList.add(extractorPlugin.getExtractorFactory());
-                    report.append(
-                            extractorPlugin.getExtractorFactory().getExtractorName()
-                    ).append("\n");
-                }
-                report.append("}\n");
+            }
 
-                for(ExtractorFactory extractorFactory : initialExtractorGroup) {
-                    newFactoryList.add(extractorFactory);
-                }
-                return new ExtractorGroup(newFactoryList);
+            for (ExtractorFactory<?> extractorFactory : initialExtractorGroup) {
+                newFactoryList.add(extractorFactory);
             }
+
+            return new ExtractorGroup(newFactoryList);
         } finally {
             logger.info(report.toString());
         }
@@ -393,156 +364,6 @@ public class Any23PluginManager {
     }
 
     /**
-     * Filters classes by criteria within a <i>JAR</i>.
-     *
-     * @param jarFile file addressing the JAR.
-     * @param packageName name of package to scan.
-     * @param filter filter class, all returned classes must extend the specified one.
-     * @param result list for writing result.
-     * @throws java.io.IOException
-     */
-    protected <T> void loadClassesInPackageFromJAR(
-            File jarFile,
-            String packageName,
-            ClassFilter filter,
-            Set<Class<T>> result
-    ) throws IOException {
-        loadJAR(jarFile);
-        packageName = packageName.replaceAll("\\.", "/");
-        JarInputStream jarInputStream = new JarInputStream(new FileInputStream(jarFile));
-        JarEntry jarEntry;
-        while (true) {
-            try {
-                jarEntry = jarInputStream.getNextJarEntry();
-            } catch (IOException ioe) {
-                throw new IllegalStateException("Error while accessing JAR.", ioe);
-            }
-            if (jarEntry == null) {
-                break;
-            }
-            final String jarEntryName = jarEntry.getName();
-            if (jarEntryName.startsWith(packageName) && isValidClassName(jarEntryName))
{
-                final String classEntry = jarEntryName.replaceAll("/", "\\.");
-                final String classStr = classEntry.substring(0, classEntry.indexOf(".class"));
-                final Class clazz;
-                try {
-                    clazz = Class.forName(classStr, true, dynamicClassLoader);
-                } catch (ClassNotFoundException cnfe) {
-                    throw new IllegalStateException("Error while creating class.", cnfe);
-                }
-                if (filter == null || filter.accept(clazz)) {
-                    result.add(clazz);
-                }
-            }
-        }
-    }
-
-    /**
-     * Filters classes by criteria within a <i>class dir</i>.
-     *
-     * @param classDir class directory.
-     * @param packageName name of package to scan.
-     * @param filter filter class, all returned classes must extend the specified one.
-     * @param result list for writing result.
-     * @param <T> class types.
-     * @throws MalformedURLException
-     */
-    protected <T> void loadClassesInPackageFromDir(
-            File classDir,
-            final String packageName,
-            final ClassFilter filter,
-            Set<Class<T>> result
-    ) throws MalformedURLException {
-        if(packageName != null && packageName.trim().length() == 0) {
-            throw new IllegalArgumentException("Invalid packageName filter '" + packageName
+ "'");
-        }
-        loadClassDir(classDir);
-        final int PREFIX_LENGTH = classDir.getAbsolutePath().length();
-        File[] classFiles = FileUtils.listFilesRecursively(
-                classDir,
-                new FilenameFilter() {
-                    @Override
-                    public boolean accept(File dir, String name) {
-                        if (!isValidClassName(name)) return false;
-                        if (packageName == null) return true;
-                        final String absolutePath = dir.getAbsolutePath();
-                        if (absolutePath.length() <= PREFIX_LENGTH) return false;
-                        return
-                                absolutePath
-                                        .substring(PREFIX_LENGTH + 1)
-                                        .replaceAll("/", "\\.")
-                                        .startsWith(packageName);
-                    }
-                }
-        );
-        final int classDirPathLength = classDir.getAbsolutePath().length();
-        for (File classFile : classFiles) {
-            final Class clazz;
-            try {
-                String className =
-                        classFile
-                                .getAbsolutePath()
-                                .substring(classDirPathLength + 1);
-                className = className.substring(0, className.length() - ".class".length()).replaceAll("/",
"\\.");
-                clazz = Class.forName(className, true, dynamicClassLoader);
-            } catch (ClassNotFoundException cnfe) {
-                throw new IllegalStateException("Error while instantiating class.", cnfe);
-            }
-            if (filter == null || filter.accept(clazz)) {
-                result.add(clazz);
-            }
-        }
-    }
-
-    /**
-     * Filters classes by criteria within the initialization <i>classpath</i>.
-     *
-     * @param packageName name of package to scan.
-     * @param filter filter class, all returned classes must extend the specified one.
-     * @param result list for writing result.
-     * @param <T>
-     * @throws IOException
-     */
-    protected <T> void loadClassesInPackageFromClasspath(
-            final String packageName,
-            final ClassFilter filter,
-            Set<Class<T>> result
-    ) throws IOException {
-        final String[] classpathEntries = getClasspathEntries();
-        for (String classPathEntry : classpathEntries) {
-            if(classPathEntry.trim().length() == 0) continue;
-            final File codePath = new File(URLDecoder.decode(classPathEntry, "UTF-8"));
-            if( ! codePath.exists() ) continue;
-            if (codePath.isDirectory()) {
-                loadClassesInPackageFromDir(codePath, packageName, filter, result);
-            } else {
-                loadClassesInPackageFromJAR(codePath, packageName, filter, result);
-            }
-        }
-    }
-
-    /**
-     * @return the classpath entries.
-     */
-    private String[] getClasspathEntries() {
-        final String classpath          = System.getProperty("java.class.path");
-        assert classpath != null : "Class path is null.";
-        final String classpathSeparator = System.getProperty("path.separator");
-        assert classpathSeparator != null : "Class path separator is null.";
-        return classpath.split("\\" + classpathSeparator);
-    }
-
-    /**
-     * Checks if the class name is valid.
-     *
-     * @param clazzName
-     * @return
-     */
-    private boolean isValidClassName(String clazzName) {
-        return clazzName.endsWith(".class") && ! clazzName.contains("$");
-    }
-
-    /**
      * Converts a column separated list of dirs in a list of files.
      *
      * @param pluginDirsList

Modified: incubator/any23/trunk/core/src/test/java/org/apache/any23/cli/ToolRunnerTest.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/core/src/test/java/org/apache/any23/cli/ToolRunnerTest.java?rev=1293434&r1=1293433&r2=1293434&view=diff
==============================================================================
--- incubator/any23/trunk/core/src/test/java/org/apache/any23/cli/ToolRunnerTest.java (original)
+++ incubator/any23/trunk/core/src/test/java/org/apache/any23/cli/ToolRunnerTest.java Fri
Feb 24 21:38:11 2012
@@ -17,14 +17,16 @@
 
 package org.apache.any23.cli;
 
-import org.junit.Assert;
-import org.junit.Test;
+import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
 import java.util.Arrays;
 import java.util.HashSet;
+import java.util.Iterator;
 import java.util.Set;
 
+import org.junit.Test;
+
 /**
  * Test case for {@link ToolRunner}.
  *
@@ -44,8 +46,10 @@ public class ToolRunnerTest {
 
     @Test
     public void testGetToolsInClasspath() throws IOException {
-        Class<Tool>[] tools = ToolRunner.getToolsInClasspath();
-        Assert.assertTrue("Some core tools have not been detected.", coreTools.containsAll(Arrays.asList(tools)));
+        Iterator<Tool> tools = ToolRunner.getToolsInClasspath();
+        while (tools.hasNext()) {
+            assertTrue("Some core tools have not been detected.", coreTools.contains(tools.next().getClass()));
+        }
     }
 
 }

Modified: incubator/any23/trunk/core/src/test/java/org/apache/any23/plugin/Any23PluginManagerTest.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/core/src/test/java/org/apache/any23/plugin/Any23PluginManagerTest.java?rev=1293434&r1=1293433&r2=1293434&view=diff
==============================================================================
--- incubator/any23/trunk/core/src/test/java/org/apache/any23/plugin/Any23PluginManagerTest.java
(original)
+++ incubator/any23/trunk/core/src/test/java/org/apache/any23/plugin/Any23PluginManagerTest.java
Fri Feb 24 21:38:11 2012
@@ -17,11 +17,8 @@
 
 package org.apache.any23.plugin;
 
-import org.apache.any23.cli.Tool;
-import org.junit.After;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
 
 import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
@@ -29,11 +26,15 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
-import java.util.HashSet;
-import java.util.Set;
+import java.util.Iterator;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipInputStream;
 
+import org.apache.any23.cli.Tool;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
 /**
  * Test case for {@link Any23PluginManager}.
  *
@@ -41,8 +42,6 @@ import java.util.zip.ZipInputStream;
  */
 public class Any23PluginManagerTest {
 
-    private static final File TARGET_TEST_JAR = new File("src/test/resources/org/apache/any23/plugin/target.jar");
-
     private Any23PluginManager manager;
 
     @Before
@@ -56,50 +55,15 @@ public class Any23PluginManagerTest {
     }
 
     @Test
-    public <T> void testGetClassesInPackageFromJAR() throws IOException {
-        Set<Class<T>> classes = new HashSet<Class<T>>();
-                manager.loadClassesInPackageFromJAR(
-                        TARGET_TEST_JAR,
-                        "org.hsqldb.store",
-                        null,
-                        classes
-                );
-        Assert.assertEquals(6, classes.size());
-    }
-
-    @Test
-    public <T> void testGetClassesInPackageFromDir() throws IOException {
-        final File tmpDir = File.createTempFile("test-plugin-manager", ".decompressed");
-        tmpDir.delete();
-        tmpDir.mkdirs();
-        decompressJar(TARGET_TEST_JAR, tmpDir);
-
-        Set<Class<T>> classes = new HashSet<Class<T>>();
-        manager.loadClassesInPackageFromDir(
-                tmpDir,
-                "org.hsqldb.store",
-                null,
-                classes
-        );
-        Assert.assertEquals(6, classes.size());
-    }
-
-    @Test
-    public <T> void testGetClassesFromClasspath() throws IOException {
-        Set<Class<T>> clazzes = manager.getClassesInPackage("org.apache.any23.plugin",
null);
-        Assert.assertTrue(clazzes.size() >= 4);
-    }
-
-    @Test
     public void testGetTools() throws IOException {
-        Class<Tool>[] tools = manager.getTools();
-        Assert.assertTrue(tools.length > 0); // NOTE: Punctual tool detection verification
done by ToolRunnerTest.java
+        Iterator<Tool> tools = manager.getTools();
+        assertTrue(tools.hasNext()); // NOTE: Punctual tool detection verification done by
ToolRunnerTest.java
     }
 
     @Test
     public void testGetPlugins() throws IOException {
-        Class<ExtractorPlugin>[] extractorPlugins = manager.getPlugins();
-        Assert.assertEquals(0, extractorPlugins.length);
+        Iterator<ExtractorPlugin> extractorPlugins = manager.getExtractors();
+        assertFalse(extractorPlugins.hasNext());
     }
 
     // TODO: move in FileUtils

Propchange: incubator/any23/trunk/plugins/basic-crawler/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Fri Feb 24 21:38:11 2012
@@ -10,3 +10,4 @@ classes
 javadoc
 projectFilesBackup*
 *.ini
+bin

Modified: incubator/any23/trunk/plugins/basic-crawler/src/main/java/org/apache/any23/cli/Crawler.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/plugins/basic-crawler/src/main/java/org/apache/any23/cli/Crawler.java?rev=1293434&r1=1293433&r2=1293434&view=diff
==============================================================================
--- incubator/any23/trunk/plugins/basic-crawler/src/main/java/org/apache/any23/cli/Crawler.java
(original)
+++ incubator/any23/trunk/plugins/basic-crawler/src/main/java/org/apache/any23/cli/Crawler.java
Fri Feb 24 21:38:11 2012
@@ -24,6 +24,7 @@ import org.apache.any23.source.StringDoc
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 import org.apache.commons.cli.Options;
+import org.kohsuke.MetaInfServices;
 
 import java.io.File;
 import java.io.IOException;
@@ -38,6 +39,7 @@ import java.util.regex.PatternSyntaxExce
  *
  * @author Michele Mostarda (mostarda@fbk.eu)
  */
+@MetaInfServices
 @ToolRunner.Description("Any23 Crawler Command Line Tool.")
 public class Crawler extends Rover {
 

Modified: incubator/any23/trunk/plugins/html-scraper/src/main/java/org/apache/any23/plugin/htmlscraper/HTMLScraperExtractor.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/plugins/html-scraper/src/main/java/org/apache/any23/plugin/htmlscraper/HTMLScraperExtractor.java?rev=1293434&r1=1293433&r2=1293434&view=diff
==============================================================================
--- incubator/any23/trunk/plugins/html-scraper/src/main/java/org/apache/any23/plugin/htmlscraper/HTMLScraperExtractor.java
(original)
+++ incubator/any23/trunk/plugins/html-scraper/src/main/java/org/apache/any23/plugin/htmlscraper/HTMLScraperExtractor.java
Fri Feb 24 21:38:11 2012
@@ -31,6 +31,7 @@ import org.apache.any23.extractor.Extrac
 import org.apache.any23.extractor.ExtractorFactory;
 import org.apache.any23.extractor.SimpleExtractorFactory;
 import org.apache.any23.vocab.SINDICE;
+import org.kohsuke.MetaInfServices;
 import org.openrdf.model.URI;
 import org.openrdf.model.impl.ValueFactoryImpl;
 
@@ -47,6 +48,7 @@ import java.util.List;
  * @see HTMLScraperPlugin
  * @author Michele Mostarda (mostarda@fbk.eu)
  */
+@MetaInfServices( value = Extractor.class )
 public class HTMLScraperExtractor implements Extractor.ContentExtractor {
 
     public final static String NAME = "html-scraper";

Modified: incubator/any23/trunk/plugins/html-scraper/src/main/java/org/apache/any23/plugin/htmlscraper/HTMLScraperPlugin.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/plugins/html-scraper/src/main/java/org/apache/any23/plugin/htmlscraper/HTMLScraperPlugin.java?rev=1293434&r1=1293433&r2=1293434&view=diff
==============================================================================
--- incubator/any23/trunk/plugins/html-scraper/src/main/java/org/apache/any23/plugin/htmlscraper/HTMLScraperPlugin.java
(original)
+++ incubator/any23/trunk/plugins/html-scraper/src/main/java/org/apache/any23/plugin/htmlscraper/HTMLScraperPlugin.java
Fri Feb 24 21:38:11 2012
@@ -20,6 +20,7 @@ package org.apache.any23.plugin.htmlscra
 import org.apache.any23.extractor.ExtractorFactory;
 import org.apache.any23.plugin.Author;
 import org.apache.any23.plugin.ExtractorPlugin;
+import org.kohsuke.MetaInfServices;
 
 /**
  * Implementation of {@link ExtractorPlugin} based on the
@@ -28,6 +29,7 @@ import org.apache.any23.plugin.Extractor
  * @see HTMLScraperExtractor
  * @author Michele Mostarda (mostarda@fbk.eu)
  */
+@MetaInfServices
 @Author(name="Michele Mostarda (mostarda@fbk.eu)")
 public class HTMLScraperPlugin implements ExtractorPlugin {
 

Propchange: incubator/any23/trunk/plugins/office-scraper/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Fri Feb 24 21:38:11 2012
@@ -6,3 +6,4 @@
 build.log
 target
 classes
+bin

Modified: incubator/any23/trunk/plugins/office-scraper/src/main/java/org/apache/any23/plugin/officescraper/ExcelExtractor.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/plugins/office-scraper/src/main/java/org/apache/any23/plugin/officescraper/ExcelExtractor.java?rev=1293434&r1=1293433&r2=1293434&view=diff
==============================================================================
--- incubator/any23/trunk/plugins/office-scraper/src/main/java/org/apache/any23/plugin/officescraper/ExcelExtractor.java
(original)
+++ incubator/any23/trunk/plugins/office-scraper/src/main/java/org/apache/any23/plugin/officescraper/ExcelExtractor.java
Fri Feb 24 21:38:11 2012
@@ -33,6 +33,7 @@ import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.kohsuke.MetaInfServices;
 import org.openrdf.model.URI;
 import org.openrdf.model.vocabulary.RDF;
 
@@ -49,6 +50,7 @@ import java.util.Arrays;
  *
  * @author Michele Mostarda (mostarda@fbk.eu)
  */
+@MetaInfServices( value = Extractor.class )
 public class ExcelExtractor implements Extractor.ContentExtractor {
 
     private static final Excel excel = Excel.getInstance();

Modified: incubator/any23/trunk/plugins/office-scraper/src/main/java/org/apache/any23/plugin/officescraper/ExcelPlugin.java
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/plugins/office-scraper/src/main/java/org/apache/any23/plugin/officescraper/ExcelPlugin.java?rev=1293434&r1=1293433&r2=1293434&view=diff
==============================================================================
--- incubator/any23/trunk/plugins/office-scraper/src/main/java/org/apache/any23/plugin/officescraper/ExcelPlugin.java
(original)
+++ incubator/any23/trunk/plugins/office-scraper/src/main/java/org/apache/any23/plugin/officescraper/ExcelPlugin.java
Fri Feb 24 21:38:11 2012
@@ -20,6 +20,7 @@ package org.apache.any23.plugin.officesc
 import org.apache.any23.extractor.ExtractorFactory;
 import org.apache.any23.plugin.Author;
 import org.apache.any23.plugin.ExtractorPlugin;
+import org.kohsuke.MetaInfServices;
 
 /**
  * Implementation of {@link org.apache.any23.plugin.ExtractorPlugin} for {@link ExcelExtractor}.
@@ -27,6 +28,7 @@ import org.apache.any23.plugin.Extractor
  * @see ExcelExtractor
  * @author Michele Mostarda (mostarda@fbk.eu)
  */
+@MetaInfServices
 @Author(name="Michele Mostarda (mostarda@fbk.eu)")
 public class ExcelPlugin implements ExtractorPlugin {
 

Modified: incubator/any23/trunk/pom.xml
URL: http://svn.apache.org/viewvc/incubator/any23/trunk/pom.xml?rev=1293434&r1=1293433&r2=1293434&view=diff
==============================================================================
--- incubator/any23/trunk/pom.xml (original)
+++ incubator/any23/trunk/pom.xml Fri Feb 24 21:38:11 2012
@@ -402,6 +402,15 @@
     </dependency>
     <!-- END: logger -->
 
+    <!-- BEGIN: plugins -->
+    <dependency>
+      <groupId>org.kohsuke.metainf-services</groupId>
+      <artifactId>metainf-services</artifactId>
+      <version>1.3</version>
+      <scope>provided</scope>
+    </dependency>
+    <!-- END: plugins -->
+
     <!-- BEGIN: Test Dependencies -->
     <dependency>
       <groupId>junit</groupId>



Mime
View raw message