Class AbstractService
- java.lang.Object
-
- de.iip_ecosphere.platform.services.environment.AbstractService
-
- All Implemented Interfaces:
ParameterConfigurerProvider,Service,ServiceBase
- Direct Known Subclasses:
AbstractRunnablesService,ConnectorServiceWrapper,DefaultServiceImpl,TraceToAasService
public abstract class AbstractService extends java.lang.Object implements Service
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 signle string argument constructor taking the service id or a constructor likeAbstractService(String, InputStream). The three types of constructors are recognized bycreateInstance(String, Class, String, String)orcreateInstance(ClassLoader, String, Class, String, String)to be used from generated service code.reconfigure(Map)is generically implemented viaService.getParameterConfigurer(String)(shall be overwritten),rollbackReconfigurationOnFailure()andreconfigure(Map, ParameterConfigurerProvider, boolean, ServiceState).- Author:
- Holger Eichelberger, SSE
-
-
Field Summary
Fields Modifier and Type Field Description private java.lang.Stringdescriptionprivate java.lang.Stringidprivate booleanisDeployableprivate booleanisTopLevelprivate ServiceKindkindprivate static java.lang.ClassLoaderloaderprivate java.lang.Stringnameprivate de.iip_ecosphere.platform.support.net.ManagedServerAddressnetKeyMgtAddressprivate ServiceStatestateprivate de.iip_ecosphere.platform.support.iip_aas.Versionversion-
Fields inherited from interface de.iip_ecosphere.platform.services.environment.switching.ServiceBase
APPLICATION_SEPARATOR, DEFAULT_APPLICATION_INSTANCE_ID
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractService(ServiceKind kind)Fallback constructor setting most fields to "empty" default values.protectedAbstractService(YamlService yaml)Creates an abstract service from YAML information.protectedAbstractService(java.lang.String id, ServiceKind kind)Fallback constructor setting most fields to "empty" default values.protectedAbstractService(java.lang.String serviceId, java.io.InputStream ymlFile)Creates an abstract service from a service id and a YAML artifact.protectedAbstractService(java.lang.String id, java.lang.String name, de.iip_ecosphere.platform.support.iip_aas.Version version, java.lang.String description, boolean isDeployable, boolean isTopLevel, ServiceKind kind)Creates an abstract service.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidactivate()Activates the service.static <T> voidaddConfigurer(java.util.Map<java.lang.String,ParameterConfigurer<?>> configurers, java.lang.String name, java.lang.Class<T> cls, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<java.lang.String,T> trans, ValueConfigurer<T> cfg)Helper method to add parameter configurers toconfigurers(without getter for rollback).static <T> voidaddConfigurer(java.util.Map<java.lang.String,ParameterConfigurer<?>> configurers, java.lang.String name, java.lang.Class<T> cls, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<java.lang.String,T> trans, ValueConfigurer<T> cfg, java.util.function.Supplier<T> getter)Helper method to add parameter configurers toconfigurerswithout binding to a system property.static <T> voidaddConfigurer(java.util.Map<java.lang.String,ParameterConfigurer<?>> configurers, java.lang.String name, java.lang.Class<T> cls, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<java.lang.String,T> trans, ValueConfigurer<T> cfg, java.util.function.Supplier<T> getter, java.lang.String systemProperty)Helper method to add parameter configurers toconfigurerswithout binding to a system property.static <S extends Service>
ScreateInstance(java.lang.ClassLoader loader, java.lang.String className, java.lang.Class<S> cls, java.lang.String serviceId, java.lang.String deploymentDescFile)Convenience method for creating service instances.static <S extends Service>
ScreateInstance(java.lang.String className, java.lang.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(java.lang.String className, java.lang.Class<S> cls, java.lang.String serviceId, java.lang.String deploymentDescFile)Convenience method for creating service instances using the class loader of this class.java.lang.StringgetDescription()The description of the service.java.lang.StringgetId()Returns the unique id of the service.ServiceKindgetKind()Returns the service kind.java.lang.StringgetName()The name of the service.de.iip_ecosphere.platform.support.ServerAddressgetNetMgtKeyAddress()Returns the resolved network address of the netMgtKey specified in the service descriptor.static java.io.InputStreamgetResourceAsStream(java.lang.ClassLoader loader, java.lang.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).ServiceStategetState()Returns the state the service is currently in.de.iip_ecosphere.platform.support.iip_aas.VersiongetVersion()The version of the service.protected voidinitializeFrom(YamlService yaml)Does further initialization of this instance from the given YAML information.booleanisDeployable()Returns whether the service is deployable in distributable manner or fixed in deployment location.booleanisTopLevel()Returns whether the service is a top-level service.protected voidnotifyReconfigured(java.lang.String paramName, java.lang.String value)Called to notify values have been reconfigured.voidpassivate()Passivates the service.protected static <T> voidreconf(ParameterConfigurer<T> configurer, java.lang.String name, java.lang.String value, java.util.Map<java.lang.String,java.lang.String> rollbackMap)Reconfigures an individual parameter.voidreconfigure(java.util.Map<java.lang.String,java.lang.String> values)Reconfigures the service.static voidreconfigure(java.util.Map<java.lang.String,java.lang.String> values, ParameterConfigurerProvider provider, boolean rollback, ServiceState state)Generic service reconfiguration via values that may be passed in throughreconfigure(Map).protected booleanrollbackReconfigurationOnFailure()Returns whether the configuration shall be rolled by on failures.static voidsetLibJars(java.net.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.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface de.iip_ecosphere.platform.services.environment.Service
getParameterConfigurer, getParameterNames, migrate, switchTo, update
-
-
-
-
Field Detail
-
loader
private static java.lang.ClassLoader loader
-
id
private java.lang.String id
-
name
private java.lang.String name
-
version
private de.iip_ecosphere.platform.support.iip_aas.Version version
-
description
private java.lang.String description
-
isDeployable
private boolean isDeployable
-
isTopLevel
private boolean isTopLevel
-
kind
private ServiceKind kind
-
state
private ServiceState state
-
netKeyMgtAddress
private de.iip_ecosphere.platform.support.net.ManagedServerAddress netKeyMgtAddress
-
-
Constructor Detail
-
AbstractService
protected AbstractService(ServiceKind kind)
Fallback constructor setting most fields to "empty" default values.- Parameters:
kind- the service kind
-
AbstractService
protected AbstractService(java.lang.String id, ServiceKind kind)Fallback constructor setting most fields to "empty" default values.- Parameters:
id- the id of the servicekind- the service kind
-
AbstractService
protected AbstractService(java.lang.String id, java.lang.String name, de.iip_ecosphere.platform.support.iip_aas.Version version, java.lang.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
protected AbstractService(YamlService yaml)
Creates an abstract service from YAML information.- Parameters:
yaml- the service information as read from YAML- See Also:
initializeFrom(YamlService)
-
AbstractService
protected AbstractService(java.lang.String serviceId, java.io.InputStream ymlFile)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 Detail
-
initializeFrom
protected void initializeFrom(YamlService yaml)
Does further initialization of this instance from the given YAML information.- Parameters:
yaml- the service information as read from YAML
-
setLibJars
public static void setLibJars(java.net.URL[] jars)
Sets shared jar libraries for the services.- Parameters:
jars- the libraries, ignored if null
-
createInstance
public static <S extends Service> S createInstance(java.lang.String className, java.lang.Class<S> cls)
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(java.lang.String className, java.lang.Class<S> cls, java.lang.String serviceId, java.lang.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
public static java.io.InputStream getResourceAsStream(java.lang.ClassLoader loader, java.lang.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).- 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(java.lang.ClassLoader loader, java.lang.String className, java.lang.Class<S> cls, java.lang.String serviceId, java.lang.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)
-
getId
public java.lang.String 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
public java.lang.String getName()
Description copied from interface:ServiceThe name of the service.
-
getVersion
public de.iip_ecosphere.platform.support.iip_aas.Version getVersion()
Description copied from interface:ServiceThe version of the service.- Specified by:
getVersionin interfaceService- Returns:
- the version
-
getDescription
public java.lang.String getDescription()
Description copied from interface:ServiceThe description of the service.- Specified by:
getDescriptionin interfaceService- Returns:
- the description, may be empty
-
getState
public ServiceState getState()
Description copied from interface:ServiceBaseReturns the state the service is currently in. [R4c]- Specified by:
getStatein interfaceServiceBase- Returns:
- the state
-
setState
public void setState(ServiceState state) throws java.util.concurrent.ExecutionException
Changes the state. [R133c]- Specified by:
setStatein interfaceServiceBase- Parameters:
state- the new state- Throws:
java.util.concurrent.ExecutionException- if changing the state fails for some reason- See Also:
start(),stop()
-
start
protected ServiceState start() throws java.util.concurrent.ExecutionException
Starts the service and the background process.- Returns:
- the state to transition to, may be null for none
- Throws:
java.util.concurrent.ExecutionException- if starting the process fails
-
stop
protected ServiceState 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
public ServiceKind 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
public void activate() throws java.util.concurrent.ExecutionExceptionDescription copied from interface:ServiceActivates the service. [adaptation]
-
passivate
public void passivate() throws java.util.concurrent.ExecutionExceptionDescription copied from interface:ServicePassivates the service. [adaptation]
-
addConfigurer
public static <T> void addConfigurer(java.util.Map<java.lang.String,ParameterConfigurer<?>> configurers, java.lang.String name, java.lang.Class<T> cls, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<java.lang.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(java.util.Map<java.lang.String,ParameterConfigurer<?>> configurers, java.lang.String name, java.lang.Class<T> cls, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<java.lang.String,T> trans, ValueConfigurer<T> cfg, java.util.function.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(java.util.Map<java.lang.String,ParameterConfigurer<?>> configurers, java.lang.String name, java.lang.Class<T> cls, de.iip_ecosphere.platform.transport.serialization.TypeTranslator<java.lang.String,T> trans, ValueConfigurer<T> cfg, java.util.function.Supplier<T> getter, java.lang.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(java.util.Map<java.lang.String,java.lang.String> values, ParameterConfigurerProvider provider, boolean rollback, ServiceState state) throws java.util.concurrent.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:
java.util.concurrent.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, java.lang.String name, java.lang.String value, java.util.Map<java.lang.String,java.lang.String> rollbackMap) throws java.util.concurrent.ExecutionException
Reconfigures 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:
java.util.concurrent.ExecutionException- if the re-configuration fails
-
reconfigure
public void reconfigure(java.util.Map<java.lang.String,java.lang.String> values) throws java.util.concurrent.ExecutionExceptionDescription 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:
java.util.concurrent.ExecutionException- if reconfiguration fails
-
notifyReconfigured
protected void notifyReconfigured(java.lang.String paramName, java.lang.String value)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:
reconfigure(Map),reconfigure(Map, ParameterConfigurerProvider, boolean, ServiceState)
-
-