Class CLIUtils

java.lang.Object
org.apache.solr.cli.CLIUtils

public final class CLIUtils extends Object
Utility class that holds various helper methods for the CLI.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static String
     
    static String
     
    static String
     
    static String
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
     
    static boolean
    Determine if a request to Solr failed due to a communication error, which is generally retry-able.
    static boolean
     
    static String
    getCliOptionOrPropValue(org.apache.commons.cli.CommandLine cli, org.apache.commons.cli.Option option, String sysprop, String defaultValue)
    Get the value of the specified CLI option with fallback to system property and default value.
    static org.apache.solr.client.solrj.impl.CloudSolrClient
    getCloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient.CloudSolrClientConnection solrConnection, HttpJettySolrClient.Builder builder)
     
    static Path
    getConfigSetsDir(Path solrInstallDir)
     
    static String
     
    static org.apache.solr.client.solrj.SolrClient
    getSolrClient(String solrUrl, String credentials)
    Helper method for all the places where we assume a /solr on the url.
    static org.apache.solr.client.solrj.SolrClient
    getSolrClient(String solrUrl, String credentials, boolean barePath)
     
    static org.apache.solr.client.solrj.SolrClient
    getSolrClient(org.apache.commons.cli.CommandLine cli)
     
    static org.apache.solr.client.solrj.SolrClient
    getSolrClient(org.apache.commons.cli.CommandLine cli, boolean barePath)
     
    static org.apache.solr.client.solrj.impl.CloudSolrClient.CloudSolrClientConnection
    getSolrConnection(org.apache.commons.cli.CommandLine cli)
    Resolves a Solr connection definition from command-line options, system properties, environment variables, or cluster discovery.
    static org.apache.solr.common.cloud.SolrZkClient
    getSolrZkClient(org.apache.commons.cli.CommandLine cli, String zkHost)
     
    static String
    getZkHost(org.apache.commons.cli.CommandLine cli)
    Get the ZooKeeper connection string from either the zk-host command-line option or if not configured, from the 'zkHost' system property aka the 'ZK_HOST' environment variable.
    static boolean
    hasConnectionOption(org.apache.commons.cli.CommandLine cli)
    Returns true if the user supplied any of the connection-related CLI options ( --solr-url, --solr-connection, or --zk-host).
    static boolean
    isCloudMode(org.apache.solr.client.solrj.SolrClient solrClient)
     
    static boolean
     
    static String
    Strips off the end of solrUrl any /solr when a legacy solrUrl like http://localhost:8983/solr is used, and warns those users.
    static String
    normalizeSolrUrl(String solrUrl, boolean logUrlFormatWarning)
    Strips off the end of solrUrl any /solr when a legacy solrUrl like http://localhost:8983/solr is used, and optionally logs a warning.
    static String
    normalizeSolrUrl(org.apache.commons.cli.CommandLine cli)
    Get the base URL of a live Solr instance from either the --solr-url command-line option or from SolrCloud.
    static int
    Extracts the port from the provided solrUrl.
    static boolean
    safeCheckCollectionExists(String solrUrl, String collection, String credentials)
     
    static boolean
    safeCheckCoreExists(String solrUrl, String coreName, String credentials)
     
    static String
    solrUrlFromConnection(org.apache.solr.client.solrj.impl.CloudSolrClient.CloudSolrClientConnection solrConnection, String credentials)
    Resolves a base Solr URL from a parsed connection.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • RED

      public static String RED
    • GREEN

      public static String GREEN
    • YELLOW

      public static String YELLOW
    • RESET

      public static String RESET
  • Method Details

    • getDefaultSolrUrl

      public static String getDefaultSolrUrl()
    • checkCommunicationError

      public static boolean checkCommunicationError(Exception exc)
      Determine if a request to Solr failed due to a communication error, which is generally retry-able.
    • checkCodeForAuthError

      public static void checkCodeForAuthError(int code)
    • exceptionIsAuthRelated

      public static boolean exceptionIsAuthRelated(Exception exc)
    • getSolrClient

      public static org.apache.solr.client.solrj.SolrClient getSolrClient(String solrUrl, String credentials, boolean barePath)
    • getSolrClient

      public static org.apache.solr.client.solrj.SolrClient getSolrClient(String solrUrl, String credentials)
      Helper method for all the places where we assume a /solr on the url.
      Parameters:
      solrUrl - The solr url that you want the client for
      credentials - The username:password for basic auth.
      Returns:
      The SolrClient
    • getSolrClient

      public static org.apache.solr.client.solrj.SolrClient getSolrClient(org.apache.commons.cli.CommandLine cli, boolean barePath) throws Exception
      Throws:
      Exception
    • getSolrClient

      public static org.apache.solr.client.solrj.SolrClient getSolrClient(org.apache.commons.cli.CommandLine cli) throws Exception
      Throws:
      Exception
    • normalizeSolrUrl

      public static String normalizeSolrUrl(String solrUrl)
      Strips off the end of solrUrl any /solr when a legacy solrUrl like http://localhost:8983/solr is used, and warns those users. In the future we'll have urls ending with /api as well.
      Parameters:
      solrUrl - The user supplied url to Solr.
      Returns:
      a URL without any path, e.g. http://localhost:8983
    • normalizeSolrUrl

      public static String normalizeSolrUrl(String solrUrl, boolean logUrlFormatWarning)
      Strips off the end of solrUrl any /solr when a legacy solrUrl like http://localhost:8983/solr is used, and optionally logs a warning. In the future we'll have urls ending with /api as well.
      Parameters:
      solrUrl - The user supplied url to Solr.
      logUrlFormatWarning - If a warning message should be logged about the url format
      Returns:
      a URL without any path, e.g. http://localhost:8983
    • normalizeSolrUrl

      public static String normalizeSolrUrl(org.apache.commons.cli.CommandLine cli) throws Exception
      Get the base URL of a live Solr instance from either the --solr-url command-line option or from SolrCloud.
      Throws:
      Exception
    • solrUrlFromConnection

      public static String solrUrlFromConnection(org.apache.solr.client.solrj.impl.CloudSolrClient.CloudSolrClientConnection solrConnection, String credentials) throws Exception
      Resolves a base Solr URL from a parsed connection. The HTTP form (e.g. -s http://host:port) already names a Solr URL, so it is used directly without spinning up a CloudSolrClient; the ZooKeeper form queries the cluster for a live node's base URL.
      Throws:
      Exception
    • hasConnectionOption

      public static boolean hasConnectionOption(org.apache.commons.cli.CommandLine cli)
      Returns true if the user supplied any of the connection-related CLI options ( --solr-url, --solr-connection, or --zk-host). Use this to gate logic that should only run when an explicit connection target was provided.
    • getCliOptionOrPropValue

      public static String getCliOptionOrPropValue(org.apache.commons.cli.CommandLine cli, org.apache.commons.cli.Option option, String sysprop, String defaultValue)
      Get the value of the specified CLI option with fallback to system property and default value.
      Parameters:
      cli - the command line
      option - the commons cli Option
      sysprop - the system property to fall back to
      defaultValue - the default value. Use null if no default value is desired
      Returns:
      the value of the option or system property or the default value
    • getZkHost

      public static String getZkHost(org.apache.commons.cli.CommandLine cli) throws Exception
      Get the ZooKeeper connection string from either the zk-host command-line option or if not configured, from the 'zkHost' system property aka the 'ZK_HOST' environment variable. If neither is configured, we attempt looking it up from a running Solr instance based on the solr-url option.
      Throws:
      Exception
    • getSolrConnection

      public static org.apache.solr.client.solrj.impl.CloudSolrClient.CloudSolrClientConnection getSolrConnection(org.apache.commons.cli.CommandLine cli) throws Exception
      Resolves a Solr connection definition from command-line options, system properties, environment variables, or cluster discovery.

      The connection is resolved in the following order of precedence:

      • solr-connection command-line option
      • solrConnection system property or SOLR_CONNECTION environment variable
      • zk-host command-line option
      • zkHost system property or ZK_HOST environment variable
      • ZooKeeper connection discovered from a running Solr instance using the solr-url option
      • null if none is specified or discoverable
      Returns:
      the resolved Solr connection, or null if unavailable
      Throws:
      Exception
    • getSolrZkClient

      public static org.apache.solr.common.cloud.SolrZkClient getSolrZkClient(org.apache.commons.cli.CommandLine cli, String zkHost)
    • getCloudSolrClient

      public static org.apache.solr.client.solrj.impl.CloudSolrClient getCloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient.CloudSolrClientConnection solrConnection, HttpJettySolrClient.Builder builder)
    • portFromUrl

      public static int portFromUrl(String solrUrl) throws URISyntaxException
      Extracts the port from the provided solrUrl. If a URL is provided with https scheme and not explicitly defines the port, the default port for HTTPS (443) is used.

      If URL does not contain a port nor https as scheme, it falls back to port 80.

      Parameters:
      solrUrl - the URL to extract the port from
      Returns:
      The port that was found.
      Throws:
      NullPointerException - If solrUrl is null
      URISyntaxException - If the given string violates RFC 2396, as augmented by the above deviations
    • safeCheckCollectionExists

      public static boolean safeCheckCollectionExists(String solrUrl, String collection, String credentials)
    • safeCheckCoreExists

      public static boolean safeCheckCoreExists(String solrUrl, String coreName, String credentials)
    • isCloudMode

      public static boolean isCloudMode(org.apache.solr.client.solrj.SolrClient solrClient) throws org.apache.solr.client.solrj.SolrServerException, IOException
      Throws:
      org.apache.solr.client.solrj.SolrServerException
      IOException
    • getConfigSetsDir

      public static Path getConfigSetsDir(Path solrInstallDir)
    • isWindows

      public static boolean isWindows()