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 Summary
Constructors Constructor Description JPService()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidawaitUserConfirmationOrExit(String infoMessage, String cancelMessage, int exitCode)Method prints the info message and blocks the system input until the user confirms the message.static booleandebugMode()static voidexitOnError()Setups all registered Properties.static booleanforceMode()static org.slf4j.LoggergetApplicationLogger()Returns the logger instance of the main class.static org.slf4j.LoggergetApplicationLogger(org.slf4j.Logger defaultLogger)Returns the logger instance of the main class.static StringgetApplicationName()Returns the configurated application name.static <C extends AbstractJavaProperty>
CgetProperty(Class<C> propertyClass)Returns the property related to the givenpropertyClass.static <V,C extends AbstractJavaProperty<V>>
VgetValue(Class<C> propertyClass)Returns the current value of the property related to the givenpropertyClass.static <V,C extends AbstractJavaProperty<V>>
VgetValue(Class<C> propertyClass, V alternative)Returns 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 voidparse(String[] args)Analyze the input arguments and setup all registered Properties.static voidparse(List<String> args)Analyze 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 voidprintHelp()Method 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 voidsetupJUnitTestMode()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..static booleantestMode()static booleanverboseMode()
-
-
-
Method Detail
-
setApplicationName
public static void setApplicationName(String name)
Set the application name. The name is displayed in the help screen in the property overview page.- Parameters:
name- the application name
-
getApplicationName
public static String getApplicationName()
Returns the configurated 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. The name is generated by using the lower case simple class name of the given class.- Parameters:
mainclass- the application mainclass which is used to generate the application 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 applicationgetParameters().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 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
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
-
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 JPServiceExceptionSetup 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.
-
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
public static void printHelp() throws JPServiceExceptionMethod 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: 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
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.
-
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()
-
-