Samples :: SBE Log Decoder

SBE Log Decoder

Description

SBE Log Decoder decodes provided summary file according to the template and writes decoded messages into a separate file.

Usage

  • Run the sample:
  • win: SbeLogDecoder.bat
  • linux: SbeLogDecoder.sh
  • Clean everything:
  • win: clean.bat
  • linux: clean.sh

Source Code

22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import biz.onixs.sbe.ByteDecoder;
import biz.onixs.sbe.ByteDecoderFactory;
import biz.onixs.sbe.IMessage;
import biz.onixs.sbe.def.MessageSchema;
import biz.onixs.util.File;
import biz.onixs.util.log.Log;
import biz.onixs.util.url.ResourceLoaderUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
  
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;
  
/**
 * Sbe Log Decoder sample.
 */
public class SbeLogDecoder implements Runnable {
    private static final Logger LOG = LoggerFactory.getLogger(SbeLogDecoder.class);
    private static final String templateFile = "sample/ilinkbinary.xml";
    private static final char FileExtensionDelimiter = '.';
    private static final String DecodedSummaryFileExtension = ".txt";
    private final String summaryFileName;
    private final ByteDecoder decoder;
  
    public SbeLogDecoder(final String summaryFileName) throws Exception {
        this.summaryFileName = summaryFileName;
        Log.debug(this, "Loading templates from file '{}'...", templateFile, LOG);
        final InputStream inputStream = ResourceLoaderUtil.DEFAULT_LOADER.getResource(templateFile);
        if (null == inputStream) {
            throw new RuntimeException("The following resource can't be found: " + templateFile);
        }
        final DocumentBuilder builder;
        final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        builder = builderFactory.newDocumentBuilder();
        final Document doc = builder.parse(inputStream);
        final MessageSchema messageSchema = new MessageSchema(doc, false);
        decoder = new ByteDecoderFactory().create(messageSchema);
    }
  
    public void run() {
        final String decodedSummaryFileName = summaryFileName.
                substring(0, summaryFileName.indexOf(FileExtensionDelimiter)) + DecodedSummaryFileExtension;
        int messageCounter = 0;
        try {
            final File file = new File(summaryFileName, "rw");
            final File outputFile = new File(decodedSummaryFileName, "rw");
            String line;
            while (!("").equals(line = file.getLine('\n'))) {
                final String rawMsg = line.substring(line.lastIndexOf(' ') + 1)
                        .replace("\n", "").replace("\r", "");
                final byte[] rawMsgBytes = Base64.getDecoder().decode(rawMsg);
                try {
                    final IMessage msg = decoder.decodePreCreatedMessage(rawMsgBytes, 0, rawMsgBytes.length);
                    outputFile.write(line.substring(0, 26));
                    outputFile.write(msg.toString());
                    outputFile.write(System.lineSeparator());
                    ++messageCounter;
                } catch (final Exception e) {
                    LOG.warn("Failed to parse message: " + rawMsg + " : " + e.getMessage());
                }
            }
        } catch (final IOException e) {
            throw new RuntimeException("Failed to open file: " + e.getMessage());
        } finally {
            LOG.info("{} messages are decoded successfully and written into {} file.",
                    messageCounter, decodedSummaryFileName);
        }
    }
  
    public static void main(final String[] args) {
        LOG.info("CME iLink 3 SbeLogDecoder Sample.");
        String summaryFileName = "";
        if (args.length >= 1) {
            summaryFileName = args[0];
        }
        if (summaryFileName.isEmpty()) {
            LOG.error("Usage: SbeLogDecoder [SummaryFileName]");
            return;
        }
        try {
            LOG.info("Decoding {} log file...", summaryFileName);
            final SbeLogDecoder sbeLogDecoder = new SbeLogDecoder(summaryFileName);
            sbeLogDecoder.run();
        } catch (final Exception e) {
            LOG.error("EXCEPTION: {}", e.getMessage());
            e.printStackTrace();
        }
    }
}