Class JPService

java.lang.Object
org.openbase.jps.core.JPService

public class JPService extends Object
Java Property Service, this is the central lib controller used to initialize and manage all properties.
Author:
Divine Threepwood

JPS Library can be used for managing the properties of an application. The argument definition is realized by registering classes which extends the AbstractJavaProperty class. Common argument types are supported by the preset properties (e.g. Integer, Boolean, String types).

The library supports the generation of a properties overview page.

  • Constructor Details

    • JPService

      public JPService()
  • Method Details

    • setApplicationName

      public static void setApplicationName(String name)
      Set the application name. The name is displayed in the help screen in the property overview page if no submodule name is declared.
      Parameters:
      name - the application name
    • getApplicationName

      public static String getApplicationName()
      Returns the configured application name.
      Returns:
      the application name.
    • setApplicationName

      public static void setApplicationName(Class<?> mainclass)
      Set the application name by the main class name. The name is displayed in the help screen in the property overview page if no submodule name is declared. The name is generated by using the lower case simple class name of the given class.
      Parameters:
      mainclass - the application main class which is used to generate the application name.
    • setSubmoduleName

      public static void setSubmoduleName(String name)
      Set the submodule name. The name is displayed in the help screen in the property overview page.
      Parameters:
      name - the application name
    • getSubmoduleName

      public static String getSubmoduleName()
      Returns the configured application name.
      Returns:
      the application name.
    • setSubmoduleName

      public static void setSubmoduleName(Class<?> mainClass)
      Set the submodule name by the main class name. The name is displayed in the help screen in the property overview page. The name is generated by using the lower case simple class name of the given class.
      Parameters:
      mainClass - the submodule submoduleClass which is used to define the binary name.
    • registerProperty

      public static <V, C extends AbstractJavaProperty<V>> void registerProperty(Class<C> propertyClass, V defaultValue)
      Register the given property and overwrite the default value of the given one.

      Do not use this method after calling the analyze method, otherwise recursive property usage is not effected by the new default value!

      Type Parameters:
      V -
      C -
      Parameters:
      propertyClass -
      defaultValue -
    • overwriteDefaultValue

      public static <V, C extends AbstractJavaProperty<V>> void overwriteDefaultValue(Class<C> propertyClass, V defaultValue)
      Overwrites the default value of the given property without displaying the property in the help overview, For overwriting a regular property default value, use the property registration method instead.

      Do not use this method after calling the analyze method, otherwise recursive property usage is not effected by the new default value!

      Type Parameters:
      V -
      C -
      Parameters:
      propertyClass -
      defaultValue -
    • registerProperty

      public static void registerProperty(Class<? extends AbstractJavaProperty<?>> propertyClass)
      Register new property.
      Parameters:
      propertyClass -
    • parseAndExitOnError

      public static void parseAndExitOnError(List<String> args)
      Analyze the input arguments and setup all registered Properties. If one argument could not be handled or something else goes wrong this methods calls System.exit(255);

      Make sure all desired properties are registered before calling this method. Otherwise the properties will not be listed in the help screen.

      Note: In case the JPUnitTestMode was enabled this method does not call exit.

      Parameters:
      args - Arguments as a string list e.g. given by a java fx application getParameters().getRaw() in the start method.
    • parseAndExitOnError

      public static void parseAndExitOnError(String[] args) throws RuntimeException
      Analyze the input arguments and setup all registered Properties. If one argument could not be handled or something else goes wrong this methods calls System.exit(255);

      Make sure all desired properties are registered before calling this method. Otherwise the properties will not be listed in the help screen.

      Note: In case the JPUnitTestMode was enabled this method does not call exit.

      Parameters:
      args - Arguments given by the main method.
      Throws:
      RuntimeException - is thrown only in test mode otherwise System.exit(255) is called.
    • parse

      public static void parse(List<String> args) throws JPServiceException
      Analyze the input arguments and setup all registered Properties.

      Make sure all desired properties are registered before calling this method. Otherwise the properties will not be listed in the help screen.

      Parameters:
      args - Arguments as a string list e.g. given by a java fx application getParameters().getRaw() in the start method.
      Throws:
      JPServiceException - is thrown if the given arguments can not be parsed.
    • exitOnError

      public static void exitOnError()
      Setups all registered Properties.

      Make sure all desired properties are registered before calling this method. Method calls system exit if at least one property could not be initialized.

    • parse

      public static void parse(String[] args) throws JPServiceException
      Analyze the input arguments and setup all registered Properties.

      Make sure all desired properties are registered before calling this method. Otherwise the properties will not be listed in the help screen.

      Parameters:
      args - Arguments given by the main method.
      Throws:
      JPServiceException - in case the args could not be parsed.
    • parse

      public static void parse(String[] args, boolean skipUnknownProperties) throws JPServiceException
      Analyze the input arguments and setup all registered Properties.

      Make sure all desired properties are registered before calling this method. Otherwise the properties will not be listed in the help screen.

      Parameters:
      args - Arguments given by the main method.
      skipUnknownProperties - flag defines if an exception should be thrown in case an unknown property is parsed, otherwise unknown properties are just skiped.
      Throws:
      JPServiceException - in case the args could not be parsed.
    • printError

      public static void printError(String message, Throwable cause)
    • printError

      public static void printError(Throwable cause)
      Parameters:
      cause -
    • printError

      protected static void printError(Throwable cause, String prefix)
      Parameters:
      cause -
      prefix -
    • setupJUnitTestMode

      public static void setupJUnitTestMode() throws JPServiceException
      Setup JPService for JUnitTests By using the JPService during JUnit Tests it's recommended to call this method after property registration instead using the parsing methods because command line property handling makes no sense in the context of unit tests..

      The following properties are activated by default while running JPService in TestMode:

      - JPVerbose is set to true to print more debug messages.

      - JPTestMode is activated.

      Throws:
      JPServiceException
    • getValue

      public static <V, C extends AbstractJavaProperty<V>> V getValue(Class<C> propertyClass, V alternative)
      Returns the current value of the property related to the given propertyClass.

      If the property is never registered but the class is known in the classpath, the method returns the default value. If not in classpath the alternative is returned.

      Note: Method should only be used if it really doesn't matter if the property is available or not because no warning will be printed in this case.

      Note: The given alternative is only used in error case and will not be used as default value. Property default values can be defined during property registration.

      Type Parameters:
      C - the property type.
      V - the value type.
      Parameters:
      alternative - the value used if the property could not be resolved.
      propertyClass - property class which defines the property.
      Returns:
      the current value of the given property type.
    • getValue

      public static <V, C extends AbstractJavaProperty<V>> V getValue(Class<C> propertyClass) throws JPNotAvailableException
      Returns the current value of the property related to the given propertyClass.

      If the property is never registered but the class is known in the classpath, the method returns the default value. If not in classpath the defaultValue is returned.

      Type Parameters:
      C - the property type.
      V - the value type.
      Parameters:
      propertyClass - property class which defines the property.
      Returns:
      the current value of the given property type.
      Throws:
      JPNotAvailableException - thrown if the given property or value could not be found.
    • getPreEvaluatedValue

      public static <V, C extends AbstractJavaProperty<V>> V getPreEvaluatedValue(Class<C> propertyClass, String[] args, V alternative)
      Returns a pre evaluated value of the property related to the given propertyClass.

      Method is similar to the getValue(..) method, while this method temporarly registers the property, evaluates its value and resets jpsservice afterwards. Therefore, this method is only useful when property values are required but the parse method was not yet called.

      Type Parameters:
      C - the property type.
      V - the value type.
      Parameters:
      propertyClass - property class which defines the property.
      args - Arguments given by the main method.
      alternative - the value used if the property could not be resolved.
      Returns:
      the current value of the given property type.
      Throws:
      RuntimeException - when method is called after application arguments were parsed.
    • getPreEvaluatedValue

      public static <V, C extends AbstractJavaProperty<V>> V getPreEvaluatedValue(Class<C> propertyClass, String[] args) throws JPServiceException
      Returns a pre evaluated value of the property related to the given propertyClass.

      Method is similar to the getValue(..) method, while this method temporarly registers the property, evaluates its value and resets jpsservice afterwards. Therefore, this method is only useful when property values are required but the parse method was not yet called.

      Type Parameters:
      C - the property type.
      V - the value type.
      Parameters:
      propertyClass - property class which defines the property.
      args - Arguments given by the main method.
      Returns:
      the current value of the given property type.
      Throws:
      JPNotAvailableException - if the given property or value could not be found.
      JPServiceException - if something went wrong during the pre-evaluation.
      RuntimeException - when method is called after application arguments were parsed.
    • getProperty

      public static <C extends AbstractJavaProperty> C getProperty(Class<C> propertyClass) throws JPNotAvailableException
      Returns the property related to the given propertyClass.

      If the property is never registered but the class is known in the classpath, the method returns the default value.

      Type Parameters:
      C - the property type.
      Parameters:
      propertyClass - property class which defines the property.
      Returns:
      the property.
      Throws:
      JPNotAvailableException - thrown if the given property could not be found.
    • printHelp

      public static void printHelp() throws JPServiceException
      Method prints the help screen.
      Throws:
      JPServiceException
    • getApplicationLogger

      public static org.slf4j.Logger getApplicationLogger(org.slf4j.Logger defaultLogger)
      Returns the logger instance of the main class.

      Note: The submodule main class is preferred in case bath are declared. Note: If the main class is not set via setApplicationName() the defaultLogger is used.

      Returns:
      a default logger instance.
    • getApplicationLogger

      public static org.slf4j.Logger getApplicationLogger()
      Returns the logger instance of the main class.

      Note: If the main class is not set via setApplicationName() the jpservice default logger instance is used.

      Returns:
      a logger instance.
    • newLineFormatter

      public static String newLineFormatter(String text, String newLineOperator, int maxChars)
      Parameters:
      text -
      newLineOperator -
      maxChars -
      Returns:
    • awaitUserConfirmationOrExit

      public static final void awaitUserConfirmationOrExit(String infoMessage, String cancelMessage, int exitCode)
      Method prints the info message and blocks the system input until the user confirms the message. Otherwise, the cancel message will be printed and System.exit is called with the given exit code.
      Parameters:
      infoMessage - the message to inform the user about the situation to confirm.
      cancelMessage - the message which is printed in case the user rejects the action.
      exitCode - the exit code to use in case the user rejects the action.
    • getArgs

      public static String[] getArgs()
      Method delivers the pure arguments that has been passed during application startup.
      Returns:
      the application arguments as string array.
    • reset

      public static void reset()
      Reset the entire property configuration.
    • testMode

      public static boolean testMode()
    • verboseMode

      public static boolean verboseMode()
    • forceMode

      public static boolean forceMode()
    • debugMode

      public static boolean debugMode()