| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.apache.any23.cli; |
| 19 | |
|
| 20 | |
import org.apache.any23.extractor.ExtractorFactory; |
| 21 | |
import org.apache.any23.mime.MIMEType; |
| 22 | |
import org.apache.any23.plugin.Any23PluginManager; |
| 23 | |
import org.apache.any23.plugin.Author; |
| 24 | |
import org.apache.any23.plugin.ExtractorPlugin; |
| 25 | |
|
| 26 | |
import java.io.File; |
| 27 | |
import java.io.PrintStream; |
| 28 | |
import java.net.MalformedURLException; |
| 29 | |
import java.util.Collection; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
@ToolRunner.Description("Utility for plugin management verification.") |
| 38 | 0 | public class PluginVerifier implements Tool { |
| 39 | |
|
| 40 | 0 | private Any23PluginManager pluginManager = Any23PluginManager.getInstance(); |
| 41 | |
|
| 42 | |
public static void main(String[] args) throws MalformedURLException { |
| 43 | 0 | System.exit( new PluginVerifier().run(args) ); |
| 44 | 0 | } |
| 45 | |
|
| 46 | |
public int run(String[] args) { |
| 47 | 0 | if(args.length != 1) { |
| 48 | 0 | printHelp("Invalid argument."); |
| 49 | 0 | return 1; |
| 50 | |
} |
| 51 | |
|
| 52 | 0 | final File pluginsDir = new File(args[0]); |
| 53 | 0 | if(!pluginsDir.isDirectory()) { |
| 54 | 0 | printHelp("<plugins-dir> must be a valid dir."); |
| 55 | 0 | return 2; |
| 56 | |
} |
| 57 | |
|
| 58 | |
final Class<ExtractorPlugin>[] plugins; |
| 59 | |
try{ |
| 60 | 0 | pluginManager.loadJARDir(pluginsDir); |
| 61 | 0 | plugins = pluginManager.getPlugins(); |
| 62 | 0 | } catch (Exception e) { |
| 63 | 0 | e.printStackTrace(System.err); |
| 64 | 0 | return 3; |
| 65 | 0 | } |
| 66 | 0 | for(Class<ExtractorPlugin> p : plugins) { |
| 67 | 0 | System.out.println("-----------------------------"); |
| 68 | 0 | printPluginData(p, System.out); |
| 69 | 0 | System.out.println("-----------------------------"); |
| 70 | |
} |
| 71 | 0 | return 0; |
| 72 | |
} |
| 73 | |
|
| 74 | |
private void printHelp(String msg) { |
| 75 | 0 | System.err.println("***ERROR: " + msg); |
| 76 | 0 | System.err.println("Usage: " + this.getClass().getSimpleName() + " <plugins-dir>"); |
| 77 | 0 | } |
| 78 | |
|
| 79 | |
private String getMimeTypesStr(Collection<MIMEType> mimeTypes) { |
| 80 | 0 | final StringBuilder sb = new StringBuilder(); |
| 81 | 0 | for(MIMEType mt : mimeTypes) { |
| 82 | 0 | sb.append(mt).append(' '); |
| 83 | |
} |
| 84 | 0 | return sb.toString(); |
| 85 | |
} |
| 86 | |
|
| 87 | |
private void printPluginData(Class<ExtractorPlugin> extractorPlugin, PrintStream ps) { |
| 88 | 0 | final Author authorAnnotation = extractorPlugin.getAnnotation(Author.class); |
| 89 | |
final ExtractorPlugin instance; |
| 90 | |
try { |
| 91 | 0 | instance = extractorPlugin.newInstance(); |
| 92 | 0 | } catch (Exception e) { |
| 93 | 0 | throw new IllegalStateException("Error while instantiating plugin.", e); |
| 94 | 0 | } |
| 95 | 0 | final ExtractorFactory extractorFactory = instance.getExtractorFactory(); |
| 96 | 0 | ps.printf("Plugin class : %s\n", extractorPlugin.getClass()); |
| 97 | 0 | ps.printf("Plugin author : %s\n", authorAnnotation == null ? "<unknown>" : authorAnnotation.name()); |
| 98 | 0 | ps.printf("Plugin factory : %s\n", extractorFactory.getClass()); |
| 99 | 0 | ps.printf("Plugin mime-types: %s\n", getMimeTypesStr( extractorFactory.getSupportedMIMETypes() )); |
| 100 | 0 | } |
| 101 | |
|
| 102 | |
} |