Class AbstractConnector<O,I,CO,CI>
java.lang.Object
de.iip_ecosphere.platform.connectors.AbstractConnector<O,I,CO,CI>
- Type Parameters:
O- the output type from the underlying machine/platformI- the input type to the underlying machine/platformCO- the output type of the connectorCI- the input type of the connector
- All Implemented Interfaces:
Connector<O,,I, CO, CI> EventHandlingConnector,AbstractModelAccess.NotificationChangedListener
- Direct Known Subclasses:
AbstractChannelConnector,AbstractThreadedConnector
public abstract class AbstractConnector<O,I,CO,CI>
extends Object
implements Connector<O,I,CO,CI>, AbstractModelAccess.NotificationChangedListener
Provides a reusable base of a
Connector implementation using the
ProtocolAdapter. Call initializeModelAccess() on
connect(ConnectorParameter) as soon as the connector is connected.
Handles interactions with ConnectorRegistry.- Author:
- Holger Eichelberger, SSE
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate ProtocolAdapter<O,I, CO, CI>[] private CachingStrategyprivate de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<CO> private DataTimeDifferenceProvider<CO> static final Stringprivate booleanprivate Stringprivate ConnectorParameterprivate TimerTaskprivate AdapterSelector<O, I, CO, CI> private Timer -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractConnector(AdapterSelector<O, I, CO, CI> selector, ProtocolAdapter<O, I, CO, CI>... adapter) Creates an instance and installs the protocol adapter(s).protectedAbstractConnector(ProtocolAdapter<O, I, CO, CI>... adapter) Creates an instance and installs the protocol adapter(s) with a default selector for the first adapter. -
Method Summary
Modifier and TypeMethodDescriptionprotected booleancheckCache(Object data) Checks the cache if configured.protected ProtocolAdapter<O, I, CO, CI> configureAdapter(ProtocolAdapter<O, I, CO, CI> adapter) Configures the adapter if needed.protected voidconfigureModelAccess(ModelAccess access) Configures the model access on all protocol adapters.voidconnect(ConnectorParameter params) Connects the connector to the underlying machine/platform.protected abstract voidconnectImpl(ConnectorParameter params) Implements theconnect(ConnectorParameter)method assuming that everything is ok if no exception have been thrown.protected SSLContextcreateTlsContext(ConnectorParameter params) Helper method to determine a SSL/TLS context.final voidDisconnects the connector from the underlying machine/platform.protected abstract voidCalled bydisconnect().voiddispose()Final cleanup when platform shuts down, e.g., for shared resources.protected voidDoes the default polling onDEFAULT_CHANNEL.voidenableNotifications(boolean enableNotifications) Enables/disables notifications/polling at all.voidenablePolling(boolean enablePolling) Enable/disable polling (does not influence the polling timer).protected abstract voidLogs an error.protected CachingStrategyReturns the actual caching strategy.Class<? extends CachingStrategy> Returns the actual caching strategy class.Returns the input type from the IIP-Ecosphere platform.Returns the output type to the IIP-Ecosphere platform.protected ConnectorParameterReturns the connector parameters after the lastconnect(ConnectorParameter).protected Class<? extends CachingStrategy> Returns the initial caching strategy class.Returns the instance identification forconnector extensions.Returns the input type to the protocol.Returns the output type of the protocol.protected AdapterSelector<O, I, CO, CI> Returns the adapter selector.getStorageValue(String key) Returns intermediate information that may be useful while transforming the results by configured transformation functions.protected voidCalled to initialize the model access, e.g., to setup notifications.protected voidinitSelector(AdapterSelector<O, I, CO, CI> selector) CallsAdapterSelector.init(de.iip_ecosphere.platform.connectors.AdapterSelector.AdapterProvider)with a provider instance pointing to the adapters in this instance.protected voidInstall poll task.protected booleanReturns whether we are polling or waiting for events.voidnotificationsChanged(boolean useNotifications) Called when the notifications setting has been changed inAbstractModelAccess.useNotifications(boolean).protected voidnotifyDataTimeDifference(int difference) Notifies this connector about a determined/changed time difference between the actual data received and the next data to be received.voidnotifyReconfigured(String parameterName, String value) Called when parameters of the containing service are changed.protected abstract Oread()Reads data from the underlying machine.protected COCall this if data was received.protected COCall this if data was received.request(boolean notifyCallback) Explicitly requests reading data from the source.protected COExplicitly requests reading data from the source.voidsetDataTimeDifference(int difference) Adjusts the simulated data time difference.voidDefines the time difference provider.voidsetInstanceIdentification(String identification) Sets an instance identification forconnector extensions.voidsetReceptionCallback(de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<CO> callback) Attaches a receptioncallbackto this connector.voidsetStorageValue(String key, Object value) Sets intermediate information that can be read out by while transforming results by configured transformation functions.voidtrigger()Trigger the ingestion of a next data item.voidtrigger(ConnectorTriggerQuery query) Trigger the ingestion of a next data item.protected voidUninstall poll task.static booleanuseTls(ConnectorParameter params) Returns whether the connector shall use TLS.voidWrites the givendatato the underlying machine/platform.protected abstract voidDoes the actual writing to the underlying machine/platform.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.connectors.Connector
createSharedSpace, disconnectSafe, enabledEncryption, enableSharedSpace, setReceptionCallbackSafe, supportedEncryptionMethods inherited from interface de.iip_ecosphere.platform.connectors.events.EventHandlingConnector
getName
-
Field Details
-
DEFAULT_CHANNEL
- See Also:
-
adapter
-
selector
-
callback
-
timer
-
pollTask
-
params
-
enablePolling
private boolean enablePolling -
cachingStrategy
-
storage
-
dataTimeDifferenceProvider
-
instanceIdentification
-
-
Constructor Details
-
AbstractConnector
Creates an instance and installs the protocol adapter(s) with a default selector for the first adapter. For integration compatibility, connector constructors are supposed to accept a var-arg parameter for adapters.- Parameters:
adapter- the protocol adapter(s)- Throws:
IllegalArgumentException- ifadapteris null or empty or adapters are null
-
AbstractConnector
@SafeVarargs protected AbstractConnector(AdapterSelector<O, I, CO, CI> selector, ProtocolAdapter<O, I, CO, CI>... adapter) Creates an instance and installs the protocol adapter(s). For integration compatibility, connector constructors are supposed to accept a var-arg parameter for adapters.- Parameters:
selector- the adapter selector (null leads to a default selector for the first adapter)adapter- the protocol adapter(s)- Throws:
IllegalArgumentException- ifadapteris null or empty or adapters are null
-
-
Method Details
-
initSelector
CallsAdapterSelector.init(de.iip_ecosphere.platform.connectors.AdapterSelector.AdapterProvider)with a provider instance pointing to the adapters in this instance.- Parameters:
selector- the selector to initialize
-
useTls
Returns whether the connector shall use TLS.- Parameters:
params- the transport parameters- Returns:
truefor TLS enabled,falseelse
-
createTlsContext
Helper method to determine a SSL/TLS context. Apply only ifuseTls(ConnectorParameter)returnstrue. Relies onIdentityStore#createTlsContext(String, String, String...)ifTransportParameter.getKeystoreKey()is given, else onSslUtils.createTlsContext(java.io.File, String, String).- Parameters:
params- the connector parameters- Returns:
- the TLS context
- Throws:
IOException- if creating the context or obtaining key information fails
-
configureModelAccess
Configures the model access on all protocol adapters.- Parameters:
access- the model access
-
getSelector
Returns the adapter selector.- Returns:
- the selector
-
connect
Connects the connector to the underlying machine/platform. CallsconnectImpl(ConnectorParameter)and if successful (no exception thrown)initializeModelAccess(). CallsConnectorRegistry.registerConnector(Connector). -
connectImpl
Implements theconnect(ConnectorParameter)method assuming that everything is ok if no exception have been thrown.- Parameters:
params- connection parameter- Throws:
IOException- in case that connecting fails
-
getConnectorParameter
Returns the connector parameters after the lastconnect(ConnectorParameter).- Returns:
- the connector params
-
installPollTask
protected void installPollTask()Install poll task. No task will be installed ifConnectorParameter.getNotificationInterval()is less than 1. Call only afterconnect(ConnectorParameter)and beforedisconnect(). -
enablePolling
public void enablePolling(boolean enablePolling) Description copied from interface:EventHandlingConnectorEnable/disable polling (does not influence the polling timer).- Specified by:
enablePollingin interfaceEventHandlingConnector- Parameters:
enablePolling- whether polling shall enabled- See Also:
-
doPolling
protected void doPolling()Does the default polling onDEFAULT_CHANNEL. -
isPolling
protected boolean isPolling()Returns whether we are polling or waiting for events.- Returns:
truefor polling,falsefor events
-
uninstallPollTask
protected void uninstallPollTask()Uninstall poll task. -
disconnect
Disconnects the connector from the underlying machine/platform. CallsdisconnectImpl(),uninstallPollTask()andConnectorRegistry.unregisterConnector(Connector).- Specified by:
disconnectin interfaceConnector<O,I, CO, CI> - Throws:
IOException- in case that connecting fails
-
disconnectImpl
Called bydisconnect().- Throws:
IOException- if problems occur while disconnecting
-
write
Description copied from interface:ConnectorWrites the givendatato the underlying machine/platform. -
writeImpl
Does the actual writing to the underlying machine/platform. Can be left empty ifMachineConnector.hasModel().- Parameters:
data- the data to be send- Throws:
IOException- if sending fails
-
received
Call this if data was received.- Parameters:
channel- the channeldatawas received on, may beDEFAULT_CHANNEL.data- the received data, further processed ifcallbackis not null- Returns:
- returns the translated received data
- Throws:
IOException- if receiving/translation fails
-
received
Call this if data was received.- Parameters:
channel- the channeldatawas received on, may beDEFAULT_CHANNELdata- the received data, further processed ifcallbackis not nullnotifyCallback- whether thecallbackshall be notified- Returns:
- returns the translated received data
- Throws:
IOException- if receiving/translation fails
-
notifyDataTimeDifference
protected void notifyDataTimeDifference(int difference) Notifies this connector about a determined/changed time difference between the actual data received and the next data to be received. Not considered if not overridden!- Parameters:
difference- the time difference in ms
-
configureAdapter
Configures the adapter if needed.- Parameters:
adapter- the adapter- Returns:
adapter
-
getCachingStrategyCls
Description copied from interface:ConnectorReturns the actual caching strategy class. -
getCachingStrategy
Returns the actual caching strategy.- Returns:
- the strategy
-
getInitCachingStrategyCls
Returns the initial caching strategy class.- Returns:
- the caching strategy class, may be null for default
-
checkCache
Checks the cache if configured. Override withtrueif not needed.- Parameters:
data- the data to send- Returns:
truefor sendingdata,falsefor not sendingdata
-
setReceptionCallback
public void setReceptionCallback(de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<CO> callback) throws IOException Description copied from interface:ConnectorAttaches a receptioncallbackto this connector. Thecallbackis called upon a reception.- Specified by:
setReceptionCallbackin interfaceConnector<O,I, CO, CI> - Parameters:
callback- the callback to attach- Throws:
IOException- in case that problems during registering the callback (e.g., during subscription) happens
-
request
Description copied from interface:ConnectorExplicitly requests reading data from the source. This is typically done by polling or events, but, in seldom cases, may be needed manually.- Specified by:
requestin interfaceConnector<O,I, CO, CI> - Parameters:
notifyCallback- whetherthe reception callbackshall be informed about new data- Returns:
- the data from the machine, null for none, i.e., also no call to
the reception callback - Throws:
IOException- in case that reading fails
-
request
Explicitly requests reading data from the source. This is typically done by polling or events, but, in seldom cases, may be needed manually.- Parameters:
channel- the channel to assign the received data to, may beDEFAULT_CHANNEL.notifyCallback- whetherthe reception callbackshall be informed about new data- Returns:
- the data from the machine, null for none, i.e., also no call to
the reception callback - Throws:
IOException- in case that reading fails
-
trigger
public void trigger()Description copied from interface:EventHandlingConnectorTrigger the ingestion of a next data item.- Specified by:
triggerin interfaceEventHandlingConnector
-
trigger
Description copied from interface:EventHandlingConnectorTrigger the ingestion of a next data item.- Specified by:
triggerin interfaceEventHandlingConnector- Parameters:
query- specification what to ingest; capabilities depend on connector
-
read
Reads data from the underlying machine. Used for polling, but shall then be implemented by returning at least a dummy object so that thepolling taskcan initiate a translation request and forward it to thecallback. In particular, can be a dummy object or the actual changes in the model ifMachineConnector.hasModel().- Returns:
- the data from the machine, null for none, i.e., also no call
to
callback - Throws:
IOException- in case that reading fails
-
error
Logs an error.- Parameters:
message- the message to logth- information about the error
-
initializeModelAccess
Called to initialize the model access, e.g., to setup notifications. Shall be called only, when the connector is connected.- Throws:
IOException- in case the initialization fails, e.g., monitors cannot be set up
-
enableNotifications
public void enableNotifications(boolean enableNotifications) Description copied from interface:ConnectorEnables/disables notifications/polling at all. -
notificationsChanged
public void notificationsChanged(boolean useNotifications) Called when the notifications setting has been changed inAbstractModelAccess.useNotifications(boolean).- Specified by:
notificationsChangedin interfaceAbstractModelAccess.NotificationChangedListener- Parameters:
useNotifications- the new value after changing
-
getProtocolInputType
Description copied from interface:ConnectorReturns the input type to the protocol. -
getConnectorInputType
Description copied from interface:ConnectorReturns the input type from the IIP-Ecosphere platform. -
getProtocolOutputType
Description copied from interface:ConnectorReturns the output type of the protocol. -
getConnectorOutputType
Description copied from interface:ConnectorReturns the output type to the IIP-Ecosphere platform. -
notifyReconfigured
Description copied from interface:ConnectorCalled when parameters of the containing service are changed. -
setStorageValue
Description copied from interface:EventHandlingConnectorSets intermediate information that can be read out by while transforming results by configured transformation functions.- Specified by:
setStorageValuein interfaceEventHandlingConnector- Parameters:
key- the key of the value to setvalue- the actual value
-
getStorageValue
Description copied from interface:EventHandlingConnectorReturns intermediate information that may be useful while transforming the results by configured transformation functions.- Specified by:
getStorageValuein interfaceEventHandlingConnector- Parameters:
key- the key of the value to set- Returns:
- the value, may be null
-
setDataTimeDifferenceProvider
Description copied from interface:ConnectorDefines the time difference provider. Depends on the connector implementation. -
setDataTimeDifference
public void setDataTimeDifference(int difference) Description copied from interface:EventHandlingConnectorAdjusts the simulated data time difference.- Specified by:
setDataTimeDifferencein interfaceEventHandlingConnector- Parameters:
difference- the difference, negative for configured fixed default, 0 for none (handle with care)
-
setInstanceIdentification
Description copied from interface:ConnectorSets an instance identification forconnector extensions. -
getInstanceIdentification
Description copied from interface:ConnectorReturns the instance identification forconnector extensions.- Specified by:
getInstanceIdentificationin interfaceConnector<O,I, CO, CI> - Returns:
- the identification, set before by
Connector.setInstanceIdentification(String)
-
dispose
public void dispose()Description copied from interface:ConnectorFinal cleanup when platform shuts down, e.g., for shared resources.
-