All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.lucene.util.CommandLineUtil Maven / Gradle / Ivy

There is a newer version: 2024.11.18751.20241128T090041Z-241100
Show newest version
/*
 * COPIED FROM APACHE LUCENE 4.7.2
 *
 * Git URL: [email protected]:apache/lucene.git, tag: releases/lucene-solr/4.7.2, path: lucene/core/src/java
 *
 * (see https://issues.apache.org/jira/browse/OAK-10786 for details)
 */

package org.apache.lucene.util;

/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

/**
 * Class containing some useful methods used by command line tools 
 *
 */
public final class CommandLineUtil {
  
  private CommandLineUtil() {
    
  }
  
  /**
   * Creates a specific FSDirectory instance starting from its class name
   * @param clazzName The name of the FSDirectory class to load
   * @param file The file to be used as parameter constructor
   * @return the new FSDirectory instance
   */
  public static FSDirectory newFSDirectory(String clazzName, File file) {
    try {
      final Class clazz = loadFSDirectoryClass(clazzName);
      return newFSDirectory(clazz, file);
    } catch (ClassNotFoundException e) {
      throw new IllegalArgumentException(FSDirectory.class.getSimpleName()
          + " implementation not found: " + clazzName, e);
    } catch (ClassCastException e) {
      throw new IllegalArgumentException(clazzName + " is not a " + FSDirectory.class.getSimpleName()
          + " implementation", e);
    } catch (NoSuchMethodException e) {
      throw new IllegalArgumentException(clazzName + " constructor with "
          + File.class.getSimpleName() + " as parameter not found", e);
    } catch (Exception e) {
      throw new IllegalArgumentException("Error creating " + clazzName + " instance", e);
    }
  }
  
  /**
   * Loads a specific Directory implementation 
   * @param clazzName The name of the Directory class to load
   * @return The Directory class loaded
   * @throws ClassNotFoundException If the specified class cannot be found.
   */
  public static Class loadDirectoryClass(String clazzName) 
      throws ClassNotFoundException {
    return Class.forName(adjustDirectoryClassName(clazzName)).asSubclass(Directory.class);
  }
  
  /**
   * Loads a specific FSDirectory implementation
   * @param clazzName The name of the FSDirectory class to load
   * @return The FSDirectory class loaded
   * @throws ClassNotFoundException If the specified class cannot be found.
   */
  public static Class loadFSDirectoryClass(String clazzName) 
      throws ClassNotFoundException {
    return Class.forName(adjustDirectoryClassName(clazzName)).asSubclass(FSDirectory.class);
  }
  
  private static String adjustDirectoryClassName(String clazzName) {
    if (clazzName == null || clazzName.trim().length() == 0) {
      throw new IllegalArgumentException("The " + FSDirectory.class.getSimpleName()
          + " implementation cannot be null or empty");
    }
    
    if (clazzName.indexOf(".") == -1) {// if not fully qualified, assume .store
      clazzName = Directory.class.getPackage().getName() + "." + clazzName;
    }
    return clazzName;
  }
  
  /**
   * Creates a new specific FSDirectory instance
   * @param clazz The class of the object to be created
   * @param file The file to be used as parameter constructor
   * @return The new FSDirectory instance
   * @throws NoSuchMethodException If the Directory does not have a constructor that takes File.
   * @throws InstantiationException If the class is abstract or an interface.
   * @throws IllegalAccessException If the constructor does not have public visibility.
   * @throws InvocationTargetException If the constructor throws an exception
   */
  public static FSDirectory newFSDirectory(Class clazz, File file) 
      throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
    // Assuming every FSDirectory has a ctor(File):
    Constructor ctor = clazz.getConstructor(File.class);
    return ctor.newInstance(file);
  }
  
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy