jpos / jPOS

jPOS Project
http://jpos.org
GNU Affero General Public License v3.0
599 stars 458 forks source link

Add protected method to connect channel to streams #572

Open alcarraz opened 7 months ago

alcarraz commented 7 months ago

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);
    }
}
ar commented 7 months ago

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.

alcarraz commented 7 months ago

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.