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
public abstract class AbstractConnector<O,I,CO,CI> extends java.lang.Object implements Connector<O,I,CO,CI>, AbstractModelAccess.NotificationChangedListener
Provides a reusable base of aConnectorimplementation using theProtocolAdapter. CallinitializeModelAccess()onconnect(ConnectorParameter)as soon as the connector is connected. Handles interactions withConnectorRegistry.- Author:
- Holger Eichelberger, SSE
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected classAbstractConnector.BasicAdapterProvider
-
Field Summary
Fields Modifier and Type Field Description private ProtocolAdapter<O,I,CO,CI>[]adapterprivate CachingStrategycachingStrategyprivate de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<CO>callbackstatic java.lang.StringDEFAULT_CHANNELprivate booleanenablePollingprivate ConnectorParameterparamsprivate java.util.TimerTaskpollTaskprivate AdapterSelector<O,I,CO,CI>selectorprivate java.util.Timertimer
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractConnector(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
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected booleancheckCache(java.lang.Object data)Checks the cache if configured.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 javax.net.ssl.SSLContextcreateTlsContext(ConnectorParameter params)Helper method to determine a SSL/TLS context.voiddisconnect()Disconnects the connector from the underlying machine/platform.protected abstract voiddisconnectImpl()Called bydisconnect().protected voiddoPolling()Does 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 voiderror(java.lang.String message, java.lang.Throwable th)Logs an error.protected CachingStrategygetCachingStrategy()Returns the actual caching strategy.java.lang.Class<? extends CachingStrategy>getCachingStrategyCls()Returns the actual caching strategy class.java.lang.Class<? extends CI>getConnectorInputType()Returns the input type from the IIP-Ecosphere platform.java.lang.Class<? extends CO>getConnectorOutputType()Returns the output type to the IIP-Ecosphere platform.protected ConnectorParametergetConnectorParameter()Returns the connector parameters after the lastconnect(ConnectorParameter).protected java.lang.Class<? extends CachingStrategy>getInitCachingStrategyCls()Returns the initial caching strategy class.java.lang.Class<? extends I>getProtocolInputType()Returns the input type to the protocol.java.lang.Class<? extends O>getProtocolOutputType()Returns the output type of the protocol.protected AdapterSelector<O,I,CO,CI>getSelector()Returns the adapter selector.protected voidinitializeModelAccess()Called 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 voidinstallPollTask()Install poll task.protected booleanisPolling()Returns whether we are polling or waiting for events.voidnotificationsChanged(boolean useNotifications)Called when the notifications setting has been changed inAbstractModelAccess.useNotifications(boolean).voidnotifyReconfigured(java.lang.String parameterName, java.lang.String value)Called when parameters of the containing service are changed.protected abstract Oread()Reads data from the underlying machine.protected COreceived(java.lang.String channel, O data)Call this if data was received.protected COreceived(java.lang.String channel, O data, boolean notifyCallback)Call this if data was received.COrequest(boolean notifyCallback)Explicitly requests reading data from the source.protected COrequest(java.lang.String channel, boolean notifyCallback)Explicitly requests reading data from the source.voidsetReceptionCallback(de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<CO> callback)Attaches a receptioncallbackto this connector.voidtrigger()Trigger the ingestion of a next data item.voidtrigger(ConnectorTriggerQuery query)Trigger the ingestion of a next data item.protected voiduninstallPollTask()Uninstall poll task.static booleanuseTls(ConnectorParameter params)Returns whether the connector shall use TLS.voidwrite(CI data)Writes the givendatato the underlying machine/platform.protected abstract voidwriteImpl(I data)Does 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, wait
-
Methods inherited from interface de.iip_ecosphere.platform.connectors.Connector
dispose, enabledEncryption, supportedEncryption
-
Methods inherited from interface de.iip_ecosphere.platform.connectors.events.EventHandlingConnector
getName
-
-
-
-
Field Detail
-
DEFAULT_CHANNEL
public static final java.lang.String DEFAULT_CHANNEL
- See Also:
- Constant Field Values
-
adapter
private ProtocolAdapter<O,I,CO,CI>[] adapter
-
selector
private AdapterSelector<O,I,CO,CI> selector
-
callback
private de.iip_ecosphere.platform.transport.connectors.ReceptionCallback<CO> callback
-
timer
private java.util.Timer timer
-
pollTask
private java.util.TimerTask pollTask
-
params
private ConnectorParameter params
-
enablePolling
private boolean enablePolling
-
cachingStrategy
private CachingStrategy cachingStrategy
-
-
Constructor Detail
-
AbstractConnector
@SafeVarargs protected AbstractConnector(ProtocolAdapter<O,I,CO,CI>... adapter)
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:
java.lang.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:
java.lang.IllegalArgumentException- ifadapteris null or empty or adapters are null
-
-
Method Detail
-
initSelector
protected void initSelector(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.- Parameters:
selector- the selector to initialize
-
useTls
public static boolean useTls(ConnectorParameter params)
Returns whether the connector shall use TLS.- Parameters:
params- the transport parameters- Returns:
truefor TLS enabled,falseelse
-
createTlsContext
protected javax.net.ssl.SSLContext createTlsContext(ConnectorParameter params) throws java.io.IOException
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:
java.io.IOException- if creating the context or obtaining key information fails
-
configureModelAccess
protected void configureModelAccess(ModelAccess access)
Configures the model access on all protocol adapters.- Parameters:
access- the model access
-
getSelector
protected AdapterSelector<O,I,CO,CI> getSelector()
Returns the adapter selector.- Returns:
- the selector
-
connect
public void connect(ConnectorParameter params) throws java.io.IOException
Connects the connector to the underlying machine/platform. CallsconnectImpl(ConnectorParameter)and if successful (no exception thrown)initializeModelAccess(). CallsConnectorRegistry.registerConnector(Connector).
-
connectImpl
protected abstract void connectImpl(ConnectorParameter params) throws java.io.IOException
Implements theconnect(ConnectorParameter)method assuming that everything is ok if no exception have been thrown.- Parameters:
params- connection parameter- Throws:
java.io.IOException- in case that connecting fails
-
getConnectorParameter
protected ConnectorParameter 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:
enableNotifications(boolean)
-
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
public final void disconnect() throws java.io.IOExceptionDisconnects the connector from the underlying machine/platform. CallsdisconnectImpl(),uninstallPollTask()andConnectorRegistry.unregisterConnector(Connector).
-
disconnectImpl
protected abstract void disconnectImpl() throws java.io.IOExceptionCalled bydisconnect().- Throws:
java.io.IOException- if problems occur while disconnecting
-
write
public void write(CI data) throws java.io.IOException
Description copied from interface:ConnectorWrites the givendatato the underlying machine/platform.
-
writeImpl
protected abstract void writeImpl(I data) throws java.io.IOException
Does the actual writing to the underlying machine/platform. Can be left empty ifMachineConnector.hasModel().- Parameters:
data- the data to be send- Throws:
java.io.IOException- if sending fails
-
received
protected CO received(java.lang.String channel, O data) throws java.io.IOException
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:
java.io.IOException- if receiving/translation fails
-
received
protected CO received(java.lang.String channel, O data, boolean notifyCallback) throws java.io.IOException
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:
java.io.IOException- if receiving/translation fails
-
getCachingStrategyCls
public java.lang.Class<? extends CachingStrategy> getCachingStrategyCls()
Description copied from interface:ConnectorReturns the actual caching strategy class.
-
getCachingStrategy
protected CachingStrategy getCachingStrategy()
Returns the actual caching strategy.- Returns:
- the strategy
-
getInitCachingStrategyCls
protected java.lang.Class<? extends CachingStrategy> getInitCachingStrategyCls()
Returns the initial caching strategy class.- Returns:
- the caching strategy class, may be null for default
-
checkCache
protected boolean checkCache(java.lang.Object data)
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 java.io.IOException
Description copied from interface:ConnectorAttaches a receptioncallbackto this connector. Thecallbackis called upon a reception.
-
request
public CO request(boolean notifyCallback) throws java.io.IOException
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:
java.io.IOException- in case that reading fails
-
request
protected CO request(java.lang.String channel, boolean notifyCallback) throws java.io.IOException
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:
java.io.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
public void trigger(ConnectorTriggerQuery query)
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
protected abstract O read() throws java.io.IOException
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:
java.io.IOException- in case that reading fails
-
error
protected abstract void error(java.lang.String message, java.lang.Throwable th)Logs an error.- Parameters:
message- the message to logth- information about the error
-
initializeModelAccess
protected void initializeModelAccess() throws java.io.IOExceptionCalled to initialize the model access, e.g., to setup notifications. Shall be called only, when the connector is connected.- Throws:
java.io.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
public java.lang.Class<? extends I> getProtocolInputType()
Description copied from interface:ConnectorReturns the input type to the protocol.
-
getConnectorInputType
public java.lang.Class<? extends CI> getConnectorInputType()
Description copied from interface:ConnectorReturns the input type from the IIP-Ecosphere platform.
-
getProtocolOutputType
public java.lang.Class<? extends O> getProtocolOutputType()
Description copied from interface:ConnectorReturns the output type of the protocol.
-
getConnectorOutputType
public java.lang.Class<? extends CO> getConnectorOutputType()
Description copied from interface:ConnectorReturns the output type to the IIP-Ecosphere platform.
-
notifyReconfigured
public void notifyReconfigured(java.lang.String parameterName, java.lang.String value)Description copied from interface:ConnectorCalled when parameters of the containing service are changed.
-
-