View Javadoc
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 com.beust.jcommander.Parameter;
21  import com.beust.jcommander.Parameters;
22  import com.beust.jcommander.converters.FileConverter;
23  import org.apache.any23.extractor.ExtractorFactory;
24  import org.apache.any23.mime.MIMEType;
25  import org.apache.any23.plugin.Any23PluginManager;
26  import org.apache.any23.plugin.Author;
27  import java.io.File;
28  import java.io.PrintStream;
29  import java.util.Collection;
30  import java.util.Iterator;
31  import java.util.LinkedList;
32  import java.util.List;
33  
34  /**
35   * Commandline utility to verify the <b>Any23</b> plugins
36   * and extract basic information.
37   *
38   * @author Michele Mostarda (mostarda@fbk.eu)
39   */
40  @Parameters(commandNames = { "verify" }, commandDescription = "Utility for plugin management verification.")
41  public class PluginVerifier extends BaseTool {
42  
43      private Any23PluginManager pluginManager = Any23PluginManager.getInstance();
44  
45      @Parameter(
46          description = "plugins-dir",
47          converter = FileConverter.class
48      )
49      private List<File> pluginsDirs = new LinkedList<>();
50  
51      private PrintStream out = System.out;
52  
53      @Override
54      PrintStream getOut() {
55          return out;
56      }
57  
58      @Override
59      void setOut(PrintStream out) {
60          this.out = out;
61      }
62  
63      public void run() throws Exception {
64          if (pluginsDirs.isEmpty()) {
65              throw new IllegalArgumentException("No plugin directory specified.");
66          }
67  
68          final File pluginsDir = pluginsDirs.get(0);
69          if (!pluginsDir.isDirectory()) {
70              throw new IllegalArgumentException("<plugins-dir> must be a valid dir.");
71          }
72  
73          pluginManager.loadJARDir(pluginsDir);
74  
75          final Iterator<ExtractorFactory> plugins = pluginManager.getExtractors();
76  
77          while (plugins.hasNext()) {
78              printPluginData(plugins.next(), out);
79              out.println("------------------------------------------------------------------------");
80          }
81      }
82  
83      private String getMimeTypesStr(Collection<MIMEType> mimeTypes) {
84          final StringBuilder sb = new StringBuilder();
85          for (MIMEType mt : mimeTypes) {
86              sb.append(mt).append(' ');
87          }
88          return sb.toString();
89      }
90  
91      private void printPluginData(ExtractorFactory<?> extractorFactory, PrintStream ps) {
92          final Author authorAnnotation = extractorFactory.getClass().getAnnotation(Author.class);
93          ps.printf("Plugin author    : %s%n", authorAnnotation == null ? "<unknown>" : authorAnnotation.name());
94          ps.printf("Plugin factory   : %s%n", extractorFactory.getClass());
95          ps.printf("Plugin mime-types: %s%n", getMimeTypesStr(extractorFactory.getSupportedMIMETypes()));
96      }
97  
98  }