##### Coverage Report - org.apache.any23.cli.PluginVerifier

Classes in this File | Line Coverage | Branch Coverage | Complexity
PluginVerifier |
0% | 0/40
0% | 0/10
| 3.2;3.2

1 |  |
     /*
2 |  |
      * Licensed to the Apache Software Foundation (ASF) under one or more
3 |  |
      * contributor license agreements.  See the NOTICE file distributed with
4 |  |
      * this work for additional information regarding copyright ownership.
5 |  |
      * The ASF licenses this file to You under the Apache License, Version 2.0
6 |  |
      * (the "License"); you may not use this file except in compliance with
7 |  |
      * the License.  You may obtain a copy of the License at
8 |  |
      *
9 |  |
      *  http://www.apache.org/licenses/LICENSE-2.0
10 |  |
      *
11 |  |
      * Unless required by applicable law or agreed to in writing, software
12 |  |
      * distributed under the License is distributed on an "AS IS" BASIS,
13 |  |
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 |  |
      * See the License for the specific language governing permissions and
15 |  |
      * limitations under the License.
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 |  |
      * Commandline utility to verify the <b>Any23</b> plugins
33 |  |
      * and extract basic information.
34 |  |
      *
35 |  |
      * @author Michele Mostarda (mostarda@fbk.eu)
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 |  |
     }