@NotThreadSafe
public class Config
extends Object
Keeps configuration informations for a service (MBean). Also supplies convenience methods to handle service context informations.
Although instances are not thread safe, the service class loader methods are.
Modifier and Type | Field and Description |
---|---|
static String |
CACHE_DIR_PROPERTY
Contains the path to the cache directory.
|
static String |
CONFIG_DIR_PROPERTY
This property is automagically created when the configuration file
loading is initiated.
|
static URI |
CWD_URI
Current working directory URI.
|
static String |
DEFAULT_CACHE_DIR
Default cache directory.
|
static String |
DEFAULT_SERVICE_DATA_DIR
Default service data directory path.
|
static String |
RVPF_PROPERTIES
Holds the path to the initial properties text.
|
static String |
SERVICE_DATA_DIR_PROPERTY
Specifies the service data directory path.
|
static String |
SERVICE_FILENAME
Service file name.
|
static String |
SUBSTITUTION_DEFERRED_PROPERTY
Asks to defer substitution when a marker is of the form '$${x}'.
|
static String |
SUBSTITUTION_ENABLED_PROPERTY
Enables or disables the marker substitution mode where a marker is of
the form '${x}'.
|
static String |
SYSTEM_PROPERTY_PREFIX
The system property prefix for RVPF specific properties.
|
static String |
UUID_FILENAME
UUID file name.
|
Constructor and Description |
---|
Config(String serviceName)
Constructs an instance.
|
Config(String serviceName,
ConfigProperties properties)
Constructs an instance.
|
Modifier and Type | Method and Description |
---|---|
void |
addClassDefEntity(ClassDefEntity classDef)
Adds a class definition entity.
|
void |
addClassLibEntity(ClassLibEntity classLib)
Adds a class library entity.
|
void |
addServiceContext(ServiceContext serviceContext)
Adds properties for a Service.
|
boolean |
containsProperties(String key)
Asks if this contains a properties group.
|
boolean |
containsProperty(String key)
Asks if this contains a property.
|
URL |
createURL(String from,
Optional<URL> context)
Creates a URL.
|
static File |
dataDir(Optional<File> parentDir,
KeyedGroups serviceProperties,
String dataDirProperty,
String defaultDirName)
Returns a data directory.
|
void |
fetchSystemProperties()
Fetches system properties.
|
boolean |
getBooleanValue(String key)
Gets a boolean value for a key, defaulting to false.
|
boolean |
getBooleanValue(String key,
boolean defaultValue)
Gets a boolean value, providing a default.
|
File |
getCacheDir(String section,
Optional<UUID> uuid,
Optional<String> ident)
Gets the cache directory.
|
Optional<? extends ClassDef> |
getClassDef(String key,
Optional<ClassDef> defaultClassDef)
Gets a ClassDef for a key, providing a default.
|
Optional<ClassDefEntity> |
getClassDefEntity(String name)
Gets a class definition entity.
|
ClassDef[] |
getClassDefs(String key)
Gets the ClassDef array for the specified key.
|
Optional<ClassLibEntity> |
getClassLibEntity(String name)
Gets a class library entity.
|
ServiceClassLoader |
getClassLoader()
Gets the service class loader.
|
Optional<String> |
getConfigDir()
Gets the 'config.dir' property.
|
File |
getDataDir()
Gets the data directory.
|
double |
getDoubleValue(String key,
double defaultValue)
Gets a double value, providing a default.
|
Optional<ElapsedTime> |
getElapsedValue(String key,
Optional<ElapsedTime> defaultValue,
Optional<ElapsedTime> emptyValue)
Gets an elapsed time value, providing a default.
|
Optional<HashMap<String,Entity>> |
getEntities()
Gets the entities.
|
File |
getFile(String path)
Gets a File instance for a path.
|
int |
getIntValue(String key,
int defaultValue)
Gets an int value, providing a default.
|
Optional<char[]> |
getPasswordValue(String key)
Gets a password value.
|
ConfigProperties |
getProperties()
Gets the values.
|
KeyedGroups |
getPropertiesGroup(String key)
Gets a group of properties.
|
KeyedGroups[] |
getPropertiesGroups(String key)
Gets the groups of configuration properties for a specified key.
|
String |
getRootName()
Gets the root element name of the document used to load this config.
|
Service |
getService()
Gets the service owning this config.
|
Optional<ServiceContext> |
getServiceContext(String key)
Gets a service context.
|
Collection<ServiceContext> |
getServiceContexts()
Gets the service contexts.
|
String |
getServiceName()
Gets the service name.
|
Optional<String> |
getServiceName(String key)
Gets a service name.
|
ConfigProperties |
getServiceProperties()
Gets the service properties.
|
Optional<UUID> |
getServiceUUID()
Gets the service UUID.
|
Optional<DateTime> |
getStamp()
Gets the last modified stamp.
|
Optional<String> |
getStringValue(String key)
Gets a string value.
|
Optional<String> |
getStringValue(String key,
Optional<String> defaultValue)
Gets a string value, providing a default.
|
String[] |
getStringValues(String key)
Gets the string values for the specified key.
|
protected Logger |
getThisLogger()
Gets the logger.
|
URL |
getURL()
Gets the URL from which this Config was loaded.
|
boolean |
hasService()
Asks if this configuration is linked to a service.
|
boolean |
isIncluded(URL url)
Asks if the supplied URL is already included.
|
void |
keepEntities(Optional<HashMap<String,Entity>> entities)
Keeps the entities.
|
boolean |
registerClassLib(String classLibName)
Registers a ClassLib.
|
void |
registerClassLoader()
Registers the class loader.
|
void |
restoreState(Object state)
Restores the state.
|
Optional<HashMap<String,Entity>> |
returnEntities()
Returns the entities that were kept.
|
Object |
saveState()
Saves the state.
|
void |
setClassLoader(Optional<ServiceClassLoader> classLoader)
Sets the service class loader.
|
void |
setConfigDir(String dir)
Sets the 'config.dir' property.
|
void |
setRootName(String rootName)
Sets the root element name of the document used to load this Config.
|
void |
setService(Service service)
Sets the service owning this Config.
|
void |
setServiceProperties(ConfigProperties properties)
Sets the service properties.
|
void |
setUpService()
Sets up the service.
|
void |
setURL(URL url)
Sets the URL from which this Config was loaded.
|
Params |
substitute(Params params)
Substitutes markers in parameter string values.
|
String |
substitute(String text,
boolean deferred)
Substitutes markers in a given text.
|
void |
tearDown()
Tears down what has been set up.
|
String |
toString() |
void |
updateStamp(Optional<DateTime> stamp)
Updates the last modified stamp.
|
public static final String CACHE_DIR_PROPERTY
public static final String CONFIG_DIR_PROPERTY
public static final URI CWD_URI
public static final String DEFAULT_CACHE_DIR
public static final String DEFAULT_SERVICE_DATA_DIR
public static final String RVPF_PROPERTIES
public static final String SERVICE_DATA_DIR_PROPERTY
public static final String SERVICE_FILENAME
public static final String SUBSTITUTION_DEFERRED_PROPERTY
public static final String SUBSTITUTION_ENABLED_PROPERTY
public static final String SYSTEM_PROPERTY_PREFIX
public static final String UUID_FILENAME
public Config(@Nonnull String serviceName)
serviceName
- The service name (may be empty).public Config(@Nonnull String serviceName, @Nonnull ConfigProperties properties)
serviceName
- The service name (may be empty).properties
- The properties.@Nonnull @CheckReturnValue public static final File dataDir(@Nonnull Optional<File> parentDir, @Nonnull KeyedGroups serviceProperties, @Nonnull String dataDirProperty, @Nonnull String defaultDirName)
parentDir
- An optional parent directory.serviceProperties
- The service properties.dataDirProperty
- The data directory property.defaultDirName
- The default directory name.public final void addClassDefEntity(@Nonnull ClassDefEntity classDef)
classDef
- The class definition entity.public final void addClassLibEntity(@Nonnull ClassLibEntity classLib)
classLib
- The class library entity.public final void addServiceContext(@Nonnull ServiceContext serviceContext)
If the Service already exists, the new informations are appended to those already present.
serviceContext
- The properties.@CheckReturnValue public final boolean containsProperties(@Nonnull String key)
key
- The properties group key (name).@CheckReturnValue public final boolean containsProperty(@Nonnull String key)
key
- The property key (name).@Nullable @CheckReturnValue public final URL createURL(@Nonnull String from, @Nonnull Optional<URL> context)
If the context is null, if the 'config.dir' property is defined, it is used as context, otherwise, the document is searched as a resource on the classpath; if not found, the context is set to the current directory.
from
- The document source.context
- The optional current directory context.public final void fetchSystemProperties()
@CheckReturnValue public final boolean getBooleanValue(@Nonnull String key)
If the key is not found or its value is unrecognized, false will be returned.
key
- The name of the value.@CheckReturnValue public final boolean getBooleanValue(@Nonnull String key, boolean defaultValue)
If the key is not found or its value is unrecognized the default will be returned.
key
- The key for the value.defaultValue
- The default value.@Nullable @CheckReturnValue public final File getCacheDir(@Nonnull String section, @Nonnull Optional<UUID> uuid, @Nonnull Optional<String> ident)
The section name is used as the name of a subdirectory of the cache directory specified in this configuration. When supplied, the UUID is used to generate a name for a further subdirectory level.
When the resulting directory is absent, it is created and initialized.
section
- The name of the cache section.uuid
- An optional UUID as subsection.ident
- An optional service identification.@Nonnull @CheckReturnValue public final Optional<? extends ClassDef> getClassDef(@Nonnull String key, @Nonnull Optional<ClassDef> defaultClassDef)
key
- The key for the ClassDef.defaultClassDef
- The optional default for the key.@Nonnull @CheckReturnValue public final Optional<ClassDefEntity> getClassDefEntity(@Nonnull String name)
name
- The name of the class definition entity.@Nonnull @CheckReturnValue public final ClassDef[] getClassDefs(@Nonnull String key)
key
- The key for the requested ClassDef array.@Nonnull @CheckReturnValue public final Optional<ClassLibEntity> getClassLibEntity(@Nonnull String name)
name
- The name of the class library entity.@Nonnull @CheckReturnValue public final ServiceClassLoader getClassLoader()
If the service class loader has not been set before, an instance is requested from its class.
@Nonnull @CheckReturnValue public final Optional<String> getConfigDir()
@Nonnull @CheckReturnValue public final File getDataDir()
@CheckReturnValue public final double getDoubleValue(String key, double defaultValue)
If the key is not found or its value is unrecognized the default will be returned.
key
- The key for the value.defaultValue
- The default value.@Nonnull @CheckReturnValue public final Optional<ElapsedTime> getElapsedValue(@Nonnull String key, @Nonnull Optional<ElapsedTime> defaultValue, @Nonnull Optional<ElapsedTime> emptyValue)
key
- The key for the value.defaultValue
- The optional default value.emptyValue
- The optional assumed value for empty.KeyedValues
@Nonnull @CheckReturnValue public final Optional<HashMap<String,Entity>> getEntities()
@Nonnull @CheckReturnValue public final File getFile(String path)
If the path is relative, it will use either the config URL as base if it is a file, or the current directory.
path
- The path.@CheckReturnValue public final int getIntValue(@Nonnull String key, int defaultValue)
If the key is not found or its value is unrecognized the default will be returned.
key
- The key for the value.defaultValue
- The default value.@Nonnull @CheckReturnValue public final Optional<char[]> getPasswordValue(@Nonnull String key)
key
- The key for the value.@Nonnull @CheckReturnValue public final ConfigProperties getProperties()
@Nonnull @CheckReturnValue public final KeyedGroups getPropertiesGroup(@Nonnull String key)
key
- The name of the group.@Nonnull @CheckReturnValue public final KeyedGroups[] getPropertiesGroups(@Nonnull String key)
key
- The name of the groups.@Nonnull @CheckReturnValue public final String getRootName()
@Nonnull @CheckReturnValue public final Service getService()
@Nonnull @CheckReturnValue public final Optional<ServiceContext> getServiceContext(@Nonnull String key)
The service aliases will be searched when the key does not correspond to a service definition.
key
- The name of the service.@Nonnull @CheckReturnValue public final Collection<ServiceContext> getServiceContexts()
@Nonnull @CheckReturnValue public final String getServiceName()
@Nonnull @CheckReturnValue public final Optional<String> getServiceName(String key)
key
- Either an alias or the name itself.@Nonnull @CheckReturnValue public final ConfigProperties getServiceProperties()
@Nonnull @CheckReturnValue public final Optional<UUID> getServiceUUID()
@Nonnull @CheckReturnValue public final Optional<DateTime> getStamp()
@Nonnull @CheckReturnValue public final Optional<String> getStringValue(@Nonnull String key)
key
- The key for the value.@Nonnull @CheckReturnValue public final Optional<String> getStringValue(@Nonnull String key, @Nonnull Optional<String> defaultValue)
key
- The key for the value.defaultValue
- The default value.@Nonnull @CheckReturnValue public final String[] getStringValues(@Nonnull String key)
key
- The key for the values.@Nonnull @CheckReturnValue public URL getURL()
@CheckReturnValue public final boolean hasService()
@CheckReturnValue public boolean isIncluded(@Nonnull URL url)
A subsequent call with an URL which is equal to the supplied URL will return true. This is used while loading the documents to avoid multiple inclusions of the same document.
url
- The supplied URL.public final void keepEntities(@Nonnull Optional<HashMap<String,Entity>> entities)
Keeps alive the entities, even if they are not referenced.
entities
- The object containing references to all the entities.@CheckReturnValue public boolean registerClassLib(@Nonnull String classLibName)
classLibName
- The name of the ClassLib.public final void registerClassLoader()
public final void restoreState(@Nonnull Object state)
Used to cancel state modifications caused by the repeated loading of child documents (metadata).
state
- The saved state.@Nonnull public final Optional<HashMap<String,Entity>> returnEntities()
@Nonnull @CheckReturnValue public final Object saveState()
public final void setClassLoader(@Nonnull Optional<ServiceClassLoader> classLoader)
When the service class loader is not supplied, an instance is requested from its class.
classLoader
- The service class loader (may be empty).public final void setConfigDir(@Nonnull String dir)
dir
- The new value for the 'config.dir' property.public final void setRootName(@Nonnull String rootName)
rootName
- The root element name.public final void setService(@Nonnull Service service)
service
- The service.public final void setServiceProperties(@Nonnull ConfigProperties properties)
properties
- The service properties.public void setURL(@Nonnull URL url)
url
- The URL.public final void setUpService()
Establishes the set of properties that are specific to the current service.
@Nonnull @CheckReturnValue public final Params substitute(@Nonnull Params params)
params
- The original parameters.@Nonnull @CheckReturnValue public final String substitute(@Nonnull String text, boolean deferred)
A substitution marker would be a '${x}' property reference.
Note: should be called only while substitutions are enabled.
text
- The text possibly containing substitution markers.deferred
- True if substitution of '$${x}' should be deferred.public void tearDown()
public String toString()
toString
in class Object
public final void updateStamp(@Nonnull Optional<DateTime> stamp)
stamp
- The last modified stamp.@Nonnull @CheckReturnValue protected final Logger getThisLogger()
Copyright © 2003-2019 Serge Brisson. All Rights Reserved.