Class JPService
- 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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic final voidawaitUserConfirmationOrExit(String infoMessage, String cancelMessage, int exitCode) Method prints the info message and blocks the system input until the user confirms the message.static booleanstatic voidSetups all registered Properties.static booleanstatic org.slf4j.LoggerReturns the logger instance of the main class.static org.slf4j.LoggergetApplicationLogger(org.slf4j.Logger defaultLogger) Returns the logger instance of the main class.static StringReturns the configured application name.static String[]getArgs()Method delivers the pure arguments that has been passed during application startup.static <V,C extends AbstractJavaProperty<V>>
VgetPreEvaluatedValue(Class<C> propertyClass, String[] args) Returns a pre evaluated value of the property related to the givenpropertyClass.static <V,C extends AbstractJavaProperty<V>>
VgetPreEvaluatedValue(Class<C> propertyClass, String[] args, V alternative) Returns a pre evaluated value of the property related to the givenpropertyClass.static <C extends AbstractJavaProperty>
CgetProperty(Class<C> propertyClass) Returns the property related to the givenpropertyClass.static StringReturns the configured application name.static <V,C extends AbstractJavaProperty<V>>
VReturns the current value of the property related to the givenpropertyClass.static <V,C extends AbstractJavaProperty<V>>
VReturns the current value of the property related to the givenpropertyClass.static StringnewLineFormatter(String text, String newLineOperator, int maxChars) static <V,C extends AbstractJavaProperty<V>>
voidoverwriteDefaultValue(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.static voidAnalyze the input arguments and setup all registered Properties.static voidAnalyze the input arguments and setup all registered Properties.static voidAnalyze the input arguments and setup all registered Properties.static voidparseAndExitOnError(String[] args) Analyze the input arguments and setup all registered Properties.static voidparseAndExitOnError(List<String> args) Analyze the input arguments and setup all registered Properties.static voidprintError(String message, Throwable cause) static voidprintError(Throwable cause) protected static voidprintError(Throwable cause, String prefix) static voidMethod prints the help screen.static voidregisterProperty(Class<? extends AbstractJavaProperty<?>> propertyClass) Register new property.static <V,C extends AbstractJavaProperty<V>>
voidregisterProperty(Class<C> propertyClass, V defaultValue) Register the given property and overwrite the default value of the given one.static voidreset()static voidsetApplicationName(Class<?> mainclass) Set the application name by the main class name.static voidsetApplicationName(String name) Set the application name.static voidsetSubmoduleName(Class<?> mainClass) Set the submodule name by the main class name.static voidsetSubmoduleName(String name) Set the submodule name.static voidSetup 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..static booleantestMode()static boolean
-
Constructor Details
-
JPService
public JPService()
-
-
Method Details
-
setApplicationName
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
Returns the configured application name.- Returns:
- the application name.
-
setApplicationName
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
Set the submodule name. The name is displayed in the help screen in the property overview page.- Parameters:
name- the application name
-
getSubmoduleName
Returns the configured application name.- Returns:
- the application name.
-
setSubmoduleName
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
Register new property.- Parameters:
propertyClass-
-
parseAndExitOnError
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 applicationgetParameters().getRaw()in the start method.
-
parseAndExitOnError
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
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 applicationgetParameters().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
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 theargscould not be parsed.
-
parse
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 theargscould not be parsed.
-
printError
-
printError
- Parameters:
cause-
-
printError
- Parameters:
cause-prefix-
-
setupJUnitTestMode
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 givenpropertyClass.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
alternativeis 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
alternativeis 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 givenpropertyClass.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
defaultValueis 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 givenpropertyClass.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 givenpropertyClass.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 givenpropertyClass.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
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()thedefaultLoggeris 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
- 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
Method delivers the pure arguments that has been passed during application startup.- Returns:
- the application arguments as string array.
-
reset
public static void reset() -
testMode
public static boolean testMode() -
verboseMode
public static boolean verboseMode() -
forceMode
public static boolean forceMode() -
debugMode
public static boolean debugMode()
-