public interface ServiceFactory
an implementation of this interface is used by a TcpServer
to fetch a fresh Service
for each incoming
connection.
Modifier and Type | Method and Description |
---|---|
Service |
createService(java.io.InputStream in,
java.io.OutputStream out,
java.lang.Object param)
creates a
Service that reads from the given input
stream, processes it and writes the result to the given output
stream. |
Service createService(java.io.InputStream in, java.io.OutputStream out, java.lang.Object param) throws ServiceCreateException
creates a Service
that reads from the given input
stream, processes it and writes the result to the given output
stream.
This method should return as fast as possible, because it is
run in the TcpServer
's main thread and thereby no
other, parallel connections can be initiated while this method is
at work. In particular nothing that can block the thread, like
reading input, should be done in this method. Either move setup
code into the constructor of the ServiceFactory
or,
if it is connection related, move it into the run()
method of the Service
itself.
A similar note obviously applies to the constructor of the
Service
which is most probably called by this
method.
Notice: The Service
created should not
close the streams. While it would be logical to close the
streams after reaching eof, this does not work well with streams
originating from a socket. Consequently the method which calls a
ServiceFactory
has to take care to eventually close
the streams. It is, however, a good idea to flush the output
stream.
param
- is an arbitrary parameter object that may be used to
tweak the service created beyond its input and output
stream. TcpServer
sets it to null
, but when
stacking up service factories (like with FilterServiceFactory
), this is useful.ServiceCreateException
- if the service is permanently
unavailable. To indicate that the service may be created the next
time this method is called, use ServiceUnavailException
.