Package de.aristaflow.ilm.model.common
Class ServerStreamStringHandler
- java.lang.Object
-
- de.aristaflow.ilm.model.common.AbstractIteratorHandler<ServerInputStream,InitialStringRemoteIteratorData,StringRemoteIteratorData>
-
- de.aristaflow.ilm.model.common.ServerStreamStringHandler
-
public class ServerStreamStringHandler extends AbstractIteratorHandler<ServerInputStream,InitialStringRemoteIteratorData,StringRemoteIteratorData>
This class handles server input streams as iterators and converts their content toString
. It assigns IDs to each server input stream and maps these IDs to the respective handler. This allows to use aUUID
instead of the server input stream instance and thus simplifying interfaces not supporting Java objects, for instance web services.
All methods of this class (exceptinit(ServerInputStream, String, Integer)
require a UUID identifying the corresponding server input stream. If the ID does not refer to a server input stream, anIllegalArgumentException
will be thrown. Additionally, this handler keeps track of the alive time of known server input stream. As soon as their alive time elapses, they will be removed from this handler.This handler tries to minimise closing effort for server input stream. It will close them implicitly if
- all content is transferred initially,
- all content is retrieved at once (
count
isnull
), - the end of the data is reached, or
- the server input stream contains no data at all.
null
will be returned instead ofStringRemoteIteratorData
if there is no content (for the current iteration). If the server input stream is closed on-the-fly, aStringRemoteIteratorData
will be returned even if there is no content.This class is thread-safe.
-
-
Field Summary
-
Fields inherited from class de.aristaflow.ilm.model.common.AbstractIteratorHandler
dataCreator, initDataCreator, iterators, iteratorTypeName, logger, removerService
-
-
Constructor Summary
Constructors Constructor Description ServerStreamStringHandler(LogService logService)
Creates a new handler for server-side proxies returning data based onString
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close(@NotNull UUID sisId)
CallsServerSideProxy.close()
on the designated iterator and removes it from this handler.@Valid StringRemoteIteratorData
getNext(@NotNull UUID sisId, @Positive Integer count)
CallsreadContentAsString(ServerInputStream, int)
on the designated server input stream.protected StringRemoteIteratorData
getNext(UUID sisId, ServerInputStream sis, Integer count)
CallsreadContentAsString(ServerInputStream, int)
on the designated server input stream.@Valid InitialStringRemoteIteratorData
init(@NotNull ServerInputStream sis, String udtName, @Positive Integer count)
Adds the designated input stream to this handler, wraps it in a server input stream, creates an ID for it and callsgetNext(UUID, Integer)
with the designated count.long
keepAlive(@NotNull UUID iteratorId, @NotNull Long aliveTime)
CallsTimeTrackingSsProxy.keepAlive(long)
on the designated server input stream.protected Pair<String,Boolean>
readContentAsString(ServerInputStream sis, int len)
Gets the next content of the designated length of the designated server input stream as string if available.-
Methods inherited from class de.aristaflow.ilm.model.common.AbstractIteratorHandler
closeOrDefer, closeOrDefer, getIterator, shutdown
-
-
-
-
Constructor Detail
-
ServerStreamStringHandler
public ServerStreamStringHandler(LogService logService)
Creates a new handler for server-side proxies returning data based onString
.- Parameters:
logService
- The log service to retrieve the logger for log messages from.
-
-
Method Detail
-
init
@Valid public @Valid InitialStringRemoteIteratorData init(@NotNull @NotNull ServerInputStream sis, String udtName, @Positive @Positive Integer count) throws IOException
Adds the designated input stream to this handler, wraps it in a server input stream, creates an ID for it and callsgetNext(UUID, Integer)
with the designated count. The added server input stream will be removed from this handler when it is closed. If all content of the server input stream is transferred now, the server input stream will be closed immediately.- Parameters:
sis
- The server input stream to add to this handler. This will be coupled to created server input stream, so do not close it; clone it if required.udtName
- The UDT name in case the designated server input stream represents aServerUDTInputStream
. Otherwise usenull
.count
- The amount of content to retrieve in forward direction. This has to be> 0
ornull
. If this isnull
, all content of the designated input stream will be returned and converted toString
.- Returns:
- The initial meta data of the (UDT) server input stream and the next content of size n
of the designated input stream represented as
String
. In case of an insufficient amount of content only the available content will be returned. If there is no content,null
will be returned. If nocount
orInteger.MAX_VALUE
is specified, all content of the designated input stream will be returned and the created server input stream will be closed. - Throws:
IOException
- If reading content from the designated stream or detecting whether we reached the end of the stream failed, anIOException
will be thrown.DataConsistencyException
will be thrown.IllegalArgumentException
- Ifcount
is non-positive (<= 0
), anIllegalArgumentException
will be thrown.IllegalStateException
- If the created server input stream has been closed implicitly, anIllegalStateException
will be thrown on server side.de.aristaflow.adept2.model.communication.ServiceConnectionException
- If the created server input stream has been closed implicitly, aServiceConnectionException
will be thrown on client side.
-
getNext
@Valid public @Valid StringRemoteIteratorData getNext(@NotNull @NotNull UUID sisId, @Positive @Positive Integer count) throws IOException
CallsreadContentAsString(ServerInputStream, int)
on the designated server input stream. The alive time of the server input stream will be extended in case of implicit extension. The server input stream will be closed as soon as the last content is transferred.- Parameters:
sisId
- The ID of the server input stream to forward the call to.count
- The amount of content to retrieve in forward direction. This has to be> 0
ornull
. If this isnull
, all content of the designated server input stream will be returned and the server input stream will be closed.- Returns:
- The next content of size n of the designated server input stream. In case of an
insufficient amount of content remaining only the available content (or no content)
will be returned. If no
count
orInteger.MAX_VALUE
is specified, all content of the designated server input stream will be returned at once. If nocount
orInteger.MAX_VALUE
is specified and there is no more content and the server input stream has been closed,null
will be returned. - Throws:
IOException
- If reading content from the designated stream or detecting whether we reached the end of the stream failed, anIOException
will be thrown.DataConsistencyException
will be thrown.IllegalArgumentException
- If the designated ID does not refer to aServerInputStream
known by this handler, anIllegalArgumentException
will be thrown.IllegalArgumentException
- Ifcount
is non-positive (<= 0
), anIllegalArgumentException
will be thrown.IllegalStateException
- If the designated server input stream has been closed implicitly, anIllegalStateException
will be thrown on server side.de.aristaflow.adept2.model.communication.ServiceConnectionException
- If the designated server input stream has been closed implicitly, aServiceConnectionException
will be thrown on client side.
-
getNext
protected StringRemoteIteratorData getNext(UUID sisId, ServerInputStream sis, Integer count) throws IOException
CallsreadContentAsString(ServerInputStream, int)
on the designated server input stream. The alive time of the server input stream will be extended in case of implicit extension. It will be set to be closed as soon as the last content is transferred. It will also be set to be closed in case all (or the all of the remaining) content is provided at once. The caller of this method has to close the server input stream otherwise unless requesting further data!- Parameters:
sisId
- The ID of the server input stream to forward the call to.sis
- The server input stream to forward the call to.count
- The amount of elements to retrieve in forward direction. This has to be> 0
ornull
. If this isnull
, all remaining content of the designated server input stream will be returned and the server input stream will be closed.- Returns:
- The next content of size n of the designated server input stream. In case of an insufficient amount of content remaining only the available content (or no content) will be returned.
- Throws:
IOException
- If reading content from the designated stream or detecting whether we reached the end of the stream failed, anIOException
will be thrown.DataConsistencyException
will be thrown.IllegalArgumentException
- If the designated ID does not refer to aServerInputStream
known by this handler, anIllegalArgumentException
will be thrown.IllegalArgumentException
- Ifcount
is non-positive (<= 0
), anIllegalArgumentException
will be thrown.IllegalStateException
- If the designated server input stream has been closed implicitly, anIllegalStateException
will be thrown on server side.de.aristaflow.adept2.model.communication.ServiceConnectionException
- If the designated server input stream has been closed implicitly, aServiceConnectionException
will be thrown on client side.
-
keepAlive
public long keepAlive(@NotNull @NotNull UUID iteratorId, @NotNull @NotNull Long aliveTime) throws IOException
CallsTimeTrackingSsProxy.keepAlive(long)
on the designated server input stream.- Parameters:
iteratorId
- The ID of the server input stream to forward the call to.aliveTime
- The time in milliseconds to keep the designated server input stream alive that is not to close it automatically.- Returns:
- The relative time the designated server input stream will be kept alive longer. 0
indicates immediate closing. A negative value represents the failure of extending the
alive time, check
TimeTrackingSsProxy.getRemainingAliveTime()
in this case whether extending the keep alive time has been successful or not. - Throws:
IOException
- If there are problems accessing the server input stream, anIOException
will be thrown.IllegalArgumentException
- If the designated ID does not refer to aServerInputStream
known by this handler, anIllegalArgumentException
will be thrown.de.aristaflow.adept2.model.communication.ServiceConnectionException
- If the designated server input stream has been closed implicitly, aServiceConnectionException
will be thrown on client side.
-
close
public void close(@NotNull @NotNull UUID sisId) throws IOException
Description copied from class:AbstractIteratorHandler
CallsServerSideProxy.close()
on the designated iterator and removes it from this handler.- Overrides:
close
in classAbstractIteratorHandler<ServerInputStream,InitialStringRemoteIteratorData,StringRemoteIteratorData>
- Parameters:
sisId
- The ID of the iterator to close.- Throws:
IOException
- If there are problems accessing the underlying data source, anIOException
will be thrown.
-
readContentAsString
protected Pair<String,Boolean> readContentAsString(ServerInputStream sis, int len) throws IOException
Gets the next content of the designated length of the designated server input stream as string if available. If there is less remaining content, this remaining content will be returned. If there is no more content,null
will be returned as first element of the pair.
The second element will specify whether the stream is empty now if this can be determined. When requesting more content than available, this is obviouslytrue
. This tries to read the next byte of data from the stream if this supports marking/resetting. Otherwise,null
will be returned as second element since the emptiness cannot be determined.- Parameters:
sis
- The server input stream of which to get content.len
- The amount of content to be retrieved from the designated server input stream. Note that due to encoding and/or transformation this length may differ from the actual length of the returned string.- Returns:
- The content of (about) the designated length if available encoded as string as first
element of the pair. If there is less content, only this will be returned, if there is
no content,
null
will be returned. The second element specifies whether the stream is empty now (true
), there is more content (false
) ornull
if the complete request can be fulfilled but the stream does not support marking/resetting for peeking the next byte. - Throws:
IOException
- if there are problems accessing the server input stream, anIOException
will be thrown.
-
-