jPOS icon indicating copy to clipboard operation
jPOS copied to clipboard

Add protected method to connect channel to streams

Open alcarraz opened this issue 2 years ago • 2 comments

These methods can be overridden by subclasses of existing channels to write to/read from streams, for example, to replay some captured stream, or just to do some tests based on ByteArrayInputStream.

Sometimes, we may want to use the existing channels, but not necessarily to connect them to a TCP/IP endpoint, for instance, we may want to input data from a file directly as an input of the channel.

Here, there is an example I'm using to read XML messages from a file or standard input:

public class XMLInputFileChannel extends XMLChannel implements AutoCloseable{
    public XMLInputFileChannel() throws ISOException {
        this(System.in);
    }
    public XMLInputFileChannel(InputStream in) throws ISOException {
        this(in, new XMLPackager());
    }

    public XMLInputFileChannel(XMLPackager packager){
        this(System.in, packager);
    }

    public XMLInputFileChannel(InputStream in, XMLPackager packager) {
        connect(in, null);
        setPackager(packager);
        
    }

    @Override
    public void close() throws IOException {
        disconnect();
    }

    @Override
    public boolean isConnected() {
        return super.isConnected() || (usable && serverIn != null);
    }
}

alcarraz avatar Nov 27 '23 23:11 alcarraz

It would be nice to have this XMLInputFileChannel as part of the PR and it wouldn't hurt to have at least a unit test so that we can make sure we don't break it in the future with changes to BaseChannel.

ar avatar Nov 28 '23 16:11 ar

Sorry, I had written a couple of tests, but somehow the test class got stashed and I didn't notice.

I don't think it is a good idea to add the XMLInputFileChannel, because it is for quite some narrow use case (where connecting in the constructor was justified). However, I understand it wouls be good to have an example usage. So, I'll add a more generic on a followup commit to this PR as soon as I can write it.

Thank you.

alcarraz avatar Nov 28 '23 23:11 alcarraz