Class AbstractService
java.lang.Object
de.iip_ecosphere.platform.services.environment.AbstractService
- All Implemented Interfaces:
ParameterConfigurerProvider,Service,ServiceBase
- Direct Known Subclasses:
AbstractRunnablesService,ConnectorServiceWrapper,DefaultServiceImpl,MultiConnectorServiceWrapper,TraceToAasService
Basic implementation of the service interface (aligned with Python). Implementing classes shall at least either
have a no-arg constructor setting up the full/fallback service information, a single string argument constructor
taking the service id or a constructor like
AbstractService(String, InputStream).
The three types of constructors are recognized by createInstance(String, Class, String, String) or
createInstance(ClassLoader, String, Class, String, String) to be used from generated service code.
reconfigure(Map) is generically implemented via Service.getParameterConfigurer(String) (shall be
overwritten), rollbackReconfigurationOnFailure() and
reconfigure(Map, ParameterConfigurerProvider, boolean, ServiceState).- Author:
- Holger Eichelberger, SSE
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classRepresents a type creator with actual type being created. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate Stringprivate Stringprivate booleanprivate booleanprivate ServiceKindprivate static ClassLoaderprivate Stringprivate de.iip_ecosphere.platform.support.net.ManagedServerAddressprivate ServiceStateprivate Map<Class<?>, AbstractService.TypeCreator<?>> private de.iip_ecosphere.platform.support.VersionFields inherited from interface de.iip_ecosphere.platform.services.environment.switching.ServiceBase
APPLICATION_SEPARATOR, DEFAULT_APPLICATION_INSTANCE_ID -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractService(ServiceKind kind) Fallback constructor setting most fields to "empty" default values.protectedAbstractService(YamlService yaml) Creates an abstract service from YAML information.protectedAbstractService(String id, ServiceKind kind) Fallback constructor setting most fields to "empty" default values.protectedAbstractService(String serviceId, InputStream ymlFile) Creates an abstract service from a service id and a YAML artifact.protectedAbstractService(String id, String name, de.iip_ecosphere.platform.support.Version version, String description, boolean isDeployable, boolean isTopLevel, ServiceKind kind) Creates an abstract service. -
Method Summary
Modifier and TypeMethodDescriptionvoidactivate()Activates the service.static <T> voidaddConfigurer(Map<String, ParameterConfigurer<?>> configurers, String name, Class<T> cls, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<String, T> trans, ValueConfigurer<T> cfg) Helper method to add parameter configurers toconfigurers(without getter for rollback).static <T> voidaddConfigurer(Map<String, ParameterConfigurer<?>> configurers, String name, Class<T> cls, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<String, T> trans, ValueConfigurer<T> cfg, Supplier<T> getter) Helper method to add parameter configurers toconfigurerswithout binding to a system property.static <T> voidaddConfigurer(Map<String, ParameterConfigurer<?>> configurers, String name, Class<T> cls, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<String, T> trans, ValueConfigurer<T> cfg, Supplier<T> getter, String systemProperty) Helper method to add parameter configurers toconfigurerswithout binding to a system property.<T> voidaddTypeSubstitution(Class<? super T> cls, Class<T> actCls, Supplier<T> creator) Sets a type mapping, usually a substitution of more generic to more specific output types.static <S extends Service,I, O>
ScreateGenericInstance(ClassLoader loader, String className, Class<S> cls, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<I, String> inTrans, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<String, O> outTrans, de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<O> callback, YamlService yaml, Object... args) Creates a service instance from a plugin via theAbstractDelegatingMultiService(YamlService)or the extended constructor with second parameterString... args.static <S extends Service,I, O>
ScreateGenericInstanceByPlugin(String pluginId, boolean log, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<I, String> inTrans, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<String, O> outTrans, de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<O> callback, YamlService yaml, Object... args) Creates a generic service instance from a plugin viaServiceDescriptor.createService(YamlService, Object...).static <S extends Service>
ScreateGenericMultiInstance(ClassLoader loader, String className, Class<S> cls, YamlService yaml, Object... args) Creates a service instance from a plugin via theAbstractDelegatingMultiService(YamlService)or the extended constructor with second parameterString... args.static <S extends Service>
ScreateGenericMultiInstanceByPlugin(String pluginId, boolean log, YamlService yaml, Object... args) Creates a generic service instance from a plugin viaServiceDescriptor.createService(YamlService, Object...).static <S extends Service>
ScreateInstance(ClassLoader loader, String className, Class<S> cls, String serviceId, String deploymentDescFile) Convenience method for creating service instances.static <S extends Service>
ScreateInstance(String className, Class<S> cls) Convenience method for creating service instances via the default constructor using the class loader of this class.static <S extends Service>
ScreateInstance(String className, Class<S> cls, String serviceId, String deploymentDescFile) Convenience method for creating service instances using the class loader of this class.static <S extends Service>
ScreateInstanceByPlugin(String pluginId, boolean log, String serviceId, String deploymentDescFile) Creates a service instance from a plugin.static ObjectReturns the i-th argument.The description of the service.getId()Returns the unique id of the service.getKind()Returns the service kind.getName()The name of the service.de.iip_ecosphere.platform.support.ServerAddressReturns the resolved network address of the netMgtKey specified in the service descriptor.static InputStreamgetResourceAsStream(ClassLoader loader, String resource) Loads a resource as stream fromloader, first with given name, as fallback usingresourcewithout leading slashes (may be present after ZIP unpacking), else as file (may be present after ZIP unpacking).getState()Returns the state the service is currently in.static StringgetStringArg(int index, Object[] args, String dflt) Returns the i-th argument as String.<T> Class<? extends T> getSubstitutedType(Class<T> cls) Returns the actual (output) type, may be a substitute.<T> Supplier<T> getTypeCreator(Class<T> cls, Supplier<T> creator) Returns the creator for a certain (output) type, may be a substitute.Returns the actual (output) type substitutions.de.iip_ecosphere.platform.support.VersionThe version of the service.protected voidinitializeFrom(YamlService yaml) Does further initialization of this instance from the given YAML information.booleanReturns whether the service is deployable in distributable manner or fixed in deployment location.booleanReturns whether the service is a top-level service.protected voidnotifyReconfigured(String paramName, String value) Called to notify values have been reconfigured.voidPassivates the service.protected static <T> voidreconf(ParameterConfigurer<T> configurer, String name, String value, Map<String, String> rollbackMap) Reconfigures an individual parameter.voidreconfigure(Map<String, String> values) Reconfigures the service.static voidreconfigure(Map<String, String> values, ParameterConfigurerProvider provider, boolean rollback, ServiceState state) Generic service reconfiguration via values that may be passed in throughreconfigure(Map).protected booleanReturns whether the configuration shall be rolled by on failures.static voidsetLibJars(URL[] jars) Sets shared jar libraries for the services.voidsetState(ServiceState state) Changes the state.protected ServiceStatestart()Starts the service and the background process.protected ServiceStatestop()Stops the service and the background process.protected voidtransferData(Object source, Object target) Transfers all fields from the source object to the target object.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface de.iip_ecosphere.platform.services.environment.Service
createType, createType, getParameterConfigurer, getParameterNames, migrate, switchTo, update
-
Field Details
-
loader
-
id
-
name
-
version
private de.iip_ecosphere.platform.support.Version version -
description
-
isDeployable
private boolean isDeployable -
isTopLevel
private boolean isTopLevel -
kind
-
state
-
netKeyMgtAddress
private de.iip_ecosphere.platform.support.net.ManagedServerAddress netKeyMgtAddress -
typeCreators
-
-
Constructor Details
-
AbstractService
Fallback constructor setting most fields to "empty" default values.- Parameters:
kind- the service kind
-
AbstractService
Fallback constructor setting most fields to "empty" default values.- Parameters:
id- the id of the servicekind- the service kind
-
AbstractService
protected AbstractService(String id, String name, de.iip_ecosphere.platform.support.Version version, String description, boolean isDeployable, boolean isTopLevel, ServiceKind kind) Creates an abstract service.- Parameters:
id- the id of the servicename- the name of the serviceversion- the version of the servicedescription- a description of the service, may be emptyisDeployable- whether the service is decentrally deployableisTopLevel- whether the service is a top-level (non-nested) servicekind- the service kind
-
AbstractService
Creates an abstract service from YAML information.- Parameters:
yaml- the service information as read from YAML- See Also:
-
AbstractService
Creates an abstract service from a service id and a YAML artifact.- Parameters:
serviceId- the service idymlFile- the YML file containing the YAML artifact with the service descriptor
-
-
Method Details
-
initializeFrom
Does further initialization of this instance from the given YAML information.- Parameters:
yaml- the service information as read from YAML
-
setLibJars
Sets shared jar libraries for the services.- Parameters:
jars- the libraries, ignored if null
-
createInstance
Convenience method for creating service instances via the default constructor using the class loader of this class.- Type Parameters:
S- the service type (parent interface ofclassName)- Parameters:
className- the name of the service class (must implementServiceand provide a no-argument constructor)cls- the class to cast to- Returns:
- the service instance (null if the service cannot be found/initialized)
-
createInstance
public static <S extends Service> S createInstance(String className, Class<S> cls, String serviceId, String deploymentDescFile) Convenience method for creating service instances using the class loader of this class.- Type Parameters:
S- the service type (parent interface ofclassName)- Parameters:
className- the name of the service class (must implementServiceand provide a no-argument constructor)cls- the class to cast toserviceId- the id of the service as given indeploymentDescFile(may be null, then the default constructor is invoked)deploymentDescFile- the resource name of the deployment descriptor containing a YAML artifact with the service description (may be null, then the default constructor is invoked)- Returns:
- the service instance (null if the service cannot be found/initialized)
-
getResourceAsStream
Loads a resource as stream fromloader, first with given name, as fallback usingresourcewithout leading slashes (may be present after ZIP unpacking), else as file (may be present after ZIP unpacking).- Parameters:
loader- the class loader to load the class withresource- the resource name/path- Returns:
- the input stream if the resource was found, null else
-
createInstance
public static <S extends Service> S createInstance(ClassLoader loader, String className, Class<S> cls, String serviceId, String deploymentDescFile) Convenience method for creating service instances.- Type Parameters:
S- the service type (parent interface ofclassName)- Parameters:
loader- the class loader to load the class withclassName- the name of the service class (must implementServiceand provide a no-argument constructor)cls- the class to cast toserviceId- the id of the service as given indeploymentDescFile(may be null, then the default constructor is invoked)deploymentDescFile- the resource name of the deployment descriptor containing a YAML artifact with the service description (may be null, then the default constructor is invoked)- Returns:
- the service instance (null if the service cannot be found/initialized)
-
createInstanceByPlugin
public static <S extends Service> S createInstanceByPlugin(String pluginId, boolean log, String serviceId, String deploymentDescFile) Creates a service instance from a plugin.- Type Parameters:
S- the service type- Parameters:
pluginId- the plugin idlog- emit log information when the plugin is not registeredserviceId- the id of the service as given indeploymentDescFile(may be null, then the default constructor is invoked)deploymentDescFile- the resource name of the deployment descriptor containing a YAML artifact with the service description (may be null, then the default constructor is invoked)- Returns:
- a new service instance or null if the service cannot be created/the plugin is not registered
-
getArg
Returns the i-th argument.- Parameters:
index- the index of the argument to returnargs- the argumentsdflt- the default value if accessing the argument fails- Returns:
- the argument or
dflt
-
getStringArg
Returns the i-th argument as String.- Parameters:
index- the index of the argument to returnargs- the argumentsdflt- the default value if accessing the argument fails- Returns:
- the argument as String or
dflt
-
createGenericMultiInstance
public static <S extends Service> S createGenericMultiInstance(ClassLoader loader, String className, Class<S> cls, YamlService yaml, Object... args) Creates a service instance from a plugin via theAbstractDelegatingMultiService(YamlService)or the extended constructor with second parameterString... args.- Type Parameters:
S- the service type- Parameters:
loader- the class loader to load the class withclassName- the name of the service class (must implementServiceand provide a no-argument constructor)cls- the class to cast toyaml- the service descriptionargs- the optional service creation arguments- Returns:
- a new service instance or null if the service cannot be created
-
createGenericMultiInstanceByPlugin
public static <S extends Service> S createGenericMultiInstanceByPlugin(String pluginId, boolean log, YamlService yaml, Object... args) Creates a generic service instance from a plugin viaServiceDescriptor.createService(YamlService, Object...).- Type Parameters:
S- the service type- Parameters:
pluginId- the plugin idlog- emit log information when the plugin is not registeredyaml- the service descriptionargs- the optional service creation arguments- Returns:
- a new service instance or null if the service cannot be created/the plugin is not registered
-
createGenericInstance
public static <S extends Service,I, S createGenericInstanceO> (ClassLoader loader, String className, Class<S> cls, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<I, String> inTrans, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<String, O> outTrans, de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<O> callback, YamlService yaml, Object... args) Creates a service instance from a plugin via theAbstractDelegatingMultiService(YamlService)or the extended constructor with second parameterString... args.- Type Parameters:
S- the service typeI- input typeO- output type- Parameters:
loader- the class loader to load the class withclassName- the name of the service class (must implementServiceand provide a no-argument constructor)cls- the class to cast toinTrans- the input translatoroutTrans- the output translatorcallback- called when a processed item is received from the serviceyaml- the service descriptionargs- the optional service creation arguments- Returns:
- a new service instance or null if the service cannot be created
-
createGenericInstanceByPlugin
public static <S extends Service,I, S createGenericInstanceByPluginO> (String pluginId, boolean log, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<I, String> inTrans, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<String, O> outTrans, de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<O> callback, YamlService yaml, Object... args) Creates a generic service instance from a plugin viaServiceDescriptor.createService(YamlService, Object...).- Type Parameters:
S- the service typeI- input typeO- output type- Parameters:
pluginId- the plugin idlog- emit log information when the plugin is not registeredinTrans- the input translatoroutTrans- the output translatorcallback- called when a processed item is received from the serviceyaml- the service descriptionargs- the optional service creation arguments- Returns:
- a new service instance or null if the service cannot be created/the plugin is not registered
-
getId
Description copied from interface:ServiceBaseReturns the unique id of the service. May be a single service id, a service id and a postfixed application id (afterServiceBase.APPLICATION_SEPARATOR) or a service id with application id and postfixed application instance id (afterServiceBase.APPLICATION_SEPARATOR)- Specified by:
getIdin interfaceServiceBase- Returns:
- the unique id
-
getName
Description copied from interface:ServiceThe name of the service. -
getVersion
public de.iip_ecosphere.platform.support.Version getVersion()Description copied from interface:ServiceThe version of the service.- Specified by:
getVersionin interfaceService- Returns:
- the version
-
getDescription
Description copied from interface:ServiceThe description of the service.- Specified by:
getDescriptionin interfaceService- Returns:
- the description, may be empty
-
getState
Description copied from interface:ServiceBaseReturns the state the service is currently in. [R4c]- Specified by:
getStatein interfaceServiceBase- Returns:
- the state
-
setState
Changes the state. [R133c]- Specified by:
setStatein interfaceServiceBase- Parameters:
state- the new state- Throws:
ExecutionException- if changing the state fails for some reason- See Also:
-
start
Starts the service and the background process.- Returns:
- the state to transition to, may be null for none
- Throws:
ExecutionException- if starting the process fails
-
stop
Stops the service and the background process.- Returns:
- the state to transition to, may be null for none
-
isDeployable
public boolean isDeployable()Description copied from interface:ServiceReturns whether the service is deployable in distributable manner or fixed in deployment location.- Specified by:
isDeployablein interfaceService- Returns:
truefor deployable,falsefor fixed
-
isTopLevel
public boolean isTopLevel()Description copied from interface:ServiceReturns whether the service is a top-level service.- Specified by:
isTopLevelin interfaceService- Returns:
truefor top-level,falsefor nested
-
getKind
Description copied from interface:ServiceReturns the service kind. -
getNetMgtKeyAddress
public de.iip_ecosphere.platform.support.ServerAddress getNetMgtKeyAddress()Description copied from interface:ServiceReturns the resolved network address of the netMgtKey specified in the service descriptor.- Specified by:
getNetMgtKeyAddressin interfaceService- Returns:
- the resolved address, may be null in particular if no key was specified in the service descriptor
-
activate
Description copied from interface:ServiceActivates the service. [adaptation]- Specified by:
activatein interfaceService- Throws:
ExecutionException- in case that activating fails, e.g., because the service is already active
-
passivate
Description copied from interface:ServicePassivates the service. [adaptation]- Specified by:
passivatein interfaceService- Throws:
ExecutionException- in case that passivating fails, e.g., because the service is already passive
-
addConfigurer
public static <T> void addConfigurer(Map<String, ParameterConfigurer<?>> configurers, String name, Class<T> cls, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<String, T> trans, ValueConfigurer<T> cfg) Helper method to add parameter configurers toconfigurers(without getter for rollback).- Type Parameters:
T- the parameter type- Parameters:
configurers- the configurers map to be modifiedname- the name of the parametercls- the class representing the parameter typetrans- the type translator to turn initial string values into internal valuescfg- the value configurer that may change the value / throw exceptions
-
addConfigurer
public static <T> void addConfigurer(Map<String, ParameterConfigurer<?>> configurers, String name, Class<T> cls, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<String, T> trans, ValueConfigurer<T> cfg, Supplier<T> getter) Helper method to add parameter configurers toconfigurerswithout binding to a system property.- Type Parameters:
T- the parameter type- Parameters:
configurers- the configurers map to be modifiedname- the name of the parametercls- the class representing the parameter typetrans- the type translator to turn initial string values into internal valuescfg- the value configurer that may change the value / throw exceptionsgetter- the getter for the value (may be null for none, prevents rollback)
-
addConfigurer
public static <T> void addConfigurer(Map<String, ParameterConfigurer<?>> configurers, String name, Class<T> cls, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<String, T> trans, ValueConfigurer<T> cfg, Supplier<T> getter, String systemProperty) Helper method to add parameter configurers toconfigurerswithout binding to a system property.- Type Parameters:
T- the parameter type- Parameters:
configurers- the configurers map to be modifiedname- the name of the parametercls- the class representing the parameter typetrans- the type translator to turn initial string values into internal valuescfg- the value configurer that may change the value / throw exceptionsgetter- the getter for the value (may be null for none, prevents rollback)systemProperty- optional system property to take the higher precedence initial/default value for the parameter from, may be empty or null for none
-
reconfigure
public static void reconfigure(Map<String, String> values, ParameterConfigurerProvider provider, boolean rollback, ServiceState state) throws ExecutionExceptionGeneric service reconfiguration via values that may be passed in throughreconfigure(Map). Prepared that code generation can hook in any parameter attributes.- Parameters:
values- the values to reconfigure in the encoding requested byreconfigure(Map). The specified configurers must provide appropriate serializers to turn individual values into compatible objectsprovider- access to the parameter configurers (may be null, then nothing happens)rollback- whether a rollback shall be performed if reconfiguration leads to exceptions on individual parametersstate- the state of the service. Currently unused, but may be used to filter out parameter reconfigurations- Throws:
ExecutionException- if the reconfiguration cannot be carried out; ifrollbackistrueand getters for individual parameters are available in theParameterConfigurer, a rollback of the values set before will be carried out
-
reconf
protected static <T> void reconf(ParameterConfigurer<T> configurer, String name, String value, Map<String, String> rollbackMap) throws ExecutionExceptionReconfigures an individual parameter.- Type Parameters:
T- the parameter type- Parameters:
configurer- the configurer to usename- the name of the parameter to reconfigurevalue- the value, may be nullrollbackMap- the rollback- Throws:
ExecutionException- if the re-configuration fails
-
reconfigure
Description copied from interface:ServiceReconfigures the service. [adaptation]- Specified by:
reconfigurein interfaceService- Parameters:
values- the (service-specific) name-value mapping that shall lead to a reconfiguration of the service; values come either as primitive values or as JSON structures complying with the parameter descriptor. The service is responsible for correct JSON de-serialization according to the respective descriptor.- Throws:
ExecutionException- if reconfiguration fails
-
notifyReconfigured
Called to notify values have been reconfigured.- Parameters:
paramName- the name of the parametervalue- the serialized value (JSON)
-
rollbackReconfigurationOnFailure
protected boolean rollbackReconfigurationOnFailure()Returns whether the configuration shall be rolled by on failures.- Returns:
true- See Also:
-
transferData
Transfers all fields from the source object to the target object. The caller is responsible that the objects are sufficiently compatible.- Parameters:
source- the source object (may be null)target- the target object (may be null)
-
addTypeSubstitution
Description copied from interface:ServiceSets a type mapping, usually a substitution of more generic to more specific output types.- Specified by:
addTypeSubstitutionin interfaceService- Type Parameters:
T- the actual type- Parameters:
cls- the original class to apply the mapping foractCls- the actual class that the supplier will create an instance ofcreator- the creator supplier that creates an instance of the substitute
-
getSubstitutedType
Description copied from interface:ServiceReturns the actual (output) type, may be a substitute.- Specified by:
getSubstitutedTypein interfaceService- Type Parameters:
T- the actual type- Parameters:
cls- the original class to return the creator for- Returns:
- the type, may be
cls - See Also:
-
getTypeCreator
Description copied from interface:ServiceReturns the creator for a certain (output) type, may be a substitute.- Specified by:
getTypeCreatorin interfaceService- Type Parameters:
T- the actual type- Parameters:
cls- the original class to return the creator forcreator- the default creator supplier if none is registered- Returns:
- a constructor supplier or
creatorif there is none - See Also:
-
getTypeSubstitutions
Description copied from interface:ServiceReturns the actual (output) type substitutions.- Specified by:
getTypeSubstitutionsin interfaceService- Returns:
- the type substitutions
- See Also:
-