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  import java.util.Locale;
    34
    35  /**
    36   * Commandline utility to verify the <b>Any23</b> plugins 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(description = "plugins-dir", converter = FileConverter.class)
    46      private List<File> pluginsDirs = new LinkedList<>();
    47
    48      private PrintStream out = System.out;
    49
    50      @Override
    51      PrintStream getOut() {
    52          return out;
    53      }
    54
    55      @Override
    56      void setOut(PrintStream out) {
    57          this.out = out;
    58      }
    59
    60      public void run() throws Exception {
    61          if (pluginsDirs.isEmpty()) {
    62              throw new IllegalArgumentException("No plugin directory specified.");
    63          }
    64
    65          final File pluginsDir = pluginsDirs.get(0);
    66          if (!pluginsDir.isDirectory()) {
    67              throw new IllegalArgumentException("<plugins-dir> must be a valid dir.");
    68          }
    69
    70          pluginManager.loadJARDir(pluginsDir);
    71
    72          final Iterator<ExtractorFactory> plugins = pluginManager.getExtractors();
    73
    74          while (plugins.hasNext()) {
    75              printPluginData(plugins.next(), out);
    76              out.println("------------------------------------------------------------------------");
    77          }
    78      }
    79
    80      private String getMimeTypesStr(Collection<MIMEType> mimeTypes) {
    81          final StringBuilder sb = new StringBuilder();
    82          for (MIMEType mt : mimeTypes) {
    83              sb.append(mt).append(' ');
    84          }
    85          return sb.toString();
    86      }
    87
    88      private void printPluginData(ExtractorFactory<?> extractorFactory, PrintStream ps) {
    89          final Author authorAnnotation = extractorFactory.getClass().getAnnotation(Author.class);
    90          ps.printf(Locale.ROOT, "Plugin author    : %s%n",
    91                  authorAnnotation == null ? "<unknown>" : authorAnnotation.name());
    92          ps.printf(Locale.ROOT, "Plugin factory   : %s%n", extractorFactory.getClass());
    93          ps.printf(Locale.ROOT, "Plugin mime-types: %s%n", getMimeTypesStr(extractorFactory.getSupportedMIMETypes()));
    94      }
    95
    96  }